Simulant  21.12-1275
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
input_axis.h
1 #pragma once
2 
3 #include <string>
4 #include "../generic/managed.h"
5 #include "../types.h"
6 #include "../keycodes.h"
7 #include "input_state.h"
8 
9 namespace smlt {
10 
11 /*
12  * See here for more information on radial deadzones:
13  * https://www.gamasutra.com/blogs/JoshSutphin/20130416/190541/Doing_Thumbstick_Dead_Zones_Right.php
14  */
15 enum DeadZoneBehaviour {
16  DEAD_ZONE_BEHAVIOUR_NONE,
17  DEAD_ZONE_BEHAVIOUR_AXIAL,
18  DEAD_ZONE_BEHAVIOUR_RADIAL
19 };
20 
21 enum AxisType {
22  AXIS_TYPE_UNSET,
23  AXIS_TYPE_KEYBOARD_KEY,
24  AXIS_TYPE_MOUSE_BUTTON,
25  AXIS_TYPE_MOUSE_AXIS,
26  AXIS_TYPE_JOYSTICK_BUTTON,
27  AXIS_TYPE_JOYSTICK_AXIS,
28  AXIS_TYPE_JOYSTICK_HAT
29 };
30 
31 class InputAxis:
32  public RefCounted<InputAxis> {
33 public:
34  InputAxis(const std::string& name);
35 
36  void set_positive_keyboard_key(const KeyboardCode& key);
37  void set_negative_keyboard_key(const KeyboardCode& key);
38  KeyboardCode positive_keyboard_key() const { return positive_key_; }
39  KeyboardCode negative_keyboard_key() const { return negative_key_; }
40 
41  void set_keyboard_source(KeyboardID keyboard);
42  KeyboardID keyboard_source() const { return keyboard_source_; }
43 
44  void set_positive_mouse_button(MouseButtonID button);
45  void set_negative_mouse_button(MouseButtonID button);
46  MouseButtonID positive_mouse_button() const { return positive_mouse_button_; }
47  MouseButtonID negative_mouse_button() const { return negative_mouse_button_; }
48  void set_mouse_source(MouseID mouse);
49  MouseID mouse_source() const { return mouse_source_; }
50 
51  void set_positive_joystick_button(JoystickButton button);
52  void set_negative_joystick_button(JoystickButton button);
53  JoystickButton positive_joystick_button() const { return positive_joystick_button_; }
54  JoystickButton negative_joystick_button() const { return negative_joystick_button_; }
55  void set_joystick_source(GameControllerIndex joystick);
56  GameControllerIndex joystick_source() const { return joystick_source_; }
57 
58  void set_mouse_axis(MouseAxis axis);
59  void set_joystick_axis(JoystickAxis axis);
60  void set_joystick_hat_axis(JoystickHatID hat, JoystickHatAxis axis);
61 
62  /* For digital inputs only. The force multiplier (in units-per-second)
63  * to change the axis value when pressed. Defaults to 3.0 (i.e.
64  * it'll take 1/3rd second to hit an axis value of 1.0 */
65  void set_force(float f);
66  void set_return_speed(float ret);
67 
68  const std::string& name() const { return name_; }
69  const AxisType& type() const { return type_; }
70 
71  float value(DeadZoneBehaviour dead_zone_behaviour=DEAD_ZONE_BEHAVIOUR_RADIAL) const;
72 
73  void set_dead_zone(float v) { dead_zone_ = v; dead_zone_reciprocal_ = 1.0f / (1.0f - dead_zone_); }
74  float dead_zone() const { return dead_zone_; }
75 
76  void set_inversed(bool value=true);
77 
78 private:
79  std::string name_;
80 
81  AxisType type_ = AXIS_TYPE_UNSET;
82 
83  KeyboardID keyboard_source_ = ALL_KEYBOARDS;
84  KeyboardCode positive_key_ = KEYBOARD_CODE_NONE;
85  KeyboardCode negative_key_ = KEYBOARD_CODE_NONE;
86 
87  MouseID mouse_source_ = ALL_MICE;
88  MouseButtonID positive_mouse_button_ = -1;
89  MouseButtonID negative_mouse_button_ = -1;
90 
91  GameControllerIndex joystick_source_ = ALL_GAME_CONTROLLERS;
92  JoystickButton positive_joystick_button_ = JOYSTICK_BUTTON_INVALID;
93  JoystickButton negative_joystick_button_ = JOYSTICK_BUTTON_INVALID;
94 
95  MouseAxis mouse_axis_ = MOUSE_AXIS_INVALID;
96  JoystickAxis joystick_axis_ = JOYSTICK_AXIS_INVALID;
97 
98  JoystickHatID joystick_hat_ = -1;
99  JoystickHatAxis joystick_hat_axis_ = JOYSTICK_HAT_AXIS_X;
100 
101  float return_speed_ = 3.0f;
102  float force_ = 3.0f;
103 
104  float value_ = 0.0f;
105  float dead_zone_ = 0.001f;
106  float dead_zone_reciprocal_ = 1.0f / (1.0f - dead_zone_);
107 
108  bool inversed_ = false;
109 
110  /* This is used where an axis has a counterpart
111  * (e.g a joystick with X + Y). It's used when
112  * calculating radial deadzones and stores the
113  * normalized value of the other axis */
114  float linked_value_ = 0.0;
115 
116  void set_type(AxisType type);
117 
118  friend class InputManager;
119 };
120 
121 }
smlt::RefCounted
Definition: managed.h:71
smlt
Definition: animation.cpp:25
smlt::InputManager
Definition: input_manager.h:48
smlt::InputAxis
Definition: input_axis.h:32