Browse Source

Adding orbit camera mode

Josh Engebretson 10 years ago
parent
commit
5e87b762a1

+ 28 - 0
Source/AtomicEditor/Editors/SceneEditor3D/SceneEditor3D.cpp

@@ -322,4 +322,32 @@ void SceneEditor3D::HandleSceneEditSceneModified(StringHash eventType, VariantMa
     SetModified(true);    
 }
 
+void SceneEditor3D::GetSelectionBoundingBox(BoundingBox& bbox)
+{
+    bbox.Clear();
+
+    if (selectedNode_.Null())
+        return;
+
+    // TODO: Adjust once multiple selection is in
+    if (selectedNode_.Null())
+        return;
+
+    // Get all the drawables, which define the bounding box of the selection
+    PODVector<Drawable*> drawables;
+    selectedNode_->GetDerivedComponents<Drawable>(drawables, true);
+
+    if (!drawables.Size())
+        return;
+
+    // Calculate the combined bounding box of all drawables
+    for (unsigned i = 0; i < drawables.Size(); i++  )
+    {
+        Drawable* drawable = drawables[i];
+        bbox.Merge(drawable->GetWorldBoundingBox());
+    }
+
+
+}
+
 }

+ 1 - 0
Source/AtomicEditor/Editors/SceneEditor3D/SceneEditor3D.h

@@ -45,6 +45,7 @@ public:
     bool OnEvent(const TBWidgetEvent &ev);
 
     void SelectNode(Node* node);
+    void GetSelectionBoundingBox(BoundingBox& bbox);
 
     Scene* GetScene() { return scene_; }
     Gizmo3D* GetGizmo() { return gizmo3D_; }

+ 31 - 21
Source/AtomicEditor/Editors/SceneEditor3D/SceneView3D.cpp

@@ -141,14 +141,21 @@ void SceneView3D::Disable()
 
 }
 
-void SceneView3D::MoveCamera(float timeStep)
+bool SceneView3D::GetOrbitting()
 {
+    Input* input = GetSubsystem<Input>();
+    return framedNode_.NotNull() && MouseInView() && input->GetKeyDown(KEY_ALT) && input->GetMouseButtonDown(MOUSEB_LEFT);
+}
+
+void SceneView3D::MoveCamera(float timeStep)
+{    
     if (!enabled_ && !GetFocus())
         return;
 
     Input* input = GetSubsystem<Input>();
 
     bool mouseInView = MouseInView();
+    bool orbitting = GetOrbitting();
 
     // Movement speed as world units per second
     float MOVE_SPEED = 20.0f;
@@ -159,9 +166,8 @@ void SceneView3D::MoveCamera(float timeStep)
         MOVE_SPEED *= 3.0f;
 
     // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
-    if (mouseInView && input->GetMouseButtonDown(MOUSEB_RIGHT))
+    if ((mouseInView && input->GetMouseButtonDown(MOUSEB_RIGHT)) || orbitting)
     {
-
         SetFocus();
         IntVector2 mouseMove = input->GetMouseMove();
         yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
@@ -170,7 +176,22 @@ void SceneView3D::MoveCamera(float timeStep)
     }
 
     // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
-    cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
+    Quaternion q(pitch_, yaw_, 0.0f);
+    cameraNode_->SetRotation(q);
+
+    if (orbitting)
+    {
+        BoundingBox bbox;
+        sceneEditor_->GetSelectionBoundingBox(bbox);
+        if (bbox.defined_)
+        {
+            Vector3 centerPoint = bbox.Center();
+            Vector3 d = cameraNode_->GetWorldPosition() - centerPoint;
+            cameraNode_->SetWorldPosition(centerPoint - q * Vector3(0.0, 0.0, d.Length()));
+
+        }
+    }
+
 
 #ifdef ATOMIC_PLATFORM_WINDOWS
     bool superdown = input->GetKeyDown(KEY_LCTRL) || input->GetKeyDown(KEY_RCTRL);
@@ -178,7 +199,7 @@ void SceneView3D::MoveCamera(float timeStep)
     bool superdown = input->GetKeyDown(KEY_LGUI) || input->GetKeyDown(KEY_RGUI);
 #endif
 
-    if (mouseInView && !superdown && input->GetMouseButtonDown(MOUSEB_RIGHT)) {
+    if (!orbitting && mouseInView && !superdown && input->GetMouseButtonDown(MOUSEB_RIGHT)) {
 
         // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
         // Use the Translate() function (default local space) to move relative to the node's orientation.
@@ -332,7 +353,7 @@ void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& event
 
     }
 
-    if (!MouseInView())
+    if (!MouseInView() || GetOrbitting())
         return;
 
     Input* input = GetSubsystem<Input>();
@@ -633,30 +654,19 @@ void SceneView3D::HandleDragEnded(StringHash eventType, VariantMap& eventData)
 
 void SceneView3D::FrameSelection()
 {
-    // TODO: Adjust once multiple selection is in
-    if (selectedNode_.Null())
-        return;
+    BoundingBox bbox;
 
-    // Get all the drawables, which define the bounding box of the selection
-    PODVector<Drawable*> drawables;
-    selectedNode_->GetDerivedComponents<Drawable>(drawables, true);
+    sceneEditor_->GetSelectionBoundingBox(bbox);
 
-    if (!drawables.Size())
+    if (!bbox.defined_)
         return;
 
-    // Calculate the combined bounding box of all drawables
-    BoundingBox bbox;
-    for (unsigned i = 0; i < drawables.Size(); i++  )
-    {
-        Drawable* drawable = drawables[i];
-        bbox.Merge(drawable->GetWorldBoundingBox());
-    }
-
     Sphere sphere(bbox);
 
     if (sphere.radius_ < .01f || sphere.radius_ > 512)
         return;
 
+    framedNode_ = selectedNode_;
     cameraMoveStart_ = cameraNode_->GetWorldPosition();
     cameraMoveTarget_ = bbox.Center() - (cameraNode_->GetWorldDirection() * sphere.radius_ * 3);
     cameraMoveTime_ = 0.0f;

+ 3 - 1
Source/AtomicEditor/Editors/SceneEditor3D/SceneView3D.h

@@ -56,6 +56,7 @@ public:
 private:
 
     bool MouseInView();
+    bool GetOrbitting();
 
     void HandleMouseMove(StringHash eventType, VariantMap& eventData);
 
@@ -85,7 +86,7 @@ private:
     bool mouseLeftDown_;
     bool mouseMoved_;
 
-    bool enabled_;
+    bool enabled_;    
 
     bool cameraMove_;
     float cameraMoveTime_;
@@ -96,6 +97,7 @@ private:
     SharedPtr<DebugRenderer> debugRenderer_;
     SharedPtr<Octree> octree_;
     SharedPtr<Node> selectedNode_;
+    WeakPtr<Node> framedNode_;
 
     SharedPtr<Scene> preloadResourceScene_;
     String dragAssetGUID_;