HelloGUI.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // Copyright (c) 2008-2020 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. auto* cache = GetSubsystem<ResourceCache>();
  53. auto* 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. // Set the mouse mode to use in the sample
  63. Sample::InitMouseMode(MM_FREE);
  64. }
  65. void HelloGUI::InitControls()
  66. {
  67. // Create a CheckBox
  68. auto* checkBox = new CheckBox(context_);
  69. checkBox->SetName("CheckBox");
  70. // Create a Button
  71. auto* button = new Button(context_);
  72. button->SetName("Button");
  73. button->SetMinHeight(24);
  74. // Create a LineEdit
  75. auto* lineEdit = new LineEdit(context_);
  76. lineEdit->SetName("LineEdit");
  77. lineEdit->SetMinHeight(24);
  78. // Add controls to Window
  79. window_->AddChild(checkBox);
  80. window_->AddChild(button);
  81. window_->AddChild(lineEdit);
  82. // Apply previously set default style
  83. checkBox->SetStyleAuto();
  84. button->SetStyleAuto();
  85. lineEdit->SetStyleAuto();
  86. }
  87. void HelloGUI::InitWindow()
  88. {
  89. // Create the Window and add it to the UI's root node
  90. window_ = new Window(context_);
  91. uiRoot_->AddChild(window_);
  92. // Set Window size and layout settings
  93. window_->SetMinWidth(384);
  94. window_->SetLayout(LM_VERTICAL, 6, IntRect(6, 6, 6, 6));
  95. window_->SetAlignment(HA_CENTER, VA_CENTER);
  96. window_->SetName("Window");
  97. // Create Window 'titlebar' container
  98. auto* titleBar = new UIElement(context_);
  99. titleBar->SetMinSize(0, 24);
  100. titleBar->SetVerticalAlignment(VA_TOP);
  101. titleBar->SetLayoutMode(LM_HORIZONTAL);
  102. // Create the Window title Text
  103. auto* windowTitle = new Text(context_);
  104. windowTitle->SetName("WindowTitle");
  105. windowTitle->SetText("Hello GUI!");
  106. // Create the Window's close button
  107. auto* buttonClose = new Button(context_);
  108. buttonClose->SetName("CloseButton");
  109. // Add the controls to the title bar
  110. titleBar->AddChild(windowTitle);
  111. titleBar->AddChild(buttonClose);
  112. // Add the title bar to the Window
  113. window_->AddChild(titleBar);
  114. // Apply styles
  115. window_->SetStyleAuto();
  116. windowTitle->SetStyleAuto();
  117. buttonClose->SetStyle("CloseButton");
  118. // Subscribe to buttonClose release (following a 'press') events
  119. SubscribeToEvent(buttonClose, E_RELEASED, URHO3D_HANDLER(HelloGUI, HandleClosePressed));
  120. // Subscribe also to all UI mouse clicks just to see where we have clicked
  121. SubscribeToEvent(E_UIMOUSECLICK, URHO3D_HANDLER(HelloGUI, HandleControlClicked));
  122. }
  123. void HelloGUI::CreateDraggableFish()
  124. {
  125. auto* cache = GetSubsystem<ResourceCache>();
  126. auto* graphics = GetSubsystem<Graphics>();
  127. // Create a draggable Fish button
  128. auto* draggableFish = new Button(context_);
  129. draggableFish->SetTexture(cache->GetResource<Texture2D>("Textures/UrhoDecal.dds")); // Set texture
  130. draggableFish->SetBlendMode(BLEND_ADD);
  131. draggableFish->SetSize(128, 128);
  132. draggableFish->SetPosition((graphics->GetWidth() - draggableFish->GetWidth()) / 2, 200);
  133. draggableFish->SetName("Fish");
  134. uiRoot_->AddChild(draggableFish);
  135. // Add a tooltip to Fish button
  136. auto* toolTip = new ToolTip(context_);
  137. draggableFish->AddChild(toolTip);
  138. toolTip->SetPosition(IntVector2(draggableFish->GetWidth() + 5, draggableFish->GetWidth() / 2)); // slightly offset from close button
  139. auto* textHolder = new BorderImage(context_);
  140. toolTip->AddChild(textHolder);
  141. textHolder->SetStyle("ToolTipBorderImage");
  142. auto* toolTipText = new Text(context_);
  143. textHolder->AddChild(toolTipText);
  144. toolTipText->SetStyle("ToolTipText");
  145. toolTipText->SetText("Please drag me!");
  146. // Subscribe draggableFish to Drag Events (in order to make it draggable)
  147. // See "Event list" in documentation's Main Page for reference on available Events and their eventData
  148. SubscribeToEvent(draggableFish, E_DRAGBEGIN, URHO3D_HANDLER(HelloGUI, HandleDragBegin));
  149. SubscribeToEvent(draggableFish, E_DRAGMOVE, URHO3D_HANDLER(HelloGUI, HandleDragMove));
  150. SubscribeToEvent(draggableFish, E_DRAGEND, URHO3D_HANDLER(HelloGUI, HandleDragEnd));
  151. }
  152. void HelloGUI::HandleDragBegin(StringHash eventType, VariantMap& eventData)
  153. {
  154. // Get UIElement relative position where input (touch or click) occurred (top-left = IntVector2(0,0))
  155. dragBeginPosition_ = IntVector2(eventData["ElementX"].GetInt(), eventData["ElementY"].GetInt());
  156. }
  157. void HelloGUI::HandleDragMove(StringHash eventType, VariantMap& eventData)
  158. {
  159. IntVector2 dragCurrentPosition = IntVector2(eventData["X"].GetInt(), eventData["Y"].GetInt());
  160. UIElement* draggedElement = static_cast<UIElement*>(eventData["Element"].GetPtr());
  161. draggedElement->SetPosition(dragCurrentPosition - dragBeginPosition_);
  162. }
  163. void HelloGUI::HandleDragEnd(StringHash eventType, VariantMap& eventData) // For reference (not used here)
  164. {
  165. }
  166. void HelloGUI::HandleClosePressed(StringHash eventType, VariantMap& eventData)
  167. {
  168. if (GetPlatform() != "Web")
  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. auto* windowTitle = window_->GetChildStaticCast<Text>("WindowTitle", true);
  175. // Get control that was clicked
  176. auto* 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. }