Scene Lab
An open source project by FPL.
 All Classes Namespaces Files Functions Pages
editor_controller.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SCENE_LAB_EDITOR_CONTROLLER_H_
16 #define SCENE_LAB_EDITOR_CONTROLLER_H_
17 
18 #include "fplbase/input.h"
19 #include "mathfu/constants.h"
20 #include "mathfu/glsl_mappings.h"
21 #include "scene_lab/entity_system_adapter.h"
22 #include "scene_lab_config_generated.h"
23 
24 namespace scene_lab {
25 
26 /// @file
27 /// Pointer and keyboard controls for Scene Lab. This class provides a simple
28 /// interface for button and key presses, and for tracking the camera's facing
29 /// when the mouse moves like in a first-person shooter.
30 /// TODO: Add gamepad and virtual thumbstick support.
32  public:
33  static const int kNumButtons = 10; // max buttons from fplbase/input.h
34 
35  /// Initialize the controller.
36  EditorController(const SceneLabConfig* config,
37  fplbase::InputSystem* input_system)
38  : config_(config),
39  input_system_(input_system),
40  mouse_locked_(false),
41  facing_current_(mathfu::kZeros3f),
42  facing_previous_(mathfu::kZeros3f),
43  pointer_current_(mathfu::kZeros2f),
44  pointer_previous_(mathfu::kZeros2f) {
45  for (int i = 0; i < kNumButtons; i++) {
46  buttons_previous_[i] = buttons_current_[i] = false;
47  }
48  }
49 
50  /// Call this every frame to update the *WentDown() functions.
51  void Update();
52 
53  /// ButtonWentDown() returns true only on the first frame the given button is
54  /// being pressed. Buttons are numbered 0 thru kNumButtons-1. The primary
55  /// button is number 0.
56  bool ButtonWentDown(int button) const {
57  return buttons_current_[button] && !buttons_previous_[button];
58  }
59  /// ButtonWentUp() returns true only on the first frame the given button has
60  /// stopped being pressed.
61  bool ButtonWentUp(int button) const {
62  return buttons_previous_[button] && !buttons_current_[button];
63  }
64  /// ButtonIsDown() returns true while the given button is being held down.
65  bool ButtonIsDown(int button) const { return buttons_current_[button]; }
66  /// ButtonIsUp() returns true while the given button is not being held down.
67  /// Equivalent to ButtonIsDown(button);
68  bool ButtonIsUp(int button) const { return !buttons_current_[button]; }
69 
70  /// KeyWentDown() returns true on the first frame a given key is being
71  /// pressed.
72  bool KeyWentDown(fplbase::FPL_Keycode key) const {
73  return input_system_->GetButton(key).went_down();
74  }
75  /// KeyWentUp() returns true on the first frame after a given key has stopped
76  /// being pressed.
77  bool KeyWentUp(fplbase::FPL_Keycode key) const {
78  return input_system_->GetButton(key).went_up();
79  }
80  /// KeyIsDown() returns true while the given key is being held down.
81  bool KeyIsDown(fplbase::FPL_Keycode key) const {
82  return input_system_->GetButton(key).is_down();
83  }
84  /// KeyIsUp() returns true while the given key is not being held down.
85  /// Equivalent to !KeyIsDown(key).
86  bool KeyIsUp(fplbase::FPL_Keycode key) const {
87  return !input_system_->GetButton(key).is_down();
88  }
89 
90  /// Get the direction we are facing. If the mouse is locked, then moving it
91  /// will cause your facing to change like in a first-person shooter.
92  const mathfu::vec3& GetFacing() const { return facing_current_; }
93 
94  /// Set the current facing to a specific value.
95  void SetFacing(const mathfu::vec3& facing) {
96  facing_current_ = facing_previous_ = facing;
97  }
98 
99  /// Get the on-screen pointer position. This is probably only useful is the
100  /// mouse is unlocked.
101  const mathfu::vec2& GetPointer() const { return pointer_current_; }
102 
103  /// Get the delta in on-screen pointer position from the previous update.
104  mathfu::vec2 GetPointerDelta() const {
105  return pointer_current_ - pointer_previous_;
106  }
107 
108  /// Lock the mouse into the middle of the screen, which will start updating
109  /// facing.
110  void LockMouse() {
111  mouse_locked_ = true;
112  input_system_->SetRelativeMouseMode(true);
113  }
114 
115  /// Stop locking the mouse to the middle of the screen; it will no longer
116  /// update facing, but will update pointer location instead.
117  void UnlockMouse() {
118  mouse_locked_ = false;
119  input_system_->SetRelativeMouseMode(false);
120  }
121 
122  /// Get the position of a screen point in the world, as a ray from the camera
123  /// position with a direction.
124  bool ScreenPointToWorldRay(const GenericCamera& camera,
125  const ViewportSettings& viewport,
126  const mathfu::vec2& screen_point,
127  const mathfu::vec2i& screen_size,
128  mathfu::vec3* near, mathfu::vec3* dir) const;
129 
130  /// Is the mouse locked? If so, moving the mouse changes our facing. If not,
131  /// moving the mouse moves the mouse pointer.
132  bool mouse_locked() const { return mouse_locked_; }
133 
134  MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
135 
136  private:
137  const SceneLabConfig* config_;
138  fplbase::InputSystem* input_system_;
139 
140  bool mouse_locked_;
141 
142  mathfu::vec3 facing_current_;
143  mathfu::vec3 facing_previous_;
144 
145  mathfu::vec2 pointer_current_;
146  mathfu::vec2 pointer_previous_;
147 
148  bool buttons_current_[kNumButtons];
149  bool buttons_previous_[kNumButtons];
150 };
151 
152 } // namespace scene_lab
153 
154 #endif // SCENE_LAB_EDITOR_CONTROLLER_H_
bool ButtonWentUp(int button) const
ButtonWentUp() returns true only on the first frame the given button has stopped being pressed...
Definition: editor_controller.h:61
void SetFacing(const mathfu::vec3 &facing)
Set the current facing to a specific value.
Definition: editor_controller.h:95
bool mouse_locked() const
Is the mouse locked? If so, moving the mouse changes our facing.
Definition: editor_controller.h:132
void LockMouse()
Lock the mouse into the middle of the screen, which will start updating facing.
Definition: editor_controller.h:110
bool ButtonIsDown(int button) const
ButtonIsDown() returns true while the given button is being held down.
Definition: editor_controller.h:65
void Update()
Call this every frame to update the *WentDown() functions.
bool ScreenPointToWorldRay(const GenericCamera &camera, const ViewportSettings &viewport, const mathfu::vec2 &screen_point, const mathfu::vec2i &screen_size, mathfu::vec3 *near, mathfu::vec3 *dir) const
Get the position of a screen point in the world, as a ray from the camera position with a direction...
bool ButtonWentDown(int button) const
ButtonWentDown() returns true only on the first frame the given button is being pressed.
Definition: editor_controller.h:56
bool KeyWentUp(fplbase::FPL_Keycode key) const
KeyWentUp() returns true on the first frame after a given key has stopped being pressed.
Definition: editor_controller.h:77
bool KeyIsUp(fplbase::FPL_Keycode key) const
KeyIsUp() returns true while the given key is not being held down.
Definition: editor_controller.h:86
const mathfu::vec2 & GetPointer() const
Get the on-screen pointer position.
Definition: editor_controller.h:101
void UnlockMouse()
Stop locking the mouse to the middle of the screen; it will no longer update facing, but will update pointer location instead.
Definition: editor_controller.h:117
mathfu::vec2 GetPointerDelta() const
Get the delta in on-screen pointer position from the previous update.
Definition: editor_controller.h:104
bool KeyIsDown(fplbase::FPL_Keycode key) const
KeyIsDown() returns true while the given key is being held down.
Definition: editor_controller.h:81
bool KeyWentDown(fplbase::FPL_Keycode key) const
KeyWentDown() returns true on the first frame a given key is being pressed.
Definition: editor_controller.h:72
Definition: editor_controller.h:31
const mathfu::vec3 & GetFacing() const
Get the direction we are facing.
Definition: editor_controller.h:92
bool ButtonIsUp(int button) const
ButtonIsUp() returns true while the given button is not being held down.
Definition: editor_controller.h:68
EditorController(const SceneLabConfig *config, fplbase::InputSystem *input_system)
Initialize the controller.
Definition: editor_controller.h:36