37_UIDrag.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. b:SetStyleAuto()
  47. b.minWidth = 250
  48. b:SetPosition(IntVector2(50*i, 50*i))
  49. -- Enable the bring-to-front flag and set the initial priority
  50. b.bringToFront = true
  51. b.priority = i
  52. -- Set the layout mode to make the child text elements aligned vertically
  53. b:SetLayout(LM_VERTICAL, 20, IntRect(40, 40, 40, 40))
  54. local dragInfos = {"Num Touch", "Text", "Event Touch"}
  55. for j=1, table.getn(dragInfos) do
  56. b:CreateChild("Text", dragInfos[j]):SetStyleAuto()
  57. end
  58. if i % 2 == 0 then
  59. b:AddTag("SomeTag")
  60. end
  61. SubscribeToEvent(b, "Click", "HandleClick")
  62. SubscribeToEvent(b, "DragMove", "HandleDragMove")
  63. SubscribeToEvent(b, "DragBegin", "HandleDragBegin")
  64. SubscribeToEvent(b, "DragCancel", "HandleDragCancel")
  65. end
  66. for i = 0, 9, 1 do
  67. local t = Text:new()
  68. ui.root:AddChild(t)
  69. t:SetStyleAuto()
  70. t:SetName("Touch " .. i)
  71. t.visible = false
  72. t.priority = 100 -- Make sure it has higher priority than the buttons
  73. end
  74. end
  75. function SubscribeToEvents()
  76. -- Subscribe HandleUpdate() function for processing update events
  77. SubscribeToEvent("Update", "HandleUpdate")
  78. end
  79. function HandleClick(eventType, eventData)
  80. local element = eventData["Element"]:GetPtr("UIElement")
  81. element:BringToFront()
  82. end
  83. function HandleDragBegin(eventType, eventData)
  84. local element = eventData["Element"]:GetPtr("UIElement")
  85. local lx = eventData["X"]:GetInt()
  86. local ly = eventData["Y"]:GetInt()
  87. local p = element.position
  88. element:SetVar(VAR_START, Variant(p))
  89. element:SetVar(VAR_DELTA, Variant(Vector2(p.x - lx, p.y - ly)))
  90. local buttons = eventData["Buttons"]:GetInt()
  91. element:SetVar(VAR_BUTTONS, Variant(buttons))
  92. element:GetChild("Text").text = "Drag Begin Buttons: " .. buttons
  93. element:GetChild("Num Touch").text = "Number of buttons: " .. eventData["NumButtons"]:GetInt()
  94. end
  95. function HandleDragMove(eventType, eventData)
  96. local element = eventData["Element"]:GetPtr("UIElement")
  97. local buttons = eventData["Buttons"]:GetInt()
  98. local d = element:GetVar(VAR_DELTA):GetVector2()
  99. local X = eventData["X"]:GetInt() + d.x
  100. local Y = eventData["Y"]:GetInt() + d.y
  101. local BUTTONS = element:GetVar(VAR_BUTTONS):GetInt()
  102. element:GetChild("Event Touch").text = "Drag Move Buttons: " .. buttons
  103. element:SetPosition(IntVector2(X, Y))
  104. end
  105. function HandleDragCancel(eventType, eventData)
  106. local element = eventData["Element"]:GetPtr("UIElement")
  107. local P = element:GetVar(VAR_START):GetIntVector2()
  108. element:SetPosition(P)
  109. end
  110. function HandleUpdate(eventType, eventData)
  111. local n = input:GetNumTouches()
  112. local i = 0
  113. while i < n do
  114. local t = tolua.cast(ui.root:GetChild("Touch " .. i), 'Text')
  115. local ts = input:GetTouch(i)
  116. t:SetText("Touch " .. ts.touchID)
  117. local pos = IntVector2(ts.position)
  118. pos.y = pos.y - 30
  119. t:SetPosition(pos)
  120. t:SetVisible(true)
  121. i = i + 1
  122. end
  123. i = n
  124. while i < 10 do
  125. local t = tolua.cast(ui.root:GetChild("Touch " .. i), 'Text')
  126. t:SetVisible(false)
  127. i = i + 1
  128. end
  129. if input:GetKeyPress(KEY_SPACE) then
  130. elements = ui.root:GetChildrenWithTag("SomeTag")
  131. for i, element in ipairs(elements) do
  132. element.visible = not element.visible
  133. end
  134. end
  135. end
  136. -- Create XML patch instructions for screen joystick layout specific to this sample app
  137. function GetScreenJoystickPatchString()
  138. return "<patch>" ..
  139. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  140. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  141. " </add>" ..
  142. "</patch>"
  143. end