UIDrag.cpp 7.0 KB

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