37_UIDrag.lua 5.0 KB

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