]> git.tdb.fi Git - libs/gui.git/commitdiff
Add constants for mouse axes and buttons
authorMikko Rasa <tdb@tdb.fi>
Sun, 20 Nov 2022 14:01:20 +0000 (16:01 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 20 Nov 2022 14:01:20 +0000 (16:01 +0200)
source/input/android/mouse.cpp
source/input/bindings.cpp
source/input/bindings.h
source/input/cocoa/mouse.cpp
source/input/keys.cpp
source/input/keys.h
source/input/mouse.cpp
source/input/windows/mouse.cpp
source/input/x11/mouse.cpp

index 3fbc5bebab3affc54b8d478fcdc412f7680dd4ca..6769d78f8402cd3765dd26e8324322cfd1c3aeed 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/graphics/window_private.h>
+#include "keys.h"
 #include "mouse.h"
 
 namespace Msp {
@@ -32,8 +33,8 @@ void Mouse::input_event(const Graphics::Window::Event &event)
        {
                float x = AMotionEvent_getX(event.aevent, pointer_zero);
                float y = AMotionEvent_getY(event.aevent, pointer_zero);
-               set_axis_value(0, x*2/window.get_width()-1, true);
-               set_axis_value(1, 1-y*2/window.get_height(), true);
+               set_axis_value(MOUSE_X_AXIS, x*2/window.get_width()-1, true);
+               set_axis_value(MOUSE_Y_AXIS, 1-y*2/window.get_height(), true);
        }
 
        switch(action)
@@ -43,7 +44,7 @@ void Mouse::input_event(const Graphics::Window::Event &event)
        case AMOTION_EVENT_ACTION_POINTER_DOWN:
        case AMOTION_EVENT_ACTION_POINTER_UP:
                if(action_pointer==0)
-                       set_button_state(1, action==AMOTION_EVENT_ACTION_DOWN, true);
+                       set_button_state(MOUSE_LEFT, action==AMOTION_EVENT_ACTION_DOWN, true);
                break;
        default:;
        }
index 85b59418a875b85b17b13ff3a58df1450ce4b004..1d33292c57ca91f4d305d0ad73bce57e65c724bc 100644 (file)
@@ -103,6 +103,8 @@ void Bindings::Binding::Loader::init_actions()
        add("button", &Loader::button);
        add("device", &Binding::device);
        add("key", &Loader::key);
+       add("mouse_axis", &Loader::mouse_axis);
+       add("mouse_button", &Loader::mouse_button);
 }
 
 void Bindings::Binding::Loader::axis(unsigned a, AxisSide s)
index b73dc51cc308bfc0d74fefea3b0d5b2f4fe18512..879a516e20c9ea6e8e156e678044719b3d260ed7 100644 (file)
@@ -64,6 +64,8 @@ public:
                        void axis(unsigned, AxisSide);
                        void button(unsigned);
                        void key(Key k) { button(k); }
+                       void mouse_axis(MouseAxis a, AxisSide s) { axis(a, s); }
+                       void mouse_button(MouseButton b) { button(b); }
                };
 
                std::string control;
index b1d3619c3c1631c0bd27ff11b122e70df3fa8e2e..abda35c65e5c3808c98bb448a6114b3257df6dae 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/graphics/window_private.h>
+#include "keys.h"
 #include "mouse.h"
 
 namespace Msp {
@@ -17,8 +18,8 @@ void Mouse::input_event(const Graphics::Window::Event &event)
                set_button_state(event.cevent.button.button, event.cevent.button.state, true);
                break;
        case MOUSE_MOVED:
-               set_axis_value(0, event.cevent.motion.x*2.0/window.get_width()-1.0, true);
-               set_axis_value(1, event.cevent.motion.y*2.0/window.get_height()-1.0, true);
+               set_axis_value(MOUSE_X_AXIS, event.cevent.motion.x*2.0/window.get_width()-1.0, true);
+               set_axis_value(MOUSE_Y_AXIS, event.cevent.motion.y*2.0/window.get_height()-1.0, true);
                break;
        default:;
        }
