UIDrag.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/UI/Button.h>
  24. #include <Urho3D/UI/Font.h>
  25. #include <Urho3D/UI/Text.h>
  26. #include <Urho3D/UI/UIEvents.h>
  27. #include "UIDrag.h"
  28. #include <Urho3D/DebugNew.h>
  29. URHO3D_DEFINE_APPLICATION_MAIN(UIDrag)
  30. UIDrag::UIDrag(Context* context) :
  31. Sample(context)
  32. {
  33. }
  34. void UIDrag::Start()
  35. {
  36. // Execute base class startup
  37. Sample::Start();
  38. // Set mouse visible
  39. String platform = GetPlatform();
  40. if (platform != "Android" && platform != "iOS")
  41. GetSubsystem<Input>()->SetMouseVisible(true);
  42. // Create the UI content
  43. CreateGUI();
  44. CreateInstructions();
  45. // Hook up to the frame update events
  46. SubscribeToEvents();
  47. // Set the mouse mode to use in the sample
  48. Sample::InitMouseMode(MM_FREE);
  49. }
  50. void UIDrag::CreateGUI()
  51. {
  52. auto* cache = GetSubsystem<ResourceCache>();
  53. auto* ui = GetSubsystem<UI>();
  54. UIElement* root = ui->GetRoot();
  55. // Load the style sheet from xml
  56. root->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  57. for (int i=0; i < 10; i++)
  58. {
  59. auto* b = new Button(context_);
  60. root->AddChild(b);
  61. // Reference a style from the style sheet loaded earlier:
  62. b->SetStyleAuto();
  63. b->SetMinWidth(250);
  64. b->SetPosition(IntVector2(50*i, 50*i));
  65. // Enable the bring-to-front flag and set the initial priority
  66. b->SetBringToFront(true);
  67. b->SetPriority(i);
  68. // Set the layout mode to make the child text elements aligned vertically
  69. b->SetLayout(LM_VERTICAL, 20, {40, 40, 40, 40});
  70. auto dragInfos = {"Num Touch", "Text", "Event Touch"};
  71. for (auto name: dragInfos)
  72. b->CreateChild<Text>(name)->SetStyleAuto();
  73. if (i % 2 == 0)
  74. b->AddTag("SomeTag");
  75. SubscribeToEvent(b, E_CLICK, URHO3D_HANDLER(UIDrag, HandleClick));
  76. SubscribeToEvent(b, E_DRAGMOVE, URHO3D_HANDLER(UIDrag, HandleDragMove));
  77. SubscribeToEvent(b, E_DRAGBEGIN, URHO3D_HANDLER(UIDrag, HandleDragBegin));
  78. SubscribeToEvent(b, E_DRAGCANCEL, URHO3D_HANDLER(UIDrag, HandleDragCancel));
  79. }
  80. for (int i = 0; i < 10; i++)
  81. {
  82. auto* t = new Text(context_);
  83. root->AddChild(t);
  84. t->SetStyleAuto();
  85. t->SetName("Touch "+ String(i));
  86. t->SetVisible(false);
  87. t->SetPriority(100); // Make sure it has higher priority than the buttons
  88. }
  89. }
  90. void UIDrag::CreateInstructions()
  91. {
  92. auto* cache = GetSubsystem<ResourceCache>();
  93. auto* ui = GetSubsystem<UI>();
  94. // Construct new Text object, set string to display and font to use
  95. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  96. instructionText->SetText("Drag on the buttons to move them around.\n"
  97. "Touch input allows also multi-drag.\n"
  98. "Press SPACE to show/hide tagged UI elements.");
  99. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  100. instructionText->SetTextAlignment(HA_CENTER);
  101. // Position the text relative to the screen center
  102. instructionText->SetHorizontalAlignment(HA_CENTER);
  103. instructionText->SetVerticalAlignment(VA_CENTER);
  104. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  105. }
  106. void UIDrag::SubscribeToEvents()
  107. {
  108. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(UIDrag, HandleUpdate));
  109. }
  110. void UIDrag::HandleClick(StringHash eventType, VariantMap& eventData)
  111. {
  112. using namespace Click;
  113. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  114. element->BringToFront();
  115. }
  116. void UIDrag::HandleDragBegin(StringHash eventType, VariantMap& eventData)
  117. {
  118. using namespace DragBegin;
  119. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  120. int lx = eventData[P_X].GetInt();
  121. int ly = eventData[P_Y].GetInt();
  122. IntVector2 p = element->GetPosition();
  123. element->SetVar("START", p);
  124. element->SetVar("DELTA", IntVector2(p.x_ - lx, p.y_ - ly));
  125. int buttons = eventData[P_BUTTONS].GetInt();
  126. element->SetVar("BUTTONS", buttons);
  127. auto* t = element->GetChildStaticCast<Text>("Text", false);
  128. t->SetText("Drag Begin Buttons: " + String(buttons));
  129. t = element->GetChildStaticCast<Text>("Num Touch", false);
  130. t->SetText("Number of buttons: " + String(eventData[P_NUMBUTTONS].GetInt()));
  131. }
  132. void UIDrag::HandleDragMove(StringHash eventType, VariantMap& eventData)
  133. {
  134. using namespace DragBegin;
  135. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  136. int buttons = eventData[P_BUTTONS].GetInt();
  137. IntVector2 d = element->GetVar("DELTA").GetIntVector2();
  138. int X = eventData[P_X].GetInt() + d.x_;
  139. int Y = eventData[P_Y].GetInt() + d.y_;
  140. int BUTTONS = element->GetVar("BUTTONS").GetInt();
  141. auto* t = element->GetChildStaticCast<Text>("Event Touch", false);
  142. t->SetText("Drag Move Buttons: " + String(buttons));
  143. if (buttons == BUTTONS)
  144. element->SetPosition(IntVector2(X, Y));
  145. }
  146. void UIDrag::HandleDragCancel(StringHash eventType, VariantMap& eventData)
  147. {
  148. using namespace DragBegin;
  149. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  150. IntVector2 P = element->GetVar("START").GetIntVector2();
  151. element->SetPosition(P);
  152. }
  153. void UIDrag::HandleUpdate(StringHash eventType, VariantMap& eventData)
  154. {
  155. auto* ui = GetSubsystem<UI>();
  156. UIElement* root = ui->GetRoot();
  157. auto* input = GetSubsystem<Input>();
  158. unsigned n = input->GetNumTouches();
  159. for (unsigned i = 0; i < n; i++)
  160. {
  161. Text* t = (Text*)root->GetChild("Touch " + String(i));
  162. TouchState* ts = input->GetTouch(i);
  163. t->SetText("Touch " + String(ts->touchID_));
  164. IntVector2 pos = ts->position_;
  165. pos.y_ -= 30;
  166. t->SetPosition(pos);
  167. t->SetVisible(true);
  168. }
  169. for (unsigned i = n; i < 10; i++)
  170. {
  171. Text* t = (Text*)root->GetChild("Touch " + String(i));
  172. t->SetVisible(false);
  173. }
  174. if (input->GetKeyPress(KEY_SPACE))
  175. {
  176. PODVector<UIElement*> elements;
  177. root->GetChildrenWithTag(elements, "SomeTag");
  178. for (PODVector<UIElement*>::ConstIterator i = elements.Begin(); i != elements.End(); ++i)
  179. {
  180. UIElement* element = *i;
  181. element->SetVisible(!element->IsVisible());
  182. }
  183. }
  184. }