HelloGUI.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // Copyright (c) 2008-2014 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 "BorderImage.h"
  24. #include "CheckBox.h"
  25. #include "CoreEvents.h"
  26. #include "Engine.h"
  27. #include "Graphics.h"
  28. #include "Input.h"
  29. #include "LineEdit.h"
  30. #include "ResourceCache.h"
  31. #include "Text.h"
  32. #include "Texture2D.h"
  33. #include "ToolTip.h"
  34. #include "UI.h"
  35. #include "UIElement.h"
  36. #include "UIEvents.h"
  37. #include "Window.h"
  38. #include "HelloGUI.h"
  39. #include "DebugNew.h"
  40. DEFINE_APPLICATION_MAIN(HelloGUI)
  41. HelloGUI::HelloGUI(Context* context) :
  42. Sample(context),
  43. uiRoot_(GetSubsystem<UI>()->GetRoot()),
  44. window_()
  45. {
  46. }
  47. void HelloGUI::Start()
  48. {
  49. // Execute base class startup
  50. Sample::Start();
  51. // Enable OS cursor
  52. GetSubsystem<Input>()->SetMouseVisible(true);
  53. // Load XML file containing default UI style sheet
  54. SharedPtr<ResourceCache> cache(GetSubsystem<ResourceCache>());
  55. SharedPtr<XMLFile> style(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  56. // Set the loaded style as default style
  57. uiRoot_->SetDefaultStyle(style);
  58. // Initialize Window
  59. InitWindow();
  60. // Create a draggable Fish
  61. CreateDraggableFish();
  62. // Create and add some controls to the Window
  63. InitControls();
  64. SubscribeToEvents();
  65. }
  66. void HelloGUI::InitControls()
  67. {
  68. // Create a CheckBox
  69. CheckBox* checkBox = new CheckBox(context_);
  70. checkBox->SetName("CheckBox");
  71. // Create a Button
  72. Button* button = new Button(context_);
  73. button->SetName("Button");
  74. button->SetMinHeight(24);
  75. // Create a LineEdit
  76. LineEdit* lineEdit = new LineEdit(context_);
  77. lineEdit->SetName("LineEdit");
  78. lineEdit->SetMinHeight(24);
  79. // Add controls to Window
  80. window_->AddChild(checkBox);
  81. window_->AddChild(button);
  82. window_->AddChild(lineEdit);
  83. // Apply previously set default style
  84. checkBox->SetStyleAuto();
  85. button->SetStyleAuto();
  86. lineEdit->SetStyleAuto();
  87. }
  88. void HelloGUI::InitWindow()
  89. {
  90. // Create the Window and add it to the UI's root node
  91. window_ = SharedPtr<Window>(new Window(context_));
  92. uiRoot_->AddChild(window_);
  93. // Set Window size and layout settings
  94. window_->SetMinSize(384, 192);
  95. window_->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  96. window_->SetAlignment(HA_CENTER, VA_CENTER);
  97. window_->SetName("Window");
  98. // Create Window 'titlebar' container
  99. UIElement* titleBar = new UIElement(context_);
  100. titleBar->SetMinSize(0, 24);
  101. titleBar->SetVerticalAlignment(VA_TOP);
  102. titleBar->SetLayoutMode(LM_HORIZONTAL);
  103. // Create the Window title Text
  104. Text* windowTitle = new Text(context_);
  105. windowTitle->SetName("WindowTitle");
  106. windowTitle->SetText("Hello GUI!");
  107. // Create the Window's close button
  108. Button* buttonClose = new Button(context_);
  109. buttonClose->SetName("CloseButton");
  110. // Add the controls to the title bar
  111. titleBar->AddChild(windowTitle);
  112. titleBar->AddChild(buttonClose);
  113. // Add the title bar to the Window
  114. window_->AddChild(titleBar);
  115. // Apply styles
  116. window_->SetStyleAuto();
  117. windowTitle->SetStyleAuto();
  118. buttonClose->SetStyle("CloseButton");
  119. // Lastly, subscribe to buttonClose release (following a 'press') events
  120. SubscribeToEvent(buttonClose, E_RELEASED, HANDLER(HelloGUI, HandleClosePressed));
  121. }
  122. void HelloGUI::SubscribeToEvents()
  123. {
  124. // Subscribe handler; invoked whenever a mouse click event is dispatched
  125. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(HelloGUI, HandleControlClicked));
  126. }
  127. void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
  128. {
  129. GetSubsystem<Engine>()->Exit();
  130. }
  131. void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
  132. {
  133. // Get the Text control acting as the Window's title
  134. SharedPtr<Text> windowTitle((Text*)window_->GetChild("WindowTitle", true));
  135. // Get control that was clicked
  136. UIElement* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  137. String name = "...?";
  138. if (clicked)
  139. {
  140. // Get the name of the control that was clicked
  141. name = clicked->GetName();
  142. }
  143. // Update the Window's title text
  144. windowTitle->SetText("Hello " + name + "!");
  145. }
  146. //----------------------------------------------------- Dragging / Tooltips ----------------------------------------------
  147. IntVector2 dragBeginPosition = IntVector2(0, 0);
  148. void HelloGUI::CreateDraggableFish()
  149. {
  150. ResourceCache* cache = GetSubsystem<ResourceCache>();
  151. Graphics* graphics = GetSubsystem<Graphics>();
  152. // Create a draggable Fish button
  153. Button* draggableFish = new Button(context_);
  154. draggableFish->SetTexture(cache->GetResource<Texture2D>("Textures/UrhoDecal.dds")); // Set texture
  155. draggableFish->SetBlendMode(BLEND_ADD);
  156. draggableFish->SetSize(128, 128);
  157. draggableFish->SetPosition((graphics->GetWidth() - draggableFish->GetWidth()) / 2, 200);
  158. draggableFish->SetName("Fish");
  159. uiRoot_->AddChild(draggableFish);
  160. // Add a tooltip to Fish button
  161. ToolTip* toolTip = new ToolTip(context_);
  162. draggableFish->AddChild(toolTip);
  163. toolTip->SetPosition(IntVector2(draggableFish->GetWidth() + 5, draggableFish->GetWidth() / 2)); // slightly offset from close button
  164. BorderImage* textHolder = new BorderImage(context_);
  165. toolTip->AddChild(textHolder);
  166. textHolder->SetStyle("ToolTipBorderImage");
  167. Text* toolTipText = new Text(context_);
  168. textHolder->AddChild(toolTipText);
  169. toolTipText->SetStyle("ToolTipText");
  170. toolTipText->SetText("Please drag me!");
  171. // Subscribe draggableFish to Drag Events (in order to make it draggable)
  172. // See "Event list" in documentation's Main Page for reference on available Events and their eventData
  173. SubscribeToEvent(draggableFish, E_DRAGBEGIN, HANDLER(HelloGUI, HandleDragBegin));
  174. SubscribeToEvent(draggableFish, E_DRAGMOVE, HANDLER(HelloGUI, HandleDragMove));
  175. SubscribeToEvent(draggableFish, E_DRAGEND, HANDLER(HelloGUI, HandleDragEnd));
  176. }
  177. void HelloGUI::HandleDragBegin(StringHash eventType, VariantMap& eventData)
  178. {
  179. // Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
  180. dragBeginPosition = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
  181. }
  182. void HelloGUI::HandleDragMove(StringHash eventType, VariantMap& eventData)
  183. {
  184. IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
  185. UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
  186. draggedElement->SetPosition(dragCurrentPosition - dragBeginPosition);
  187. }
  188. void HelloGUI::HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
  189. {
  190. }