From: Mikko Rasa Date: Sun, 20 Nov 2022 14:01:20 +0000 (+0200) Subject: Add constants for mouse axes and buttons X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=75286abb276e0f7e7a1fb8f5a7a7c1c33ca5bcda;p=libs%2Fgui.git Add constants for mouse axes and buttons --- diff --git a/source/input/android/mouse.cpp b/source/input/android/mouse.cpp index 3fbc5be..6769d78 100644 --- a/source/input/android/mouse.cpp +++ b/source/input/android/mouse.cpp @@ -1,4 +1,5 @@ #include +#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:; } diff --git a/source/input/bindings.cpp b/source/input/bindings.cpp index 85b5941..1d33292 100644 --- a/source/input/bindings.cpp +++ b/source/input/bindings.cpp @@ -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) diff --git a/source/input/bindings.h b/source/input/bindings.h index b73dc51..879a516 100644 --- a/source/input/bindings.h +++ b/source/input/bindings.h @@ -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; diff --git a/source/input/cocoa/mouse.cpp b/source/input/cocoa/mouse.cpp index b1d3619..abda35c 100644 --- a/source/input/cocoa/mouse.cpp +++ b/source/input/cocoa/mouse.cpp @@ -1,4 +1,5 @@ #include +#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:; } diff --git a/source/input/keys.cpp b/source/input/keys.cpp index 4a6df37..6be00ba 100644 --- a/source/input/keys.cpp +++ b/source/input/keys.cpp @@ -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(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(btn))); + } +} + } // namespace Input } // namespace Msp diff --git a/source/input/keys.h b/source/input/keys.h index c974dc2..63d0926 100644 --- a/source/input/keys.h +++ b/source/input/keys.h @@ -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 diff --git a/source/input/mouse.cpp b/source/input/mouse.cpp index 48d0f69..937a89e 100644 --- a/source/input/mouse.cpp +++ b/source/input/mouse.cpp @@ -1,4 +1,5 @@ #include +#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); diff --git a/source/input/windows/mouse.cpp b/source/input/windows/mouse.cpp index aae5141..c9cb8a1 100644 --- a/source/input/windows/mouse.cpp +++ b/source/input/windows/mouse.cpp @@ -1,5 +1,6 @@ #include #include +#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; } } diff --git a/source/input/x11/mouse.cpp b/source/input/x11/mouse.cpp index 3533d56..b8a7717 100644 --- a/source/input/x11/mouse.cpp +++ b/source/input/x11/mouse.cpp @@ -1,4 +1,5 @@ #include +#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; } }