1 /* bzflag
2  * Copyright (c) 1993-2021 Tim Riker
3  *
4  * This package is free software;  you can redistribute it and/or
5  * modify it under the terms of the license found in the file
6  * named COPYING that should have accompanied this file.
7  *
8  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
9  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 
13 
14 /* interface headers */
15 #include "MainWindow.h"
16 
17 /* common implementation headers */
18 #include "global.h"
19 #include "SceneRenderer.h"
20 
21 //
22 // MainWindow
23 //
24 
MainWindow(BzfWindow * _window,BzfJoystick * _joystick)25 MainWindow::MainWindow(BzfWindow* _window, BzfJoystick* _joystick) :
26     window(_window),
27     joystick(_joystick),
28     quit(false),
29     quadrant(FullWindow),
30     isFullscreen(false),
31     isFullView(true),
32     allowMouseGrab(true),
33     grabEnabled(true),
34     width(0),
35     minWidth(MinX),
36     minHeight(MinY),
37     faulting(false)
38 {
39     if (!window->create())
40         faulting = true;
41 
42     window->addResizeCallback(resizeCB, this);
43     resize();
44 }
45 
~MainWindow()46 MainWindow::~MainWindow()
47 {
48     window->removeResizeCallback(resizeCB, this);
49 }
50 
setMinSize(int _minWidth,int _minHeight)51 void            MainWindow::setMinSize(int _minWidth, int _minHeight)
52 {
53     minWidth = _minWidth;
54     minHeight = _minHeight;
55     window->setMinSize(minWidth, minHeight);
56     resize();
57 }
58 
setPosition(int x,int y)59 void            MainWindow::setPosition(int x, int y)
60 {
61     window->setPosition(x, y);
62 }
63 
setSize(int _width,int _height)64 void            MainWindow::setSize(int _width, int _height)
65 {
66     window->setSize(_width, _height);
67     resize();
68 }
69 
showWindow(bool on)70 void            MainWindow::showWindow(bool on)
71 {
72     window->showWindow(on);
73     if (on) resize();
74 }
75 
76 
getYOffset() const77 inline int MainWindow::getYOffset() const
78 {
79     if (quadrant == FullWindow)
80         return 0;
81     return ((trueHeight + 1) >> 1) - yOrigin;
82 }
83 
warpMouse()84 void            MainWindow::warpMouse()
85 {
86     // move mouse to center of view window (zero motion box)
87     window->warpMouse((width >> 1) + xOrigin, (viewHeight >> 1) + getYOffset());
88 }
89 
warpMouse(int mx,int my)90 void            MainWindow::warpMouse(int mx, int my)
91 {
92     window->warpMouse(mx, my);
93 }
94 
warpMouseCenterX()95 void            MainWindow::warpMouseCenterX()
96 {
97     int mx, my;
98     getMousePosition(mx, my);
99     window->warpMouse((width >> 1) + xOrigin, my + (viewHeight >> 1) + getYOffset());
100 }
101 
warpMouseCenterY()102 void            MainWindow::warpMouseCenterY()
103 {
104     int mx, my;
105     getMousePosition(mx, my);
106     const int y = (viewHeight >> 1) + getYOffset();
107     window->warpMouse(mx + (width >> 1) + xOrigin, y);
108 }
109 
getMousePosition(int & mx,int & my) const110 void            MainWindow::getMousePosition(int& mx, int& my) const
111 {
112     window->getMouse(mx, my);
113     mx -= (width >> 1) + xOrigin;
114     my -= (viewHeight >> 1) + getYOffset();
115 }
116 
disableConfineToMotionbox()117 void            MainWindow::disableConfineToMotionbox()
118 {
119     window->disableConfineToMotionbox();
120 }
121 
confineToMotionbox(int x1,int y1,int x2,int y2)122 void            MainWindow::confineToMotionbox(int x1, int y1, int x2, int y2)
123 {
124     window->confineToMotionbox(x1, y1, x2, y2);
125 }
126 
grabMouse()127 void            MainWindow::grabMouse()
128 {
129     if (allowMouseGrab) window->grabMouse();
130 }
131 
ungrabMouse()132 void            MainWindow::ungrabMouse()
133 {
134     if (allowMouseGrab) window->ungrabMouse();
135 }
136 
enableGrabMouse(bool on)137 void            MainWindow::enableGrabMouse(bool on)
138 {
139     window->enableGrabMouse(on);
140     grabEnabled = on;
141 }
142 
isGrabEnabled(void) const143 bool            MainWindow::isGrabEnabled(void) const
144 {
145     return grabEnabled;
146 }
147 
getFullscreen() const148 bool            MainWindow::getFullscreen() const
149 {
150     return isFullscreen;
151 }
152 
setFullscreen()153 void            MainWindow::setFullscreen()
154 {
155     isFullscreen = false;
156     toggleFullscreen();
157 }
158 
toggleFullscreen()159 void            MainWindow::toggleFullscreen()
160 {
161     isFullscreen = !isFullscreen;
162     window->setFullscreen(isFullscreen);
163     window->create();
164     resize();
165 }
166 
setFullView(bool _isFullView)167 void            MainWindow::setFullView(bool _isFullView)
168 {
169     isFullView = _isFullView;
170 }
171 
setNoMouseGrab()172 void            MainWindow::setNoMouseGrab()
173 {
174     allowMouseGrab = false;
175 }
176 
setQuadrant(Quadrant _quadrant)177 void            MainWindow::setQuadrant(Quadrant _quadrant)
178 {
179     int inWidth = trueWidth;
180     if (inWidth < MinX) inWidth = MinX;
181     int inHeight = trueHeight;
182     if (inHeight < MinY) inHeight = MinY;
183 
184     quadrant = _quadrant;
185     switch (quadrant)
186     {
187     default:
188     case FullWindow:
189         width = inWidth;
190         height = inHeight;
191         if (isFullView)
192             viewHeight = height;
193         else
194             viewHeight = inHeight * (46 - RENDERER.getRadarSize()) / 60;
195         xOrigin = 0;
196         yOrigin = 0;
197         break;
198     case UpperLeft:
199         width = inWidth >> 1;
200         height = inHeight >> 1;
201         viewHeight = height;
202         xOrigin = 0;
203         yOrigin = (inHeight+1) >> 1;
204         break;
205     case UpperRight:
206         width = (inWidth+1) >> 1;
207         height = inHeight >> 1;
208         viewHeight = height;
209         xOrigin = inWidth >> 1;
210         yOrigin = (inHeight+1) >> 1;
211         break;
212     case LowerLeft:
213         width = inWidth >> 1;
214         height = (inHeight+1) >> 1;
215         viewHeight = height;
216         xOrigin = 0;
217         yOrigin = 0;
218         break;
219     case LowerRight:
220         width = (inWidth+1) >> 1;
221         height = (inHeight+1) >> 1;
222         viewHeight = height;
223         xOrigin = inWidth >> 1;
224         yOrigin = 0;
225         break;
226     case UpperHalf:
227         width = inWidth;
228         height = inHeight >> 1;
229         viewHeight = height;
230         xOrigin = 0;
231         yOrigin = (inHeight+1) >> 1;
232         break;
233     case LowerHalf:
234         width = inWidth;
235         height = inHeight >> 1;
236         viewHeight = height;
237         xOrigin = 0;
238         yOrigin = 0;
239         break;
240     case ZoomRegion:
241         width = inWidth;
242         height = inHeight;
243         viewHeight = height;
244         xOrigin = 0;
245         yOrigin = 0;
246         break;
247     }
248 
249     if (quadrant == ZoomRegion)
250     {
251         width = inWidth + 1;
252         height = inHeight + 1;
253     }
254 
255     glViewport(xOrigin, yOrigin, width, height);
256 }
257 
setProjectionHUD() const258 void MainWindow::setProjectionHUD() const
259 {
260     glLoadIdentity();
261     glOrtho(0.0, width, viewHeight - height, viewHeight, -1.0, 1.0);
262 }
263 
setProjectionPlay() const264 void MainWindow::setProjectionPlay() const
265 {
266     glLoadIdentity();
267     glOrtho(0.0, width, 0.0, height, -1.0, 1.0);
268 }
269 
setProjectionRadar(int x,int y,int w,int h,float radarRange,float maxHeight) const270 void MainWindow::setProjectionRadar(int x, int y, int w, int h, float radarRange, float maxHeight) const
271 {
272     const float w2 = w / 2.0f;
273     const float h2 = h / 2.0f;
274     const float xUnit = radarRange / w2;
275     const float yUnit = radarRange / h2;
276     const float xCenter = x + w2;
277     const float yCenter = y + h2;
278 
279 
280     glLoadIdentity();
281     glOrtho(-xCenter * xUnit, (width - xCenter) * xUnit,
282             -yCenter * yUnit, (height - yCenter) * yUnit,
283             -maxHeight, maxHeight);
284 }
285 
resize()286 void            MainWindow::resize()
287 {
288     window->getSize(trueWidth, trueHeight);
289     window->makeCurrent();
290     setQuadrant(quadrant);
291 }
292 
resizeCB(void * _self)293 void            MainWindow::resizeCB(void* _self)
294 {
295     MainWindow* self = (MainWindow*)_self;
296     self->resize();
297 }
298 
iconify()299 void            MainWindow::iconify()
300 {
301     window->iconify();
302 }
303 
304 
haveJoystick() const305 bool            MainWindow::haveJoystick() const
306 {
307     return joystick->joystick();
308 }
309 
getJoyPosition(int & mx,int & my) const310 void            MainWindow::getJoyPosition(int& mx, int& my) const
311 {
312     // joystick axes inversion values
313     // 0: no inversion
314     // 1: invert X
315     // 2: invert Y
316     // 3: invert both
317     joystick->getJoy(mx, my);
318     mx = ((width >> 1) * mx * (BZDB.evalInt("jsInvertAxes") % 2 == 1 ? -1 : 1)) / (900);
319     my = ((height >> 1) * my * (BZDB.evalInt("jsInvertAxes") > 1 ? -1 : 1)) / (900);
320 }
321 
getNumHats() const322 int         MainWindow::getNumHats() const
323 {
324     return joystick->getNumHats();
325 }
326 
getJoyHat(int hat,float & hatX,float & hatY) const327 void            MainWindow::getJoyHat(int hat, float &hatX, float &hatY) const
328 {
329     joystick->getJoyHat(hat, hatX, hatY);
330 }
331 
getJoyButtonSet() const332 unsigned long       MainWindow::getJoyButtonSet() const
333 {
334     return joystick->getJoyButtons();
335 }
336 
getJoyDevices(std::vector<std::string> & list) const337 void            MainWindow::getJoyDevices(std::vector<std::string>
338         &list) const
339 {
340     joystick->getJoyDevices(list);
341 }
342 
getJoyDeviceAxes(std::vector<std::string> & list) const343 void            MainWindow::getJoyDeviceAxes(std::vector<std::string>
344         &list) const
345 {
346     joystick->getJoyDeviceAxes(list);
347 }
348 
setJoyXAxis(const std::string & axis)349 void            MainWindow::setJoyXAxis(const std::string &axis)
350 {
351     joystick->setXAxis(axis);
352 }
353 
setJoyYAxis(const std::string & axis)354 void            MainWindow::setJoyYAxis(const std::string &axis)
355 {
356     joystick->setYAxis(axis);
357 }
358 
initJoystick(std::string & joystickName)359 void            MainWindow::initJoystick(std::string &joystickName)
360 {
361     joystick->initJoystick(joystickName.c_str());
362 }
363 
364 // Local Variables: ***
365 // mode: C++ ***
366 // tab-width: 4 ***
367 // c-basic-offset: 4 ***
368 // indent-tabs-mode: nil ***
369 // End: ***
370 // ex: shiftwidth=4 tabstop=4
371