From 75a3f0fd4152a71aefec72cb053846646071af03 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 17 Nov 2022 11:54:28 +0200 Subject: [PATCH] Add a type enumeration for devices This will help in storing control bindings in a way that isn't tied to a particular instance of a device. --- source/input/device.cpp | 3 ++- source/input/device.h | 14 +++++++++++++- source/input/generic/gamecontroller.cpp | 3 ++- source/input/gesturedetector.cpp | 1 + source/input/hub.cpp | 3 ++- source/input/keyboard.cpp | 1 + source/input/linux/gamecontroller.cpp | 7 ++++--- source/input/mouse.cpp | 1 + source/input/touchscreen.cpp | 1 + source/input/windows/gamecontroller.cpp | 3 ++- 10 files changed, 29 insertions(+), 8 deletions(-) diff --git a/source/input/device.cpp b/source/input/device.cpp index 2283136..7dbe8ea 100644 --- a/source/input/device.cpp +++ b/source/input/device.cpp @@ -4,7 +4,8 @@ namespace Msp { namespace Input { -Device::Device() +Device::Device(DeviceType t): + type(t) { } Device::~Device() diff --git a/source/input/device.h b/source/input/device.h index b42ed3f..be708e2 100644 --- a/source/input/device.h +++ b/source/input/device.h @@ -17,6 +17,16 @@ public: }; +enum DeviceType +{ + UNSPECIFIED, + KEYBOARD, + MOUSE, + TOUCH_SURFACE, + GAME_CONTROLLER +}; + + /** Base class for input devices. Input devices have two types of controls: buttons and axes. Buttons are either on or off. Axes have a floating point @@ -47,14 +57,16 @@ public: sigc::signal::accumulated signal_axis_motion; protected: + DeviceType type; std::string name; std::vector buttons; std::vector axes; - Device(); + Device(DeviceType); public: virtual ~Device(); + DeviceType get_type() const { return type; } const std::string &get_name() const { return name; } bool get_button_state(unsigned) const; float get_axis_value(unsigned) const; diff --git a/source/input/generic/gamecontroller.cpp b/source/input/generic/gamecontroller.cpp index 9ab6738..8f14e02 100644 --- a/source/input/generic/gamecontroller.cpp +++ b/source/input/generic/gamecontroller.cpp @@ -6,7 +6,8 @@ using namespace std; namespace Msp { namespace Input { -GameController::GameController(unsigned) +GameController::GameController(unsigned): + Device(GAME_CONTROLLER) { throw device_not_available("GameController"); } diff --git a/source/input/gesturedetector.cpp b/source/input/gesturedetector.cpp index 97e3d56..d53c07f 100644 --- a/source/input/gesturedetector.cpp +++ b/source/input/gesturedetector.cpp @@ -10,6 +10,7 @@ namespace Msp { namespace Input { GestureDetector::GestureDetector(Touchscreen &ts): + Device(UNSPECIFIED), touchscreen(ts), current_gesture(GESTURE_NONE), pending_tap(GESTURE_NONE), diff --git a/source/input/hub.cpp b/source/input/hub.cpp index 635ea8a..b83d10d 100644 --- a/source/input/hub.cpp +++ b/source/input/hub.cpp @@ -10,7 +10,8 @@ using namespace std; namespace Msp { namespace Input { -Hub::Hub() +Hub::Hub(): + Device(UNSPECIFIED) { name = "Hub"; } diff --git a/source/input/keyboard.cpp b/source/input/keyboard.cpp index 0a690d1..30a6145 100644 --- a/source/input/keyboard.cpp +++ b/source/input/keyboard.cpp @@ -6,6 +6,7 @@ namespace Msp { namespace Input { Keyboard::Keyboard(Graphics::Window &w): + Device(KEYBOARD), window(w) { name = "Keyboard"; diff --git a/source/input/linux/gamecontroller.cpp b/source/input/linux/gamecontroller.cpp index f0139e9..d0029e8 100644 --- a/source/input/linux/gamecontroller.cpp +++ b/source/input/linux/gamecontroller.cpp @@ -16,6 +16,7 @@ namespace Input { vector GameController::Private::detected_controllers; GameController::GameController(unsigned index): + Device(GAME_CONTROLLER), event_disp(0) { if(!detect_done) @@ -79,11 +80,11 @@ void GameController::tick() unsigned count = len/sizeof(js_event); for(unsigned i=0; i GameController::Private::detected_controllers; -GameController::GameController(unsigned index) +GameController::GameController(unsigned index): + Device(GAME_CONTROLLER) { if(!detect_done) detect(); -- 2.45.2