37_UIDrag.as 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Urho3D UI Drag Example:
  2. // This sample demonstrates:
  3. // - Creating GUI elements from AngelScript
  4. // - Loading GUI Style from xml
  5. // - Subscribing to GUI drag events and handling them
  6. // - Working with GUI elements with specific tags.
  7. #include "Scripts/Utilities/Sample.as"
  8. StringHash VAR_BUTTONS("BUTTONS");
  9. StringHash VAR_START("START");
  10. StringHash VAR_DELTA("DELTA");
  11. void Start()
  12. {
  13. // Execute base class startup
  14. SampleStart();
  15. // Set mouse visible
  16. String platform = GetPlatform();
  17. if (platform != "Android" and platform != "iOS")
  18. input.mouseVisible = true;
  19. // Create the UI content
  20. CreateGUI();
  21. CreateInstructions();
  22. // Hook up to the frame update events
  23. SubscribeToEvents();
  24. }
  25. void CreateGUI()
  26. {
  27. UIElement@ root = ui.root;
  28. // Load the style sheet from xml
  29. root.defaultStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  30. for (int i=0; i < 10; i++)
  31. {
  32. Button@ b = Button();
  33. root.AddChild(b);
  34. // Reference a style from the style sheet loaded earlier:
  35. b.style = "Button";
  36. b.size = IntVector2(300, 100);
  37. b.position = IntVector2(50*i, 50*i);
  38. if (i % 2 == 0)
  39. b.AddTag("SomeTag");
  40. SubscribeToEvent(b, "DragMove", "HandleDragMove");
  41. SubscribeToEvent(b, "DragBegin", "HandleDragBegin");
  42. SubscribeToEvent(b, "DragCancel", "HandleDragCancel");
  43. {
  44. Text@ t = Text();
  45. b.AddChild(t);
  46. t.style = "Text";
  47. t.horizontalAlignment = HA_CENTER;
  48. t.verticalAlignment = VA_CENTER;
  49. t.name = "Text";
  50. }
  51. {
  52. Text@ t = Text();
  53. b.AddChild(t);
  54. t.style = "Text";
  55. t.name = "Event Touch";
  56. t.horizontalAlignment = HA_CENTER;
  57. t.verticalAlignment = VA_BOTTOM;
  58. }
  59. {
  60. Text@ t = Text();
  61. b.AddChild(t);
  62. t.style ="Text";
  63. t.name = "Num Touch";
  64. t.horizontalAlignment = HA_CENTER;
  65. t.verticalAlignment = VA_TOP;
  66. }
  67. }
  68. for (int i = 0; i < 10; i++)
  69. {
  70. Text@ t = Text();
  71. root.AddChild(t);
  72. t.style = "Text";
  73. t.name = "Touch "+ String(i);
  74. t.visible = false;
  75. }
  76. }
  77. void CreateInstructions()
  78. {
  79. // Construct new Text object, set string to display and font to use
  80. Text@ instructionText = ui.root.CreateChild("Text");
  81. instructionText.text = "Drag on the buttons to move them around.\n" +
  82. "Touch input allows also multi-drag.\n" +
  83. "Press SPACE to show/hide tagged UI elements.";
  84. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  85. instructionText.textAlignment = HA_CENTER;
  86. // Position the text relative to the screen center
  87. instructionText.horizontalAlignment = HA_CENTER;
  88. instructionText.verticalAlignment = VA_CENTER;
  89. instructionText.SetPosition(0, ui.root.height / 4);
  90. }
  91. void SubscribeToEvents()
  92. {
  93. // Subscribe HandleUpdate() function for processing update events
  94. SubscribeToEvent("Update", "HandleUpdate");
  95. }
  96. void HandleDragBegin(StringHash eventType, VariantMap& eventData)
  97. {
  98. Button@ element = eventData["Element"].GetPtr();
  99. int lx = eventData["X"].GetInt();
  100. int ly = eventData["Y"].GetInt();
  101. IntVector2 p = element.position;
  102. element.vars[VAR_START] = p;
  103. element.vars[VAR_DELTA] = IntVector2(p.x - lx, p.y - ly);
  104. int buttons = eventData["Buttons"].GetInt();
  105. element.vars[VAR_BUTTONS] = buttons;
  106. Text@ t = element.GetChild(String("Text"));
  107. t.text = "Drag Begin Buttons: " + String(buttons);
  108. t = element.GetChild(String("Num Touch"));
  109. t.text = "Number of buttons: " + String(eventData["NumButtons"].GetInt());
  110. }
  111. void HandleDragMove(StringHash eventType, VariantMap& eventData)
  112. {
  113. Button@ element = eventData["Element"].GetPtr();
  114. int buttons = eventData["Buttons"].GetInt();
  115. IntVector2 d = element.vars[VAR_DELTA].GetIntVector2();
  116. int X = eventData["X"].GetInt() + d.x;
  117. int Y = eventData["Y"].GetInt() + d.y;
  118. int BUTTONS = element.vars[VAR_BUTTONS].GetInt();
  119. Text@ t = element.GetChild(String("Event Touch"));
  120. t.text = "Drag Move Buttons: " + String(buttons);
  121. if (buttons == BUTTONS)
  122. element.position = IntVector2(X, Y);
  123. }
  124. void HandleDragCancel(StringHash eventType, VariantMap& eventData)
  125. {
  126. Button@ element = eventData["Element"].GetPtr();
  127. IntVector2 P = element.vars[VAR_START].GetIntVector2();
  128. element.position = P;
  129. }
  130. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  131. {
  132. UIElement@ root = ui.root;
  133. uint n = input.numTouches;
  134. for (uint i = 0; i < n; i++)
  135. {
  136. Text@ t = root.GetChild("Touch " + String(i));
  137. TouchState@ ts = input.touches[i];
  138. t.text = "Touch "+ String(ts.touchID);
  139. IntVector2 pos = ts.position;
  140. pos.y -= 30;
  141. t.position = pos;
  142. t.visible = true;
  143. }
  144. for (uint i = n; i < 10; i++)
  145. {
  146. Text@ t = root.GetChild("Touch " + String(i));
  147. t.visible = false;
  148. }
  149. if (input.keyPress[KEY_SPACE])
  150. {
  151. Array<UIElement@>@ elements = root.GetChildrenWithTag("SomeTag");
  152. for (uint i = 0; i < elements.length; ++i)
  153. elements[i].visible = !elements[i].visible;
  154. }
  155. }
  156. // Create XML patch instructions for screen joystick layout specific to this sample app
  157. String patchInstructions =
  158. "<patch>" +
  159. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  160. " <attribute name=\"Is Visible\" value=\"false\" />" +
  161. " </add>" +
  162. "</patch>";