]> git.tdb.fi Git - libs/gui.git/blob - source/input/mouse.cpp
Normalize device axis ranges to [-1, 1]
[libs/gui.git] / source / input / mouse.cpp
1 /* $Id$
2
3 This file is part of libmspgbase
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/strings/formatter.h>
9 #include "mouse.h"
10
11 namespace Msp {
12 namespace Input {
13
14 Mouse::Mouse(Graphics::Window &w):
15         window(w)
16 {
17         name="Mouse";
18
19         buttons.resize(3);
20         axes.resize(2);
21
22         window.signal_button_press.connect(sigc::mem_fun(this, &Mouse::button_press));
23         window.signal_button_release.connect(sigc::mem_fun(this, &Mouse::button_release));
24         window.signal_pointer_motion.connect(sigc::mem_fun(this, &Mouse::pointer_motion));
25 }
26
27 std::string Mouse::get_button_name(unsigned btn) const
28 {
29         switch(btn)
30         {
31         case 1:
32                 return "Left";
33         case 2:
34                 return "Middle";
35         case 3:
36                 return "Right";
37         case 4:
38                 return "Wheel Up";
39         case 5:
40                 return "Wheel Down";
41         default:
42                 return format("Button %d", btn);
43         }
44 }
45
46 std::string Mouse::get_axis_name(unsigned axis) const
47 {
48         switch(axis)
49         {
50         case 0:
51                 return "X axis";
52         case 1:
53                 return "Y axis";
54         default:
55                 return format("Axis %d", axis);
56         };
57 }
58
59 void Mouse::button_press(int, int, unsigned btn, unsigned)
60 {
61         set_button_state(btn, true, true);
62 }
63
64 void Mouse::button_release(int, int, unsigned btn, unsigned)
65 {
66         set_button_state(btn, false, true);
67 }
68
69 void Mouse::pointer_motion(int x, int y)
70 {
71         set_axis_value(0, x*2.0f/window.get_width()-1.0f, true);
72         set_axis_value(1, 1.0f-y*2.0f/window.get_height(), true);
73 }
74
75 } // namespace Input
76 } // namespace Msp