UIDrag.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/UI/Button.h>
  5. #include <Urho3D/UI/Font.h>
  6. #include <Urho3D/UI/Text.h>
  7. #include <Urho3D/UI/UIEvents.h>
  8. #include "UIDrag.h"
  9. #include <Urho3D/DebugNew.h>
  10. URHO3D_DEFINE_APPLICATION_MAIN(UIDrag)
  11. UIDrag::UIDrag(Context* context) :
  12. Sample(context)
  13. {
  14. }
  15. void UIDrag::Start()
  16. {
  17. // Execute base class startup
  18. Sample::Start();
  19. // Set mouse visible
  20. String platform = GetPlatform();
  21. if (platform != "Android" && platform != "iOS")
  22. GetSubsystem<Input>()->SetMouseVisible(true);
  23. // Create the UI content
  24. CreateGUI();
  25. CreateInstructions();
  26. // Hook up to the frame update events
  27. SubscribeToEvents();
  28. // Set the mouse mode to use in the sample
  29. Sample::InitMouseMode(MM_FREE);
  30. }
  31. void UIDrag::CreateGUI()
  32. {
  33. auto* cache = GetSubsystem<ResourceCache>();
  34. auto* ui = GetSubsystem<UI>();
  35. UIElement* root = ui->GetRoot();
  36. // Load the style sheet from xml
  37. root->SetDefaultStyle(cache->GetResource<XMLFile>("UI/DefaultStyle.xml"));
  38. for (int i=0; i < 10; i++)
  39. {
  40. auto* b = new Button(context_);
  41. root->AddChild(b);
  42. // Reference a style from the style sheet loaded earlier:
  43. b->SetStyleAuto();
  44. b->SetMinWidth(250);
  45. b->SetPosition(IntVector2(50*i, 50*i));
  46. // Enable the bring-to-front flag and set the initial priority
  47. b->SetBringToFront(true);
  48. b->SetPriority(i);
  49. // Set the layout mode to make the child text elements aligned vertically
  50. b->SetLayout(LM_VERTICAL, 20, {40, 40, 40, 40});
  51. auto dragInfos = {"Num Touch", "Text", "Event Touch"};
  52. for (auto name: dragInfos)
  53. b->CreateChild<Text>(name)->SetStyleAuto();
  54. if (i % 2 == 0)
  55. b->AddTag("SomeTag");
  56. SubscribeToEvent(b, E_CLICK, URHO3D_HANDLER(UIDrag, HandleClick));
  57. SubscribeToEvent(b, E_DRAGMOVE, URHO3D_HANDLER(UIDrag, HandleDragMove));
  58. SubscribeToEvent(b, E_DRAGBEGIN, URHO3D_HANDLER(UIDrag, HandleDragBegin));
  59. SubscribeToEvent(b, E_DRAGCANCEL, URHO3D_HANDLER(UIDrag, HandleDragCancel));
  60. }
  61. for (int i = 0; i < 10; i++)
  62. {
  63. auto* t = new Text(context_);
  64. root->AddChild(t);
  65. t->SetStyleAuto();
  66. t->SetName("Touch "+ String(i));
  67. t->SetVisible(false);
  68. t->SetPriority(100); // Make sure it has higher priority than the buttons
  69. }
  70. }
  71. void UIDrag::CreateInstructions()
  72. {
  73. auto* cache = GetSubsystem<ResourceCache>();
  74. auto* ui = GetSubsystem<UI>();
  75. // Construct new Text object, set string to display and font to use
  76. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  77. instructionText->SetText("Drag on the buttons to move them around.\n"
  78. "Touch input allows also multi-drag.\n"
  79. "Press SPACE to show/hide tagged UI elements.");
  80. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  81. instructionText->SetTextAlignment(HA_CENTER);
  82. // Position the text relative to the screen center
  83. instructionText->SetHorizontalAlignment(HA_CENTER);
  84. instructionText->SetVerticalAlignment(VA_CENTER);
  85. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  86. }
  87. void UIDrag::SubscribeToEvents()
  88. {
  89. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(UIDrag, HandleUpdate));
  90. }
  91. void UIDrag::HandleClick(StringHash eventType, VariantMap& eventData)
  92. {
  93. using namespace Click;
  94. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  95. element->BringToFront();
  96. }
  97. void UIDrag::HandleDragBegin(StringHash eventType, VariantMap& eventData)
  98. {
  99. using namespace DragBegin;
  100. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  101. int lx = eventData[P_X].GetI32();
  102. int ly = eventData[P_Y].GetI32();
  103. IntVector2 p = element->GetPosition();
  104. element->SetVar("START", p);
  105. element->SetVar("DELTA", IntVector2(p.x_ - lx, p.y_ - ly));
  106. int buttons = eventData[P_BUTTONS].GetI32();
  107. element->SetVar("BUTTONS", buttons);
  108. auto* t = element->GetChildStaticCast<Text>("Text", false);
  109. t->SetText("Drag Begin Buttons: " + String(buttons));
  110. t = element->GetChildStaticCast<Text>("Num Touch", false);
  111. t->SetText("Number of buttons: " + String(eventData[P_NUMBUTTONS].GetI32()));
  112. }
  113. void UIDrag::HandleDragMove(StringHash eventType, VariantMap& eventData)
  114. {
  115. using namespace DragBegin;
  116. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  117. int buttons = eventData[P_BUTTONS].GetI32();
  118. IntVector2 d = element->GetVar("DELTA").GetIntVector2();
  119. int X = eventData[P_X].GetI32() + d.x_;
  120. int Y = eventData[P_Y].GetI32() + d.y_;
  121. int BUTTONS = element->GetVar("BUTTONS").GetI32();
  122. auto* t = element->GetChildStaticCast<Text>("Event Touch", false);
  123. t->SetText("Drag Move Buttons: " + String(buttons));
  124. if (buttons == BUTTONS)
  125. element->SetPosition(IntVector2(X, Y));
  126. }
  127. void UIDrag::HandleDragCancel(StringHash eventType, VariantMap& eventData)
  128. {
  129. using namespace DragBegin;
  130. auto* element = (Button*)eventData[P_ELEMENT].GetVoidPtr();
  131. IntVector2 P = element->GetVar("START").GetIntVector2();
  132. element->SetPosition(P);
  133. }
  134. void UIDrag::HandleUpdate(StringHash eventType, VariantMap& eventData)
  135. {
  136. auto* ui = GetSubsystem<UI>();
  137. UIElement* root = ui->GetRoot();
  138. auto* input = GetSubsystem<Input>();
  139. unsigned n = input->GetNumTouches();
  140. for (unsigned i = 0; i < n; i++)
  141. {
  142. Text* t = (Text*)root->GetChild("Touch " + String(i));
  143. TouchState* ts = input->GetTouch(i);
  144. t->SetText("Touch " + String(ts->touchID_));
  145. IntVector2 pos = ts->position_;
  146. pos.y_ -= 30;
  147. t->SetPosition(pos);
  148. t->SetVisible(true);
  149. }
  150. for (unsigned i = n; i < 10; i++)
  151. {
  152. Text* t = (Text*)root->GetChild("Touch " + String(i));
  153. t->SetVisible(false);
  154. }
  155. if (input->GetKeyPress(KEY_SPACE))
  156. {
  157. Vector<UIElement*> elements;
  158. root->GetChildrenWithTag(elements, "SomeTag");
  159. for (Vector<UIElement*>::ConstIterator i = elements.Begin(); i != elements.End(); ++i)
  160. {
  161. UIElement* element = *i;
  162. element->SetVisible(!element->IsVisible());
  163. }
  164. }
  165. }