-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathEventManager.cxx
More file actions
608 lines (571 loc) · 20.9 KB
/
EventManager.cxx
File metadata and controls
608 lines (571 loc) · 20.9 KB
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
#include "EventManager.hxx"
#include "basics/Camera.hxx"
#include "basics/isoMath.hxx"
#include "basics/PointFunctions.hxx"
#include "basics/mapEdit.hxx"
#include "basics/Settings.hxx"
#include "basics/GameStates.hxx"
#include "common/enums.hxx"
#include "map/MapLayers.hxx"
#include "Map.hxx"
#include "Sprite.hxx"
#include <MapFunctions.hxx>
#include "LOG.hxx"
#ifdef MICROPROFILE_ENABLED
#include "microprofile/microprofile.h"
#endif
void EventManager::unHighlightNodes()
{
if (!m_isPuttingTile)
{
for (auto node : m_nodesToPlace)
{
MapFunctions::instance().unHighlightNode(node);
}
m_nodesToPlace.clear();
}
for (auto node : m_nodesToHighlight)
{
MapFunctions::instance().unHighlightNode(node);
}
m_nodesToHighlight.clear();
}
void EventManager::pickTileUnderCursor(Point mouseIsoCoords)
{
Layer topMostActiveLayer;
std::vector<MapNodeData> mapNodeData;
const MapNode &node = MapFunctions::instance().getMapNode(mouseIsoCoords);
topMostActiveLayer = node.getTopMostActiveLayer();
// all layers are supported except terrain
if (topMostActiveLayer == Layer::TERRAIN || topMostActiveLayer == Layer::NONE)
return;
// update placement mode
switch (topMostActiveLayer)
{
case Layer::BUILDINGS:
GameStates::instance().placementMode = PlacementMode::SINGLE;
break;
case Layer::ROAD:
case Layer::POWERLINES:
case Layer::UNDERGROUND:
GameStates::instance().placementMode = PlacementMode::LINE;
break;
case Layer::GROUND_DECORATION:
case Layer::WATER:
case Layer::ZONE:
GameStates::instance().placementMode = PlacementMode::RECTANGLE;
break;
default:
break;
}
mapNodeData = node.getMapNodeData();
tileToPlace = mapNodeData[topMostActiveLayer].tileID;
highlightSelection = true;
}
void EventManager::checkEvents(SDL_Event &event)
{
#ifdef MICROPROFILE_ENABLED
MICROPROFILE_SCOPEI("EventManager", "checkEvents", MP_BEIGE);
#endif
// check for UI events first
SDL_Point mouseScreenCoords;
Point mouseIsoCoords{};
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
SignalMediator::instance().signalQuitGame.emit();
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
if (!tileToPlace.empty())
{
m_uiManager.closeOpenMenus();
tileToPlace.clear();
highlightSelection = false;
}
else
{
m_uiManager.toggleGroupVisibility("PauseMenu");
}
break;
case SDLK_0:
break;
case SDLK_LCTRL:
GameStates::instance().demolishMode = DemolishMode::GROUND_DECORATION;
GameStates::instance().rectangularRoads = true;
break;
case SDLK_LSHIFT:
GameStates::instance().demolishMode = DemolishMode::DE_ZONE;
if (GameStates::instance().placementMode == PlacementMode::LINE)
{
GameStates::instance().placementMode = PlacementMode::STRAIGHT_LINE;
}
break;
case SDLK_F11:
m_uiManager.toggleDebugMenu();
break;
case SDLK_1:
MapLayers::toggleLayer(Layer::TERRAIN);
break;
case SDLK_2:
MapLayers::toggleLayer(Layer::BUILDINGS);
break;
case SDLK_3:
MapLayers::toggleLayer(Layer::BLUEPRINT);
break;
case SDLK_4:
MapLayers::toggleLayer(Layer::UNDERGROUND);
break;
case SDLK_5:
MapLayers::toggleLayer(Layer::ZONE);
break;
case SDLK_6:
MapLayers::toggleLayer(Layer::GROUND_DECORATION);
break;
case SDLK_i:
m_tileInfoMode = !m_tileInfoMode;
break;
case SDLK_h:
// TODO: This is only temporary until the new UI is ready. Remove this afterwards
GameStates::instance().drawUI = !GameStates::instance().drawUI;
break;
case SDLK_f:
WindowManager::instance().toggleFullScreen();
break;
case SDLK_UP:
case SDLK_w:
if (MapFunctions::instance().getMap() &&
Camera::instance().cameraOffset().y > -2 * Settings::instance().screenHeight * Camera::instance().zoomLevel())
{
Camera::instance().moveCamera(0, Settings::instance().screenHeight / 16);
}
break;
case SDLK_LEFT:
case SDLK_a:
if (MapFunctions::instance().getMap() &&
Camera::instance().cameraOffset().x > -0.25 * Settings::instance().screenWidth * Camera::instance().zoomLevel())
{
Camera::instance().moveCamera(Settings::instance().screenWidth / 16, 0);
}
break;
case SDLK_DOWN:
case SDLK_s:
if (MapFunctions::instance().getMap() &&
Camera::instance().cameraOffset().y < 1.25 * Settings::instance().screenHeight * Camera::instance().zoomLevel())
{
Camera::instance().moveCamera(0, -Settings::instance().screenHeight / 16);
}
break;
case SDLK_RIGHT:
case SDLK_d:
if (MapFunctions::instance().getMap() &&
Camera::instance().cameraOffset().x < 5 * Settings::instance().screenWidth * Camera::instance().zoomLevel())
{
// check if map exists to see, if we're ingame already.
if (MapFunctions::instance().getMap())
{
Camera::instance().moveCamera(-Settings::instance().screenWidth / 16, 0);
}
}
break;
default:
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_LCTRL:
GameStates::instance().demolishMode = DemolishMode::DEFAULT;
GameStates::instance().rectangularRoads = false;
break;
case SDLK_LSHIFT:
GameStates::instance().demolishMode = DemolishMode::DEFAULT;
if (GameStates::instance().placementMode == PlacementMode::STRAIGHT_LINE)
{
GameStates::instance().placementMode = PlacementMode::LINE;
}
break;
default:
break;
}
case SDL_MULTIGESTURE:
if (event.mgesture.numFingers == 2)
{
m_panning = true;
// check if we're pinching
if (event.mgesture.dDist != 0)
{
// store pinchCenterCoords so they stay the same for all zoom levels
if (m_pinchCenterCoords.x == 0 && m_pinchCenterCoords.y == 0)
{
m_pinchCenterCoords =
convertScreenToIsoCoordinates({static_cast<int>(event.mgesture.x * Settings::instance().screenWidth),
static_cast<int>(event.mgesture.y * Settings::instance().screenHeight)});
}
Camera::instance().setPinchDistance(event.mgesture.dDist * 15.0F, m_pinchCenterCoords.x, m_pinchCenterCoords.y);
m_skipLeftClick = true;
break;
}
if (m_panning)
{
Camera::instance().moveCamera(static_cast<int>(Settings::instance().screenWidth * event.tfinger.dx),
static_cast<int>(Settings::instance().screenHeight * event.tfinger.dy));
m_skipLeftClick = true;
break;
}
m_skipLeftClick = true;
}
break;
case SDL_MOUSEMOTION:
m_placementAllowed = false;
m_cancelTileSelection = false;
// check for UI events first
for (const auto &it : m_uiManager.getAllUiElements())
{
// if element isn't visible then don't event check it
if (it->isVisible())
{
// spawn tooltip timer, if we're over an UI Element
if (it->isMouseOver(event.button.x, event.button.y) && !it->getUiElementData().tooltipText.empty())
{
m_uiManager.startTooltip(event, it->getUiElementData().tooltipText);
}
// if the mouse cursor left an element, we're not hovering any more and we need to reset the pointer to null
if (m_lastHoveredElement && !m_lastHoveredElement->isMouseOverHoverableArea(event.button.x, event.button.y))
{
// we're not hovering, so stop the tooltips
m_uiManager.stopTooltip();
// tell the previously hovered element we left it before resetting it
m_lastHoveredElement->onMouseLeave(event);
m_lastHoveredElement = nullptr;
break;
}
// If we're over a UI element that has no click functionality, abort the event loop, so no clicks go through the UiElement.
//Note: This is handled here because UIGroups have no dimensions, but are UiElements
if (it->isMouseOverHoverableArea(event.button.x, event.button.y))
{
it->onMouseMove(event);
// if the element we're hovering over is not the same as the stored "lastHoveredElement", update it
if (it.get() != m_lastHoveredElement)
{
if (m_lastHoveredElement)
{
m_lastHoveredElement->onMouseLeave(event);
}
it->onMouseEnter(event);
m_lastHoveredElement = it.get();
}
break;
}
// definitely figure out a better way to do this, this was done for the Slider
if (it->isMouseOver(event.button.x, event.button.y))
{
it->onMouseMove(event);
}
}
}
// Game Event Handling
if (MapFunctions::instance().getMap())
{
// clear highlighting
unHighlightNodes();
// if we're panning, move the camera and break
if (m_panning)
{
if ((event.motion.xrel == 0) && (event.motion.yrel == 0))
{
return;
}
Camera::instance().moveCamera(event.motion.xrel, event.motion.yrel);
}
// check if we should highlight tiles and if we're in placement mode
if (highlightSelection)
{
mouseScreenCoords = {event.button.x, event.button.y};
mouseIsoCoords = convertScreenToIsoCoordinates(mouseScreenCoords);
// canceling transparent buildings
for (const auto &it : m_transparentBuildings)
{
if (it != Point::INVALID())
{
(MapFunctions::instance().getMapNode(it)).setNodeTransparency(0, Layer::BUILDINGS);
}
}
m_transparentBuildings.clear();
// if there's no tileToPlace use the current mouse coordinates
if (tileToPlace.empty())
{
m_nodesToHighlight.push_back(mouseIsoCoords);
}
else
{
m_nodesToHighlight = TileManager::instance().getTargetCoordsOfTileID(mouseIsoCoords, tileToPlace);
// get all node coordinates the tile we'll place occupies
if (m_nodesToHighlight.empty() && mouseIsoCoords.isWithinMapBoundaries())
{
m_nodesToHighlight.push_back(mouseIsoCoords);
}
}
// if mouse is held down, we need to check for plamentmodes LINE and RECTANGLE
if (SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(SDL_BUTTON_LEFT))
{
switch (GameStates::instance().placementMode)
{
case PlacementMode::SINGLE:
m_nodesToPlace.push_back(mouseIsoCoords);
break;
case PlacementMode::LINE:
m_nodesToPlace = PointFunctions::getLine(m_clickDownCoords, mouseIsoCoords);
m_nodesToHighlight = m_nodesToPlace;
break;
case PlacementMode::STRAIGHT_LINE:
m_nodesToPlace = PointFunctions::getStraightLine(m_clickDownCoords, mouseIsoCoords);
m_nodesToHighlight = m_nodesToPlace;
break;
case PlacementMode::RECTANGLE:
m_nodesToPlace = PointFunctions::getArea(m_clickDownCoords, mouseIsoCoords);
m_nodesToHighlight = m_nodesToPlace;
break;
}
}
// if we haven't any nodes to place yet, use the mouse coordinates
if (m_nodesToPlace.empty())
{
m_nodesToPlace.push_back(mouseIsoCoords);
}
m_placementAllowed = false;
std::vector<Point> nodesToAdd;
TileData *tileToPlaceData = TileManager::instance().getTileData(tileToPlace);
// if we touch a bigger than 1x1 tile also add all nodes of the building to highlight.
for (const auto &coords : m_nodesToHighlight)
{
// If we place a ground decoration tile, we must add all tiles of bigger than 1x1 buildings from the Layer BUILDINGS
Layer layer;
if (demolishMode || (tileToPlaceData && tileToPlaceData->tileType == +TileType::GROUNDDECORATION))
{
layer = Layer::BUILDINGS;
}
else
{
layer = TileManager::instance().getTileLayer(tileToPlace);
}
Point currentOriginPoint = MapFunctions::instance().getNodeOrigCornerPoint(coords, layer);
std::string currentTileID = MapFunctions::instance().getTileID(currentOriginPoint, layer);
for (auto &foundNode : TileManager::instance().getTargetCoordsOfTileID(currentOriginPoint, currentTileID))
{
// only add the node if it's unique
if (std::find(m_nodesToHighlight.begin(), m_nodesToHighlight.end(), foundNode) == m_nodesToHighlight.end())
{
nodesToAdd.push_back(foundNode);
}
}
}
// add the nodes we've found
m_nodesToHighlight.insert(m_nodesToHighlight.end(), nodesToAdd.begin(), nodesToAdd.end());
// for ground decoration, place all ground decoration files beneath the building
if (tileToPlaceData && tileToPlaceData->tileType == +TileType::GROUNDDECORATION)
{
m_nodesToPlace = m_nodesToHighlight;
}
m_placementAllowed = MapFunctions::instance().isPlacementOnAreaAllowed(m_nodesToHighlight, tileToPlace);
// Finally highlight all the tiles we've found
// Set highlighted tiles that can be placed and can't be placed different color
for (const auto &highlitNode : m_nodesToHighlight)
{
if (!MapFunctions::instance().isPlacementOnNodeAllowed(highlitNode, tileToPlace) || demolishMode)
{
// mark red
MapFunctions::instance().highlightNode(highlitNode, SpriteHighlightColor::RED);
}
else
{
// place allowed tile, mark gray
MapFunctions::instance().highlightNode(highlitNode, SpriteHighlightColor::GRAY);
}
const Point &buildingCoordinates =
MapFunctions::instance().findNodeInMap(convertIsoToScreenCoordinates(highlitNode), Layer::BUILDINGS);
auto transparentBuildingIt =
std::find(m_transparentBuildings.begin(), m_transparentBuildings.end(), buildingCoordinates);
if ((transparentBuildingIt == m_transparentBuildings.end()) && (buildingCoordinates != Point::INVALID()))
{
const TileData *tileData = MapFunctions::instance().getMapNode(buildingCoordinates).getTileData(Layer::BUILDINGS);
if (tileData && tileData->category != "Flora")
{
MapFunctions::instance().getMapNode(buildingCoordinates).setNodeTransparency(0.6f, Layer::BUILDINGS);
m_transparentBuildings.push_back(buildingCoordinates);
}
}
}
}
}
break;
case SDL_MOUSEBUTTONDOWN:
m_placementAllowed = false;
m_skipLeftClick = false;
// check for UI events first
for (const auto &it : m_uiManager.getAllUiElementsForEventHandling())
{
// only check visible elements
if (it->isVisible() && it->onMouseButtonDown(event))
{
break;
}
}
if (event.button.button == SDL_BUTTON_RIGHT)
{
m_panning = true;
m_cancelTileSelection = true;
}
else if (event.button.button == SDL_BUTTON_LEFT)
{
// game event handling
mouseScreenCoords = {event.button.x, event.button.y};
mouseIsoCoords = convertScreenToIsoCoordinates(mouseScreenCoords);
const std::vector targetObjectNodes = TileManager::instance().getTargetCoordsOfTileID(mouseIsoCoords, tileToPlace);
//check if the coords for the click and for the occpuied tiles of the tileID we want to place are within map boundaries
bool canPlaceTileID = false;
if (mouseIsoCoords.isWithinMapBoundaries())
{
canPlaceTileID = true;
for (auto coordinate : targetObjectNodes)
{
if (!coordinate.isWithinMapBoundaries())
{
canPlaceTileID = false;
break;
}
}
}
if (canPlaceTileID)
{
m_clickDownCoords = mouseIsoCoords;
m_placementAllowed = true;
// Nodes to place are collected during the mouse move.
// In case of multiple left clicks without moving the mouse, node to place will be the node of the mouse click.
if (m_nodesToPlace.empty())
{
m_nodesToPlace.push_back(mouseIsoCoords);
}
m_isPuttingTile = true;
}
}
break;
case SDL_MOUSEBUTTONUP:
{
if (m_cancelTileSelection)
{
m_uiManager.closeOpenMenus();
tileToPlace.clear();
highlightSelection = false;
}
if (m_panning)
{
m_panning = false;
}
// reset pinchCenterCoords when fingers are released
m_pinchCenterCoords = {0, 0, 0, 0};
// check for UI events first
for (const auto &it : m_uiManager.getAllUiElementsForEventHandling())
{
// only check visible elements
if (it->isVisible() && event.button.button == SDL_BUTTON_LEFT)
{
// first, check if the element is a group and send the event
if (it->onMouseButtonUp(event))
{
m_skipLeftClick = true;
break;
}
// If we're over a UI element that has no click functionality, abort the event loop, so no clicks go through the UiElement.
//Note: This is handled here because UIGroups have no dimensions, but are UiElements
if (it->isMouseOver(event.button.x, event.button.y))
{
m_skipLeftClick = true;
}
}
}
// If we're over a ui element, don't handle game events
if (m_skipLeftClick)
{
m_isPuttingTile = false;
break;
}
// game event handling
mouseScreenCoords = {event.button.x, event.button.y};
mouseIsoCoords = convertScreenToIsoCoordinates(mouseScreenCoords);
// gather all nodes the objects that'll be placed is going to occupy.
std::vector targetObjectNodes = TileManager::instance().getTargetCoordsOfTileID(mouseIsoCoords, tileToPlace);
if (event.button.button == SDL_BUTTON_LEFT)
{
if (m_tileInfoMode)
{
MapFunctions::instance().getNodeInformation({mouseIsoCoords.x, mouseIsoCoords.y, 0, 0});
}
else if (terrainEditMode == TerrainEdit::RAISE)
{
MapFunctions::instance().changeHeight(mouseIsoCoords, true);
}
else if (terrainEditMode == TerrainEdit::LOWER)
{
MapFunctions::instance().changeHeight(mouseIsoCoords, false);
}
else if (demolishMode)
{
MapFunctions::instance().demolishNode(m_nodesToHighlight, true);
}
// select the tile our cursor is over
else if (!demolishMode && tileToPlace.empty())
{
pickTileUnderCursor(mouseIsoCoords);
// pick the tile in mousemove, player will find mousedown
break;
}
else if (!tileToPlace.empty() && m_placementAllowed)
{
if (!MapFunctions::instance().setTileID(tileToPlace, m_nodesToPlace))
{
// If can't put picked tile here,
// pick tile under cursor as the new picked tile
// Thus the picker would always work without
// having to right click or enter Esc(abort tile placing) first
pickTileUnderCursor(mouseIsoCoords);
}
}
}
// when we're done, reset highlighting
m_isPuttingTile = false;
unHighlightNodes();
if (highlightSelection)
{
m_nodesToHighlight.push_back(mouseIsoCoords);
if (!tileToPlace.empty() && !MapFunctions::instance().setTileID(tileToPlace, mouseIsoCoords))
{
MapFunctions::instance().highlightNode(mouseIsoCoords, SpriteHighlightColor::RED);
}
else
{
MapFunctions::instance().highlightNode(mouseIsoCoords, SpriteHighlightColor::GRAY);
}
}
break;
}
case SDL_MOUSEWHEEL:
if (event.wheel.y > 0)
{
Camera::instance().increaseZoomLevel();
}
else if (event.wheel.y < 0)
{
Camera::instance().decreaseZoomLevel();
}
break;
default:
break;
}
}
}