-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_editor_mode.inl
166 lines (143 loc) · 5.49 KB
/
default_editor_mode.inl
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
#include "DefaultEditorMode.h"
#include "stdafx.h"
void DefaultEditorMode::InitVariables() {
textureRect = sf::IntRect(0, 0, static_cast<int>(stateData->gridSize),
static_cast<int>(stateData->gridSize));
collision = false;
type = TileTypes::kDefault;
layer = 0;
tileAddLock = false;
}
void DefaultEditorMode::initGui() {
// Text
cursorText.setFont(*editorStateData->font);
cursorText.setFillColor(sf::Color::White);
cursorText.setCharacterSize(12);
cursorText.setPosition(editorStateData->mousePosView->x,
editorStateData->mousePosView->y);
// General GUI
sidebar.setSize(sf::Vector2f(
80.f, static_cast<float>(stateData->gfxSettings->resolution.height)));
sidebar.setFillColor(sf::Color(50, 50, 50, 100));
sidebar.setOutlineColor(sf::Color(200, 200, 200, 150));
sidebar.setOutlineThickness(1.f);
selectorRect.setSize(sf::Vector2f(stateData->gridSize, stateData->gridSize));
selectorRect.setFillColor(sf::Color(255, 255, 255, 150));
selectorRect.setOutlineThickness(1.f);
selectorRect.setOutlineColor(sf::Color::Green);
selectorRect.setTexture(tileMap->getTileSheet());
selectorRect.setTextureRect(textureRect);
textureSelector = new gui::TextureSelector(
20.f, 20.f, 1000.f, 500.f, stateData->gridSize, tileMap->getTileSheet(),
*editorStateData->font, "TS");
// Buttons
}
DefaultEditorMode::DefaultEditorMode(StateData* state_data, TileMap* tile_map,
EditorStateData* editor_state_data)
: EditorMode(state_data, tile_map, editor_state_data) {
InitVariables();
initGui();
}
DefaultEditorMode::~DefaultEditorMode() { delete textureSelector; }
// Functions
void DefaultEditorMode::updateInput(const float& dt) {
// Add a tile to the tilemap
if (sf::Mouse::isButtonPressed(sf::Mouse::Left) && getKeytime()) {
if (!sidebar.getGlobalBounds().contains(
sf::Vector2f(*editorStateData->mousePosWindow))) {
if (!textureSelector->getActive()) {
if (tileAddLock) {
if (tileMap->tileEmpty(editorStateData->mousePosGrid->x,
editorStateData->mousePosGrid->y, 0)) {
tileMap->addTile(editorStateData->mousePosGrid->x,
editorStateData->mousePosGrid->y, 0, textureRect,
collision, type);
}
} else {
tileMap->addTile(editorStateData->mousePosGrid->x,
editorStateData->mousePosGrid->y, 0, textureRect,
collision, type);
}
} else {
textureRect = textureSelector->getTextureRect();
}
}
}
// Remove a tile from the tilemap
else if (sf::Mouse::isButtonPressed(sf::Mouse::Right) && getKeytime()) {
if (!sidebar.getGlobalBounds().contains(
sf::Vector2f(*editorStateData->mousePosWindow))) {
if (!textureSelector->getActive())
tileMap->removeTile(editorStateData->mousePosGrid->x,
editorStateData->mousePosGrid->y, 0);
}
}
// Toggle collision
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(
editorStateData->keybinds->at("TOGGLE_COLLISION"))) &&
getKeytime()) {
if (collision)
collision = false;
else
collision = true;
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(
editorStateData->keybinds->at("INCREASE_TYPE"))) &&
getKeytime()) {
// CHANGE TO LIMIT TO MAX TYPE!!!!
++type;
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(
editorStateData->keybinds->at("DECREASE_TYPE"))) &&
getKeytime()) {
if (type > 0) --type;
}
// Set lock on / off
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(
editorStateData->keybinds->at("TOGGLE_TILE_LOCK"))) &&
getKeytime()) {
if (tileAddLock)
tileAddLock = false;
else
tileAddLock = true;
}
}
void DefaultEditorMode::updateGui(const float& dt) {
textureSelector->Update(*editorStateData->mousePosWindow, dt);
if (!textureSelector->getActive()) {
selectorRect.setTextureRect(textureRect);
selectorRect.setPosition(
editorStateData->mousePosGrid->x * stateData->gridSize,
editorStateData->mousePosGrid->y * stateData->gridSize);
}
cursorText.setPosition(editorStateData->mousePosView->x + 100.f,
editorStateData->mousePosView->y - 50.f);
std::stringstream ss;
ss << editorStateData->mousePosView->x << " "
<< editorStateData->mousePosView->y << "\n"
<< editorStateData->mousePosGrid->x << " "
<< editorStateData->mousePosGrid->y << "\n"
<< textureRect.left << " " << textureRect.top << "\n"
<< "Collision: " << collision << "\n"
<< "Type: " << type << "\n"
<< "Tiles: "
<< tileMap->getLayerSize(editorStateData->mousePosGrid->x,
editorStateData->mousePosGrid->y, layer)
<< "\n"
<< "Tile lock: " << tileAddLock;
cursorText.setString(ss.str());
}
void DefaultEditorMode::Update(const float& dt) {
updateInput(dt);
updateGui(dt);
}
void DefaultEditorMode::renderGui(sf::RenderTarget& target) {
if (!textureSelector->getActive()) {
target.setView(*editorStateData->view);
target.draw(selectorRect);
}
target.setView(stateData->window->getDefaultView());
textureSelector->Render(target);
target.draw(sidebar);
target.setView(*editorStateData->view);
target.draw(cursorText);
}
void DefaultEditorMode::Render(sf::RenderTarget& target) { renderGui(target); }