UIDrag.cpp 7.4 KB

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