-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultEditorMode.cpp
222 lines (193 loc) · 6.38 KB
/
DefaultEditorMode.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
#include "stdafx.h"
#include "DefaultEditorMode.h"
//private: Functions:
void DefaultEditorMode::initVariables()
{
this->textureRect = sf::IntRect
(
0,
0,
static_cast<int>(this->stateData->gridSize),
static_cast<int>(this->stateData->gridSize)
);
this->collision = false;
this->type = static_cast<int>(TileTypes::Default);
this->layer = 0;
this->tileAddLock = false;
}
void DefaultEditorMode::initGui()
{
//Cursor text
this->cursorText.setFont(*this->editorStateData->font);
this->cursorText.setFillColor(sf::Color::White);
this->cursorText.setCharacterSize(18);
this->cursorText.setPosition(this->editorStateData->mousePositionView->x + 100.f, this->editorStateData->mousePositionView->y - 50.f);
//Sidebar
this->sideBar.setSize(sf::Vector2f(this->stateData->gridSize, static_cast<float>(this->stateData->gfxSettings->resolution.height)));
this->sideBar.setFillColor(sf::Color(50, 50, 50, 100));
this->sideBar.setOutlineColor(sf::Color(200, 200, 200, 150));
this->sideBar.setOutlineThickness(-1.f);
//Tile selector
this->selectorRect.setSize(sf::Vector2f(this->stateData->gridSize, this->stateData->gridSize));
this->selectorRect.setFillColor(sf::Color(255, 255, 255, 150));
this->selectorRect.setOutlineThickness(1.f);
this->selectorRect.setOutlineColor(sf::Color::Green);
this->selectorRect.setTexture(this->tileMap->getTileSheet());
this->selectorRect.setTextureRect(this->textureRect);
//Texture selector
this->textureSelector = new gui::TextureSelector
(
10.f, 20.f, this->stateData->gridSize * 10.f, this->stateData->gridSize * 10.f,
this->stateData->gridSize,
this->tileMap->getTileSheet(),
*this->editorStateData->font, "TS"
);
//Buttons
}
//Constructors and Destructor:
DefaultEditorMode::DefaultEditorMode(StateData* state_data, TileMap* tile_map, EditorStateData* editor_state_data)
: EditorMode(state_data, tile_map, editor_state_data)
{
this->initVariables();
this->initGui();
}
DefaultEditorMode::~DefaultEditorMode()
{
//Deleting texture selector
delete this->textureSelector;
}
//Functions:
void DefaultEditorMode::updateInput(const float& dt)
{
//Adding a tile to the tilemap
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) && this->getKeyTime())
{
if (!this->sideBar.getGlobalBounds().contains(sf::Vector2f(*this->editorStateData->mousePositionWindow)))
{
if (!this->textureSelector->isActive())
{
if (this->tileAddLock)
{
if (this->tileMap->tileEmpty(this->editorStateData->mousePositionGrid->x, this->editorStateData->mousePositionGrid->y, this->tileMap->getLayer()))
{
this->tileMap->addTile(this->editorStateData->mousePositionGrid->x, this->editorStateData->mousePositionGrid->y, this->tileMap->getLayer(), this->textureRect, this->collision, this->type);
}
}
else
{
this->tileMap->addTile(this->editorStateData->mousePositionGrid->x, this->editorStateData->mousePositionGrid->y, this->tileMap->getLayer(), this->textureRect, this->collision, this->type);
}
}
else
{
this->textureRect = this->textureSelector->getTextureRect();
}
}
}//Removing a tile from the tilemap
else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right) && this->getKeyTime())
{
if (!this->sideBar.getGlobalBounds().contains(sf::Vector2f(*this->editorStateData->mousePositionWindow)))
{
if (!this->textureSelector->isActive())
{
this->tileMap->removeTile(this->editorStateData->mousePositionGrid->x, this->editorStateData->mousePositionGrid->y, this->tileMap->getLayer());
}
else
{
}
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(this->editorStateData->keybinds->at("COLLISION_TOGGLE"))) && this->getKeyTime())
{
if (this->collision)
{
this->collision = false;
}
else
{
this->collision = true;
}
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(this->editorStateData->keybinds->at("TYPE_INCREASE"))) && this->getKeyTime())
{
if (this->type < static_cast<int>(TileTypes::Doodad))
{
++this->type;
}
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(this->editorStateData->keybinds->at("TYPE_DECREASE"))) && this->getKeyTime())
{
if (this->type > static_cast<int>(TileTypes::Default))
{
--this->type;
}
}
//Setting lock ON/OFF
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key(this->editorStateData->keybinds->at("TILE_LOCK_TOGGLE"))) && this->getKeyTime())
{
if (this->tileAddLock)
{
this->tileAddLock = false;
}
else
{
this->tileAddLock = true;
}
}
}
void DefaultEditorMode::updateGui(const float& dt)
{
//Texture selector
this->textureSelector->update(*this->editorStateData->mousePositionWindow, dt);
//Tile selector
if (!this->textureSelector->isActive())
{
this->selectorRect.setTextureRect(this->textureRect);
this->selectorRect.setPosition
(
this->editorStateData->mousePositionGrid->x * this->stateData->gridSize,
this->editorStateData->mousePositionGrid->y * this->stateData->gridSize
);
}
//Cursor text
this->cursorText.setPosition
(
this->editorStateData->mousePositionView->x + 100.f,
this->editorStateData->mousePositionView->y - 100.f
);
std::stringstream ss;
ss << this->editorStateData->mousePositionView->x << " " << this->editorStateData->mousePositionView->y
<< std::endl << this->editorStateData->mousePositionGrid->x << " " << this->editorStateData->mousePositionGrid->y << " " << this->tileMap->getLayer()
<< std::endl << this->textureRect.left << " " << this->textureRect.top
<< std::endl << "Collision: " << this->collision
<< std::endl << "Type: " << this->type
<< std::endl << "Tiles: " << this->tileMap->getLayerSize(this->editorStateData->mousePositionGrid->x, this->editorStateData->mousePositionGrid->y, this->layer)
<< std::endl << "Tile lock: " << this->tileAddLock;
this->cursorText.setString(ss.str());
}
void DefaultEditorMode::update(const float& dt)
{
this->updateInput(dt);
this->updateGui(dt);
}
void DefaultEditorMode::renderGui(sf::RenderTarget& target)
{
//Tile selector
if (!this->textureSelector->isActive())
{
target.setView(*this->editorStateData->view);
target.draw(this->selectorRect);
}
//Texture selector
target.setView(this->stateData->window->getDefaultView());
this->textureSelector->render(target);
//Sidebar
target.draw(this->sideBar);
//Cursor text
target.setView(*this->editorStateData->view);
target.draw(this->cursorText);
}
void DefaultEditorMode::render(sf::RenderTarget& target)
{
this->renderGui(target);
}