AppState_MainScreen.cpp 3.3 KB

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