UIDrag.cpp 6.8 KB

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