Urho2DSpriterAnimation.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Engine/Engine.h>
  5. #include <Urho3D/Graphics/Camera.h>
  6. #include <Urho3D/Graphics/Graphics.h>
  7. #include <Urho3D/Graphics/Octree.h>
  8. #include <Urho3D/Graphics/Renderer.h>
  9. #include <Urho3D/Graphics/Zone.h>
  10. #include <Urho3D/Input/Input.h>
  11. #include <Urho3D/Resource/ResourceCache.h>
  12. #include <Urho3D/Scene/Scene.h>
  13. #include <Urho3D/UI/Font.h>
  14. #include <Urho3D/UI/Text.h>
  15. #include <Urho3D/Urho2D/AnimatedSprite2D.h>
  16. #include <Urho3D/Urho2D/AnimationSet2D.h>
  17. #include "Urho2DSpriterAnimation.h"
  18. #include <Urho3D/DebugNew.h>
  19. URHO3D_DEFINE_APPLICATION_MAIN(Urho2DSpriterAnimation)
  20. Urho2DSpriterAnimation::Urho2DSpriterAnimation(Context* context) :
  21. Sample(context),
  22. spriterAnimationIndex_(0)
  23. {
  24. }
  25. void Urho2DSpriterAnimation::Start()
  26. {
  27. // Execute base class startup
  28. Sample::Start();
  29. // Create the scene content
  30. CreateScene();
  31. // Create the UI content
  32. CreateInstructions();
  33. // Setup the viewport for displaying the scene
  34. SetupViewport();
  35. // Set the mouse mode to use in the sample
  36. Sample::InitMouseMode(MM_FREE);
  37. // Hook up to the frame update events
  38. SubscribeToEvents();
  39. }
  40. void Urho2DSpriterAnimation::CreateScene()
  41. {
  42. scene_ = new Scene(context_);
  43. scene_->CreateComponent<Octree>();
  44. // Create camera node
  45. cameraNode_ = scene_->CreateChild("Camera");
  46. // Set camera's position
  47. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  48. auto* camera = cameraNode_->CreateComponent<Camera>();
  49. camera->SetOrthographic(true);
  50. auto* graphics = GetSubsystem<Graphics>();
  51. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  52. 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)
  53. auto* cache = GetSubsystem<ResourceCache>();
  54. auto* spriterAnimationSet = cache->GetResource<AnimationSet2D>("Urho2D/imp/imp.scml");
  55. if (!spriterAnimationSet)
  56. return;
  57. spriterNode_ = scene_->CreateChild("SpriterAnimation");
  58. auto* spriterAnimatedSprite = spriterNode_->CreateComponent<AnimatedSprite2D>();
  59. spriterAnimatedSprite->SetAnimationSet(spriterAnimationSet);
  60. spriterAnimatedSprite->SetAnimation(spriterAnimationSet->GetAnimation(spriterAnimationIndex_));
  61. }
  62. void Urho2DSpriterAnimation::CreateInstructions()
  63. {
  64. auto* cache = GetSubsystem<ResourceCache>();
  65. auto* ui = GetSubsystem<UI>();
  66. // Construct new Text object, set string to display and font to use
  67. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  68. instructionText->SetText("Mouse click to play next animation, \nUse WASD keys to move, use PageUp PageDown keys to zoom.");
  69. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  70. instructionText->SetTextAlignment(HA_CENTER); // Center rows in relation to each other
  71. // Position the text relative to the screen center
  72. instructionText->SetHorizontalAlignment(HA_CENTER);
  73. instructionText->SetVerticalAlignment(VA_CENTER);
  74. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  75. }
  76. void Urho2DSpriterAnimation::SetupViewport()
  77. {
  78. auto* renderer = GetSubsystem<Renderer>();
  79. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  80. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  81. renderer->SetViewport(0, viewport);
  82. }
  83. void Urho2DSpriterAnimation::MoveCamera(float timeStep)
  84. {
  85. // Do not move if the UI has a focused element (the console)
  86. if (GetSubsystem<UI>()->GetFocusElement())
  87. return;
  88. auto* input = GetSubsystem<Input>();
  89. // Movement speed as world units per second
  90. const float MOVE_SPEED = 4.0f;
  91. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  92. if (input->GetKeyDown(KEY_W))
  93. cameraNode_->Translate(Vector3::UP * MOVE_SPEED * timeStep);
  94. if (input->GetKeyDown(KEY_S))
  95. cameraNode_->Translate(Vector3::DOWN * MOVE_SPEED * timeStep);
  96. if (input->GetKeyDown(KEY_A))
  97. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  98. if (input->GetKeyDown(KEY_D))
  99. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  100. if (input->GetKeyDown(KEY_PAGEUP))
  101. {
  102. auto* camera = cameraNode_->GetComponent<Camera>();
  103. camera->SetZoom(camera->GetZoom() * 1.01f);
  104. }
  105. if (input->GetKeyDown(KEY_PAGEDOWN))
  106. {
  107. auto* camera = cameraNode_->GetComponent<Camera>();
  108. camera->SetZoom(camera->GetZoom() * 0.99f);
  109. }
  110. }
  111. void Urho2DSpriterAnimation::SubscribeToEvents()
  112. {
  113. // Subscribe HandleUpdate() function for processing update events
  114. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Urho2DSpriterAnimation, HandleUpdate));
  115. SubscribeToEvent(E_MOUSEBUTTONDOWN, URHO3D_HANDLER(Urho2DSpriterAnimation, HandleMouseButtonDown));
  116. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  117. UnsubscribeFromEvent(E_SCENEUPDATE);
  118. }
  119. void Urho2DSpriterAnimation::HandleUpdate(StringHash eventType, VariantMap& eventData)
  120. {
  121. using namespace Update;
  122. // Take the frame time step, which is stored as a float
  123. float timeStep = eventData[P_TIMESTEP].GetFloat();
  124. // Move the camera, scale movement with time step
  125. MoveCamera(timeStep);
  126. }
  127. void Urho2DSpriterAnimation::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
  128. {
  129. auto* spriterAnimatedSprite = spriterNode_->GetComponent<AnimatedSprite2D>();
  130. AnimationSet2D* spriterAnimationSet = spriterAnimatedSprite->GetAnimationSet();
  131. spriterAnimationIndex_ = (spriterAnimationIndex_ + 1) % spriterAnimationSet->GetNumAnimations();
  132. spriterAnimatedSprite->SetAnimation(spriterAnimationSet->GetAnimation(spriterAnimationIndex_), LM_FORCE_LOOPED);
  133. }