37_UIDrag.lua 5.6 KB

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