UIDrag.cpp 6.8 KB

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