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
|
#include "editor.hpp"
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Vector2.hpp>
#include <optional>
#include <string>
#include "imgui-SFML.h"
#include "imgui.h"
#include "nfd.h"
#include "nfd.hpp"
#include "tileset.hpp"
namespace silly::editor {
void Editor::update(const sf::RenderWindow &window) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) ||
sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) {
sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
for (int x = 0; x < this->floor.get_width(); x++) {
for (int y = 0; y < this->floor.get_height(); y++) {
int rx = x * 16, ry = y * 16;
if ((rx < mousePosition.x && mousePosition.x < rx + 16) &&
(ry < mousePosition.y && mousePosition.y < ry + 16)) {
sf::Vector2i pos(x, y);
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
this->floor.place_tile(pos);
} else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) {
this->floor.remove_tile(pos);
}
}
}
}
}
}
void Editor::render(const sf::RenderWindow &window) {
sf::Vector2u windowSize = window.getSize();
int width = 400;
int height = windowSize.y;
int x = windowSize.x - width;
int y = 0;
ImGui::SetNextWindowPos(ImVec2(x, y));
ImGui::SetNextWindowSize(ImVec2(width, height));
ImGui::Begin("Editor", NULL,
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoTitleBar);
// --- "CREATING A NEW TILE" WINDOW ---
if (ImGui::Button("Add tile") && !this->newTileState.has_value()) {
this->newTileState =
std::make_optional((NewTileState){"", {}, TILE_FLOOR});
}
if (this->newTileState.has_value()) {
NewTileState &state = this->newTileState.value();
ImGui::SetNextWindowPos(
ImVec2(windowSize.x / 2.0f - 200, windowSize.y / 2.0f - 250));
ImGui::SetNextWindowSize(ImVec2(400, 500));
ImGui::Begin("Creating a new tile", NULL,
ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize);
// -- Tile type --
ImGui::Text("Tile type");
if (ImGui::RadioButton("Floor", state.type == TILE_FLOOR)) {
state.type = TILE_FLOOR;
}
ImGui::SameLine();
if (ImGui::RadioButton("Wall", state.type == TILE_WALL)) {
state.type = TILE_WALL;
}
// -- Texture loading --
ImGui::Text("2D");
if (!state.path.empty()) {
ImGui::Image(state.texture);
ImGui::SameLine();
}
if (ImGui::Button("Load texture file")) {
NFD::UniquePath outPath;
nfdfilteritem_t filterItem[1] = {{"Images", "png"}};
nfdresult_t result = NFD::OpenDialog(outPath, filterItem, 1);
if (result == NFD_OKAY) {
state.path = outPath.get();
if (!state.texture.loadFromFile(state.path)) {
state.path = "";
}
}
}
// -- Tile preview --
if (!state.path.empty()) {
for (int y = 0; y <= 5; y++) {
for (int x = 0; x <= 10; x++) {
ImGui::Image(state.texture);
if (x < 10) {
ImGui::SameLine(0, 0);
}
}
}
}
// -- Tile creation --
if (ImGui::Button("Create a new tile")) {
this->tileset.add_tile(state.path, state.type);
ImGui::SetWindowCollapsed(true);
}
if (ImGui::IsWindowCollapsed()) {
ImGui::SetWindowCollapsed(false);
this->newTileState = std::nullopt;
}
ImGui::End();
}
ImGui::End();
}
}
|