37_UIDrag.as 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. b.SetStyleAuto();
  37. b.minWidth = 250;
  38. b.position = IntVector2(50*i, 50*i);
  39. // Enable the bring-to-front flag and set the initial priority
  40. b.bringToFront = true;
  41. b.priority = i;
  42. // Set the layout mode to make the child text elements aligned vertically
  43. b.SetLayout(LM_VERTICAL, 20, IntRect(40, 40, 40, 40));
  44. Array<String> dragInfos = {"Num Touch", "Text", "Event Touch"};
  45. for (uint j = 0; j < dragInfos.length; ++j)
  46. b.CreateChild("Text", dragInfos[j]).SetStyleAuto();
  47. if (i % 2 == 0)
  48. b.AddTag("SomeTag");
  49. SubscribeToEvent(b, "Click", "HandleClick");
  50. SubscribeToEvent(b, "DragMove", "HandleDragMove");
  51. SubscribeToEvent(b, "DragBegin", "HandleDragBegin");
  52. SubscribeToEvent(b, "DragCancel", "HandleDragCancel");
  53. }
  54. for (int i = 0; i < 10; i++)
  55. {
  56. Text@ t = Text();
  57. root.AddChild(t);
  58. t.SetStyleAuto();
  59. t.name = "Touch "+ String(i);
  60. t.visible = false;
  61. t.priority = 100; // Make sure it has higher priority than the buttons
  62. }
  63. }
  64. void CreateInstructions()
  65. {
  66. // Construct new Text object, set string to display and font to use
  67. Text@ instructionText = ui.root.CreateChild("Text");
  68. instructionText.text = "Drag on the buttons to move them around.\n" +
  69. "Touch input allows also multi-drag.\n" +
  70. "Press SPACE to show/hide tagged UI elements.";
  71. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  72. instructionText.textAlignment = HA_CENTER;
  73. // Position the text relative to the screen center
  74. instructionText.horizontalAlignment = HA_CENTER;
  75. instructionText.verticalAlignment = VA_CENTER;
  76. instructionText.SetPosition(0, ui.root.height / 4);
  77. }
  78. void SubscribeToEvents()
  79. {
  80. // Subscribe HandleUpdate() function for processing update events
  81. SubscribeToEvent("Update", "HandleUpdate");
  82. }
  83. void HandleClick(StringHash eventType, VariantMap& eventData)
  84. {
  85. Button@ element = eventData["Element"].GetPtr();
  86. element.BringToFront();
  87. }
  88. void HandleDragBegin(StringHash eventType, VariantMap& eventData)
  89. {
  90. Button@ element = eventData["Element"].GetPtr();
  91. int lx = eventData["X"].GetInt();
  92. int ly = eventData["Y"].GetInt();
  93. IntVector2 p = element.position;
  94. element.vars[VAR_START] = p;
  95. element.vars[VAR_DELTA] = IntVector2(p.x - lx, p.y - ly);
  96. int buttons = eventData["Buttons"].GetInt();
  97. element.vars[VAR_BUTTONS] = buttons;
  98. Text@ t = element.GetChild(String("Text"));
  99. t.text = "Drag Begin Buttons: " + String(buttons);
  100. t = element.GetChild(String("Num Touch"));
  101. t.text = "Number of buttons: " + String(eventData["NumButtons"].GetInt());
  102. }
  103. void HandleDragMove(StringHash eventType, VariantMap& eventData)
  104. {
  105. Button@ element = eventData["Element"].GetPtr();
  106. int buttons = eventData["Buttons"].GetInt();
  107. IntVector2 d = element.vars[VAR_DELTA].GetIntVector2();
  108. int X = eventData["X"].GetInt() + d.x;
  109. int Y = eventData["Y"].GetInt() + d.y;
  110. int BUTTONS = element.vars[VAR_BUTTONS].GetInt();
  111. Text@ t = element.GetChild(String("Event Touch"));
  112. t.text = "Drag Move Buttons: " + String(buttons);
  113. if (buttons == BUTTONS)
  114. element.position = IntVector2(X, Y);
  115. }
  116. void HandleDragCancel(StringHash eventType, VariantMap& eventData)
  117. {
  118. Button@ element = eventData["Element"].GetPtr();
  119. IntVector2 P = element.vars[VAR_START].GetIntVector2();
  120. element.position = P;
  121. }
  122. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  123. {
  124. UIElement@ root = ui.root;
  125. uint n = input.numTouches;
  126. for (uint i = 0; i < n; i++)
  127. {
  128. Text@ t = root.GetChild("Touch " + String(i));
  129. TouchState@ ts = input.touches[i];
  130. t.text = "Touch "+ String(ts.touchID);
  131. IntVector2 pos = ts.position;
  132. pos.y -= 30;
  133. t.position = pos;
  134. t.visible = true;
  135. }
  136. for (uint i = n; i < 10; i++)
  137. {
  138. Text@ t = root.GetChild("Touch " + String(i));
  139. t.visible = false;
  140. }
  141. if (input.keyPress[KEY_SPACE])
  142. {
  143. Array<UIElement@>@ elements = root.GetChildrenWithTag("SomeTag");
  144. for (uint i = 0; i < elements.length; ++i)
  145. elements[i].visible = !elements[i].visible;
  146. }
  147. }
  148. // Create XML patch instructions for screen joystick layout specific to this sample app
  149. String patchInstructions =
  150. "<patch>" +
  151. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  152. " <attribute name=\"Is Visible\" value=\"false\" />" +
  153. " </add>" +
  154. "</patch>";