UIDrag.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // Copyright (c) 2008-2017 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->SetStyle("Button");
  63. b->SetSize(300, 100);
  64. b->SetPosition(IntVector2(50*i, 50*i));
  65. if (i % 2 == 0)
  66. b->AddTag("SomeTag");
  67. SubscribeToEvent(b, E_DRAGMOVE, URHO3D_HANDLER(UIDrag, HandleDragMove));
  68. SubscribeToEvent(b, E_DRAGBEGIN, URHO3D_HANDLER(UIDrag, HandleDragBegin));
  69. SubscribeToEvent(b, E_DRAGCANCEL, URHO3D_HANDLER(UIDrag, HandleDragCancel));
  70. SubscribeToEvent(b, E_DRAGEND, URHO3D_HANDLER(UIDrag, HandleDragEnd));
  71. {
  72. auto* t = new Text(context_);
  73. b->AddChild(t);
  74. t->SetStyle("Text");
  75. t->SetHorizontalAlignment(HA_CENTER);
  76. t->SetVerticalAlignment(VA_CENTER);
  77. t->SetName("Text");
  78. }
  79. {
  80. auto* t = new Text(context_);
  81. b->AddChild(t);
  82. t->SetStyle("Text");
  83. t->SetName("Event Touch");
  84. t->SetHorizontalAlignment(HA_CENTER);
  85. t->SetVerticalAlignment(VA_BOTTOM);
  86. }
  87. {
  88. auto* t = new Text(context_);
  89. b->AddChild(t);
  90. t->SetStyle("Text");
  91. t->SetName("Num Touch");
  92. t->SetHorizontalAlignment(HA_CENTER);
  93. t->SetVerticalAlignment(VA_TOP);
  94. }
  95. }
  96. for (int i = 0; i < 10; i++)
  97. {
  98. auto* t = new Text(context_);
  99. root->AddChild(t);
  100. t->SetStyle("Text");
  101. t->SetName("Touch "+ String(i));
  102. t->SetVisible(false);
  103. }
  104. }
  105. void UIDrag::CreateInstructions()
  106. {
  107. auto* cache = GetSubsystem<ResourceCache>();
  108. auto* ui = GetSubsystem<UI>();
  109. // Construct new Text object, set string to display and font to use
  110. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  111. instructionText->SetText("Drag on the buttons to move them around.\n"
  112. "Touch input allows also multi-drag.\n"
  113. "Press SPACE to show/hide tagged UI elements.");
  114. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  115. instructionText->SetTextAlignment(HA_CENTER);
  116. // Position the text relative to the screen center
  117. instructionText->SetHorizontalAlignment(HA_CENTER);
  118. instructionText->SetVerticalAlignment(VA_CENTER);
  119. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  120. }
  121. void UIDrag::SubscribeToEvents()
  122. {
  123. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(UIDrag, HandleUpdate));
  124. }
  125. void UIDrag::HandleDragBegin(StringHash eventType, VariantMap& eventData)
  126. {
  127. using namespace DragBegin;
  128. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  129. int lx = eventData[P_X].GetInt();
  130. int ly = eventData[P_Y].GetInt();
  131. IntVector2 p = element->GetPosition();
  132. element->SetVar("START", p);
  133. element->SetVar("DELTA", IntVector2(p.x_ - lx, p.y_ - ly));
  134. int buttons = eventData[P_BUTTONS].GetInt();
  135. element->SetVar("BUTTONS", buttons);
  136. Text* t = (Text*)element->GetChild(String("Text"));
  137. t->SetText("Drag Begin Buttons: " + String(buttons));
  138. t = (Text*)element->GetChild(String("Num Touch"));
  139. t->SetText("Number of buttons: " + String(eventData[P_NUMBUTTONS].GetInt()));
  140. }
  141. void UIDrag::HandleDragMove(StringHash eventType, VariantMap& eventData)
  142. {
  143. using namespace DragBegin;
  144. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  145. int buttons = eventData[P_BUTTONS].GetInt();
  146. IntVector2 d = element->GetVar("DELTA").GetIntVector2();
  147. int X = eventData[P_X].GetInt() + d.x_;
  148. int Y = eventData[P_Y].GetInt() + d.y_;
  149. int BUTTONS = element->GetVar("BUTTONS").GetInt();
  150. Text* t = (Text*)element->GetChild(String("Event Touch"));
  151. t->SetText("Drag Move Buttons: " + String(buttons));
  152. if (buttons == BUTTONS)
  153. element->SetPosition(IntVector2(X, Y));
  154. }
  155. void UIDrag::HandleDragCancel(StringHash eventType, VariantMap& eventData)
  156. {
  157. using namespace DragBegin;
  158. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  159. IntVector2 P = element->GetVar("START").GetIntVector2();
  160. element->SetPosition(P);
  161. }
  162. void UIDrag::HandleDragEnd(StringHash eventType, VariantMap& eventData)
  163. {
  164. using namespace DragBegin;
  165. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  166. }
  167. void UIDrag::HandleUpdate(StringHash eventType, VariantMap& eventData)
  168. {
  169. auto* ui = GetSubsystem<UI>();
  170. UIElement* root = ui->GetRoot();
  171. auto* input = GetSubsystem<Input>();
  172. unsigned n = input->GetNumTouches();
  173. for (unsigned i = 0; i < n; i++)
  174. {
  175. Text* t = (Text*)root->GetChild("Touch " + String(i));
  176. TouchState* ts = input->GetTouch(i);
  177. t->SetText("Touch " + String(ts->touchID_));
  178. IntVector2 pos = ts->position_;
  179. pos.y_ -= 30;
  180. t->SetPosition(pos);
  181. t->SetVisible(true);
  182. }
  183. for (unsigned i = n; i < 10; i++)
  184. {
  185. Text* t = (Text*)root->GetChild("Touch " + String(i));
  186. t->SetVisible(false);
  187. }
  188. if (input->GetKeyPress(KEY_SPACE))
  189. {
  190. PODVector<UIElement*> elements;
  191. root->GetChildrenWithTag(elements, "SomeTag");
  192. for (PODVector<UIElement*>::ConstIterator i = elements.Begin(); i != elements.End(); ++i)
  193. {
  194. UIElement* element = *i;
  195. element->SetVisible(!element->IsVisible());
  196. }
  197. }
  198. }