Urho2DStretchableSprite.cpp 7.9 KB

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