index 4a6df371776a843ab159c9dc4493e92ee9c586f8..6be00ba591cbb0618733e0ba1533f867c16fe146 100644 (file)
@@ -333,5 +333,54 @@ void operator<<(LexicalConverter &conv, Key key)
        }
 }
 
+void operator>>(const LexicalConverter &conv, MouseAxis &axis)
+{
+       if(conv.get()=="X")
+               axis = MOUSE_X_AXIS;
+       else if(conv.get()=="Y")
+               axis = MOUSE_Y_AXIS;
+       else
+               throw lexical_error(format("conversion of '%s' to MouseAxis", conv.get()));
+}
+
+void operator<<(LexicalConverter &conv, MouseAxis axis)
+{
+       switch(axis)
+       {
+       case MOUSE_X_AXIS: conv.result("X"); break;
+       case MOUSE_Y_AXIS: conv.result("Y"); break;
+       default: conv.result(format("MouseAxis(%#x)", static_cast<int>(axis)));
+       }
+}
+
+void operator>>(const LexicalConverter &conv, MouseButton &btn)
+{
+       if(conv.get()=="LEFT")
+               btn = MOUSE_LEFT;
+       else if(conv.get()=="MIDDLE")
+               btn = MOUSE_MIDDLE;
+       else if(conv.get()=="RIGHT")
+               btn = MOUSE_RIGHT;
+       else if(conv.get()=="WHEEL_UP")
+               btn = MOUSE_WHEEL_UP;
+       else if(conv.get()=="WHEEL_DOWN")
+               btn = MOUSE_WHEEL_DOWN;
+       else
+               throw lexical_error(format("conversion of '%s' to MouseButton", conv.get()));
+}
+
+void operator<<(LexicalConverter &conv, MouseButton btn)
+{
+       switch(btn)
+       {
+       case MOUSE_LEFT: conv.result("LEFT"); break;
+       case MOUSE_MIDDLE: conv.result("MIDDLE"); break;
+       case MOUSE_RIGHT: conv.result("RIGHT"); break;
+       case MOUSE_WHEEL_UP: conv.result("WHEEL_UP"); break;
+       case MOUSE_WHEEL_DOWN: conv.result("WHEEL_DOWN"); break;
+       default: conv.result(format("MouseButton(%#x)", static_cast<int>(btn)));
+       }
+}
+
 } // namespace Input
 } // namespace Msp
index c974dc26b8896b6e23d7372aaa0b3fbc24ba5abb..63d0926cf48565ea806faec7593819660a8d9bda 100644 (file)
@@ -146,8 +146,27 @@ enum Key
        N_KEYS_ = 0x100
 };
 
+enum MouseAxis
+{
+       MOUSE_X_AXIS = 0,
+       MOUSE_Y_AXIS,
+};
+
+enum MouseButton
+{
+       MOUSE_LEFT = 1,
+       MOUSE_MIDDLE,
+       MOUSE_RIGHT,
+       MOUSE_WHEEL_UP,
+       MOUSE_WHEEL_DOWN
+};
+
 void operator>>(const LexicalConverter &, Key &);
 void operator<<(LexicalConverter &, Key);
+void operator>>(const LexicalConverter &, MouseAxis &);
+void operator<<(LexicalConverter &, MouseAxis);
+void operator>>(const LexicalConverter &, MouseButton &);
+void operator<<(LexicalConverter &, MouseButton);
 
 } // namespace Input
 } // namespace Msp
