SceneEditor3D.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. /*
  79. if (ev.ref_id == TBIDC("save"))
  80. {
  81. File file(context_);
  82. if (file.Open(fullpath_, FILE_WRITE))
  83. {
  84. scene_->SaveXML(file);
  85. file.Close();
  86. }
  87. return true;
  88. }
  89. else */if (ev.ref_id == TBIDC("copy"))
  90. {
  91. if (selectedNode_.NotNull())
  92. {
  93. clipboardNode_ = selectedNode_;
  94. }
  95. }
  96. else if (ev.ref_id == TBIDC("paste"))
  97. {
  98. if (clipboardNode_.NotNull() && selectedNode_.NotNull())
  99. {
  100. SharedPtr<Node> pasteNode(clipboardNode_->Clone());
  101. VariantMap eventData;
  102. eventData[EditorActiveNodeChange::P_NODE] = pasteNode;
  103. SendEvent(E_EDITORACTIVENODECHANGE, eventData);
  104. }
  105. }
  106. }
  107. if (ev.type == EVENT_TYPE_CLICK)
  108. {
  109. SetFocus();
  110. if (ev.target)
  111. {
  112. if (ev.target->GetID() == TBIDC("3d_translate"))
  113. {
  114. gizmo3D_->SetEditMode(EDIT_MOVE);
  115. return false;
  116. }
  117. else if (ev.target->GetID() == TBIDC("3d_rotate"))
  118. {
  119. gizmo3D_->SetEditMode(EDIT_ROTATE);
  120. return false;
  121. }
  122. else if (ev.target->GetID() == TBIDC("3d_scale"))
  123. {
  124. gizmo3D_->SetEditMode(EDIT_SCALE);
  125. return false;
  126. }
  127. }
  128. }
  129. return false;
  130. }
  131. void SceneEditor3D::SetFocus()
  132. {
  133. sceneView_->SetFocus();
  134. }
  135. void SceneEditor3D::SelectNode(Node* node)
  136. {
  137. selectedNode_ = node;
  138. }
  139. void SceneEditor3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  140. {
  141. Vector<Node*> editNodes;
  142. if (selectedNode_.NotNull())
  143. editNodes.Push(selectedNode_);
  144. gizmo3D_->Update(editNodes);
  145. }
  146. void SceneEditor3D::HandleEditorActiveNodeChange(StringHash eventType, VariantMap& eventData)
  147. {
  148. Node* node = (Node*) (eventData[EditorActiveNodeChange::P_NODE].GetPtr());
  149. SelectNode(node);
  150. }
  151. void SceneEditor3D::HandlePlayStarted(StringHash eventType, VariantMap& eventData)
  152. {
  153. sceneView_->Disable();
  154. }
  155. void SceneEditor3D::HandlePlayStopped(StringHash eventType, VariantMap& eventData)
  156. {
  157. sceneView_->Enable();
  158. }
  159. void SceneEditor3D::HandleGizmoEditModeChanged(StringHash eventType, VariantMap& eventData)
  160. {
  161. EditMode mode = (EditMode) ((int)eventData[GizmoEditModeChanged::P_MODE].GetFloat());
  162. gizmo3D_->SetEditMode(mode);
  163. }
  164. void SceneEditor3D::Close(bool navigateToAvailableResource)
  165. {
  166. VariantMap data;
  167. data["Scene"] = scene_;
  168. SendEvent("EditorSceneClosed", data);
  169. ResourceEditor::Close(navigateToAvailableResource);
  170. }
  171. bool SceneEditor3D::Save()
  172. {
  173. File file(context_);
  174. if (file.Open(fullpath_, FILE_WRITE))
  175. {
  176. scene_->SaveXML(file);
  177. file.Close();
  178. }
  179. return true;
  180. }
  181. }