HelloGUI.cpp 6.5 KB

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