summaryrefslogtreecommitdiff
path: root/src/editor.cpp
blob: f19cf65cb9deb9336af0d08e13e978307d599b55 (plain)
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
#include "editor.hpp"

#include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/View.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
#include <memory>
#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::Event &event, sf::RenderWindow &window) {
    // tile rotation
    if (const auto *e = event.getIf<sf::Event::KeyPressed>()) {
      if (e->code == sf::Keyboard::Key::R) {
        this->rotation += 90.0f;
        if (this->rotation >= 360.0f) {
          this->rotation = 0.0f;
        }
      }
    }

    // world movement
    if (const auto *e = event.getIf<sf::Event::MouseButtonPressed>()) {
      if (e->button == sf::Mouse::Button::Middle) {
        isDragging = true;
        lastMousePosition =
            window.mapPixelToCoords(sf::Mouse::getPosition(window));
      }
    }

    if (const auto *e = event.getIf<sf::Event::MouseButtonReleased>()) {
      if (e->button == sf::Mouse::Button::Middle) {
        isDragging = false;
        lastMousePosition = {};
      }
    }

    // world zoom
    if (const auto *e = event.getIf<sf::Event::MouseWheelScrolled>()) {
      sf::FloatRect visibleArea({0.f, 0.f}, sf::Vector2f(window.getSize()));
      sf::View view(visibleArea);

      this->zoom -= e->delta;

      this->zoom = (this->zoom * 10.0f) / 10.0f;

      if (this->zoom >= 1.0f) {
        this->zoom = 1.0f;
      } else if (this->zoom <= 0.3f) {
        this->zoom = 0.3f;
      }

      view.zoom(this->zoom);
      view.setCenter(window.getView().getCenter());
      window.setView(view);
    }
  }

  void Editor::update(sf::RenderWindow &window) {
    // tile placement
    if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) ||
        sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) {
      sf::Vector2f mousePosition =
          window.mapPixelToCoords(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 * TILE_WIDTH, ry = y * TILE_HEIGHT;

          if ((rx < mousePosition.x && mousePosition.x < rx + TILE_WIDTH) &&
              (ry < mousePosition.y && mousePosition.y < ry + TILE_HEIGHT) &&
              // editor related
              mousePosition.x < window.getSize().x - 400.0f &&
              !this->newTileState.has_value()) {
            sf::Vector2i pos(x, y);

            if (this->selectedTile.has_value()) {
              auto tile = this->selectedTile.value();
              if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
                this->floor.place_tile(tile, pos, this->rotation);
              } else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) {
                this->floor.remove_tile(tile->type, pos);
              }
            }
          }
        }
      }
    }

    // world movement
    if (isDragging) {
      sf::Vector2f mousePosition =
          window.mapPixelToCoords(sf::Mouse::getPosition(window));

      sf::Vector2f deltaPosition = lastMousePosition - mousePosition;

      sf::View view = window.getView();
      view.move(deltaPosition);
      window.setView(view);
    }
  }

  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);

    // --- SELECTED TILE ---
    ImGui::BeginChild("SelectedTileRegion", ImVec2(200, 100));
    if (this->selectedTile.has_value()) {
      TilesetTile *t = this->selectedTile->get();

      if (t->type == TILE_FLOOR) {
        ImGui::Text("FLOOR");
      } else if (t->type == TILE_WALL) {
        ImGui::Text("WALL");
      } else {
        ImGui::Text("UNKNOWN");
      }

      ImGui::Image(t->texture, sf::Vector2f(64, 64));
    } else {
      ImGui::Text("NO TILE SELECTED!");
      ImGui::Dummy(ImVec2(64, 64));
    }
    ImGui::EndChild();

    ImGui::SameLine();

    // --- EDITOR SETTINGS ---
    ImGui::BeginChild("EditorSettingsRegion", ImVec2(170, 100));
    ImGui::Text("Rotation is %.0f degrees", this->rotation);
    ImGui::EndChild();

    // --- TILE SELECTION ---
    ImGui::BeginChild("TileSelectionRegion", ImVec2(0, 400),
                      ImGuiChildFlags_Border,
                      ImGuiWindowFlags_HorizontalScrollbar);

    float padding = 10.0f;
    float imageSize = TILE_WIDTH * 2.5f;
    int columns = ImGui::GetContentRegionAvail().x / (imageSize + padding);
    int count = 0;

    for (auto it = this->tileset.get_tiles().begin();
         it != this->tileset.get_tiles().end(); ++it) {
      if (ImGui::ImageButton(std::to_string(it->get()->id).c_str(),
                             it->get()->texture,
                             sf::Vector2f(imageSize, imageSize))) {
        this->selectedTile = std::make_optional(*it);
      }

      count++;

      if (count % columns != 0) {
        ImGui::SameLine();
      }
    }

    ImGui::EndChild();

    // --- "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();
  }

  const float Editor::get_zoom() const { return this->zoom; }
}