SpriterAnimation.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/Engine/Engine.h>
  25. #include <Atomic/Graphics/Camera.h>
  26. #include <Atomic/Graphics/Graphics.h>
  27. #include <Atomic/Graphics/Octree.h>
  28. #include <Atomic/Graphics/Renderer.h>
  29. #include <Atomic/Graphics/Zone.h>
  30. #include <Atomic/Input/Input.h>
  31. #include <Atomic/Resource/ResourceCache.h>
  32. #include <Atomic/Scene/Scene.h>
  33. #include <Atomic/Scene/SceneEvents.h>
  34. #include <Atomic/Atomic2D/AnimatedSprite2D.h>
  35. #include <Atomic/Atomic2D/AnimationSet2D.h>
  36. #include "SpriterAnimation.h"
  37. #include <Atomic/DebugNew.h>
  38. SpriterAnimation::SpriterAnimation(Context* context) :
  39. Sample(context),
  40. spriterAnimationIndex_(0)
  41. {
  42. }
  43. void SpriterAnimation::Start()
  44. {
  45. // Execute base class startup
  46. Sample::Start();
  47. // Create the scene content
  48. CreateScene();
  49. // Create the UI content
  50. CreateInstructions();
  51. // Setup the viewport for displaying the scene
  52. SetupViewport();
  53. // Set the mouse mode to use in the sample
  54. Sample::InitMouseMode(MM_FREE);
  55. // Hook up to the frame update events
  56. SubscribeToEvents();
  57. }
  58. void SpriterAnimation::CreateScene()
  59. {
  60. scene_ = new Scene(context_);
  61. scene_->CreateComponent<Octree>();
  62. // Create camera node
  63. cameraNode_ = scene_->CreateChild("Camera");
  64. // Set camera's position
  65. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  66. Camera* camera = cameraNode_->CreateComponent<Camera>();
  67. camera->SetOrthographic(true);
  68. Graphics* graphics = GetSubsystem<Graphics>();
  69. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  70. camera->SetZoom(1.5f * Min((float)graphics->GetWidth() / 1280.0f, (float)graphics->GetHeight() / 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)
  71. ResourceCache* cache = GetSubsystem<ResourceCache>();
  72. AnimationSet2D* spriterAnimationSet = cache->GetResource<AnimationSet2D>("Urho2D/imp/imp.scml");
  73. if (!spriterAnimationSet)
  74. return;
  75. spriterNode_ = scene_->CreateChild("SpriterAnimation");
  76. AnimatedSprite2D* spriterAnimatedSprite = spriterNode_->CreateComponent<AnimatedSprite2D>();
  77. spriterAnimatedSprite->SetAnimationSet(spriterAnimationSet);
  78. spriterAnimatedSprite->SetAnimation(spriterAnimationSet->GetAnimation(spriterAnimationIndex_));
  79. }
  80. void SpriterAnimation::CreateInstructions()
  81. {
  82. SimpleCreateInstructions("Mouse click to play next animation, \nUse WASD keys to move, use PageUp PageDown keys to zoom.");
  83. }
  84. void SpriterAnimation::SetupViewport()
  85. {
  86. Renderer* renderer = GetSubsystem<Renderer>();
  87. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  88. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  89. renderer->SetViewport(0, viewport);
  90. }
  91. void SpriterAnimation::MoveCamera(float timeStep)
  92. {
  93. Input* input = GetSubsystem<Input>();
  94. // Movement speed as world units per second
  95. const float MOVE_SPEED = 4.0f;
  96. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  97. if (input->GetKeyDown(KEY_W))
  98. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  99. if (input->GetKeyDown(KEY_S))
  100. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  101. if (input->GetKeyDown(KEY_A))
  102. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  103. if (input->GetKeyDown(KEY_D))
  104. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  105. if (input->GetKeyDown(KEY_PAGEUP))
  106. {
  107. Camera* camera = cameraNode_->GetComponent<Camera>();
  108. camera->SetZoom(camera->GetZoom() * 1.01f);
  109. }
  110. if (input->GetKeyDown(KEY_PAGEDOWN))
  111. {
  112. Camera* camera = cameraNode_->GetComponent<Camera>();
  113. camera->SetZoom(camera->GetZoom() * 0.99f);
  114. }
  115. }
  116. void SpriterAnimation::SubscribeToEvents()
  117. {
  118. // Subscribe HandleUpdate() function for processing update events
  119. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(SpriterAnimation, HandleUpdate));
  120. SubscribeToEvent(E_MOUSEBUTTONDOWN, ATOMIC_HANDLER(SpriterAnimation, HandleMouseButtonDown));
  121. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  122. UnsubscribeFromEvent(E_SCENEUPDATE);
  123. }
  124. void SpriterAnimation::HandleUpdate(StringHash eventType, VariantMap& eventData)
  125. {
  126. using namespace Update;
  127. // Take the frame time step, which is stored as a float
  128. float timeStep = eventData[P_TIMESTEP].GetFloat();
  129. // Move the camera, scale movement with time step
  130. MoveCamera(timeStep);
  131. }
  132. void SpriterAnimation::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  133. {
  134. AnimatedSprite2D* spriterAnimatedSprite = spriterNode_->GetComponent<AnimatedSprite2D>();
  135. AnimationSet2D* spriterAnimationSet = spriterAnimatedSprite->GetAnimationSet();
  136. spriterAnimationIndex_ = (spriterAnimationIndex_ + 1) % spriterAnimationSet->GetNumAnimations();
  137. spriterAnimatedSprite->SetAnimation(spriterAnimationSet->GetAnimation(spriterAnimationIndex_), LM_FORCE_LOOPED);
  138. }