37_UIDrag.as 5.0 KB

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