1 /************************************************************************************
2 
3 	AstroMenace
4 	Hardcore 3D space scroll-shooter with spaceship upgrade possibilities.
5 	Copyright (c) 2006-2019 Mikhail Kurinnoi, Viewizard
6 
7 
8 	AstroMenace is free software: you can redistribute it and/or modify
9 	it under the terms of the GNU General Public License as published by
10 	the Free Software Foundation, either version 3 of the License, or
11 	(at your option) any later version.
12 
13 	AstroMenace is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 	GNU General Public License for more details.
17 
18 	You should have received a copy of the GNU General Public License
19 	along with AstroMenace. If not, see <https://www.gnu.org/licenses/>.
20 
21 
22 	Website: https://viewizard.com/
23 	Project: https://github.com/viewizard/astromenace
24 	E-mail: viewizard@viewizard.com
25 
26 *************************************************************************************/
27 
28 #include "../graphics/graphics.h"
29 #include "../math/math.h"
30 
31 namespace viewizard {
32 
33 namespace {
34 
35 // right mouse button status
36 bool MouseRightClick{false};
37 // left mouse button status
38 bool MouseLeftClick{false};
39 // mouse button status array
40 // SDL use Uint8 value type for mouse button status, that starts from 1,
41 // so, this could be up to 255 buttons (in future)
42 std::vector<bool> MouseButtonArray{};
43 
44 // X mouse pointer position
45 int MouseX{0};
46 // Y mouse pointer position
47 int MouseY{0};
48 // wheel status
49 int MouseWheelStatus{0};
50 // left mouse button double click status
51 bool MouseLeftDoubleClick{false};
52 
53 } // unnamed namespace
54 
55 
56 /*
57  * Get mouse position.
58  */
vw_GetMousePos(int & X,int & Y)59 void vw_GetMousePos(int &X, int &Y)
60 {
61 	float tmpViewportWidth, tmpViewportHeight;
62 	vw_GetViewport(nullptr, nullptr, &tmpViewportWidth, &tmpViewportHeight);
63 
64 	float tmpInternalWidth, tmpInternalHeight;
65 	if (vw_GetInternalResolution(&tmpInternalWidth, &tmpInternalHeight)) {
66 		// WARNING probably, we could move mouse position's vatiables to float type (blocked by sRECT)
67 		X = static_cast<int>(static_cast<float>(MouseX) * tmpInternalWidth / tmpViewportWidth);
68 		Y = static_cast<int>(static_cast<float>(MouseY) * tmpInternalHeight / tmpViewportHeight);
69 	} else {
70 		X = MouseX;
71 		Y = MouseY;
72 	}
73 }
74 
75 /*
76  * Set left mouse button double click status.
77  */
vw_SetMouseLeftDoubleClick(bool NewStatus)78 void vw_SetMouseLeftDoubleClick(bool NewStatus)
79 {
80 	MouseLeftDoubleClick = NewStatus;
81 }
82 
83 /*
84  * Get left mouse button double click status.
85  */
vw_GetMouseLeftDoubleClick(bool ResetStatus)86 bool vw_GetMouseLeftDoubleClick(bool ResetStatus)
87 {
88 	bool tmp = MouseLeftDoubleClick;
89 	if (ResetStatus)
90 		MouseLeftDoubleClick = false;
91 
92 	return tmp;
93 }
94 
95 /*
96  * Set left mouse button status.
97  */
vw_SetMouseLeftClick(bool NewStatus)98 void vw_SetMouseLeftClick(bool NewStatus)
99 {
100 	MouseLeftClick = NewStatus;
101 }
102 
103 /*
104  * Get left mouse button status.
105  */
vw_GetMouseLeftClick(bool ResetStatus)106 bool vw_GetMouseLeftClick(bool ResetStatus)
107 {
108 	bool tmp = MouseLeftClick;
109 	if (ResetStatus)
110 		MouseLeftClick = false;
111 
112 	return tmp;
113 }
114 
115 /*
116  * Set right mouse button status.
117  */
vw_SetMouseRightClick(bool NewStatus)118 void vw_SetMouseRightClick(bool NewStatus)
119 {
120 	MouseRightClick = NewStatus;
121 }
122 
123 /*
124  * Get right mouse button status.
125  */
vw_GetMouseRightClick(bool ResetStatus)126 bool vw_GetMouseRightClick(bool ResetStatus)
127 {
128 	bool tmp = MouseRightClick;
129 	if (ResetStatus)
130 		MouseRightClick = false;
131 
132 	return tmp;
133 }
134 
135 /*
136  * Set mouse button status.
137  */
vw_SetMouseButtonStatus(unsigned Button,bool NewStatus)138 void vw_SetMouseButtonStatus(unsigned Button, bool NewStatus)
139 {
140 	assert(Button);
141 
142 	// note, SDL provide button's number that starts from 1
143 	if (Button > MouseButtonArray.size())
144 		MouseButtonArray.resize(Button, false);
145 
146 	MouseButtonArray[Button - 1] = NewStatus;
147 }
148 
149 /*
150  * Get mouse button status.
151  */
vw_GetMouseButtonStatus(unsigned Button)152 bool vw_GetMouseButtonStatus(unsigned Button)
153 {
154 	assert(Button);
155 
156 	// note, SDL provide button's number that starts from 1
157 	if (Button > MouseButtonArray.size())
158 		MouseButtonArray.resize(Button, false);
159 
160 	return MouseButtonArray[Button - 1];
161 }
162 
163 /*
164  * Get maximum mouse button number.
165  */
vw_GetMaxMouseButtonNum()166 unsigned vw_GetMaxMouseButtonNum()
167 {
168 	return static_cast<unsigned>(MouseButtonArray.size());
169 }
170 
171 /*
172  * Reset mouse buttons.
173  */
vw_ResetMouseButtons()174 void vw_ResetMouseButtons()
175 {
176 	std::fill(MouseButtonArray.begin(), MouseButtonArray.end(), false);
177 }
178 
179 /*
180  * Change mouse wheel status.
181  */
vw_ChangeWheelStatus(int Value)182 void vw_ChangeWheelStatus(int Value)
183 {
184 	MouseWheelStatus += Value;
185 }
186 
187 /*
188  * Reset mouse wheel status.
189  */
vw_ResetWheelStatus()190 void vw_ResetWheelStatus()
191 {
192 	MouseWheelStatus = 0;
193 }
194 
195 /*
196  * Get mouse wheel status.
197  */
vw_GetWheelStatus()198 int vw_GetWheelStatus()
199 {
200 	return MouseWheelStatus;
201 }
202 
203 /*
204  * Set mouse position in relative coordinates.
205  */
vw_SetMousePosRel(int X,int Y)206 void vw_SetMousePosRel(int X, int Y)
207 {
208 	MouseX += X;
209 	MouseY += Y;
210 
211 	int tmpViewportWidth, tmpViewportHeight;
212 	vw_GetViewport(nullptr, nullptr, &tmpViewportWidth, &tmpViewportHeight);
213 
214 	vw_Clamp(MouseX, 0, tmpViewportWidth);
215 	vw_Clamp(MouseY, 0, tmpViewportHeight);
216 }
217 
218 /*
219  * Set mouse position in absolute coordinates.
220  */
vw_SetMousePos(int X,int Y)221 void vw_SetMousePos(int X, int Y)
222 {
223 	MouseX = X;
224 	MouseY = Y;
225 }
226 
227 /*
228  * Check mouse position over rectangle.
229  */
vw_MouseOverRect(const sRECT & MDetect)230 bool vw_MouseOverRect(const sRECT &MDetect)
231 {
232 	int tmpMouseX, tmpMouseY;
233 	vw_GetMousePos(tmpMouseX, tmpMouseY);
234 
235 	if  ((MDetect.right >= tmpMouseX) &&
236 	     (MDetect.left <= tmpMouseX) &&
237 	     (MDetect.bottom >= tmpMouseY) &&
238 	     (MDetect.top <= tmpMouseY))
239 		return true;
240 
241 	return false;
242 }
243 
244 } // viewizard namespace
245