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