37_UIDrag.as 5.7 KB

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