-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.h
158 lines (139 loc) · 3.86 KB
/
mouse.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Copyright (C) 2000 Nate Miller [email protected]
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
See gpl.txt for more information regarding the GNU General Public License.
*/
/*
To use this mouse class with GLUT make sure that you #define __GLUTMOUSE__
before you #include "mouse.h". I was thinking about having this header check
for GLUT #defines, but you would have had to #include <glut.h> before this
header which could lead to problems.
*/
#ifndef __MOUSEH__
#define __MOUSEH__
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#ifdef __GLUTMOUSE__
#include <GL/glut.h>
#endif
enum
{
buttonLeft = 1,
buttonRight = 2,
buttonMiddle = 4,
};
struct mouse_t
{
mouse_t() : oldX(-1), oldY(-1), currX(-1), currY(-1) {Reset();}
inline void SetState(int btn, int ste = 0);
inline void SetNewPos(int x, int y);
int IsLeft(void) const {return (state & buttonLeft);}
int IsRight(void) const {return (state & buttonRight);}
int IsMiddle(void) const {return (state & buttonMiddle);}
void Reset(void) {state = 0;}
int GetOldX(void) const {return oldX;}
int GetOldY(void) const {return oldY;}
int GetX(void) const {return currX;}
int GetY(void) const {return currY;}
int GetDiffX(int x) const {return x - oldX;}
int GetDiffY(int y) const {return y - oldY;}
int GetDiffX(void) const {return currX - oldX;}
int GetDiffY(void) const {return currY - oldY;}
void SetOldPos(int x, int y) {oldX = x; oldY = y;}
#ifdef _WIN32
void SetCapture(HWND hWnd) const {SetCapture(hWnd);}
void Release(void) const {ReleaseCapture();}
#endif
protected:
int state;
int oldX;
int oldY;
int currX;
int currY;
};
inline void mouse_t::SetState(int btn, int ste)
{
#ifdef _WIN32
#ifndef __GLUTMOUSE__
switch (btn)
{
case WM_LBUTTONDOWN:
state |= buttonLeft;
break;
case WM_RBUTTONDOWN:
state |= buttonRight;
break;
case WM_MBUTTONDOWN:
state |= buttonMiddle;
break;
case WM_LBUTTONUP:
state &= ~buttonLeft;
break;
case WM_RBUTTONUP:
state &= ~buttonRight;
break;
case WM_MBUTTONUP:
state &= ~buttonMiddle;
break;
}
#endif
#endif
#ifdef __GLUTMOUSE__
if (ste == GLUT_DOWN)
{
switch(btn)
{
case GLUT_LEFT_BUTTON:
state |= buttonLeft;
break;
case GLUT_RIGHT_BUTTON:
state |= buttonRight;
break;
case GLUT_MIDDLE_BUTTON:
state |= buttonMiddle;
break;
}
}
else if (ste == GLUT_UP)
{
switch(btn)
{
case GLUT_LEFT_BUTTON:
state &= ~buttonLeft;
break;
case GLUT_RIGHT_BUTTON:
state &= ~buttonRight;
break;
case GLUT_MIDDLE_BUTTON:
state &= ~buttonMiddle;
break;
}
}
#endif
}
inline void mouse_t::SetNewPos(int x, int y)
{
currX = x;
currY = y;
#ifdef _WIN32
#ifndef __GLUTMOUSE__
if(currX & 1 << 15)
currX -= (1 << 16);
if(currY & 1 << 15)
currY -= (1 << 16);
#endif
#endif
}
#endif