Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
input_manager.h
1 #pragma once
2 
3 #include <vector>
4 #include <functional>
5 #include <map>
6 #include <unordered_map>
7 #include "../generic/property.h"
8 #include "../generic/managed.h"
9 #include "../keycodes.h"
10 #include "../signals/signal.h"
11 #include "../event_listener.h"
12 
13 namespace smlt {
14 
15 class SceneBase;
16 class InputState;
17 class InputAxis;
18 class GameController;
19 
20 namespace ui {
21  class Keyboard;
22 }
23 
24 typedef std::function<void (InputAxis*)> EachAxisCallback;
25 typedef std::vector<InputAxis*> AxisList;
26 
28  friend class InputManager;
29 
30  /* Some keys will trigger additional text input events
31  * that may be without content. These include backspace, enter, delete,
32  * arrow keys, home, end, and space */
33  KeyboardCode keyboard_code = KEYBOARD_CODE_NONE;
34 
35  void cancel() {
36  cancelled = true;
37  }
38 
39 private:
40  bool cancelled = false;
41 };
42 
44 
45 enum JoystickButton : int8_t;
46 
48  public RefCounted<InputManager> {
49 
54  DEFINE_SIGNAL(TextInputReceivedSignal, signal_text_input_received);
55 
56 public:
57  // You can't copy input managers
58  InputManager(const InputManager&) = delete;
59  InputManager& operator=(const InputManager&) = delete;
60 
61  InputManager(InputState* controller);
62  ~InputManager();
63 
64  InputAxis* new_axis(const std::string& name);
65  AxisList axises(const std::string& name);
66  void each_axis(EachAxisCallback callback);
67  void destroy_axises(const std::string& name);
68  void destroy_axis(InputAxis* axis);
69  std::size_t axis_count(const std::string& name) const;
70 
71  float axis_value(const std::string& name) const;
72  int8_t axis_value_hard(const std::string& name) const;
73 
74  void update(float dt);
75 
76  /* Returns true if the axis was just pressed this frame */
77  bool axis_was_pressed(const std::string& name) const;
78 
79  /* Returns true if the axis was just released this frame */
80  bool axis_was_released(const std::string& name) const;
81 
85  bool start_text_input(bool force_onscreen=false);
86 
90 
94  bool text_input_active() const {
95  return text_input_enabled_;
96  }
97 
98  bool onscreen_keyboard_active() const {
99  return bool(keyboard_);
100  }
101 
102 private:
103  InputState* controller_;
104 
105  std::multimap<std::string, std::shared_ptr<InputAxis>> axises_;
106 
107  std::unordered_map<std::string, bool> prev_axis_states_;
108  std::unordered_map<std::string, bool> axis_states_;
109 
110  float _calculate_value(InputAxis* axis) const;
111 
112  bool _update_keyboard_axis(InputAxis* axis, float dt);
113  bool _update_mouse_button_axis(InputAxis* axis, float dt);
114  bool _update_joystick_button_axis(InputAxis* axis, float dt);
115  void _update_mouse_axis_axis(InputAxis *axis, float dt);
116  bool _update_joystick_axis_axis(InputAxis* axis, float dt);
117  bool _update_joystick_hat_axis(InputAxis* axis, float dt);
118 
119  void _process_mouse(int8_t id, int8_t pbtn, int8_t nbtn, bool* positive_pressed, bool* negative_pressed);
120  void _process_game_controller(GameController* controller, JoystickButton pbtn, JoystickButton nbtn, bool *positive_pressed, bool *negative_pressed);
121  void _process_keyboard(int8_t id, KeyboardCode pbtn, KeyboardCode nbtn, bool *positive_pressed, bool *negative_pressed);
122 
123  bool text_input_enabled_ = false;
124 
125 
126  /* Watch for keyboard inputs from the window */
127  class TextInputHandler : public EventListener {
128  public:
129  TextInputHandler(InputManager* self): self_(self) {}
130 
131  void on_key_down(const KeyEvent& evt) override;
132  private:
133  InputManager* self_;
134  };
135 
136  TextInputHandler event_listener_ = {this};
137 
138  StagePtr keyboard_stage_;
139  CameraPtr keyboard_camera_;
140  PipelinePtr keyboard_pipeline_;
141  ui::Keyboard* keyboard_ = nullptr;
142 
143  sig::connection scene_deactivated_conn_;
144  void on_scene_deactivated(std::string, SceneBase* scene);
145 
146 public:
147  S_DEFINE_PROPERTY(state, &InputManager::controller_);
148  S_DEFINE_PROPERTY(onscreen_keyboard, &InputManager::keyboard_);
149 };
150 
151 }
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::InputManager
Definition: input_manager.h:48
smlt::InputManager::text_input_active
bool text_input_active() const
Definition: input_manager.h:94
smlt::InputAxis
Definition: input_axis.h:32
unicode
Definition: unicode.h:36
smlt::InputState
Definition: input_state.h:230
smlt::InputManager::stop_text_input
unicode stop_text_input()
Definition: input_manager.cpp:641
smlt::InputManager::start_text_input
bool start_text_input(bool force_onscreen=false)
Definition: input_manager.cpp:556
smlt::TextInputEvent
Definition: input_manager.h:27
smlt::sig::signal
Definition: signal.h:330