SDLMouse.cpp 4.73 KB
Newer Older
1
2
3
4
5
6
7
8
/*
The zlib/libpng License

Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)

This software is provided 'as-is', without any express or implied warranty. In no event will
the authors be held liable for any damages arising from the use of this software.

Ben Hymers's avatar
Ben Hymers committed
9
Permission is granted to anyone to use this software for any purpose, including commercial
10
11
12
applications, and to alter it and redistribute it freely, subject to the following
restrictions:

Ben Hymers's avatar
Ben Hymers committed
13
14
15
    1. The origin of this software must not be misrepresented; you must not claim that
		you wrote the original software. If you use this software in a product,
		an acknowledgment in the product documentation would be appreciated but is
16
17
		not required.

Ben Hymers's avatar
Ben Hymers committed
18
    2. Altered source versions must be plainly marked as such, and must not be
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
		misrepresented as being the original software.

    3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL/SDLMouse.h"
#include "SDL/SDLInputManager.h"
#include "OISException.h"
#include "OISEvents.h"

using namespace OIS;

//-------------------------------------------------------------------//
SDLMouse::SDLMouse( bool buffered ) : mGrabbed(false), mRegainFocus(false)
{
	mBuffered = buffered;
	mType = OISMouse;
	listener = 0;
}

//-------------------------------------------------------------------//
void SDLMouse::_initialize()
{
	//Clear old state
	mState.clear();
	mRegainFocus = false;

	_setGrab(true);
	_setVisible(false);
	static_cast<SDLInputManager*>(InputManager::getSingletonPtr())->_setGrabMode(true);
}

//-------------------------------------------------------------------//
SDLMouse::~SDLMouse()
{
	_setGrab(true);
	_setVisible(true);

	static_cast<SDLInputManager*>(InputManager::getSingletonPtr())->_setGrabMode(false);
}

//-------------------------------------------------------------------//
void SDLMouse::capture()
{
	//Used for going from SDL Button to OIS button
	static const MouseButtonID ButtonMask[4] = {MB_Left, MB_Left, MB_Middle, MB_Right};

	//Clear old relative values
	mState.relX = mState.relY = mState.relZ = 0;

	SDL_Event events[OIS_SDL_MOUSE_BUFF];
	int count = SDL_PeepEvents(events, OIS_SDL_MOUSE_BUFF, SDL_GETEVENT, SDL_MOUSEEVENTMASK);

	bool mouseXYMoved = false;
	bool mouseZMoved = false;
	for( int i = 0; i < count; ++i )
	{
		switch( events[i].type )
		{
Ben Hymers's avatar
Ben Hymers committed
77
			case SDL_MOUSEMOTION: mouseXYMoved = true; break;
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
			case SDL_MOUSEBUTTONDOWN:
			{
				mRegainFocus = true;
				int sdlButton = events[i].button.button;
				if( sdlButton <= SDL_BUTTON_RIGHT )
				{	//Left, Right, or Middle
					mState.buttons |= (1 << ButtonMask[sdlButton]);
					if( mBuffered && listener )
						if( listener->mousePressed(MouseEvent(this,0,mState), ButtonMask[sdlButton]) == false )
							return;
				}
				else
				{	//mouse Wheel
					mouseZMoved = true;
					if( sdlButton == SDL_BUTTON_WHEELUP )
						mState.relZ += 120;
					else if( sdlButton == SDL_BUTTON_WHEELDOWN )
						mState.relZ -= 120;
				}
				break;
			}
			case SDL_MOUSEBUTTONUP:
			{
				int sdlButton = events[i].button.button;
				if( sdlButton <= SDL_BUTTON_RIGHT )
				{	//Left, Right, or Middle
					mState.buttons &= ~(1 << ButtonMask[sdlButton]);
					if( mBuffered && listener )
						if( listener->mouseReleased(MouseEvent(this,0,mState), ButtonMask[sdlButton]) == false )
							return;
				}
				break;
			}
		}
	}

	//Handle X/Y axis move
	if( mouseXYMoved )
	{
		SDL_GetMouseState( &mState.abX, &mState.abY );
		SDL_GetRelativeMouseState( &mState.relX, &mState.relY );

		if( mBuffered && listener )
			listener->mouseMoved(MouseEvent(this, 0, mState));
	}
	//Handle Z Motion
	if( mouseZMoved )
	{
		mState.abZ += mState.relZ;
		if( mBuffered && listener )
			listener->mouseMoved(MouseEvent(this, 0, mState));
	}

	//Handle Alt-Tabbing
	SDLInputManager* man = static_cast<SDLInputManager*>(InputManager::getSingletonPtr());
	if( man->_getGrabMode() == false )
	{
		if( mRegainFocus == false && mGrabbed == true )
		{	//We had focus, but must release it now
			_setGrab(false);
			_setVisible(true);
		}
		else if( mRegainFocus == true && mGrabbed == false )
		{	//We are gaining focus back (mouse clicked in window)
			_setGrab(true);
			_setVisible(false);
			man->_setGrabMode(true);	//Notify manager
		}
	}
}

//-------------------------------------------------------------------//
void SDLMouse::setBuffered(bool buffered)
{
	mBuffered = buffered;
}

//-------------------------------------------------------------------//
void SDLMouse::_setGrab(bool grabbed)
{
	if( grabbed )
		SDL_WM_GrabInput(SDL_GRAB_ON);
	else
		SDL_WM_GrabInput(SDL_GRAB_OFF);

	mGrabbed = grabbed;
}

//-------------------------------------------------------------------//
void SDLMouse::_setVisible(bool visible)
{

	if( visible )
		SDL_ShowCursor(SDL_ENABLE);
	else
		SDL_ShowCursor(SDL_DISABLE);
}