UIDrag.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. // Create the scene content
  52. CreateScene();
  53. // Create the UI content
  54. CreateGUI();
  55. CreateInstructions();
  56. // Setup the viewport for displaying the scene
  57. SetupViewport();
  58. // Hook up to the frame update events
  59. SubscribeToEvents();
  60. }
  61. void UIDrag::CreateScene()
  62. {
  63. scene_ = new Scene(context_);
  64. scene_->CreateComponent<Octree>();
  65. // Create camera node
  66. cameraNode_ = scene_->CreateChild("Camera");
  67. // Set camera's position
  68. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  69. Camera* camera = cameraNode_->CreateComponent<Camera>();
  70. camera->SetOrthographic(true);
  71. Graphics* graphics = GetSubsystem<Graphics>();
  72. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  73. GetSubsystem<Input>()->SetMouseVisible(true);
  74. }
  75. void UIDrag::CreateGUI()
  76. {
  77. ResourceCache* cache = GetSubsystem<ResourceCache>();
  78. UI* ui = GetSubsystem<UI>();
  79. UIElement* root = ui->GetRoot();
  80. // Load the style sheet from xml
  81. root->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  82. for (int i=0; i < 10; i++)
  83. {
  84. Button* b = new Button(context_);
  85. root->AddChild(b);
  86. // Reference a style from the style sheet loaded earlier:
  87. b->SetStyle("Button");
  88. b->SetSize(300, 100);
  89. b->SetPosition(IntVector2(50*i, 50*i));
  90. SubscribeToEvent(b, E_DRAGMOVE, HANDLER(UIDrag, HandleDragMove));
  91. SubscribeToEvent(b, E_DRAGBEGIN, HANDLER(UIDrag, HandleDragBegin));
  92. SubscribeToEvent(b, E_DRAGCANCEL, HANDLER(UIDrag, HandleDragCancel));
  93. SubscribeToEvent(b, E_DRAGEND, HANDLER(UIDrag, HandleDragEnd));
  94. {
  95. Text* t = new Text(context_);
  96. b->AddChild(t);
  97. t->SetStyle("Text");
  98. t->SetHorizontalAlignment(HA_CENTER);
  99. t->SetVerticalAlignment(VA_CENTER);
  100. t->SetName("Text");
  101. }
  102. {
  103. Text* t = new Text(context_);
  104. b->AddChild(t);
  105. t->SetStyle("Text");
  106. t->SetName("Event Touch");
  107. t->SetHorizontalAlignment(HA_CENTER);
  108. t->SetVerticalAlignment(VA_BOTTOM);
  109. }
  110. {
  111. Text* t = new Text(context_);
  112. b->AddChild(t);
  113. t->SetStyle("Text");
  114. t->SetName("Num Touch");
  115. t->SetHorizontalAlignment(HA_CENTER);
  116. t->SetVerticalAlignment(VA_TOP);
  117. }
  118. }
  119. for (int i = 0; i < 10; i++)
  120. {
  121. Text* t = new Text(context_);
  122. root->AddChild(t);
  123. t->SetStyle("Text");
  124. t->SetText("Touch "+ String(i));
  125. t->SetName("Touch "+ String(i));
  126. t->SetVisible(false);
  127. }
  128. }
  129. void UIDrag::CreateInstructions()
  130. {
  131. ResourceCache* cache = GetSubsystem<ResourceCache>();
  132. UI* ui = GetSubsystem<UI>();
  133. // Construct new Text object, set string to display and font to use
  134. Text* instructionText = ui->GetRoot()->CreateChild<Text>();
  135. instructionText->SetText("Drag on the buttons to move them around.\nMulti- button drag also supported.");
  136. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  137. // Position the text relative to the screen center
  138. instructionText->SetHorizontalAlignment(HA_CENTER);
  139. instructionText->SetVerticalAlignment(VA_CENTER);
  140. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  141. }
  142. void UIDrag::SetupViewport()
  143. {
  144. Renderer* renderer = GetSubsystem<Renderer>();
  145. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  146. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  147. renderer->SetViewport(0, viewport);
  148. }
  149. void UIDrag::SubscribeToEvents()
  150. {
  151. SubscribeToEvent(E_UPDATE, HANDLER(UIDrag, HandleUpdate));
  152. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  153. UnsubscribeFromEvent(E_SCENEUPDATE);
  154. }
  155. void UIDrag::HandleDragBegin(StringHash eventType, VariantMap& eventData)
  156. {
  157. using namespace DragBegin;
  158. Button* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  159. int lx = eventData[P_X].GetInt();
  160. int ly = eventData[P_Y].GetInt();
  161. IntVector2 p = element->GetPosition();
  162. element->SetVar("STARTX", p.x_);
  163. element->SetVar("STARTY", p.y_);
  164. element->SetVar("DX", p.x_ - lx);
  165. element->SetVar("DY", p.y_ - ly);
  166. int buttons = eventData[P_BUTTONS].GetInt();
  167. element->SetVar("BUTTONS", buttons);
  168. Text* t = (Text*)element->GetChild(String("Text"));
  169. t->SetText("Drag Begin Buttons: " + String(buttons));
  170. t = (Text*)element->GetChild(String("Num Touch"));
  171. t->SetText("Number of buttons: " + String(eventData[P_NUMBUTTONS].GetInt()));
  172. }
  173. void UIDrag::HandleDragMove(StringHash eventType, VariantMap& eventData)
  174. {
  175. using namespace DragBegin;
  176. Button* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  177. int buttons = eventData[P_BUTTONS].GetInt();
  178. int X = eventData[P_X].GetInt() + element->GetVar("DX").GetInt();
  179. int Y = eventData[P_Y].GetInt() + element->GetVar("DY").GetInt();
  180. int BUTTONS = element->GetVar("BUTTONS").GetInt();
  181. Text* t = (Text*)element->GetChild(String("Event Touch"));
  182. t->SetText("Drag Move Buttons: " + String(buttons));
  183. if (buttons == BUTTONS)
  184. element->SetPosition(IntVector2(X, Y));
  185. }
  186. void UIDrag::HandleDragCancel(StringHash eventType, VariantMap& eventData)
  187. {
  188. using namespace DragBegin;
  189. Button* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  190. int X = element->GetVar("STARTX").GetInt();
  191. int Y = element->GetVar("STARTY").GetInt();
  192. element->SetPosition(IntVector2(X, Y));
  193. }
  194. void UIDrag::HandleDragEnd(StringHash eventType, VariantMap& eventData)
  195. {
  196. using namespace DragBegin;
  197. Button* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  198. }
  199. void UIDrag::HandleUpdate(StringHash eventType, VariantMap& eventData)
  200. {
  201. UI* ui = GetSubsystem<UI>();
  202. UIElement* root = ui->GetRoot();
  203. Input* input = GetSubsystem<Input>();
  204. unsigned n = input->GetNumTouches();
  205. for (unsigned i = 0; i < n; i++)
  206. {
  207. Text* t = (Text*)root->GetChild("Touch " + String(i));
  208. TouchState* ts = input->GetTouch(i);
  209. IntVector2 pos = ts->position_;
  210. pos.y_ -= 30;
  211. t->SetPosition(pos);
  212. t->SetVisible(true);
  213. }
  214. for (unsigned i = n; i < 10; i++)
  215. {
  216. Text* t = (Text*)root->GetChild("Touch " + String(i));
  217. t->SetVisible(false);
  218. }
  219. }