HelloSystemUi.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Copyright (c) 2017 the Atomic project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Atomic/Core/CoreEvents.h>
  23. #include <Atomic/Core/ProcessUtils.h>
  24. #include <Atomic/Input/Input.h>
  25. #include <Atomic/Graphics/Graphics.h>
  26. #include <Atomic/Graphics/Octree.h>
  27. #include <Atomic/Graphics/Zone.h>
  28. #include <Atomic/Graphics/Camera.h>
  29. #include <Atomic/Graphics/Renderer.h>
  30. #include <Atomic/Resource/ResourceCache.h>
  31. #include <Atomic/Scene/Scene.h>
  32. #include <Atomic/UI/SystemUI/SystemUI.h>
  33. #include <Atomic/UI/SystemUI/SystemUIEvents.h>
  34. #include <Atomic/UI/SystemUI/Console.h>
  35. #include "FeatureExamples.h"
  36. #include "HelloSystemUi.h"
  37. #include <Atomic/DebugNew.h>
  38. HelloSystemUi::HelloSystemUi(Context* context) :
  39. Sample(context)
  40. {
  41. }
  42. void HelloSystemUi::Start()
  43. {
  44. // Execute base class startup
  45. Sample::Start();
  46. SimpleCreateInstructions();
  47. // Scene is only required to give screen a background color.
  48. CreateScene();
  49. // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
  50. // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
  51. // could subscribe in the constructor instead.
  52. SubscribeToEvents();
  53. // Set the mouse mode to use in the sample
  54. Sample::InitMouseMode(MM_FREE);
  55. }
  56. void HelloSystemUi::SubscribeToEvents()
  57. {
  58. SubscribeToEvent(E_KEYDOWN, ATOMIC_HANDLER(HelloSystemUi, HandleKeyDown));
  59. SubscribeToEvent(E_SYSTEMUIFRAME, ATOMIC_HANDLER(HelloSystemUi, RenderUi));
  60. }
  61. void HelloSystemUi::RenderUi(StringHash eventType, VariantMap& eventData)
  62. {
  63. ImGui::SetNextWindowSize(ImVec2(200, 300), ImGuiSetCond_FirstUseEver);
  64. ImGui::SetNextWindowPos(ImVec2(200, 300), ImGuiSetCond_FirstUseEver);
  65. if (ImGui::Begin("Sample SystemUI", 0, ImGuiWindowFlags_NoSavedSettings))
  66. {
  67. if (messageBox_.NotNull())
  68. {
  69. if (ImGui::Button("Close message box"))
  70. messageBox_ = nullptr;
  71. }
  72. else
  73. {
  74. if (ImGui::Button("Show message box"))
  75. {
  76. messageBox_ = new MessageBox(context_, "Hello from SystemUI", "Sample Message Box");
  77. SubscribeToEvent(E_MESSAGEACK, [&](StringHash, VariantMap&) {
  78. messageBox_ = nullptr;
  79. });
  80. }
  81. }
  82. if (ImGui::Button("Toggle console"))
  83. GetSubsystem<Console>()->Toggle();
  84. }
  85. ImGui::End();
  86. }
  87. void HelloSystemUi::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  88. {
  89. if (eventData[KeyDown::P_KEY].GetUInt() == KEY_BACKQUOTE)
  90. GetSubsystem<Console>()->Toggle();
  91. }
  92. void HelloSystemUi::CreateScene()
  93. {
  94. scene_ = new Scene(context_);
  95. // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  96. // (-1000, -1000, -1000) to (1000, 1000, 1000)
  97. scene_->CreateComponent<Octree>();
  98. // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
  99. // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
  100. // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
  101. Node* zoneNode = scene_->CreateChild("Zone");
  102. Zone* zone = zoneNode->CreateComponent<Zone>();
  103. // Set same volume as the Octree, set a close bluish fog and some ambient light
  104. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  105. zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
  106. zone->SetFogColor(Color(0.5f, 0.5f, 0.7f));
  107. zone->SetFogStart(100.0f);
  108. zone->SetFogEnd(300.0f);
  109. cameraNode_ = scene_->CreateChild("Camera");
  110. auto camera = cameraNode_->CreateComponent<Camera>();
  111. GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));
  112. }