Urho2DParticle.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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/ParticleEffect2D.h>
  16. #include <Urho3D/Urho2D/ParticleEmitter2D.h>
  17. #include "Urho2DParticle.h"
  18. #include <Urho3D/DebugNew.h>
  19. URHO3D_DEFINE_APPLICATION_MAIN(Urho2DParticle)
  20. Urho2DParticle::Urho2DParticle(Context* context) :
  21. Sample(context)
  22. {
  23. }
  24. void Urho2DParticle::Start()
  25. {
  26. // Execute base class startup
  27. Sample::Start();
  28. // Set mouse visible
  29. auto* input = GetSubsystem<Input>();
  30. input->SetMouseVisible(true);
  31. // Create the scene content
  32. CreateScene();
  33. // Create the UI content
  34. CreateInstructions();
  35. // Setup the viewport for displaying the scene
  36. SetupViewport();
  37. // Hook up to the frame update events
  38. SubscribeToEvents();
  39. // Set the mouse mode to use in the sample
  40. Sample::InitMouseMode(MM_FREE);
  41. }
  42. void Urho2DParticle::CreateScene()
  43. {
  44. scene_ = new Scene(context_);
  45. scene_->CreateComponent<Octree>();
  46. // Create camera node
  47. cameraNode_ = scene_->CreateChild("Camera");
  48. // Set camera's position
  49. cameraNode_->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
  50. auto* camera = cameraNode_->CreateComponent<Camera>();
  51. camera->SetOrthographic(true);
  52. auto* graphics = GetSubsystem<Graphics>();
  53. camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
  54. camera->SetZoom(1.2f * 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.2) is set for full visibility at 1280x800 resolution)
  55. auto* cache = GetSubsystem<ResourceCache>();
  56. auto* particleEffect = cache->GetResource<ParticleEffect2D>("Urho2D/sun.pex");
  57. if (!particleEffect)
  58. return;
  59. particleNode_ = scene_->CreateChild("ParticleEmitter2D");
  60. auto* particleEmitter = particleNode_->CreateComponent<ParticleEmitter2D>();
  61. particleEmitter->SetEffect(particleEffect);
  62. auto* greenSpiralEffect = cache->GetResource<ParticleEffect2D>("Urho2D/greenspiral.pex");
  63. if (!greenSpiralEffect)
  64. return;
  65. Node* greenSpiralNode = scene_->CreateChild("GreenSpiral");
  66. auto* greenSpiralEmitter = greenSpiralNode->CreateComponent<ParticleEmitter2D>();
  67. greenSpiralEmitter->SetEffect(greenSpiralEffect);
  68. }
  69. void Urho2DParticle::CreateInstructions()
  70. {
  71. auto* cache = GetSubsystem<ResourceCache>();
  72. auto* ui = GetSubsystem<UI>();
  73. // Construct new Text object, set string to display and font to use
  74. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  75. instructionText->SetText("Use mouse/touch to move the particle.");
  76. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  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 Urho2DParticle::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 Urho2DParticle::SubscribeToEvents()
  90. {
  91. SubscribeToEvent(E_MOUSEMOVE, URHO3D_HANDLER(Urho2DParticle, HandleMouseMove));
  92. if (touchEnabled_)
  93. SubscribeToEvent(E_TOUCHMOVE, URHO3D_HANDLER(Urho2DParticle, HandleMouseMove));
  94. // Unsubscribe the SceneUpdate event from base class to prevent camera pitch and yaw in 2D sample
  95. UnsubscribeFromEvent(E_SCENEUPDATE);
  96. }
  97. void Urho2DParticle::HandleMouseMove(StringHash eventType, VariantMap& eventData)
  98. {
  99. if (particleNode_)
  100. {
  101. using namespace MouseMove;
  102. auto x = (float)eventData[P_X].GetI32();
  103. auto y = (float)eventData[P_Y].GetI32();
  104. auto* graphics = GetSubsystem<Graphics>();
  105. auto* camera = cameraNode_->GetComponent<Camera>();
  106. particleNode_->SetPosition(camera->ScreenToWorldPoint(Vector3(x / graphics->GetWidth(), y / graphics->GetHeight(), 10.0f)));
  107. }
  108. }