SceneEditor3D.cpp 5.0 KB

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