1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #include <GLEXT/GLViewPort.h>
22 #include <graph/Mouse.h>
23 #include <graph/OptionsDisplay.h>
24 #include <common/OptionsScorched.h>
25 #include <client/ScorchedClient.h>
26 #include <engine/GameState.h>
27 
28 Mouse *Mouse::instance_ = 0;
29 
instance()30 Mouse *Mouse::instance()
31 {
32 	if (!instance_) {
33 		instance_ = new Mouse;
34 	}
35 
36 	return instance_;
37 }
38 
Mouse()39 Mouse::Mouse() : mouse_sensitivity_(120)
40 {
41 
42 }
43 
~Mouse()44 Mouse::~Mouse()
45 {
46 
47 }
48 
49 
50 
mouseDown(SDL_Event & event)51 void Mouse::mouseDown(SDL_Event &event)
52 {
53 	int defaultY = (int) (event.button.y * GLViewPort::getHeightMult());
54 	if (!OptionsDisplay::instance()->getSwapYAxis())
55 	{
56 		defaultY = GLViewPort::getHeight() - defaultY;
57 	}
58 
59 	switch (event.button.button) {
60 	case SDL_BUTTON_LEFT:
61 		ScorchedClient::instance()->getGameState().
62 		    mouseDown(GameState::MouseButtonLeft,
63 				(int) (event.button.x * GLViewPort::getWidthMult()),
64 				defaultY);
65 		break;
66 	case SDL_BUTTON_MIDDLE:
67 		ScorchedClient::instance()->getGameState().
68 		    mouseDown(GameState::MouseButtonMiddle,
69 			      (int) (event.button.x * GLViewPort::getWidthMult()),
70 			      defaultY);
71 		break;
72 	case SDL_BUTTON_RIGHT:
73 		ScorchedClient::instance()->getGameState().
74 		    mouseDown(GameState::MouseButtonRight,
75 			      (int) (event.button.x * GLViewPort::getWidthMult()),
76 			      defaultY);
77 		break;
78 	case 4:
79 		ScorchedClient::instance()->getGameState().
80 			mouseWheel(-mouse_sensitivity_);
81 		break;
82 	case 5:
83 		ScorchedClient::instance()->getGameState().
84 			mouseWheel(mouse_sensitivity_);
85 		break;
86 	default:
87 		break;
88 	}
89 }
90 
mouseUp(SDL_Event & event)91 void Mouse::mouseUp(SDL_Event &event)
92 {
93 	int defaultY = (int) (event.button.y * GLViewPort::getHeightMult());
94 	if (!OptionsDisplay::instance()->getSwapYAxis())
95 	{
96 		defaultY = GLViewPort::getHeight() - defaultY;
97 	}
98 
99 	switch (event.button.button) {
100 	case SDL_BUTTON_LEFT:
101 		ScorchedClient::instance()->getGameState().
102 			mouseUp(GameState::MouseButtonLeft,
103 				(int) (event.button.x * GLViewPort::getWidthMult()),
104 				defaultY);
105 		break;
106 	case SDL_BUTTON_MIDDLE:
107 		ScorchedClient::instance()->getGameState().
108 		    mouseUp(GameState::MouseButtonMiddle,
109 			    (int) (event.button.x * GLViewPort::getWidthMult()),
110 				defaultY);
111 		break;
112 	case SDL_BUTTON_RIGHT:
113 		ScorchedClient::instance()->getGameState().
114 			mouseUp(GameState::MouseButtonRight,
115 				(int) (event.button.x * GLViewPort::getWidthMult()),
116 				defaultY);
117 		break;
118 	default:
119 		break;
120 	}
121 }
122 
mouseMove(SDL_Event & event)123 void Mouse::mouseMove(SDL_Event &event)
124 {
125 	int defaultY = (int) (event.button.y * GLViewPort::getHeightMult());
126 	if (!OptionsDisplay::instance()->getSwapYAxis())
127 	{
128 		defaultY = GLViewPort::getHeight() - defaultY;
129 	}
130 
131 	ScorchedClient::instance()->getGameState().
132 		mouseMove(
133 			(int) (event.button.x * GLViewPort::getWidthMult()),
134 			defaultY);
135 }
136 
processMouseEvent(SDL_Event & event)137 void Mouse::processMouseEvent(SDL_Event & event)
138 {
139 	switch (event.type) {
140 	case SDL_MOUSEBUTTONDOWN:
141 		mouseDown(event);
142 		break;
143 	case SDL_MOUSEBUTTONUP:
144 		mouseUp(event);
145 		break;
146 	case SDL_MOUSEMOTION:
147 		mouseMove(event);
148 		break;
149 	}
150 }
151