HelloGUI.cpp 7.5 KB

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