51_Urho2DStretchableSprite.lua 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. -- Urho2D stretchable sprite example.
  2. -- This sample demonstrates:
  3. -- - Creating a 2D scene with both static and stretchable sprites
  4. -- - Difference in scaling static and stretchable sprites
  5. -- - Similarity in otherwise transforming static and stretchable sprites
  6. -- - Displaying the scene using the Renderer subsystem
  7. -- - Handling keyboard to transform nodes
  8. require "LuaScripts/Utilities/Sample"
  9. local selectTransform = 0 -- Transform mode tracking index.
  10. local refSpriteNode = nil -- Reference (static) sprite node.
  11. local stretchSpriteNode = nil -- Stretchable sprite node.
  12. function Start()
  13. -- Execute base class startup
  14. SampleStart()
  15. -- Create the scene content
  16. CreateScene()
  17. -- Create the UI content
  18. CreateInstructions()
  19. -- Setup the viewport for displaying the scene
  20. SetupViewport()
  21. -- Set the mouse mode to use in the sample
  22. SampleInitMouseMode(MM_FREE)
  23. -- Hook up to the frame update events
  24. SubscribeToEvents()
  25. end
  26. function CreateScene()
  27. scene_ = Scene()
  28. -- Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  29. -- show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates it
  30. -- is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  31. -- optimizing manner
  32. scene_:CreateComponent("Octree")
  33. -- Create a scene node for the camera, which we will move around
  34. -- The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  35. cameraNode = scene_:CreateChild("Camera")
  36. -- Set an initial position for the camera scene node above the plane
  37. cameraNode.position = Vector3(0.0, 0.0, -10.0)
  38. local camera = cameraNode:CreateComponent("Camera")
  39. camera.orthographic = true
  40. camera.orthoSize = graphics.height * PIXEL_SIZE
  41. -- Setup sprites and nodes on the scene
  42. refSpriteNode = scene_:CreateChild("regular sprite")
  43. stretchSpriteNode = scene_:CreateChild("stretch sprite")
  44. local sprite = cache:GetResource("Sprite2D", "Urho2D/Stretchable.png")
  45. if sprite then
  46. local staticSprite = refSpriteNode:CreateComponent("StaticSprite2D")
  47. staticSprite.sprite = sprite
  48. local stretchSprite = stretchSpriteNode:CreateComponent("StretchableSprite2D")
  49. stretchSprite.sprite = sprite
  50. stretchSprite.border = IntRect(25, 25, 25, 25)
  51. refSpriteNode:Translate2D(Vector2(-2, 0))
  52. stretchSpriteNode:Translate2D(Vector2(2, 0))
  53. end
  54. end
  55. function CreateInstructions()
  56. -- Construct new Text object, set string to display and font to use
  57. local instructionText = ui.root:CreateChild("Text")
  58. instructionText:SetText("Use WASD keys and mouse to move, Use PageUp PageDown to zoom.")
  59. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  60. -- Position the text relative to the screen center
  61. instructionText.horizontalAlignment = HA_CENTER
  62. instructionText.verticalAlignment = VA_CENTER
  63. instructionText:SetPosition(0, ui.root.height / 4)
  64. end
  65. function SetupViewport()
  66. -- 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
  67. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  68. -- use, but now we just use full screen and default render path configured in the engine command line options
  69. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  70. renderer:SetViewport(0, viewport)
  71. end
  72. function SubscribeToEvents()
  73. -- Subscribe HandleUpdate() function for processing update events
  74. SubscribeToEvent("Update", "HandleUpdate")
  75. -- Subscribe HandleUpdate() function for processing update events
  76. SubscribeToEvent("KeyUp", "HandleKeyUp")
  77. -- Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  78. UnsubscribeFromEvent("SceneUpdate")
  79. end
  80. function HandleUpdate(eventType, eventData)
  81. local timeStep = eventData["TimeStep"]:GetFloat()
  82. if selectTransform == 0 then
  83. ScaleSprites(timeStep)
  84. elseif selectTransform == 1 then
  85. RotateSprites(timeStep)
  86. elseif selectTransform == 2 then
  87. TranslateSprites(timeStep)
  88. else
  89. log:Write(LOG_ERROR, "bad transform selection " .. selectTransform)
  90. end
  91. end
  92. function HandleKeyUp(eventType, eventData)
  93. local key = eventData["Key"]:GetInt()
  94. if key == KEY_TAB then
  95. selectTransform = (selectTransform + 1) % 3
  96. elseif key == KEY_ESCAPE then
  97. engine:Exit()
  98. end
  99. end
  100. function TranslateSprites(timeStep)
  101. local speed = 1
  102. local left = input:GetKeyDown(KEY_A)
  103. local right = input:GetKeyDown(KEY_D)
  104. local up = input:GetKeyDown(KEY_W)
  105. local down = input:GetKeyDown(KEY_S)
  106. if left or right or up or down then
  107. local quantum = timeStep * speed
  108. local translate = Vector2((left and -quantum or 0) + (right and quantum or 0),
  109. (down and -quantum or 0) + (up and quantum or 0))
  110. refSpriteNode:Translate2D(translate)
  111. stretchSpriteNode:Translate2D(translate)
  112. end
  113. end
  114. function RotateSprites(timeStep)
  115. local speed = 45
  116. local left = input:GetKeyDown(KEY_A)
  117. local right = input:GetKeyDown(KEY_D)
  118. local up = input:GetKeyDown(KEY_W)
  119. local down = input:GetKeyDown(KEY_S)
  120. local ctrl = input:GetKeyDown(KEY_CTRL)
  121. if left or right or up or down then
  122. local quantum = timeStep * speed
  123. local xrot = (up and -quantum or 0) + (down and quantum or 0)
  124. local rot2 = (left and -quantum or 0) + (right and quantum or 0)
  125. local totalRot = Quaternion(xrot, ctrl and 0 or rot2, ctrl and rot2 or 0)
  126. refSpriteNode:Rotate(totalRot)
  127. stretchSpriteNode:Rotate(totalRot)
  128. end
  129. end
  130. function ScaleSprites(timeStep)
  131. local speed = 0.5
  132. local left = input:GetKeyDown(KEY_A)
  133. local right = input:GetKeyDown(KEY_D)
  134. local up = input:GetKeyDown(KEY_W)
  135. local down = input:GetKeyDown(KEY_S)
  136. if left or right or up or down then
  137. local quantum = timeStep * speed
  138. local scale = Vector2(1 + (right and quantum or left and -quantum or 0),
  139. 1 + (up and quantum or down and -quantum or 0))
  140. refSpriteNode:Scale2D(scale)
  141. stretchSpriteNode:Scale2D(scale)
  142. end
  143. end
  144. -- Create XML patch instructions for screen joystick layout specific to this sample app
  145. function GetScreenJoystickPatchString()
  146. return
  147. "<patch>" ..
  148. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" ..
  149. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">TAB</replace>" ..
  150. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" ..
  151. " <element type=\"Text\">" ..
  152. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  153. " <attribute name=\"Text\" value=\"TAB\" />" ..
  154. " </element>" ..
  155. " </add>" ..
  156. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" ..
  157. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">CTRL</replace>" ..
  158. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" ..
  159. " <element type=\"Text\">" ..
  160. " <attribute name=\"Name\" value=\"KeyBinding\" />" ..
  161. " <attribute name=\"Text\" value=\"LCTRL\" />" ..
  162. " </element>" ..
  163. " </add>" ..
  164. "</patch>"
  165. end