SceneEditor3D.cpp 4.7 KB

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