AppState_ResultScreen.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "AppState_ResultScreen.h"
  4. #include "AppStateManager.h"
  5. #include <Urho3D/Input/Input.h>
  6. #include <Urho3D/Scene/SceneEvents.h>
  7. #include <Urho3D/UI/Button.h>
  8. #include <Urho3D/UI/Text.h>
  9. #include <Urho3D/UI/UI.h>
  10. #include <Urho3D/UI/UIEvents.h>
  11. #include <Urho3D/UI/Window.h>
  12. #include <Urho3D/DebugNew.h>
  13. using namespace Urho3D;
  14. static const U3D::String RESULT_WINDOW_STR = "Result Window";
  15. void AppState_ResultScreen::OnEnter()
  16. {
  17. assert(!scene_);
  18. LoadSceneXml("99_Benchmark/Scenes/ResultScreen.xml");
  19. GetSubsystem<Input>()->SetMouseVisible(true);
  20. SetupViewport();
  21. SubscribeToEvent(scene_, E_SCENEUPDATE, URHO3D_HANDLER(AppState_ResultScreen, HandleSceneUpdate));
  22. fpsCounter_.Clear();
  23. ShowResultWindow();
  24. }
  25. void AppState_ResultScreen::OnLeave()
  26. {
  27. DestroyViewport();
  28. DestroyResultWindow();
  29. scene_ = nullptr;
  30. }
  31. void AppState_ResultScreen::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  32. {
  33. float timeStep = eventData[SceneUpdate::P_TIMESTEP].GetFloat();
  34. fpsCounter_.Update(timeStep);
  35. UpdateCurrentFpsElement();
  36. Input* input = GetSubsystem<Input>();
  37. if (input->GetKeyDown(KEY_ESCAPE) || input->GetKeyDown(KEY_RETURN) || input->GetKeyDown(KEY_KP_ENTER))
  38. GetSubsystem<AppStateManager>()->SetRequiredAppStateId(APPSTATEID_MAINSCREEN);
  39. }
  40. void AppState_ResultScreen::ShowResultWindow()
  41. {
  42. UIElement* root = GetSubsystem<UI>()->GetRoot();
  43. Window* window = root->CreateChild<Window>(RESULT_WINDOW_STR);
  44. window->SetStyleAuto();
  45. window->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  46. window->SetAlignment(HA_CENTER, VA_CENTER);
  47. Text* windowTitle = window->CreateChild<Text>();
  48. windowTitle->SetStyleAuto();
  49. windowTitle->SetText("Result");
  50. AppStateManager* appStateManager = GetSubsystem<AppStateManager>();
  51. AppStateId prevAppStateId = appStateManager->GetPreviousAppStateId();
  52. const String& benchmarkName = appStateManager->GetName(prevAppStateId);
  53. const FpsCounter& benchmarkResult = appStateManager->GetResult(prevAppStateId);
  54. Text* resultText = window->CreateChild<Text>();
  55. resultText->SetStyleAuto();
  56. resultText->SetText(benchmarkName + ": " + String(benchmarkResult.GetResultFps()) + " FPS (min: " +
  57. String(benchmarkResult.GetResultMinFps()) + ", max: " + String(benchmarkResult.GetResultMaxFps()) + ")");
  58. Button* okButton = window->CreateChild<Button>();
  59. okButton->SetStyleAuto();
  60. okButton->SetFixedHeight(24);
  61. Text* buttonText = okButton->CreateChild<Text>();
  62. buttonText->SetStyleAuto();
  63. buttonText->SetText("Ok");
  64. buttonText->SetAlignment(HA_CENTER, VA_CENTER);
  65. SubscribeToEvent(okButton, E_RELEASED, URHO3D_HANDLER(AppState_ResultScreen, HandleResultOkButtonPressed));
  66. }
  67. void AppState_ResultScreen::DestroyResultWindow()
  68. {
  69. UIElement* root = GetSubsystem<UI>()->GetRoot();
  70. UIElement* window = root->GetChild(RESULT_WINDOW_STR);
  71. window->Remove();
  72. }
  73. void AppState_ResultScreen::HandleResultOkButtonPressed(StringHash eventType, VariantMap& eventData)
  74. {
  75. GetSubsystem<AppStateManager>()->SetRequiredAppStateId(APPSTATEID_MAINSCREEN);
  76. }