37_UIDrag.lua 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 scene content
  14. CreateScene()
  15. -- Create the UI content
  16. CreateGUI()
  17. CreateInstructions()
  18. -- Setup the viewport for displaying the scene
  19. SetupViewport()
  20. -- Hook up to the frame update events
  21. SubscribeToEvents()
  22. end
  23. function CreateScene()
  24. scene_ = Scene()
  25. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  26. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  27. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  28. -- optimizing manner
  29. scene_:CreateComponent("Octree")
  30. -- Create a scene node for the camera, which we will move around
  31. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  32. cameraNode = scene_:CreateChild("Camera")
  33. -- Set an initial position for the camera scene node above the plane
  34. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  35. local camera = cameraNode:CreateComponent("Camera")
  36. camera.orthographic = true
  37. camera.orthoSize = graphics.height * PIXEL_SIZE
  38. input:SetMouseVisible(true)
  39. end
  40. function CreateInstructions()
  41. -- Construct new Text object, set string to display and font to use
  42. local instructionText = ui.root:CreateChild("Text")
  43. instructionText:SetText("Drag on the buttons to move them around.\nMulti- button drag also supported.")
  44. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  45. -- Position the text relative to the screen center
  46. instructionText.horizontalAlignment = HA_CENTER
  47. instructionText.verticalAlignment = VA_CENTER
  48. instructionText:SetPosition(0, ui.root.height / 4)
  49. end
  50. function CreateGUI()
  51. -- Load the style sheet from xml
  52. ui.root.defaultStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  53. for i=0, 9, 1 do
  54. local b = Button:new()
  55. ui.root:AddChild(b)
  56. -- Reference a style from the style sheet loaded earlier:
  57. b:SetStyle("Button")
  58. b:SetMinSize(IntVector2(300, 100))
  59. b:SetPosition(IntVector2(50*i, 50*i))
  60. SubscribeToEvent(b, "DragMove", "HandleDragMove")
  61. SubscribeToEvent(b, "DragBegin", "HandleDragBegin")
  62. SubscribeToEvent(b, "DragCancel", "HandleDragCancel")
  63. local t = Text:new()
  64. b:AddChild(t)
  65. t:SetStyle("Text")
  66. t:SetHorizontalAlignment(HA_CENTER)
  67. t:SetVerticalAlignment(VA_CENTER)
  68. t:SetName("Text")
  69. t = Text:new()
  70. b:AddChild(t)
  71. t:SetStyle("Text")
  72. t:SetName("Event Touch")
  73. t:SetHorizontalAlignment(HA_CENTER)
  74. t:SetVerticalAlignment(VA_BOTTOM)
  75. t = Text:new()
  76. b:AddChild(t)
  77. t:SetStyle("Text")
  78. t:SetName("Num Touch")
  79. t:SetHorizontalAlignment(HA_CENTER)
  80. t:SetVerticalAlignment(VA_TOP)
  81. end
  82. for i = 0, 9, 1 do
  83. local t = Text:new()
  84. ui.root:AddChild(t)
  85. t:SetStyle("Text")
  86. t:SetText("Touch " .. i)
  87. t:SetName("Touch " .. i)
  88. t:SetVisible(false)
  89. end
  90. end
  91. function SetupViewport()
  92. -- Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  93. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  94. -- use, but now we just use full screen and default render path configured in the engine command line options
  95. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  96. renderer:SetViewport(0, viewport)
  97. end
  98. function SubscribeToEvents()
  99. -- Subscribe HandleUpdate() function for processing update events
  100. SubscribeToEvent("Update", "HandleUpdate")
  101. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  102. UnsubscribeFromEvent("SceneUpdate")
  103. end
  104. function HandleDragBegin(eventType, eventData)
  105. local element = eventData:GetPtr("UIElement", "Element")
  106. local lx = eventData:GetInt("X")
  107. local ly = eventData:GetInt("Y")
  108. local p = element.position
  109. element:SetVar(VAR_START, Variant(p))
  110. element:SetVar(VAR_DELTA, Variant(Vector2(p.x - lx, p.y - ly)))
  111. local buttons = eventData:GetInt("Buttons")
  112. element:SetVar(VAR_BUTTONS, Variant(buttons))
  113. local t = tolua.cast(element:GetChild("Text"), 'Text')
  114. t:SetText("Drag Begin Buttons: " .. buttons)
  115. t = tolua.cast(element:GetChild("Num Touch"), 'Text')
  116. t:SetText("Number of buttons: " .. eventData:GetInt("NumButtons"))
  117. end
  118. function HandleDragMove(eventType, eventData)
  119. local element = eventData:GetPtr("UIElement", "Element")
  120. local buttons = eventData:GetInt("Buttons")
  121. local d = element:GetVar(VAR_DELTA):GetVector2()
  122. local X = eventData:GetInt("X") + d.x
  123. local Y = eventData:GetInt("Y") + d.y
  124. local BUTTONS = element:GetVar(VAR_BUTTONS):GetInt()
  125. local t = tolua.cast(element:GetChild("Event Touch"), 'Text')
  126. t:SetText("Drag Move Buttons: " .. buttons)
  127. element:SetPosition(IntVector2(X, Y))
  128. end
  129. function HandleDragCancel(eventType, eventData)
  130. local element = eventData:GetPtr("UIElement", "Element")
  131. local P = element:GetVar(VAR_START):GetIntVector2()
  132. element:SetPosition(P)
  133. end
  134. function HandleUpdate(eventType, eventData)
  135. local n = input:GetNumTouches()
  136. local i = 0
  137. while i < n do
  138. local t = tolua.cast(ui.root:GetChild("Touch " .. i), 'Text')
  139. local ts = input:GetTouch(i)
  140. local pos = ts.position
  141. pos.y = pos.y - 30
  142. t:SetPosition(pos)
  143. t:SetVisible(true)
  144. i = i + 1
  145. end
  146. i = n
  147. while i < 10 do
  148. local t = tolua.cast(ui.root:GetChild("Touch " .. i), 'Text')
  149. t:SetVisible(false)
  150. i = i + 1
  151. end
  152. end
  153. -- Create XML patch instructions for screen joystick layout specific to this sample app
  154. function GetScreenJoystickPatchString()
  155. return "<patch>" ..
  156. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  157. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  158. " </add>" ..
  159. "</patch>"
  160. end