-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionGrid.cpp
64 lines (55 loc) · 1.78 KB
/
SelectionGrid.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
#include "SelectionGrid.h"
SelectionGrid::SelectionGrid(
sf::Vector2i dimensions = sf::Vector2i(1,1),
sf::Vector2f cellDimensions = sf::Vector2f(5.0,5.0),
sf::Vector2f orig = sf::Vector2f(0,0),
bool doLoop = false):
gridSize(dimensions),
cellSize(cellDimensions),
origin(orig),
selectorPosition(0,0),
doesLoop(doLoop)
{}
sf::Vector2f SelectionGrid::getCellPosition(sf::Vector2i cell) {
//encapsulate x and y
if (cell.x < 0) cell.x = 0;
if (cell.y < 0) cell.y = 0;
if (cell.x >= gridSize.x) cell.x = gridSize.x - 1;
if (cell.y >= gridSize.y) cell.y = gridSize.y - 1;
return origin + sf::Vector2f(cell.x * cellSize.x, cell.y * cellSize.y);
}
sf::Vector2i SelectionGrid::getSelectorPosition() {
return selectorPosition;
}
void SelectionGrid::setOrigin(sf::Vector2f orig) {
origin = orig;
}
void SelectionGrid::moveSelector(Direction dir) {
sf::Vector2i oldPos = selectorPosition;
switch(dir) {
case UP:
selectorPosition.y += -1;
break;
case DOWN:
selectorPosition.y += 1;
break;
case LEFT:
selectorPosition.x += -1;
break;
case RIGHT:
selectorPosition.x += 1;
break;
}
if (selectorPosition.x < 0) selectorPosition.x = (doesLoop) ? gridSize.x-1 : oldPos.x;
if (selectorPosition.y < 0) selectorPosition.y = (doesLoop) ? gridSize.y-1 : oldPos.y;
if (selectorPosition.x >= gridSize.x) selectorPosition.x = (doesLoop) ? 0 : oldPos.x;
if (selectorPosition.y >= gridSize.y) selectorPosition.y = (doesLoop) ? 0 : oldPos.y;
}
void SelectionGrid::moveSelectorTo(sf::Vector2i newPos) {
//encapsulate x and y
if (newPos.x < 0) newPos.x = 0;
if (newPos.y < 0) newPos.y = 0;
if (newPos.x >= gridSize.x) newPos.x = gridSize.x - 1;
if (newPos.y >= gridSize.y) newPos.y = gridSize.y - 1;
selectorPosition = newPos;
}