1 #ifndef BTANKS_SIMPLE_JOY_BINDINGS 2 #define BTANKS_SIMPLE_JOY_BINDINGS 3 4 #include <string> 5 #include "sdlx/sdlx.h" 6 7 namespace sdlx { 8 class Joystick; 9 } 10 11 class PlayerState; 12 13 class SimpleJoyBindings { 14 public: 15 struct State { 16 enum Type {None, Axis, Button, Hat} type; 17 int index, value; 18 bool need_save; 19 StateState20 inline State() : type(None), index(-1), value(0), need_save(false) {} StateState21 inline State(Type type, int index, int value) : type(type), index(index), value(value), need_save(false) {} 22 23 const std::string get_name() const; 24 const std::string to_string() const; 25 void from_string(const std::string &value); clearState26 inline void clear() { 27 type = None; index = -1; value = 0; need_save = false; 28 } 29 inline bool operator<(const State &o) const { 30 if (type != o.type) 31 return type < o.type; 32 if (index != o.index) 33 return index < o.index; 34 35 return value < o.value; 36 } 37 inline bool operator==(const State &o) const { 38 return type == o.type && index == o.index && value == o.value; 39 } 40 }; 41 42 //by index (0-8) get joystick state (Axis, 1, -1 (negative)), (Button, 2), (Hat, Center) SimpleJoyBindings()43 SimpleJoyBindings() : axis(0), buttons(0), hats(0) {} 44 SimpleJoyBindings(const std::string &profile, const sdlx::Joystick &joy); 45 void save(); 46 void load(); 47 void clear(); 48 void set(int idx, const State &state); 49 const State &get(int idx) const; 50 bool valid() const; 51 52 //static const char * names[] = {"left", "right", "up", "down", "fire", "alt-fire", "disembark", "hint-ctrl"}; 53 54 const std::string get_name(int idx) const; 55 56 void update(PlayerState &state, const SDL_Event &event) const; 57 58 void set_dead_zone(const float dz); get_dead_zone()59 const float get_dead_zone() const { return dead_zone; } 60 61 private: 62 void validate(); 63 static void set_opposite(State &dst, const State &src); 64 65 std::string profile; 66 67 State state[8]; 68 int axis, buttons, hats; 69 float dead_zone; 70 }; 71 72 #endif 73