]> git.tdb.fi Git - libs/gui.git/blob - source/input/mouse.cpp
Discard the EventSource abstraction
[libs/gui.git] / source / input / mouse.cpp
1 #include <msp/graphics/window.h>
2 #include <msp/graphics/window_priv.h>
3 #include <msp/strings/format.h>
4 #include "mouse.h"
5
6 namespace Msp {
7 namespace Input {
8
9 Mouse::Mouse(Graphics::Window &w):
10         window(w)
11 {
12         name = "Mouse";
13
14         buttons.resize(3);
15         axes.resize(2);
16
17         window.signal_input_event.connect(sigc::mem_fun(this, &Mouse::input_event));
18 }
19
20 std::string Mouse::get_button_name(unsigned btn) const
21 {
22         switch(btn)
23         {
24         case 1:
25                 return "Left";
26         case 2:
27                 return "Middle";
28         case 3:
29                 return "Right";
30         case 4:
31                 return "Wheel Up";
32         case 5:
33                 return "Wheel Down";
34         default:
35                 return format("Button %d", btn);
36         }
37 }
38
39 std::string Mouse::get_axis_name(unsigned axis) const
40 {
41         switch(axis)
42         {
43         case 0:
44                 return "X axis";
45         case 1:
46                 return "Y axis";
47         default:
48                 return format("Axis %d", axis);
49         };
50 }
51
52 void Mouse::input_event(const Graphics::Window::Event &event)
53 {
54 #ifdef WIN32
55         switch(event.msg)
56         {
57         case WM_LBUTTONDOWN:
58         case WM_LBUTTONUP:
59                 set_button_state(1, event.msg==WM_LBUTTONDOWN, true);
60                 break;
61         case WM_MBUTTONDOWN:
62         case WM_MBUTTONUP:
63                 set_button_state(2, event.msg==WM_LBUTTONDOWN, true);
64                 break;
65         case WM_RBUTTONDOWN:
66         case WM_RBUTTONUP:
67                 set_button_state(3, event.msg==WM_LBUTTONDOWN, true);
68                 break;
69         case WM_MOUSEWHEEL:
70                 {
71                         unsigned btn = (HIWORD(wp)&0x8000) ? 5 : 4;
72                         set_button_state(btn, true, true);
73                         set_button_state(btn, false, true);
74                 }
75                 break;
76         case WM_MOUSEMOVE:
77                 set_axis_value(0, GET_X_LPARAM(event.lparam)*2.0/window.get_width()-1.0, true);
78                 set_axis_value(1, 1.0-GET_Y_LPARAM(event.lparam)*2.0/window.get_height(), true);
79                 break;
80         }
81 #else
82         switch(event.xevent.type)
83         {
84         case ButtonPress:
85         case ButtonRelease:
86                 set_button_state(event.xevent.xbutton.button, event.xevent.type==ButtonPress, true);
87                 break;
88         case MotionNotify:
89                 set_axis_value(0, event.xevent.xmotion.x*2.0/window.get_width()-1.0, true);
90                 set_axis_value(1, 1.0-event.xevent.xmotion.y*2.0/window.get_height(), true);
91                 break;
92         }
93 #endif
94 }
95
96 } // namespace Input
97 } // namespace Msp