37_UIDrag.as 5.1 KB

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