Urho2DSpriterAnimation.cpp 7.2 KB

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