| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- // Please see LICENSE.md in repository root for license information
- // https://github.com/AtomicGameEngine/AtomicGameEngine
- #include "AtomicEditor.h"
- #include <Atomic/IO/Log.h>
- #include <Atomic/Core/CoreEvents.h>
- #include <Atomic/Scene/Scene.h>
- #include <Atomic/Graphics/Camera.h>
- #include <Atomic/Graphics/Graphics.h>
- #include <Atomic/Graphics/DebugRenderer.h>
- #include <Atomic/Graphics/Viewport.h>
- #include <Atomic/Graphics/Octree.h>
- #include <Atomic/Input/Input.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Resource/ResourceCache.h>
- #include <Atomic/Physics/PhysicsWorld.h>
- #include "AEEditor.h"
- #include "AEEvents.h"
- #include "SceneView3D.h"
- #include "SceneEditor3D.h"
- #include <Atomic/UI/UI.h>
- #include <Atomic/UI/TBUI.h>
- namespace AtomicEditor
- {
- SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
- View3D(context),
- yaw_(0.0f),
- pitch_(0.0f)
- {
- sceneEditor_ = sceneEditor;
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- scene_ = sceneEditor->GetScene();
- debugRenderer_ = scene_->GetComponent<DebugRenderer>();
- if (debugRenderer_.Null())
- {
- debugRenderer_ = scene_->CreateComponent<DebugRenderer>();
- }
- octree_ = scene_->GetComponent<Octree>();
- if (octree_.Null())
- {
- LOGWARNING("Scene without an octree loaded");
- octree_ = scene_->CreateComponent<Octree>();
- }
- cameraNode_ = scene_->CreateChild("Camera");
- camera_ = cameraNode_->CreateComponent<Camera>();
- debugRenderer_ = scene_->GetComponent<DebugRenderer>();
- assert(debugRenderer_.NotNull());
- octree_ = scene_->GetComponent<Octree>();
- assert(octree_.NotNull());
- cameraNode_->SetPosition(Vector3(0, 0, -10));
- SetView(scene_, camera_);
- SetAutoUpdate(false);
- SubscribeToEvent(E_UPDATE, HANDLER(SceneView3D, HandleUpdate));
- SubscribeToEvent(E_EDITORACTIVENODECHANGE, HANDLER(SceneView3D, HandleEditorActiveNodeChange));
- SubscribeToEvent(E_POSTRENDERUPDATE, HANDLER(SceneView3D, HandlePostRenderUpdate));
- // TODO: generate this event properly
- VariantMap eventData;
- eventData[EditorActiveSceneChange::P_SCENE] = scene_;
- SendEvent(E_EDITORACTIVESCENECHANGE, eventData);
- }
- SceneView3D::~SceneView3D()
- {
- }
- void SceneView3D::MoveCamera(float timeStep)
- {
- // Do not move if the UI has a focused element (the console)
- if (GetSubsystem<UI>()->GetFocusElement())
- return;
- Input* input = GetSubsystem<Input>();
- // Movement speed as world units per second
- const float MOVE_SPEED = 20.0f;
- // Mouse sensitivity as degrees per pixel
- const float MOUSE_SENSITIVITY = 0.2f;
- // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
- if (input->GetKeyDown('F'))
- {
- IntVector2 mouseMove = input->GetMouseMove();
- yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
- pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
- pitch_ = Clamp(pitch_, -90.0f, 90.0f);
- }
- // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
- cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
- // 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.
- if (input->GetKeyDown('W'))
- cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
- if (input->GetKeyDown('S'))
- cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
- if (input->GetKeyDown('A'))
- cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
- if (input->GetKeyDown('D'))
- cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
- }
- Ray SceneView3D::GetCameraRay()
- {
- Ray camRay;
- UI* ui = GetSubsystem<UI>();
- IntVector2 cpos = ui->GetCursorPosition();
- TBRect rect = GetWidgetDelegate()->GetRect();
- if (!rect.w || !rect.h)
- return camRay;
- GetWidgetDelegate()->ConvertToRoot(rect.x, rect.y);
- return camera_->GetScreenRay(float(cpos.x_ - rect.x) / rect.w,
- float(cpos.y_ - rect.y) /rect.h);
- }
- void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
- {
- Ray camRay = GetCameraRay();
- PODVector<RayQueryResult> result;
- RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_ANY, DEFAULT_VIEWMASK);
- octree_->RaycastSingle(query);
- if (query.result_.Size())
- {
- const RayQueryResult& r = result[0];
- if (r.drawable_)
- {
- debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
- r.drawable_->DrawDebugGeometry(debugRenderer_, false);
- }
- }
- }
- void SceneView3D::SelectNode(Node* node)
- {
- }
- void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
- {
- // Timestep parameter is same no matter what event is being listened to
- float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
- MoveCamera(timeStep);
- QueueUpdate();
- }
- void SceneView3D::HandleEditorActiveNodeChange(StringHash eventType, VariantMap& eventData)
- {
- Node* node = (Node*) (eventData[EditorActiveNodeChange::P_NODE].GetPtr());
- SelectNode(node);
- }
- }
|