NAP
orbitcontroller.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 #include <component.h>
8 #include <componentptr.h>
9 #include "perspcameracomponent.h"
10 #include <glm/glm.hpp>
11 
12 namespace nap
13 {
14  class OrbitControllerInstance;
15  class KeyPressEvent;
16  class KeyReleaseEvent;
17  class PointerPressEvent;
18  class PointerMoveEvent;
19  class PointerReleaseEvent;
20  class TransformComponentInstance;
21  class TransformComponent;
22  class KeyInputComponent;
23  class KeyInputComponent;
24 
31  class NAPAPI OrbitController : public Component
32  {
33  RTTI_ENABLE(Component)
34  DECLARE_COMPONENT(OrbitController, OrbitControllerInstance)
35  public:
39  virtual void getDependentComponents(std::vector<rtti::TypeInfo>& components) const override;
40 
41  float mMovementSpeed = 0.5f;
42  float mRotateSpeed = 0.005f;
43  float mMinZoomDistance = 0.5f;
44  bool mLimitZoomDistance = false;
45  glm::vec3 mLookAtPos;
46 
48  };
49 
50 
58  {
59  RTTI_ENABLE(ComponentInstance)
60  public:
62 
66  virtual bool init(utility::ErrorState& errorState) override;
67 
73  void enable(const glm::vec3& cameraPos, const glm::vec3& lookAtPos);
74 
79  void enable(const glm::vec3& lookAtPos);
80 
84  void disable() { mEnabled = false; }
85 
89  const glm::vec3 getLookAtPos() const { return mLookAtPos; }
90 
94  CameraComponentInstance& getCameraComponent();
95 
96  private:
100  void onMouseDown(const PointerPressEvent& pointerPressEvent);
101 
105  void onMouseMove(const PointerMoveEvent& pointerMoveEvent);
106 
110  void onMouseUp(const PointerReleaseEvent& pointerReleaseEvent);
111 
112  private:
113  // Camera mode
114  enum class EMode
115  {
116  Idle,
117  Rotating,
118  Zooming
119  };
120 
121  ComponentInstancePtr<PerspCameraComponent> mPerspCameraComponent = { this, &OrbitController::mPerspCameraComponent };
122  TransformComponentInstance* mTransformComponent = nullptr; // The transform component used to move the entity
123  EMode mMode = EMode::Idle; // Camera mode
124  glm::vec3 mLookAtPos; // Target position to orbit around
125  bool mEnabled = false; // Enables responding to input
126  };
127 
128 }
nap::OrbitControllerInstance::getLookAtPos
const glm::vec3 getLookAtPos() const
Definition: orbitcontroller.h:89
nap::OrbitController::mLookAtPos
glm::vec3 mLookAtPos
Property: "LookAtPosition" The world space position to look at.
Definition: orbitcontroller.h:45
nap::CameraComponentInstance
Definition: cameracomponent.h:38
nap::utility::ErrorState
Definition: errorstate.h:19
nap::OrbitController
Definition: orbitcontroller.h:31
nap::PointerReleaseEvent
Definition: inputevent.h:189
nap::OrbitController::mPerspCameraComponent
ComponentPtr< PerspCameraComponent > mPerspCameraComponent
Property: "PerspCameraComponent" Link to perspective camera that we are controlling.
Definition: orbitcontroller.h:47
nap::OrbitControllerInstance::disable
void disable()
Definition: orbitcontroller.h:84
nap::OrbitControllerInstance
Definition: orbitcontroller.h:57
nap::EntityInstance
Definition: entity.h:34
nap::ComponentInstance
Definition: component.h:43
nap::Component
Definition: component.h:151
nap::PointerMoveEvent
Definition: inputevent.h:202
nap
Definition: templateapp.h:17
nap::ComponentPtr
Definition: component.h:31
nap::PointerPressEvent
Definition: inputevent.h:176