]> git.tdb.fi Git - libs/gui.git/blobdiff - source/input/device.cpp
Fix a compiler warning from MSVC
[libs/gui.git] / source / input / device.cpp
index 8bca47671d0079127af2be446089ed0498574c25..22429a0371bfcb9062e7c96d478e4a7c1d3a579f 100644 (file)
@@ -1,12 +1,31 @@
-#include <msp/strings/formatter.h>
 #include "device.h"
+#include <msp/strings/format.h>
+
+using namespace std;
 
 namespace Msp {
 namespace Input {
 
+Device::Device(DeviceType t):
+       type(t)
+{ }
+
+Device::~Device()
+{ }
+
+Device *Device::find_subdevice(const string &n)
+{
+       return (n==name ? this : nullptr);
+}
+
+Device *Device::find_subdevice(DeviceType t, unsigned i)
+{
+       return (t==type && i==0 ? this : nullptr);
+}
+
 bool Device::get_button_state(unsigned btn) const
 {
-       if(btn>buttons.size())
+       if(btn>=buttons.size())
                return false;
 
        return buttons[btn];
@@ -14,18 +33,18 @@ bool Device::get_button_state(unsigned btn) const
 
 float Device::get_axis_value(unsigned axis) const
 {
-       if(axis>axes.size())
+       if(axis>=axes.size())
                return 0;
 
        return axes[axis];
 }
 
-std::string Device::get_button_name(unsigned btn) const
+string Device::get_button_name(unsigned btn) const
 {
        return format("Button %d", btn);
 }
 
-std::string Device::get_axis_name(unsigned axis) const
+string Device::get_axis_name(unsigned axis) const
 {
        return format("Axis %d", axis);
 }
@@ -35,9 +54,9 @@ void Device::set_button_state(unsigned btn, bool state, bool event)
        if(btn>=buttons.size())
                buttons.resize(btn+1, false);
 
-       if(state!=buttons[btn])
+       if(state!=static_cast<bool>(buttons[btn]))
        {
-               buttons[btn]=state;
+               buttons[btn] = state;
 
                if(event)
                {
@@ -56,13 +75,43 @@ void Device::set_axis_value(unsigned axis, float value, bool event)
 
        if(value!=axes[axis])
        {
-               float old=axes[axis];
-               axes[axis]=value;
+               float old = axes[axis];
+               axes[axis] = value;
 
                if(event)
                        signal_axis_motion.emit(axis, value, value-old);
        }
 }
 
+
+void operator>>(const LexicalConverter &conv, DeviceType &type)
+{
+       if(conv.get()=="UNSPECIFIED")
+               type = UNSPECIFIED;
+       else if(conv.get()=="KEYBOARD")
+               type = KEYBOARD;
+       else if(conv.get()=="MOUSE")
+               type = MOUSE;
+       else if(conv.get()=="TOUCH_SURFACE")
+               type = TOUCH_SURFACE;
+       else if(conv.get()=="GAME_CONTROLLER")
+               type = GAME_CONTROLLER;
+       else
+               throw lexical_error(format("conversion of '%s' to DeviceType", conv.get()));
+}
+
+void operator<<(LexicalConverter &conv, DeviceType type)
+{
+       switch(type)
+       {
+       case UNSPECIFIED: conv.result("UNSPECIFIED"); break;
+       case KEYBOARD: conv.result("KEYBOARD"); break;
+       case MOUSE: conv.result("MOUSE"); break;
+       case TOUCH_SURFACE: conv.result("TOUCH_SURFACE"); break;
+       case GAME_CONTROLLER: conv.result("GAME_CONTROLLER"); break;
+       default: conv.result(format("DeviceType(%#x)", static_cast<int>(type)));
+       }
+}
+
 } // namespace Input
 } // namespace Msp