index 48d0f6922338652763cea0c0b1578ee9d74ff04e..937a89ea6aa3fbe52cac884de1827904785bf375 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/graphics/window.h>
+#include "keys.h"
 #include "mouse.h"
 
 namespace Msp {
@@ -20,15 +21,15 @@ std::string Mouse::get_button_name(unsigned btn) const
 {
        switch(btn)
        {
-       case 1:
+       case MOUSE_LEFT:
                return "Left";
-       case 2:
+       case MOUSE_MIDDLE:
                return "Middle";
-       case 3:
+       case MOUSE_RIGHT:
                return "Right";
-       case 4:
+       case MOUSE_WHEEL_UP:
                return "Wheel Up";
-       case 5:
+       case MOUSE_WHEEL_DOWN:
                return "Wheel Down";
        default:
                return Device::get_button_name(btn);
@@ -39,9 +40,9 @@ std::string Mouse::get_axis_name(unsigned axis) const
 {
        switch(axis)
        {
-       case 0:
+       case MOUSE_X_AXIS:
                return "X axis";
-       case 1:
+       case MOUSE_Y_AXIS:
                return "Y axis";
        default:
                return Device::get_axis_name(axis);
index aae5141b6d7f16d7593e2a6dc246472d77230801..c9cb8a1489b993ca2e553c2d07b10dfe6d678a8f 100644 (file)
@@ -1,5 +1,6 @@
 #include <windowsx.h>
 #include <msp/graphics/window_private.h>
+#include "keys.h"
 #include "mouse.h"
 
 namespace Msp {
@@ -15,26 +16,26 @@ void Mouse::input_event(const Graphics::Window::Event &event)
        {
        case WM_LBUTTONDOWN:
        case WM_LBUTTONUP:
-               set_button_state(1, event.msg==WM_LBUTTONDOWN, true);
+               set_button_state(MOUSE_LEFT, event.msg==WM_LBUTTONDOWN, true);
                break;
        case WM_MBUTTONDOWN:
        case WM_MBUTTONUP:
-               set_button_state(2, event.msg==WM_MBUTTONDOWN, true);
+               set_button_state(MOUSE_MIDDLE, event.msg==WM_MBUTTONDOWN, true);
                break;
        case WM_RBUTTONDOWN:
        case WM_RBUTTONUP:
-               set_button_state(3, event.msg==WM_RBUTTONDOWN, true);
+               set_button_state(MOUSE_RIGHT, event.msg==WM_RBUTTONDOWN, true);
                break;
        case WM_MOUSEWHEEL:
                {
-                       unsigned btn = (HIWORD(event.wparam)&0x8000) ? 5 : 4;
+                       unsigned btn = (HIWORD(event.wparam)&0x8000) ? MOUSE_WHEEL_DOWN : MOUSE_WHEEL_UP;
                        set_button_state(btn, true, true);
                        set_button_state(btn, false, true);
                }
                break;
        case WM_MOUSEMOVE:
-               set_axis_value(0, GET_X_LPARAM(event.lparam)*2.0/window.get_width()-1.0, true);
-               set_axis_value(1, 1.0-GET_Y_LPARAM(event.lparam)*2.0/window.get_height(), true);
+               set_axis_value(MOUSE_X_AXIS, GET_X_LPARAM(event.lparam)*2.0/window.get_width()-1.0, true);
+               set_axis_value(MOUSE_Y_AXIS, 1.0-GET_Y_LPARAM(event.lparam)*2.0/window.get_height(), true);
                break;
        }
 }
index 3533d561fc17b4c325c60b06288a84b4e44fcfab..b8a771786a901a73a6dd8bc2a5d238b1c0b04ba9 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/graphics/window_private.h>
+#include "keys.h"
 #include "mouse.h"
 
 namespace Msp {
@@ -13,8 +14,8 @@ void Mouse::input_event(const Graphics::Window::Event &event)
                set_button_state(event.xevent.xbutton.button, event.xevent.type==ButtonPress, true);
                break;
        case MotionNotify:
-               set_axis_value(0, event.xevent.xmotion.x*2.0/window.get_width()-1.0, true);
-               set_axis_value(1, 1.0-event.xevent.xmotion.y*2.0/window.get_height(), true);
+               set_axis_value(MOUSE_X_AXIS, event.xevent.xmotion.x*2.0/window.get_width()-1.0, true);
+               set_axis_value(MOUSE_Y_AXIS, 1.0-event.xevent.xmotion.y*2.0/window.get_height(), true);
                break;
        }
 }