37_UIDrag.lua 5.5 KB

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