SceneEditor3D.cpp 4.8 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/TBUI.h>
  19. #include "SceneEditor3D.h"
  20. namespace AtomicEditor
  21. {
  22. SceneEditor3D ::SceneEditor3D(Context* context, const String &fullpath, TBTabContainer *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. TBWidgetDelegate* delegate = sceneView_->GetWidgetDelegate();
  53. delegate->SetGravity(WIDGET_GRAVITY_ALL);
  54. rootContentWidget_->AddChild(delegate);
  55. gizmo3D_ = new Gizmo3D(context_);
  56. gizmo3D_->SetView(sceneView_);
  57. gizmo3D_->Show();
  58. SubscribeToEvent(E_UPDATE, HANDLER(SceneEditor3D, HandleUpdate));
  59. SubscribeToEvent(E_EDITORACTIVENODECHANGE, HANDLER(SceneEditor3D, HandleEditorActiveNodeChange));
  60. // FIXME: Set the size at the end of setup, so all children are updated accordingly
  61. // future size changes will be handled automatically
  62. TBRect rect = container_->GetContentRoot()->GetRect();
  63. rootContentWidget_->SetSize(rect.w, rect.h);
  64. // TODO: generate this event properly
  65. VariantMap eventData;
  66. eventData[EditorActiveSceneChange::P_SCENE] = scene_;
  67. SendEvent(E_EDITORACTIVESCENECHANGE, eventData);
  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("save"))
  79. {
  80. File file(context_);
  81. if (file.Open(fullpath_, FILE_WRITE))
  82. {
  83. scene_->SaveXML(file);
  84. file.Close();
  85. }
  86. return true;
  87. }
  88. }
  89. if (ev.type == EVENT_TYPE_CLICK)
  90. {
  91. SetFocus();
  92. if (ev.target)
  93. {
  94. if (ev.target->GetID() == TBIDC("3d_translate"))
  95. {
  96. gizmo3D_->SetEditMode(Gizmo3D::EDIT_MOVE);
  97. return false;
  98. }
  99. else if (ev.target->GetID() == TBIDC("3d_rotate"))
  100. {
  101. gizmo3D_->SetEditMode(Gizmo3D::EDIT_ROTATE);
  102. return false;
  103. }
  104. else if (ev.target->GetID() == TBIDC("3d_scale"))
  105. {
  106. gizmo3D_->SetEditMode(Gizmo3D::EDIT_SCALE);
  107. return false;
  108. }
  109. }
  110. }
  111. return false;
  112. }
  113. void SceneEditor3D::SetFocus()
  114. {
  115. sceneView_->GetWidgetDelegate()->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  116. }
  117. void SceneEditor3D::SelectNode(Node* node)
  118. {
  119. selectedNode_ = node;
  120. }
  121. void SceneEditor3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  122. {
  123. Vector<Node*> editNodes;
  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. }