Urho2DStretchableSprite.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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/Input/Input.h>
  10. #include <Urho3D/Resource/ResourceCache.h>
  11. #include <Urho3D/Scene/Scene.h>
  12. #include <Urho3D/UI/Font.h>
  13. #include <Urho3D/UI/Text.h>
  14. #include <Urho3D/Urho2D/Sprite2D.h>
  15. #include <Urho3D/Urho2D/StaticSprite2D.h>
  16. #include <Urho3D/Urho2D/StretchableSprite2D.h>
  17. #include "Urho2DStretchableSprite.h"
  18. #include <Urho3D/DebugNew.h>
  19. URHO3D_DEFINE_APPLICATION_MAIN(Urho2DStretchableSprite)
  20. Urho2DStretchableSprite::Urho2DStretchableSprite(Context* context) :
  21. Sample(context)
  22. {
  23. }
  24. void Urho2DStretchableSprite::Start()
  25. {
  26. // Execute base class startup
  27. Sample::Start();
  28. // Create the scene content
  29. CreateScene();
  30. // Create the UI content
  31. CreateInstructions();
  32. // Setup the viewport for displaying the scene
  33. SetupViewport();
  34. // Hook up to the frame update events
  35. SubscribeToEvents();
  36. // Set the mouse mode to use in the sample
  37. Sample::InitMouseMode(MM_FREE);
  38. }
  39. void Urho2DStretchableSprite::CreateScene()
  40. {
  41. scene_ = new Scene(context_);
  42. scene_->CreateComponent<Octree>();
  43. // Create camera node
  44. cameraNode_ = scene_->CreateChild("Camera");
  45. // Set camera's position
  46. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  47. auto* camera = cameraNode_->CreateComponent<Camera>();
  48. camera->SetOrthographic(true);
  49. auto* graphics = GetSubsystem<Graphics>();
  50. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  51. refSpriteNode_ = scene_->CreateChild("regular sprite");
  52. stretchSpriteNode_ = scene_->CreateChild("stretchable sprite");
  53. auto* cache = GetSubsystem<ResourceCache>();
  54. auto* sprite = cache->GetResource<Sprite2D>("Urho2D/Stretchable.png");
  55. if (sprite)
  56. {
  57. refSpriteNode_->CreateComponent<StaticSprite2D>()->SetSprite(sprite);
  58. auto stretchSprite = stretchSpriteNode_->CreateComponent<StretchableSprite2D>();
  59. stretchSprite->SetSprite(sprite);
  60. stretchSprite->SetBorder({25, 25, 25, 25});
  61. refSpriteNode_->Translate2D(Vector2{-2.0f, 0.0f});
  62. stretchSpriteNode_->Translate2D(Vector2{2.0f, 0.0f});
  63. }
  64. }
  65. void Urho2DStretchableSprite::CreateInstructions()
  66. {
  67. auto* cache = GetSubsystem<ResourceCache>();
  68. auto* ui = GetSubsystem<UI>();
  69. // Construct new Text object, set string to display and font to use
  70. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  71. instructionText->SetText(
  72. "Use WASD keys to transform, Tab key to cycle through\n"
  73. "Scale, Rotate, and Translate transform modes. In Rotate\n"
  74. "mode, combine A/D keys with Ctrl key to rotate about\n"
  75. "the Z axis");
  76. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 12);
  77. // Position the text relative to the screen center
  78. instructionText->SetHorizontalAlignment(HA_CENTER);
  79. instructionText->SetVerticalAlignment(VA_CENTER);
  80. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  81. }
  82. void Urho2DStretchableSprite::SetupViewport()
  83. {
  84. auto* renderer = GetSubsystem<Renderer>();
  85. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  86. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  87. renderer->SetViewport(0, viewport);
  88. }
  89. void Urho2DStretchableSprite::SubscribeToEvents()
  90. {
  91. SubscribeToEvent(E_KEYUP, URHO3D_HANDLER(Urho2DStretchableSprite, OnKeyUp));
  92. // Subscribe HandleUpdate() function for processing update events
  93. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(Urho2DStretchableSprite, HandleUpdate));
  94. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  95. UnsubscribeFromEvent(E_SCENEUPDATE);
  96. }
  97. void Urho2DStretchableSprite::HandleUpdate(StringHash /*eventType*/, VariantMap& eventData)
  98. {
  99. using namespace Update;
  100. // Take the frame time step, which is stored as a float
  101. float timeStep = eventData[P_TIMESTEP].GetFloat();
  102. switch (selectTransform_)
  103. {
  104. case 0: ScaleSprites(timeStep);
  105. break;
  106. case 1: RotateSprites(timeStep);
  107. break;
  108. case 2: TranslateSprites(timeStep);
  109. break;
  110. default: URHO3D_LOGERRORF("bad transform selection: %d", selectTransform_);
  111. }
  112. }
  113. void Urho2DStretchableSprite::OnKeyUp(StringHash /*eventType*/, VariantMap& eventData)
  114. {
  115. using namespace KeyUp;
  116. const auto key = eventData[P_KEY].GetI32();
  117. if (key == KEY_TAB)
  118. {
  119. ++selectTransform_;
  120. selectTransform_ %= 3;
  121. }
  122. else if (key == KEY_ESCAPE)
  123. engine_->Exit();
  124. }
  125. void Urho2DStretchableSprite::TranslateSprites(float timeStep)
  126. {
  127. static constexpr auto speed = 1.0f;
  128. const auto input = GetSubsystem<Input>();
  129. const auto left = input->GetKeyDown(KEY_A),
  130. right = input->GetKeyDown(KEY_D),
  131. up = input->GetKeyDown(KEY_W),
  132. down = input->GetKeyDown(KEY_S);
  133. if (left || right || up || down)
  134. {
  135. const auto quantum = timeStep * speed;
  136. const auto translate = Vector2{(left ? -quantum : 0.0f) + (right ? quantum : 0.0f),
  137. (down ? -quantum : 0.0f) + (up ? quantum : 0.0f)};
  138. refSpriteNode_->Translate2D(translate);
  139. stretchSpriteNode_->Translate2D(translate);
  140. }
  141. }
  142. void Urho2DStretchableSprite::RotateSprites(float timeStep)
  143. {
  144. static constexpr auto speed = 45.0f;
  145. const auto input = GetSubsystem<Input>();
  146. const auto left = input->GetKeyDown(KEY_A),
  147. right = input->GetKeyDown(KEY_D),
  148. up = input->GetKeyDown(KEY_W),
  149. down = input->GetKeyDown(KEY_S),
  150. ctrl = input->GetKeyDown(KEY_CTRL);
  151. if (left || right || up || down)
  152. {
  153. const auto quantum = timeStep * speed;
  154. const auto xrot = (up ? -quantum : 0.0f) + (down ? quantum : 0.0f);
  155. const auto rot2 = (left ? -quantum : 0.0f) + (right ? quantum : 0.0f);
  156. const auto totalRot = Quaternion{xrot, ctrl ? 0.0f : rot2, ctrl ? rot2 : 0.0f};
  157. refSpriteNode_->Rotate(totalRot);
  158. stretchSpriteNode_->Rotate(totalRot);
  159. }
  160. }
  161. void Urho2DStretchableSprite::ScaleSprites(float timeStep)
  162. {
  163. static constexpr auto speed = 0.5f;
  164. const auto input = GetSubsystem<Input>();
  165. const auto left = input->GetKeyDown(KEY_A),
  166. right = input->GetKeyDown(KEY_D),
  167. up = input->GetKeyDown(KEY_W),
  168. down = input->GetKeyDown(KEY_S);
  169. if (left || right || up || down)
  170. {
  171. const auto quantum = timeStep * speed;
  172. const auto scale = Vector2{1.0f + (right ? quantum : left ? -quantum : 0.0f),
  173. 1.0f + (up ? quantum : down ? -quantum : 0.0f)};
  174. refSpriteNode_->Scale2D(scale);
  175. stretchSpriteNode_->Scale2D(scale);
  176. }
  177. }