2
0

HelloGUI.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D 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 "Button.h"
  23. #include "CheckBox.h"
  24. #include "CoreEvents.h"
  25. #include "Engine.h"
  26. #include "Input.h"
  27. #include "LineEdit.h"
  28. #include "Text.h"
  29. #include "UI.h"
  30. #include "UIEvents.h"
  31. #include "Window.h"
  32. #include "HelloGUI.h"
  33. #include "DebugNew.h"
  34. DEFINE_APPLICATION_MAIN(HelloGUI)
  35. HelloGUI::HelloGUI(Context* context) :
  36. Sample(context),
  37. uiRoot_(GetSubsystem<UI>()->GetRoot()),
  38. window_()
  39. {
  40. }
  41. void HelloGUI::Start()
  42. {
  43. // Execute base class startup
  44. Sample::Start();
  45. // Enable OS cursor
  46. GetSubsystem<Input>()->SetMouseVisible(true);
  47. // Load XML file containing default UI style sheet
  48. SharedPtr<ResourceCache> cache(GetSubsystem<ResourceCache>());
  49. SharedPtr<XMLFile> style(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  50. // Set the loaded style as default style
  51. this->uiRoot_->SetDefaultStyle(style);
  52. // Initialize Window
  53. this->InitWindow();
  54. // Create and add some controls to the Window
  55. this->InitControls();
  56. SubscribeToEvents();
  57. }
  58. void HelloGUI::InitControls()
  59. {
  60. // Create a CheckBox
  61. CheckBox* checkBox = new CheckBox(context_);
  62. checkBox->SetName("CheckBox");
  63. // Create a Button
  64. Button* button = new Button(context_);
  65. button->SetName("Button");
  66. button->SetMinHeight(24);
  67. // Create a LineEdit
  68. LineEdit* lineEdit = new LineEdit(context_);
  69. lineEdit->SetName("LineEdit");
  70. lineEdit->SetMinHeight(24);
  71. // Add controls to Window
  72. window_->AddChild(checkBox);
  73. window_->AddChild(button);
  74. window_->AddChild(lineEdit);
  75. // Apply previously set default style
  76. checkBox->SetStyleAuto();
  77. button->SetStyleAuto();
  78. lineEdit->SetStyleAuto();
  79. }
  80. void HelloGUI::InitWindow()
  81. {
  82. // Create the Window and add it to the UI's root node
  83. this->window_ = SharedPtr<Window>(new Window(context_));
  84. this->uiRoot_->AddChild(window_);
  85. // Set Window size and layout settings
  86. window_->SetMinSize(384, 192);
  87. window_->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  88. window_->SetAlignment(HA_CENTER, VA_CENTER);
  89. window_->SetName("Window");
  90. // Create Window 'titlebar' container
  91. UIElement* titleBar = new UIElement(context_);
  92. titleBar->SetMinSize(0, 24);
  93. titleBar->SetVerticalAlignment(VA_TOP);
  94. titleBar->SetLayoutMode(LM_HORIZONTAL);
  95. // Create the Window title Text
  96. Text* windowTitle = new Text(context_);
  97. windowTitle->SetName("WindowTitle");
  98. windowTitle->SetText("Hello GUI!");
  99. // Create the Window's close button
  100. Button* buttonClose = new Button(context_);
  101. buttonClose->SetName("CloseButton");
  102. // Add the controls to the title bar
  103. titleBar->AddChild(windowTitle);
  104. titleBar->AddChild(buttonClose);
  105. // Add the title bar to the Window
  106. window_->AddChild(titleBar);
  107. // Apply styles
  108. window_->SetStyleAuto();
  109. windowTitle->SetStyleAuto();
  110. buttonClose->SetStyle("CloseButton");
  111. // Lastly, subscribe to buttonClose release (following a 'press') events
  112. SubscribeToEvent(buttonClose, E_RELEASED, HANDLER(HelloGUI, HandleClosePressed));
  113. }
  114. void HelloGUI::SubscribeToEvents()
  115. {
  116. // Subscribe handler; invoked whenever a mouse click event is dispatched
  117. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(HelloGUI, HandleControlClicked));
  118. }
  119. void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
  120. {
  121. GetSubsystem<Engine>()->Exit();
  122. }
  123. void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
  124. {
  125. // Get the Text control acting as the Window's title
  126. SharedPtr<Text> windowTitle((Text*)window_->GetChild(String("WindowTitle"), true));
  127. // Get control that was clicked
  128. UIElement* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  129. String name = "...?";
  130. if (clicked)
  131. {
  132. // Get the name of the control that was clicked
  133. name = clicked->GetName();
  134. }
  135. // Update the Window's title text
  136. windowTitle->SetText(String("Hello " + name + "!"));
  137. }