SceneResourceEditor.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include "AtomicEditor.h"
  5. #include <Atomic/IO/Log.h>
  6. #include <Atomic/Core/CoreEvents.h>
  7. #include <Atomic/Scene/Scene.h>
  8. #include <Atomic/Graphics/Camera.h>
  9. #include <Atomic/IO/FileSystem.h>
  10. #include <Atomic/Resource/ResourceCache.h>
  11. #include <Atomic/Physics/PhysicsWorld.h>
  12. #include "../AEEditor.h"
  13. #include <Atomic/Input/Input.h>
  14. #include "SceneResourceEditor.h"
  15. #include <Atomic/UI/TBUI.h>
  16. #include <Atomic/UI/UI.h>
  17. #include <Atomic/UI/View3D.h>
  18. namespace AtomicEditor
  19. {
  20. SceneResourceEditor ::SceneResourceEditor(Context* context, const String &fullpath, TBTabContainer *container) :
  21. ResourceEditor(context, fullpath, container),
  22. layout_(0),
  23. view3DContainer_(0),
  24. yaw_(0.0f),
  25. pitch_(0.0f)
  26. {
  27. layout_ = new TBLayout();
  28. layout_->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
  29. layout_->SetSize(container_->GetRect().w, container_->GetRect().h);
  30. view3DContainer_ = new TBContainer();
  31. view3DContainer_->SetGravity(WIDGET_GRAVITY_ALL);
  32. ResourceCache* cache = GetSubsystem<ResourceCache>();
  33. scene_ = new Scene(context_);
  34. SharedPtr<File> xmlFile = cache->GetFile(fullpath);
  35. if (GetExtension(fullpath) == ".xml")
  36. scene_->LoadXML(*xmlFile);
  37. else
  38. scene_->Load(*xmlFile);
  39. // TODO: Fix this properly, currently simulates immediately
  40. PhysicsWorld* physics = scene_->GetComponent<PhysicsWorld>();
  41. if (physics)
  42. scene_->RemoveComponent(physics);
  43. cameraNode_ = scene_->CreateChild("Camera");
  44. camera_ = cameraNode_->CreateComponent<Camera>();
  45. if (fullpath_.Contains("CompartmentScene"))
  46. {
  47. pitch_ = 19.4f;
  48. yaw_ = -228.6f;
  49. cameraNode_->SetPosition(Vector3(-42.66f, 6.68f, 9.34f));
  50. }
  51. else
  52. {
  53. pitch_ = 26.8f;
  54. yaw_ = 121.4f;
  55. cameraNode_->SetPosition(Vector3(-135.81f, 76.56f, 16.42f));
  56. }
  57. // in a view3D_ procsky renders wrong (inversed?)
  58. //Node* skyNode = scene_->CreateChild("ProcSky", LOCAL );
  59. //skyNode->CreateComponent<ProcSky>();
  60. layout_->AddChild(view3DContainer_);
  61. container_->GetContentRoot()->AddChild(layout_);
  62. view3D_ = new View3D(context_);
  63. view3D_->SetView(scene_, camera_);
  64. view3D_->SetAutoUpdate(false);
  65. GetSubsystem<TBUI>()->AddChild(view3D_);
  66. SubscribeToEvent(E_UPDATE, HANDLER(SceneResourceEditor, HandleUpdate));
  67. }
  68. SceneResourceEditor::~SceneResourceEditor()
  69. {
  70. }
  71. bool SceneResourceEditor::OnEvent(const TBWidgetEvent &ev)
  72. {
  73. return false;
  74. }
  75. void SceneResourceEditor::MoveCamera(float timeStep)
  76. {
  77. // Do not move if the UI has a focused element (the console)
  78. if (GetSubsystem<UI>()->GetFocusElement())
  79. return;
  80. Input* input = GetSubsystem<Input>();
  81. // Movement speed as world units per second
  82. const float MOVE_SPEED = 20.0f;
  83. // Mouse sensitivity as degrees per pixel
  84. const float MOUSE_SENSITIVITY = 0.2f;
  85. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  86. if (input->GetKeyDown('F'))
  87. {
  88. IntVector2 mouseMove = input->GetMouseMove();
  89. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  90. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  91. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  92. }
  93. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  94. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  95. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  96. // Use the Translate() function (default local space) to move relative to the node's orientation.
  97. if (input->GetKeyDown('W'))
  98. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  99. if (input->GetKeyDown('S'))
  100. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  101. if (input->GetKeyDown('A'))
  102. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  103. if (input->GetKeyDown('D'))
  104. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  105. }
  106. void SceneResourceEditor::HandleUpdate(StringHash eventType, VariantMap& eventData)
  107. {
  108. if ((layout_->GetVisibility() != WIDGET_VISIBILITY_VISIBLE) || GetSubsystem<Editor>()->IsPlayingProject())
  109. {
  110. if (view3D_->IsVisible())
  111. view3D_->SetVisible(false);
  112. return;
  113. }
  114. if (!view3D_->IsVisible())
  115. view3D_->SetVisible(true);
  116. /*
  117. Vector3 cpos = cameraNode_->GetPosition();
  118. Quaternion crot = cameraNode_->GetRotation();
  119. LOGINFOF("%f %f %f - %f %f", cpos.x_, cpos.y_, cpos.z_, pitch_, yaw_);
  120. */
  121. // Timestep parameter is same no matter what event is being listened to
  122. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  123. MoveCamera(timeStep);
  124. bool dirty = false;
  125. TBRect rect = layout_->GetRect();
  126. TBWidget* parent = layout_->GetParent();
  127. while (parent)
  128. {
  129. TBRect prect = parent->GetRect();
  130. rect.x += prect.x;
  131. rect.y += prect.y;
  132. parent = parent->GetParent();
  133. }
  134. const IntVector2& pos = view3D_->GetPosition();
  135. if (pos.x_ != rect.x || pos.y_ != rect.y)
  136. dirty = true;
  137. const IntVector2& size = view3D_->GetSize();
  138. if (size.x_ != rect.w || size.y_ != rect.h)
  139. dirty = true;
  140. if (dirty)
  141. {
  142. view3D_->SetPosition(rect.x, rect.y);
  143. view3D_->SetWidth(rect.w);
  144. view3D_->SetHeight(rect.h);
  145. }
  146. view3D_->QueueUpdate();
  147. }
  148. }