2
0

37_UIDrag.lua 4.8 KB

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