forked from OpenXcom/OpenXcom
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Camera.cpp
634 lines (581 loc) · 16 KB
/
Camera.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
* Copyright 2010-2016 OpenXcom Developers.
*
* This file is part of OpenXcom.
*
* OpenXcom 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 3 of the License, or
* (at your option) any later version.
*
* OpenXcom 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 OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Camera.h"
#include "Map.h"
#include "../Engine/Action.h"
#include "../Engine/Options.h"
#include "../Engine/Timer.h"
#include "../fmath.h"
namespace OpenXcom
{
/**
* Sets up a camera.
* @param spriteWidth Width of map sprite.
* @param spriteHeight Height of map sprite.
* @param mapsize_x Current map size in X axis.
* @param mapsize_y Current map size in Y axis.
* @param mapsize_z Current map size in Z axis.
* @param map Pointer to map surface.
* @param visibleMapHeight Current height the view is at.
*/
Camera::Camera(int spriteWidth, int spriteHeight, int mapsize_x, int mapsize_y, int mapsize_z, Map *map, int visibleMapHeight) : _scrollMouseTimer(0), _scrollKeyTimer(0), _spriteWidth(spriteWidth), _spriteHeight(spriteHeight), _mapsize_x(mapsize_x), _mapsize_y(mapsize_y), _mapsize_z(mapsize_z), _screenWidth(map->getWidth()), _screenHeight(map->getHeight()),
_mapOffset(-250,250,0), _scrollMouseX(0), _scrollMouseY(0), _scrollKeyX(0), _scrollKeyY(0), _scrollTrigger(false), _visibleMapHeight(visibleMapHeight), _showAllLayers(false), _map(map)
{
}
/**
* Deletes the Camera.
*/
Camera::~Camera()
{
}
/**
* Sets the camera's scrolling timer.
* @param mouse Pointer to mouse timer.
* @param key Pointer to key timer.
*/
void Camera::setScrollTimer(Timer *mouse, Timer *key)
{
_scrollMouseTimer = mouse;
_scrollKeyTimer = key;
}
/**
* Handles camera mouse shortcuts.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Camera::mousePress(Action *action, State *)
{
if (action->getDetails()->button.button == SDL_BUTTON_LEFT && Options::battleEdgeScroll == SCROLL_TRIGGER)
{
_scrollTrigger = true;
mouseOver(action, 0);
}
else if (Options::battleDragScrollButton != SDL_BUTTON_MIDDLE || (SDL_GetMouseState(0,0)&SDL_BUTTON(Options::battleDragScrollButton)) == 0)
{
if (action->getDetails()->button.button == SDL_BUTTON_WHEELUP)
{
up();
}
else if (action->getDetails()->button.button == SDL_BUTTON_WHEELDOWN)
{
down();
}
}
}
/**
* Handles camera mouse shortcuts.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Camera::mouseRelease(Action *action, State *)
{
if (action->getDetails()->button.button == SDL_BUTTON_LEFT && Options::battleEdgeScroll == SCROLL_TRIGGER)
{
_scrollMouseX = 0;
_scrollMouseY = 0;
_scrollMouseTimer->stop();
_scrollTrigger = false;
int posX = action->getXMouse();
int posY = action->getYMouse();
if ((posX < (SCROLL_BORDER * action->getXScale()) && posX > 0)
|| (posX > (_screenWidth - SCROLL_BORDER) * action->getXScale())
|| (posY < (SCROLL_BORDER * action->getYScale()) && posY > 0)
|| (posY > (_screenHeight - SCROLL_BORDER) * action->getYScale()))
// A cheap hack to avoid handling this event as a click
// on the map when the mouse is on the scroll-border
action->getDetails()->button.button = 0;
}
}
/**
* Handles mouse over events.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Camera::mouseOver(Action *action, State *)
{
if (_map->getCursorType() == CT_NONE)
{
return;
}
if (Options::battleEdgeScroll == SCROLL_AUTO || _scrollTrigger)
{
int posX = action->getXMouse();
int posY = action->getYMouse();
int scrollSpeed = Options::battleScrollSpeed;
//left scroll
if (posX < (SCROLL_BORDER * action->getXScale()) && posX >= 0)
{
_scrollMouseX = scrollSpeed;
// if close to top or bottom, also scroll diagonally
//downleft
if (posY < (SCROLL_DIAGONAL_EDGE * action->getYScale()) && posY >= 0)
{
_scrollMouseY = scrollSpeed/2;
}
//upleft
else if (posY > (_screenHeight - SCROLL_DIAGONAL_EDGE) * action->getYScale())
{
_scrollMouseY = -scrollSpeed/2;
}
else _scrollMouseY = 0;
}
//right scroll
else if (posX > (_screenWidth - SCROLL_BORDER) * action->getXScale())
{
_scrollMouseX = -scrollSpeed;
// if close to top or bottom, also scroll diagonally
//downright
if (posY <= (SCROLL_DIAGONAL_EDGE * action->getYScale()) && posY >= 0)
{
_scrollMouseY = scrollSpeed/2;
}
//upright
else if (posY > (_screenHeight - SCROLL_DIAGONAL_EDGE) * action->getYScale())
{
_scrollMouseY = -scrollSpeed/2;
}
else _scrollMouseY = 0;
}
else if (posX)
{
_scrollMouseX = 0;
}
//up
if (posY < (SCROLL_BORDER * action->getYScale()) && posY >= 0)
{
_scrollMouseY = scrollSpeed;
// if close to left or right edge, also scroll diagonally
//up left
if (posX < (SCROLL_DIAGONAL_EDGE * action->getXScale()) && posX >= 0)
{
_scrollMouseX = scrollSpeed;
_scrollMouseY /=2;
}
//up right
else if (posX > (_screenWidth - SCROLL_DIAGONAL_EDGE) * action->getXScale())
{
_scrollMouseX = -scrollSpeed;
_scrollMouseY /=2;
}
}
//down
else if (posY > (_screenHeight- SCROLL_BORDER) * action->getYScale())
{
_scrollMouseY = -scrollSpeed;
// if close to left or right edge, also scroll diagonally
//down left
if (posX < (SCROLL_DIAGONAL_EDGE * action->getXScale()) && posX >= 0)
{
_scrollMouseX = scrollSpeed;
_scrollMouseY /=2;
}
//down right
else if (posX > (_screenWidth - SCROLL_DIAGONAL_EDGE) * action->getXScale())
{
_scrollMouseX = -scrollSpeed;
_scrollMouseY /=2;
}
}
else if (posY && _scrollMouseX == 0)
{
_scrollMouseY = 0;
}
if ((_scrollMouseX || _scrollMouseY) && !_scrollMouseTimer->isRunning() && !_scrollKeyTimer->isRunning() && 0==(SDL_GetMouseState(0,0)&SDL_BUTTON(Options::battleDragScrollButton)))
{
_scrollMouseTimer->start();
}
else if ((!_scrollMouseX && !_scrollMouseY) && _scrollMouseTimer->isRunning())
{
_scrollMouseTimer->stop();
}
}
}
/**
* Handles camera keyboard shortcuts.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Camera::keyboardPress(Action *action, State *)
{
if (_map->getCursorType() == CT_NONE)
{
return;
}
int key = action->getDetails()->key.keysym.sym;
int scrollSpeed = Options::battleScrollSpeed;
if (key == Options::keyBattleLeft)
{
_scrollKeyX = scrollSpeed;
}
else if (key == Options::keyBattleRight)
{
_scrollKeyX = -scrollSpeed;
}
else if (key == Options::keyBattleUp)
{
_scrollKeyY = scrollSpeed;
}
else if (key == Options::keyBattleDown)
{
_scrollKeyY = -scrollSpeed;
}
if ((_scrollKeyX || _scrollKeyY) && !_scrollKeyTimer->isRunning() && !_scrollMouseTimer->isRunning() && 0==(SDL_GetMouseState(0,0)&SDL_BUTTON(Options::battleDragScrollButton)))
{
_scrollKeyTimer->start();
}
else if ((!_scrollKeyX && !_scrollKeyY) && _scrollKeyTimer->isRunning())
{
_scrollKeyTimer->stop();
}
}
/**
* Handles camera keyboard shortcuts.
* @param action Pointer to an action.
* @param state State that the action handlers belong to.
*/
void Camera::keyboardRelease(Action *action, State *)
{
if (_map->getCursorType() == CT_NONE)
{
return;
}
int key = action->getDetails()->key.keysym.sym;
if (key == Options::keyBattleLeft)
{
_scrollKeyX = 0;
}
else if (key == Options::keyBattleRight)
{
_scrollKeyX = 0;
}
else if (key == Options::keyBattleUp)
{
_scrollKeyY = 0;
}
else if (key == Options::keyBattleDown)
{
_scrollKeyY = 0;
}
if ((_scrollKeyX || _scrollKeyY) && !_scrollKeyTimer->isRunning() && !_scrollMouseTimer->isRunning() && 0==(SDL_GetMouseState(0,0)&SDL_BUTTON(Options::battleDragScrollButton)))
{
_scrollKeyTimer->start();
}
else if ((!_scrollKeyX && !_scrollKeyY) && _scrollKeyTimer->isRunning())
{
_scrollKeyTimer->stop();
}
}
/**
* Handles mouse-scrolling.
*/
void Camera::scrollMouse()
{
scrollXY(_scrollMouseX, _scrollMouseY, true);
}
/**
* Handles keyboard-scrolling.
*/
void Camera::scrollKey()
{
scrollXY(_scrollKeyX, _scrollKeyY, true);
}
/**
* Handles scrolling with given deviation.
* @param x X deviation.
* @param y Y deviation.
* @param redraw Redraw map or not.
*/
void Camera::scrollXY(int x, int y, bool redraw)
{
_mapOffset.x += x;
_mapOffset.y += y;
do
{
int xx = 0;
int yy = 0;
convertScreenToMap((_screenWidth / 2), (_visibleMapHeight / 2), &xx, &yy);
_center.x = xx;
_center.y = yy;
// Handling map bounds...
// Ok, this is a prototype, it should be optimized.
// Actually this should be calculated instead of slow-approximation.
if (_center.x < 0) { _mapOffset.x -= 1; _mapOffset.y -= 1; continue; }
if (_center.x > _mapsize_x -1) { _mapOffset.x += 1; _mapOffset.y += 1; continue; }
if (_center.y < 0) { _mapOffset.x += 1; _mapOffset.y -= 1; continue; }
if (_center.y > _mapsize_y -1) { _mapOffset.x -= 1; _mapOffset.y += 1; continue; }
break;
}
while (true);
_map->refreshSelectorPosition();
if (redraw) _map->invalidate();
}
/**
* Handles jumping with given deviation.
* @param x X deviation.
* @param y Y deviation.
*/
void Camera::jumpXY(int x, int y)
{
_mapOffset.x += x;
_mapOffset.y += y;
int xx = 0;
int yy = 0;
convertScreenToMap((_screenWidth / 2), (_visibleMapHeight / 2), &xx, &yy);
_center.x = xx;
_center.y = yy;
}
/**
* Goes one level up.
*/
void Camera::up()
{
if (_mapOffset.z < _mapsize_z - 1)
{
_mapOffset.z++;
_mapOffset.y += _spriteHeight * 3 / 5;
_map->draw();
}
}
/**
* Goes one level down.
*/
void Camera::down()
{
if (_mapOffset.z > 0)
{
_mapOffset.z--;
_mapOffset.y -= _spriteHeight * 3 / 5;
_map->draw();
}
}
/**
* Sets the view level.
* @param viewlevel New view level.
*/
void Camera::setViewLevel(int viewlevel)
{
_mapOffset.z = Clamp(viewlevel, 0, _mapsize_z - 1);
_map->draw();
}
/**
* Centers map on a certain position.
* @param mapPos Position to center on.
* @param redraw Redraw map or not.
*/
void Camera::centerOnPosition(Position mapPos, bool redraw)
{
Position screenPos;
_center = mapPos;
_center.x = Clamp<int>(_center.x, -1, _mapsize_x);
_center.y = Clamp<int>(_center.y, -1, _mapsize_y);
convertMapToScreen(_center, &screenPos);
_mapOffset.x = -(screenPos.x - (_screenWidth / 2));
_mapOffset.y = -(screenPos.y - (_visibleMapHeight / 2));
_mapOffset.z = _center.z;
if (redraw) _map->draw();
}
/**
* Gets map's center position.
* @return Map's center position.
*/
Position Camera::getCenterPosition()
{
_center.z = _mapOffset.z;
return _center;
}
/**
* Converts screen coordinates to map coordinates.
* @param screenX Screen x position.
* @param screenY Screen y position.
* @param mapX Map x position.
* @param mapY Map y position.
*/
void Camera::convertScreenToMap(int screenX, int screenY, int *mapX, int *mapY) const
{
// add half a tile height to the mouse position per layer we are above the floor
screenY += (-_spriteWidth/2) + (_mapOffset.z) * ((_spriteHeight + _spriteWidth / 4) / 2);
// calculate the actual x/y pixel position on a diamond shaped map
// taking the view offset into account
*mapY = - screenX + _mapOffset.x + 2 * screenY - 2 * _mapOffset.y;
*mapX = screenY - _mapOffset.y - *mapY / 4 - (_spriteWidth/4);
// to get the row & column itself, divide by the size of a tile
*mapX /= (_spriteWidth / 4);
*mapY /= _spriteWidth;
*mapX = Clamp(*mapX, -1, _mapsize_x);
*mapY = Clamp(*mapY, -1, _mapsize_y);
}
/**
* Converts map coordinates X,Y,Z to screen positions X, Y.
* @param mapPos X,Y,Z coordinates on the map.
* @param screenPos Screen position.
*/
void Camera::convertMapToScreen(Position mapPos, Position *screenPos) const
{
screenPos->z = 0; // not used
screenPos->x = mapPos.x * (_spriteWidth / 2) - mapPos.y * (_spriteWidth / 2);
screenPos->y = mapPos.x * (_spriteWidth / 4) + mapPos.y * (_spriteWidth / 4) - mapPos.z * ((_spriteHeight + _spriteWidth / 4) / 2);
}
/**
* Converts voxel coordinates X,Y,Z to screen positions X, Y.
* @param voxelPos X,Y,Z coordinates of the voxel.
* @param screenPos Screen position.
*/
void Camera::convertVoxelToScreen(Position voxelPos, Position *screenPos) const
{
Position mapPosition = voxelPos.toTile();
convertMapToScreen(mapPosition, screenPos);
double dx = voxelPos.x - (mapPosition.x * 16);
double dy = voxelPos.y - (mapPosition.y * 16);
double dz = voxelPos.z - (mapPosition.z * 24);
screenPos->x += (int)(dx - dy) + (_spriteWidth/2);
screenPos->y += (int)(((_spriteHeight / 2.0)) + (dx / 2.0) + (dy / 2.0) - dz);
screenPos->x += _mapOffset.x;
screenPos->y += _mapOffset.y;
}
/**
* Gets the displayed level.
* @return The displayed layer.
*/
int Camera::getViewLevel() const
{
return _mapOffset.z;
}
/**
* Gets the map size x.
* @return The map size x.
*/
int Camera::getMapSizeX() const
{
return _mapsize_x;
}
/**
* Gets the map size y.
* @return The map size y.
*/
int Camera::getMapSizeY() const
{
return _mapsize_y;
}
/**
* Gets the map offset.
* @return The map offset.
*/
Position Camera::getMapOffset() const
{
return _mapOffset;
}
/**
* Sets the map offset.
* @param pos The map offset.
*/
void Camera::setMapOffset(const Position& pos)
{
_mapOffset = pos;
}
/**
* Toggles showing all map layers.
* @return New layer setting.
*/
int Camera::toggleShowAllLayers()
{
_showAllLayers = !_showAllLayers;
return _showAllLayers?2:1;
}
/**
* Checks if the camera is showing all map layers.
* @return Current layer setting.
*/
bool Camera::getShowAllLayers() const
{
return _showAllLayers;
}
/**
* Checks if map coordinates X,Y,Z are on screen.
* @param mapPos Coordinates to check.
* @param unitWalking True to offset coordinates for a unit walking.
* @param unitSize size of unit (0 - single, 1 - 2x2, etc, used for walking only
* @param boundary True if it's for caching calculation
* @return True if the map coordinates are on screen.
*/
bool Camera::isOnScreen(Position mapPos, const bool unitWalking, const int unitSize, const bool boundary) const
{
Position screenPos;
convertMapToScreen(mapPos, &screenPos);
int posx = _spriteWidth/2, posy = _spriteHeight - _spriteWidth/4;
int sizex = _spriteWidth/2, sizey = _spriteHeight/2;
if (unitSize > 0)
{
posy -= _spriteWidth /4;
sizex = _spriteWidth*unitSize;
sizey = _spriteWidth*unitSize/2;
}
screenPos.x += _mapOffset.x + posx;
screenPos.y += _mapOffset.y + posy;
if (unitWalking)
{
/* pretty hardcoded hack to handle overlapping by icons
(they are always in the center at the bottom of the screen)
Free positioned icons would require more complex workaround.
__________
|________|
|| ||
|| ____ ||
||_|XX|_||
|________|
*/
if (boundary) //to make sprite updates even being slightly outside of screen
{
sizex += _spriteWidth;
sizey += _spriteWidth/2;
}
if ( screenPos.x < 0 - sizex
|| screenPos.x >= _screenWidth + sizex
|| screenPos.y < 0 - sizey
|| screenPos.y >= _screenHeight + sizey ) return false; //totally outside
int side = ( _screenWidth - _map->getIconWidth() ) / 2;
if ( (screenPos.y < (_screenHeight - _map->getIconHeight()) + sizey) ) return true; //above icons
if ( (side > 1) && ( (screenPos.x < side + sizex) || (screenPos.x >= (_screenWidth - side - sizex)) ) ) return true; //at sides (if there are any)
return false;
}
else
{
return screenPos.x >= 0
&& screenPos.x <= _screenWidth - 10
&& screenPos.y >= 0
&& screenPos.y <= _screenHeight - 10;
}
}
/**
* Resizes the viewable window of the camera.
*/
void Camera::resize()
{
_screenWidth = _map->getWidth();
_screenHeight = _map->getHeight();
_visibleMapHeight = _map->getHeight() - _map->getIconHeight();
}
void Camera::stopKeyScrolling()
{
_scrollKeyTimer->stop();
}
void Camera::stopMouseScrolling()
{
_scrollMouseTimer->stop();
}
}