33_Urho2DSpriterAnimation.as 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Urho2D sprite example.
  2. // This sample demonstrates:
  3. // - Creating a 2D scene with spriter animation
  4. // - Displaying the scene using the Renderer subsystem
  5. // - Handling keyboard to move and zoom 2D camera
  6. #include "Scripts/Utilities/Sample.as"
  7. Node@ spriterNode;
  8. int spriterAnimationIndex = 0;
  9. void Start()
  10. {
  11. // Execute the common startup for samples
  12. SampleStart();
  13. // Create the scene content
  14. CreateScene();
  15. // Create the UI content
  16. CreateInstructions();
  17. // Setup the viewport for displaying the scene
  18. SetupViewport();
  19. // Set the mouse mode to use in the sample
  20. SampleInitMouseMode(MM_FREE);
  21. // Hook up to the frame update events
  22. SubscribeToEvents();
  23. }
  24. void CreateScene()
  25. {
  26. scene_ = Scene();
  27. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  28. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  29. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  30. // optimizing manner
  31. scene_.CreateComponent("Octree");
  32. // Create a scene node for the camera, which we will move around
  33. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  34. cameraNode = scene_.CreateChild("Camera");
  35. // Set an initial position for the camera scene node above the plane
  36. cameraNode.position = Vector3(0.0f, 0.0f, -10.0f);
  37. Camera@ camera = cameraNode.CreateComponent("Camera");
  38. camera.orthographic = true;
  39. camera.orthoSize = graphics.height * PIXEL_SIZE;
  40. camera.zoom = 1.5f * Min(graphics.width / 1280.0f, graphics.height / 800.0f); // Set zoom according to user's resolution to ensure full visibility (initial zoom (1.5) is set for full visibility at 1280x800 resolution)
  41. AnimationSet2D@ spriterAnimationSet = cache.GetResource("AnimationSet2D", "Urho2D/imp/imp.scml");
  42. if (spriterAnimationSet is null)
  43. return;
  44. spriterNode = scene_.CreateChild("SpriterAnimation");
  45. AnimatedSprite2D@ spriterAnimatedSprite = spriterNode.CreateComponent("AnimatedSprite2D");
  46. spriterAnimatedSprite.animationSet = spriterAnimationSet;
  47. spriterAnimatedSprite.SetAnimation(spriterAnimationSet.GetAnimation(spriterAnimationIndex), LM_FORCE_LOOPED);
  48. }
  49. void CreateInstructions()
  50. {
  51. // Construct new Text object, set string to display and font to use
  52. Text@ instructionText = ui.root.CreateChild("Text");
  53. instructionText.text = "Mouse click to play next animation, \nUse WASD keys to move, use PageUp PageDown keys to zoom.";
  54. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  55. instructionText.textAlignment = HA_CENTER; // Center rows in relation to each other
  56. // Position the text relative to the screen center
  57. instructionText.horizontalAlignment = HA_CENTER;
  58. instructionText.verticalAlignment = VA_CENTER;
  59. instructionText.SetPosition(0, ui.root.height / 4);
  60. }
  61. void SetupViewport()
  62. {
  63. // 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
  64. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  65. // use, but now we just use full screen and default render path configured in the engine command line options
  66. Viewport@ viewport = Viewport(scene_, cameraNode.GetComponent("Camera"));
  67. renderer.viewports[0] = viewport;
  68. }
  69. void MoveCamera(float timeStep)
  70. {
  71. // Do not move if the UI has a focused element (the console)
  72. if (ui.focusElement !is null)
  73. return;
  74. // Movement speed as world units per second
  75. const float MOVE_SPEED = 4.0f;
  76. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  77. if (input.keyDown[KEY_W])
  78. cameraNode.Translate(Vector3::UP * MOVE_SPEED * timeStep);
  79. if (input.keyDown[KEY_S])
  80. cameraNode.Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  81. if (input.keyDown[KEY_A])
  82. cameraNode.Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  83. if (input.keyDown[KEY_D])
  84. cameraNode.Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  85. if (input.keyDown[KEY_PAGEUP])
  86. {
  87. Camera@ camera = cameraNode.GetComponent("Camera");
  88. camera.zoom = camera.zoom * 1.01f;
  89. }
  90. if (input.keyDown[KEY_PAGEDOWN])
  91. {
  92. Camera@ camera = cameraNode.GetComponent("Camera");
  93. camera.zoom = camera.zoom * 0.99f;
  94. }
  95. }
  96. void SubscribeToEvents()
  97. {
  98. // Subscribe HandleUpdate() function for processing update events
  99. SubscribeToEvent("Update", "HandleUpdate");
  100. SubscribeToEvent("MouseButtonDown", "HandleMouseButtonDown");
  101. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  102. UnsubscribeFromEvent("SceneUpdate");
  103. }
  104. void HandleUpdate(StringHash eventType, VariantMap& eventData)
  105. {
  106. // Take the frame time step, which is stored as a float
  107. float timeStep = eventData["TimeStep"].GetFloat();
  108. // Move the camera, scale movement with time step
  109. MoveCamera(timeStep);
  110. }
  111. void HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  112. {
  113. AnimatedSprite2D@ spriterAnimatedSprite = spriterNode.GetComponent("AnimatedSprite2D");
  114. AnimationSet2D@ spriterAnimationSet = spriterAnimatedSprite.animationSet;
  115. spriterAnimationIndex = (spriterAnimationIndex + 1) % spriterAnimationSet.numAnimations;
  116. spriterAnimatedSprite.SetAnimation(spriterAnimationSet.GetAnimation(spriterAnimationIndex), LM_FORCE_LOOPED);
  117. }
  118. // Create XML patch instructions for screen joystick layout specific to this sample app
  119. String patchInstructions =
  120. "<patch>" +
  121. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/attribute[@name='Is Visible']\" />" +
  122. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom In</replace>" +
  123. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button0']]\">" +
  124. " <element type=\"Text\">" +
  125. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  126. " <attribute name=\"Text\" value=\"PAGEUP\" />" +
  127. " </element>" +
  128. " </add>" +
  129. " <remove sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/attribute[@name='Is Visible']\" />" +
  130. " <replace sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]/element[./attribute[@name='Name' and @value='Label']]/attribute[@name='Text']/@value\">Zoom Out</replace>" +
  131. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button1']]\">" +
  132. " <element type=\"Text\">" +
  133. " <attribute name=\"Name\" value=\"KeyBinding\" />" +
  134. " <attribute name=\"Text\" value=\"PAGEDOWN\" />" +
  135. " </element>" +
  136. " </add>" +
  137. "</patch>";