HelloGui.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/Core/ProcessUtils.h>
  25. #include <Atomic/Input/Input.h>
  26. #include <Atomic/Graphics/Graphics.h>
  27. #include <Atomic/UI/UI.h>
  28. #include <Atomic/UI/UIEvents.h>
  29. #include <Atomic/UI/UIFontDescription.h>
  30. #include <Atomic/UI/UIView.h>
  31. #include <Atomic/UI/UILayout.h>
  32. #include <Atomic/UI/UICheckBox.h>
  33. #include <Atomic/UI/UITextField.h>
  34. #include <Atomic/UI/UIButton.h>
  35. #include <Atomic/UI/UIEditField.h>
  36. #include <Atomic/UI/UIWindow.h>
  37. #include <Atomic/Resource/ResourceCache.h>
  38. #include <Atomic/Scene/Scene.h>
  39. #include "FeatureExamples.h"
  40. #include "HelloGui.h"
  41. #include <Atomic/DebugNew.h>
  42. HelloGui::HelloGui(Context* context) :
  43. Sample(context)
  44. {
  45. }
  46. void HelloGui::Start()
  47. {
  48. // Execute base class startup
  49. Sample::Start();
  50. SimpleCreateInstructions();
  51. // Create "Hello GUI"
  52. CreateUI();
  53. // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
  54. // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
  55. // could subscribe in the constructor instead.
  56. SubscribeToEvents();
  57. // Set the mouse mode to use in the sample
  58. Sample::InitMouseMode(MM_FREE);
  59. }
  60. void HelloGui::CreateUI()
  61. {
  62. UILayout* layout = new UILayout(context_);
  63. layout->SetAxis(UI_AXIS_Y);
  64. UICheckBox* checkBox = new UICheckBox(context_);
  65. checkBox->SetId("Checkbox");
  66. layout->AddChild(checkBox);
  67. UIButton* button = new UIButton(context_);
  68. button->SetText("Button");
  69. button->SetId("Button");
  70. layout->AddChild(button);
  71. UIEditField* edit = new UIEditField(context_);
  72. layout->AddChild(edit);
  73. edit->SetId("EditField");
  74. window_ = new UIWindow(context_);
  75. window_->SetSettings( (UI_WINDOW_SETTINGS) (UI_WINDOW_SETTINGS_TITLEBAR | UI_WINDOW_SETTINGS_CLOSE_BUTTON));
  76. window_->SetText("Hello Atomic GUI!");
  77. window_->AddChild(layout);
  78. window_->ResizeToFitContent();
  79. FeatureExamples::GetUIView()->AddChild(window_);
  80. window_->Center();
  81. }
  82. void HelloGui::SubscribeToEvents()
  83. {
  84. // Subscribe HandleUpdate() function for processing update events
  85. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(HelloGui, HandleUpdate));
  86. SubscribeToEvent(E_WIDGETEVENT, ATOMIC_HANDLER(HelloGui, HandleWidgetEvent));
  87. SubscribeToEvent(E_WIDGETDELETED, ATOMIC_HANDLER(HelloGui, HandleWidgetDeleted));
  88. }
  89. void HelloGui::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
  90. {
  91. using namespace WidgetEvent;
  92. if (eventData[P_TYPE] == UI_EVENT_TYPE_CLICK)
  93. {
  94. UIWidget* widget = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  95. if (widget)
  96. {
  97. window_->SetText(ToString("Hello: %s", widget->GetId().CString()));
  98. }
  99. }
  100. }
  101. void HelloGui::HandleWidgetDeleted(StringHash eventType, VariantMap& eventData)
  102. {
  103. BackToSelector();
  104. }
  105. void HelloGui::HandleUpdate(StringHash eventType, VariantMap& eventData)
  106. {
  107. // Do nothing for now, could be extended to eg. animate the display
  108. }