HelloGUI.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <Urho3D/Urho3D.h>
  23. #include <Urho3D/UI/Button.h>
  24. #include <Urho3D/UI/BorderImage.h>
  25. #include <Urho3D/UI/CheckBox.h>
  26. #include <Urho3D/Core/CoreEvents.h>
  27. #include <Urho3D/Engine/Engine.h>
  28. #include <Urho3D/Graphics/Graphics.h>
  29. #include <Urho3D/Input/Input.h>
  30. #include <Urho3D/UI/LineEdit.h>
  31. #include <Urho3D/Resource/ResourceCache.h>
  32. #include <Urho3D/UI/Text.h>
  33. #include <Urho3D/Graphics/Texture2D.h>
  34. #include <Urho3D/UI/ToolTip.h>
  35. #include <Urho3D/UI/UI.h>
  36. #include <Urho3D/UI/UIElement.h>
  37. #include <Urho3D/UI/UIEvents.h>
  38. #include <Urho3D/UI/Window.h>
  39. #include "HelloGUI.h"
  40. #include <Urho3D/DebugNew.h>
  41. DEFINE_APPLICATION_MAIN(HelloGUI)
  42. HelloGUI::HelloGUI(Context* context) :
  43. Sample(context),
  44. uiRoot_(GetSubsystem<UI>()->GetRoot()),
  45. dragBeginPosition_(IntVector2::ZERO)
  46. {
  47. }
  48. void HelloGUI::Start()
  49. {
  50. // Execute base class startup
  51. Sample::Start();
  52. // Enable OS cursor
  53. GetSubsystem<Input>()->SetMouseVisible(true);
  54. // Load XML file containing default UI style sheet
  55. ResourceCache* cache = GetSubsystem<ResourceCache>();
  56. XMLFile* style = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  57. // Set the loaded style as default style
  58. uiRoot_->SetDefaultStyle(style);
  59. // Initialize Window
  60. InitWindow();
  61. // Create and add some controls to the Window
  62. InitControls();
  63. // Create a draggable Fish
  64. CreateDraggableFish();
  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_ = 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. // Subscribe to buttonClose release (following a 'press') events
  120. SubscribeToEvent(buttonClose, E_RELEASED, HANDLER(HelloGUI, HandleClosePressed));
  121. // Subscribe also to all UI mouse clicks just to see where we have clicked
  122. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(HelloGUI, HandleControlClicked));
  123. }
  124. void HelloGUI::CreateDraggableFish()
  125. {
  126. ResourceCache* cache = GetSubsystem<ResourceCache>();
  127. Graphics* graphics = GetSubsystem<Graphics>();
  128. // Create a draggable Fish button
  129. Button* draggableFish = new Button(context_);
  130. draggableFish->SetTexture(cache->GetResource<Texture2D>("Textures/UrhoDecal.dds")); // Set texture
  131. draggableFish->SetBlendMode(BLEND_ADD);
  132. draggableFish->SetSize(128, 128);
  133. draggableFish->SetPosition((graphics->GetWidth() - draggableFish->GetWidth()) / 2, 200);
  134. draggableFish->SetName("Fish");
  135. uiRoot_->AddChild(draggableFish);
  136. // Add a tooltip to Fish button
  137. ToolTip* toolTip = new ToolTip(context_);
  138. draggableFish->AddChild(toolTip);
  139. toolTip->SetPosition(IntVector2(draggableFish->GetWidth() + 5, draggableFish->GetWidth() / 2)); // slightly offset from close button
  140. BorderImage* textHolder = new BorderImage(context_);
  141. toolTip->AddChild(textHolder);
  142. textHolder->SetStyle("ToolTipBorderImage");
  143. Text* toolTipText = new Text(context_);
  144. textHolder->AddChild(toolTipText);
  145. toolTipText->SetStyle("ToolTipText");
  146. toolTipText->SetText("Please drag me!");
  147. // Subscribe draggableFish to Drag Events (in order to make it draggable)
  148. // See "Event list" in documentation's Main Page for reference on available Events and their eventData
  149. SubscribeToEvent(draggableFish, E_DRAGBEGIN, HANDLER(HelloGUI, HandleDragBegin));
  150. SubscribeToEvent(draggableFish, E_DRAGMOVE, HANDLER(HelloGUI, HandleDragMove));
  151. SubscribeToEvent(draggableFish, E_DRAGEND, HANDLER(HelloGUI, HandleDragEnd));
  152. }
  153. void HelloGUI::HandleDragBegin(StringHash eventType, VariantMap& eventData)
  154. {
  155. // Get UIElement relative position where input (touch or click) occured (top-left = IntVector2(0,0))
  156. dragBeginPosition_ = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
  157. }
  158. void HelloGUI::HandleDragMove(StringHash eventType, VariantMap& eventData)
  159. {
  160. IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
  161. UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
  162. draggedElement->SetPosition(dragCurrentPosition - dragBeginPosition_);
  163. }
  164. void HelloGUI::HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
  165. {
  166. }
  167. void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
  168. {
  169. engine_->Exit();
  170. }
  171. void HelloGUI::HandleControlClicked(StringHash eventType, VariantMap& eventData)
  172. {
  173. // Get the Text control acting as the Window's title
  174. Text* windowTitle = static_cast<Text*>(window_->GetChild("WindowTitle", true));
  175. // Get control that was clicked
  176. UIElement* clicked = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  177. String name = "...?";
  178. if (clicked)
  179. {
  180. // Get the name of the control that was clicked
  181. name = clicked->GetName();
  182. }
  183. // Update the Window's title text
  184. windowTitle->SetText("Hello " + name + "!");
  185. }