SceneEditor3D.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/Graphics/DebugRenderer.h>
  10. #include <Atomic/Graphics/Viewport.h>
  11. #include <Atomic/Graphics/Octree.h>
  12. #include <Atomic/IO/FileSystem.h>
  13. #include <Atomic/Resource/ResourceCache.h>
  14. #include <Atomic/Physics/PhysicsWorld.h>
  15. #include "AEEditor.h"
  16. #include "AEEvents.h"
  17. #include <Atomic/Input/Input.h>
  18. #include <Atomic/UI/UI.h>
  19. #include <ToolCore/Assets/AssetDatabase.h>
  20. #include <ToolCore/Assets/Asset.h>
  21. #include "SceneEditor3D.h"
  22. #include "SceneEditor3DEvents.h"
  23. using namespace ToolCore;
  24. namespace AtomicEditor
  25. {
  26. SceneEditor3D ::SceneEditor3D(Context* context, const String &fullpath, UITabContainer *container) :
  27. ResourceEditor(context, fullpath, container)
  28. {
  29. ResourceCache* cache = GetSubsystem<ResourceCache>();
  30. scene_ = new Scene(context_);
  31. SharedPtr<File> xmlFile = cache->GetFile(fullpath);
  32. if (GetExtension(fullpath) == ".scene")
  33. scene_->LoadXML(*xmlFile);
  34. else
  35. scene_->Load(*xmlFile);
  36. scene_->SetUpdateEnabled(false);
  37. sceneView_ = new SceneView3D(context_, this);
  38. // EARLY ACCESS
  39. if (fullpath.Find(String("ToonTown")) != String::NPOS)
  40. {
  41. sceneView_->GetCameraNode()->SetWorldPosition(Vector3(-119.073f, 76.1121f, 16.47763f));
  42. Quaternion q(0.55f, 0.14f, 0.8f, -0.2f);
  43. sceneView_->SetYaw(q.YawAngle());
  44. sceneView_->SetPitch(q.PitchAngle());
  45. sceneView_->GetCameraNode()->SetWorldRotation(q);
  46. }
  47. else
  48. {
  49. Node* playerSpawn = scene_->GetChild("PlayerInfoStart", true);
  50. if (playerSpawn)
  51. {
  52. sceneView_->GetCameraNode()->SetPosition(playerSpawn->GetPosition());
  53. sceneView_->SetYaw(playerSpawn->GetRotation().EulerAngles().y_);
  54. }
  55. }
  56. sceneView_->SetGravity(UI_GRAVITY_ALL);
  57. rootContentWidget_->AddChild(sceneView_);
  58. gizmo3D_ = new Gizmo3D(context_);
  59. gizmo3D_->SetView(sceneView_);
  60. gizmo3D_->Show();
  61. SubscribeToEvent(E_UPDATE, HANDLER(SceneEditor3D, HandleUpdate));
  62. SubscribeToEvent(E_EDITORACTIVENODECHANGE, HANDLER(SceneEditor3D, HandleEditorActiveNodeChange));
  63. SubscribeToEvent(E_GIZMOEDITMODECHANGED, HANDLER(SceneEditor3D, HandleGizmoEditModeChanged));
  64. // FIXME: Set the size at the end of setup, so all children are updated accordingly
  65. // future size changes will be handled automatically
  66. IntRect rect = container_->GetContentRoot()->GetRect();
  67. rootContentWidget_->SetSize(rect.Width(), rect.Height());
  68. SubscribeToEvent(E_EDITORPLAYSTARTED, HANDLER(SceneEditor3D, HandlePlayStarted));
  69. SubscribeToEvent(E_EDITORPLAYSTOPPED, HANDLER(SceneEditor3D, HandlePlayStopped));
  70. }
  71. SceneEditor3D::~SceneEditor3D()
  72. {
  73. }
  74. bool SceneEditor3D::OnEvent(const TBWidgetEvent &ev)
  75. {
  76. if (ev.type == EVENT_TYPE_SHORTCUT)
  77. {
  78. if (ev.ref_id == TBIDC("copy"))
  79. {
  80. if (selectedNode_.NotNull())
  81. {
  82. clipboardNode_ = selectedNode_;
  83. }
  84. }
  85. else if (ev.ref_id == TBIDC("paste"))
  86. {
  87. if (clipboardNode_.NotNull() && selectedNode_.NotNull())
  88. {
  89. SharedPtr<Node> pasteNode(clipboardNode_->Clone());
  90. VariantMap eventData;
  91. eventData[EditorActiveNodeChange::P_NODE] = pasteNode;
  92. SendEvent(E_EDITORACTIVENODECHANGE, eventData);
  93. }
  94. }
  95. }
  96. if (ev.type == EVENT_TYPE_CLICK)
  97. {
  98. SetFocus();
  99. if (ev.target)
  100. {
  101. if (ev.target->GetID() == TBIDC("3d_translate"))
  102. {
  103. gizmo3D_->SetEditMode(EDIT_MOVE);
  104. return false;
  105. }
  106. else if (ev.target->GetID() == TBIDC("3d_rotate"))
  107. {
  108. gizmo3D_->SetEditMode(EDIT_ROTATE);
  109. return false;
  110. }
  111. else if (ev.target->GetID() == TBIDC("3d_scale"))
  112. {
  113. gizmo3D_->SetEditMode(EDIT_SCALE);
  114. return false;
  115. }
  116. }
  117. }
  118. return false;
  119. }
  120. void SceneEditor3D::SetFocus()
  121. {
  122. sceneView_->SetFocus();
  123. }
  124. void SceneEditor3D::SelectNode(Node* node)
  125. {
  126. selectedNode_ = node;
  127. }
  128. void SceneEditor3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  129. {
  130. Vector<Node*> editNodes;
  131. if (selectedNode_.NotNull())
  132. editNodes.Push(selectedNode_);
  133. gizmo3D_->Update(editNodes);
  134. }
  135. void SceneEditor3D::HandleEditorActiveNodeChange(StringHash eventType, VariantMap& eventData)
  136. {
  137. Node* node = (Node*) (eventData[EditorActiveNodeChange::P_NODE].GetPtr());
  138. SelectNode(node);
  139. }
  140. void SceneEditor3D::HandlePlayStarted(StringHash eventType, VariantMap& eventData)
  141. {
  142. sceneView_->Disable();
  143. }
  144. void SceneEditor3D::HandlePlayStopped(StringHash eventType, VariantMap& eventData)
  145. {
  146. sceneView_->Enable();
  147. }
  148. void SceneEditor3D::HandleGizmoEditModeChanged(StringHash eventType, VariantMap& eventData)
  149. {
  150. EditMode mode = (EditMode) ((int)eventData[GizmoEditModeChanged::P_MODE].GetFloat());
  151. gizmo3D_->SetEditMode(mode);
  152. }
  153. void SceneEditor3D::Close(bool navigateToAvailableResource)
  154. {
  155. VariantMap data;
  156. data["Scene"] = scene_;
  157. SendEvent("EditorSceneClosed", data);
  158. ResourceEditor::Close(navigateToAvailableResource);
  159. }
  160. bool SceneEditor3D::Save()
  161. {
  162. File file(context_);
  163. if (file.Open(fullpath_, FILE_WRITE))
  164. {
  165. scene_->SaveXML(file);
  166. file.Close();
  167. }
  168. return true;
  169. }
  170. }