Simulant  21.12-194
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 
8 #include "../generic/managed.h"
9 #include "../keycodes.h"
10 
11 namespace smlt {
12 
13 class InputState;
14 class InputAxis;
15 
16 typedef std::function<void (InputAxis*)> EachAxisCallback;
17 typedef std::vector<InputAxis*> AxisList;
18 
19 enum JoystickButton : int8_t;
20 
22  public RefCounted<InputManager> {
23 
24 public:
25  InputManager(InputState* controller);
26 
27  InputAxis* new_axis(const std::string& name);
28  AxisList axises(const std::string& name);
29  void each_axis(EachAxisCallback callback);
30  void destroy_axises(const std::string& name);
31  void destroy_axis(InputAxis* axis);
32  std::size_t axis_count(const std::string& name) const;
33 
34  float axis_value(const std::string& name) const;
35  int8_t axis_value_hard(const std::string& name) const;
36 
37  void update(float dt);
38 
39  /* Returns true if the axis was just pressed this frame */
40  bool axis_was_pressed(const std::string& name) const;
41 
42  /* Returns true if the axis was just released this frame */
43  bool axis_was_released(const std::string& name) const;
44 
45 private:
46  InputState* controller_;
47 
48  std::multimap<std::string, std::shared_ptr<InputAxis>> axises_;
49 
50  std::unordered_map<std::string, bool> prev_axis_states_;
51  std::unordered_map<std::string, bool> axis_states_;
52 
53  float _calculate_value(InputAxis* axis) const;
54 
55  bool _update_keyboard_axis(InputAxis* axis, float dt);
56  bool _update_mouse_button_axis(InputAxis* axis, float dt);
57  bool _update_joystick_button_axis(InputAxis* axis, float dt);
58  void _update_mouse_axis_axis(InputAxis *axis, float dt);
59  bool _update_joystick_axis_axis(InputAxis* axis, float dt);
60  bool _update_joystick_hat_axis(InputAxis* axis, float dt);
61 
62  void _process_mouse(int8_t id, int8_t pbtn, int8_t nbtn, bool* positive_pressed, bool* negative_pressed);
63  void _process_joystick(int8_t id, JoystickButton pbtn, JoystickButton nbtn, bool *positive_pressed, bool *negative_pressed);
64  void _process_keyboard(int8_t id, KeyboardCode pbtn, KeyboardCode nbtn, bool *positive_pressed, bool *negative_pressed);
65 };
66 
67 }
smlt::RefCounted
Definition: managed.h:65
smlt
Definition: animation.cpp:25
smlt::InputManager
Definition: input_manager.h:22
smlt::InputAxis
Definition: input_axis.h:32
smlt::InputState
Definition: input_state.h:150