AppState_MainScreen.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "AppState_MainScreen.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 String MAIN_SCREEN_WINDOW_STR = "Main Screen Window";
  15. static const String BENCHMARK_01_STR = "Benchmark 01";
  16. static const String BENCHMARK_02_STR = "Benchmark 02";
  17. static const String BENCHMARK_03_STR = "Benchmark 03";
  18. static const String BENCHMARK_04_STR = "Benchmark 04";
  19. void AppState_MainScreen::HandleButtonPressed(StringHash eventType, VariantMap& eventData)
  20. {
  21. Button* pressedButton = static_cast<Button*>(eventData["Element"].GetPtr());
  22. AppStateManager* appStateManager = GetSubsystem<AppStateManager>();
  23. if (pressedButton->GetName() == BENCHMARK_01_STR)
  24. appStateManager->SetRequiredAppStateId(APPSTATEID_BENCHMARK01);
  25. else if (pressedButton->GetName() == BENCHMARK_02_STR)
  26. appStateManager->SetRequiredAppStateId(APPSTATEID_BENCHMARK02);
  27. else if (pressedButton->GetName() == BENCHMARK_03_STR)
  28. appStateManager->SetRequiredAppStateId(APPSTATEID_BENCHMARK03);
  29. else if (pressedButton->GetName() == BENCHMARK_04_STR)
  30. appStateManager->SetRequiredAppStateId(APPSTATEID_BENCHMARK04);
  31. }
  32. void AppState_MainScreen::CreateButton(const String& name, const String& text, Window& parent)
  33. {
  34. Button* button = parent.CreateChild<Button>(name);
  35. button->SetStyleAuto();
  36. button->SetFixedHeight(24);
  37. Text* buttonText = button->CreateChild<Text>();
  38. buttonText->SetStyleAuto();
  39. buttonText->SetText(text);
  40. buttonText->SetAlignment(HA_CENTER, VA_CENTER);
  41. SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(AppState_MainScreen, HandleButtonPressed));
  42. }
  43. void AppState_MainScreen::CreateGui()
  44. {
  45. UIElement* root = GetSubsystem<UI>()->GetRoot();
  46. Window* window = root->CreateChild<Window>(MAIN_SCREEN_WINDOW_STR);
  47. window->SetStyleAuto();
  48. window->SetMinWidth(384);
  49. window->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  50. window->SetPosition(10, 34);
  51. Text* windowTitle = window->CreateChild<Text>();
  52. windowTitle->SetStyleAuto();
  53. windowTitle->SetText("Benchmark list");
  54. AppStateManager* appStateManager = GetSubsystem<AppStateManager>();
  55. CreateButton(BENCHMARK_01_STR, appStateManager->GetName(APPSTATEID_BENCHMARK01), *window);
  56. CreateButton(BENCHMARK_02_STR, appStateManager->GetName(APPSTATEID_BENCHMARK02), *window);
  57. CreateButton(BENCHMARK_03_STR, appStateManager->GetName(APPSTATEID_BENCHMARK03), *window);
  58. CreateButton(BENCHMARK_04_STR, appStateManager->GetName(APPSTATEID_BENCHMARK04), *window);
  59. }
  60. void AppState_MainScreen::DestroyGui()
  61. {
  62. UIElement* root = GetSubsystem<UI>()->GetRoot();
  63. Window* window = root->GetChildStaticCast<Window>(MAIN_SCREEN_WINDOW_STR);
  64. window->Remove();
  65. }
  66. void AppState_MainScreen::OnEnter()
  67. {
  68. assert(!scene_);
  69. LoadSceneXml("99_Benchmark/Scenes/MainScreen.xml");
  70. CreateGui();
  71. SetupViewport();
  72. GetSubsystem<Input>()->SetMouseVisible(true);
  73. SubscribeToEvent(scene_, E_SCENEUPDATE, URHO3D_HANDLER(AppState_MainScreen, HandleSceneUpdate));
  74. fpsCounter_.Clear();
  75. }
  76. void AppState_MainScreen::OnLeave()
  77. {
  78. DestroyViewport();
  79. DestroyGui();
  80. scene_ = nullptr;
  81. }
  82. void AppState_MainScreen::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
  83. {
  84. float timeStep = eventData[SceneUpdate::P_TIMESTEP].GetFloat();
  85. fpsCounter_.Update(timeStep);
  86. UpdateCurrentFpsElement();
  87. }