Debian Codegraph @

source/code/puzzle_editor/PuzzleEditorState.cpp

raw symbols SCIP
1 /*
2 ===========================================================================
3 blockattack - Block Attack - Rise of the Blocks
4 Copyright (C) 2005-2025 Poul Sander
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see http://www.gnu.org/licenses/
18
19 Source information and contacts persons can be found at
20 https://blockattack.net
21 ===========================================================================
22 */
23
24 #include "PuzzleEditorState.hpp"
25
26 #include <unistd.h>
27 #include <fmt/core.h>
28
29 #include "imgui.h"
30 #include "imgui_internal.h"
31 #include "backends/imgui_impl_sdl2.h"
32 #include "backends/imgui_impl_sdlrenderer2.h"
33 #include "../sago/SagoMisc.hpp"
34 #include "../puzzlehandler.hpp"
35
36
37 void SetSinglePuzzleMode(int level);
38
39 PuzzleEditorState::PuzzleEditorState() {
40 this->window_resize = sago::SagoLogicalResize(BOARD_WIDTH+5, BOARD_HEIGHT+5);
41 }
42
43 bool PuzzleEditorState::IsActive() {
44 return isActive;
45 }
46
47 void PuzzleEditorState::ProcessInput(const SDL_Event& event, bool& processed) {
48 if (event.type == SDL_KEYDOWN && !read_only) {
49 const Uint8* keystate = SDL_GetKeyboardState(nullptr);
50 const bool ctrl_held = keystate[SDL_SCANCODE_LCTRL] || keystate[SDL_SCANCODE_RCTRL];
51
52 if (ctrl_held && event.key.keysym.sym == SDLK_z) {
53 if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT]) {
54 Redo();
55 } else {
56 Undo();
57 }
58 processed = true;
59 } else if (ctrl_held && event.key.keysym.sym == SDLK_y) {
60 Redo();
61 processed = true;
62 }
63 }
64 }
65
66 void PuzzleEditorState::Init() {
67 this->selected_file = "";
68 this->puzzle_files = sago::GetFileList("puzzles");
69 }
70
71 void PuzzleEditorState::SelectFile(const std::string& file) {
72 undo_file_stack[this->selected_file] = undo_stack;
73 redo_file_stack[this->selected_file] = redo_stack;
74 this->selected_file = file;
75 PuzzleSetName(file);
76 LoadPuzzleStages();
77 read_only = false;
78 undo_stack = undo_file_stack[this->selected_file];
79 redo_stack = redo_file_stack[this->selected_file];
80 if (file == "puzzle.levels") {
81 read_only = true;
82 }
83 }
84
85 static void LogicalToPhysical(const sago::SagoLogicalResize& resize, ImVec2& inout) {
86 const int inx = inout.x;
87 const int iny = inout.y;
88 int out_x = inout.x;
89 int out_y = inout.y;
90 resize.LogicalToPhysical(inx, iny, out_x, out_y);
91 inout.x = out_x;
92 inout.y = out_y;
93 }
94
95
96 static void ImGuiWritePartOfTexture(SDL_Texture* texture, int topx, int topy, int tex_w, int tex_h, int w, int h) {
97 const float sprite_w = tex_w;
98 const float sprite_h = tex_h;
99 const float top_x_f = topx;
100 const float top_y_f = topy;
101 const ImVec2 uv0 = ImVec2(top_x_f / tex_w, top_y_f / tex_h);
102 const ImVec2 uv1 = ImVec2((top_x_f + sprite_w) / tex_w, (top_y_f + sprite_h) / tex_h);
103 ImGui::Image((ImTextureID)(intptr_t)texture, ImVec2((float)w, (float)h), uv0, uv1);
104 }
105
106
107 static void DrawBrick(int brick, int x, int y, int width, int height) {
108 if (brick < 0 || brick > 6) {
109 return;
110 }
111
112 sago::SagoSprite& sprite = globalData.bricks[brick];
113 ImGui::SetCursorScreenPos({ static_cast<float>(x), static_cast<float>(y) });
114 ImGuiWritePartOfTexture(sprite.GetTextureHandler().get(), sprite.GetImageCord().x, sprite.GetImageCord().y, sprite.GetWidth(), sprite.GetHeight(), width, height);
115 }
116
117 /**
118 * Takes the brick pointed to and moves up all bricks above it to leave an empty space.
119 * If bricks are pushed out of the board then the disappear.
120 * @param level The level to edit
121 * @param x
122 * @param y
123 */
124 static void BricksMoveUp(int level, int x, int y) {
125 for (int i = 11; i > y; i--) {
126 PuzzleSetBrick(level, x, i, PuzzleGetBrick(level, x, i-1));
127 }
128 PuzzleSetBrick(level, x, y, -1); //Clear the space we started
129 }
130
131 static void BricksMoveDown(int level, int x, int y) {
132 for (int i = 0; i < y; i++) {
133 PuzzleSetBrick(level, x, i, PuzzleGetBrick(level, x, i+1));
134 }
135 PuzzleSetBrick(level, x, y, -1); //Clear the space we started
136 }
137
138 static void BricksMoveColumnLeft(int level, int x) {
139 for (int i=0; i < x; i++) {
140 for (int j=0; j<12 ; j++) {
141 PuzzleSetBrick(level, i, j, PuzzleGetBrick(level, i+1, j));
142 }
143 }
144 for (int j=0; j<12 ; j++) {
145 PuzzleSetBrick(level, x, j, -1);
146 }
147 }
148
149 static void BricksMoveColumnRight(int level, int x) {
150 for (int i=5; i > x; i--) {
151 for (int j=0; j<12 ; j++) {
152 PuzzleSetBrick(level, i, j, PuzzleGetBrick(level, i-1, j));
153 }
154 }
155 for (int j=0; j<12 ; j++) {
156 PuzzleSetBrick(level, x, j, -1);
157 }
158 }
159
160
161 void PuzzleEditorState::SaveCurrentState() {
162 if (undo_stack.size() >= MAX_UNDO_STATES) {
163 std::vector<PuzzleState> temp;
164 for (size_t i = 0; i < MAX_UNDO_STATES - 1; ++i) {
165 temp.push_back(undo_stack.back());
166 undo_stack.pop_back();
167 }
168 undo_stack = std::vector<PuzzleState>();
169 while (temp.size()) {
170 undo_stack.push_back(temp.back());
171 temp.pop_back();
172 }
173 }
174
175 PuzzleState current_state;
176 current_state.puzzle_count = PuzzleGetNumberOfPuzzles();
177 current_state.current_puzzle = this->selected_puzzle;
178 current_state.moves_allowed.resize(current_state.puzzle_count);
179
180 for (int level = 0; level < current_state.puzzle_count; ++level) {
181 current_state.moves_allowed[level] = PuzzleNumberOfMovesAllowed(level);
182 for (int x = 0; x < 6; ++x) {
183 for (int y = 0; y < 12; ++y) {
184 current_state.puzzle_data[level][x][y] = PuzzleGetBrick(level, x, y);
185 }
186 }
187 }
188
189 undo_stack.push_back(current_state);
190 while (redo_stack.size()) {
191 redo_stack.pop_back();
192 }
193 }
194
195 void PuzzleEditorState::Undo() {
196 if (!CanUndo()) {
197 return;
198 }
199
200 PuzzleState current_state;
201 current_state.puzzle_count = PuzzleGetNumberOfPuzzles();
202 current_state.current_puzzle = this->selected_puzzle;
203 current_state.moves_allowed.resize(current_state.puzzle_count);
204
205 for (int level = 0; level < current_state.puzzle_count; ++level) {
206 current_state.moves_allowed[level] = PuzzleNumberOfMovesAllowed(level);
207 for (int x = 0; x < 6; ++x) {
208 for (int y = 0; y < 12; ++y) {
209 current_state.puzzle_data[level][x][y] = PuzzleGetBrick(level, x, y);
210 }
211 }
212 }
213 redo_stack.push_back(current_state);
214
215 PuzzleState previous_state = undo_stack.back();
216 undo_stack.pop_back();
217
218 EditorResizePuzzleNumber(previous_state.puzzle_count);
219 for (int level = 0; level < previous_state.puzzle_count; ++level) {
220 PuzzleNumberOfMovesAllowedSet(level, previous_state.moves_allowed[level]);
221 for (int x = 0; x < 6; ++x) {
222 for (int y = 0; y < 12; ++y) {
223 PuzzleSetBrick(level, x, y, previous_state.puzzle_data[level][x][y]);
224 }
225 }
226 }
227
228 this->selected_puzzle = previous_state.current_puzzle;
229 SavePuzzleStages();
230 }
231
232 void PuzzleEditorState::Redo() {
233 if (!CanRedo()) {
234 return;
235 }
236
237 PuzzleState current_state;
238 current_state.puzzle_count = PuzzleGetNumberOfPuzzles();
239 current_state.current_puzzle = this->selected_puzzle;
240 current_state.moves_allowed.resize(current_state.puzzle_count);
241
242 for (int level = 0; level < current_state.puzzle_count; ++level) {
243 current_state.moves_allowed[level] = PuzzleNumberOfMovesAllowed(level);
244 for (int x = 0; x < 6; ++x) {
245 for (int y = 0; y < 12; ++y) {
246 current_state.puzzle_data[level][x][y] = PuzzleGetBrick(level, x, y);
247 }
248 }
249 }
250 undo_stack.push_back(current_state);
251
252 PuzzleState next_state = redo_stack.back();
253 redo_stack.pop_back();
254
255 EditorResizePuzzleNumber(next_state.puzzle_count);
256 for (int level = 0; level < next_state.puzzle_count; ++level) {
257 PuzzleNumberOfMovesAllowedSet(level, next_state.moves_allowed[level]);
258 for (int x = 0; x < 6; ++x) {
259 for (int y = 0; y < 12; ++y) {
260 PuzzleSetBrick(level, x, y, next_state.puzzle_data[level][x][y]);
261 }
262 }
263 }
264
265 this->selected_puzzle = next_state.current_puzzle;
266 SavePuzzleStages();
267 }
268
269 bool PuzzleEditorState::CanUndo() const {
270 return undo_stack.size();
271 }
272
273 bool PuzzleEditorState::CanRedo() const {
274 return redo_stack.size();
275 }
276
277 void PuzzleEditorState::BrickClicked(int x, int y) {
278 if (read_only) {
279 return;
280 }
281 if (x < 0 || x >= 6) {
282 return;
283 }
284 if (y <0 || y >= 12) {
285 return;
286 }
287
288 SaveCurrentState();
289
290 if (selected_action >= 0 && selected_action < 7) {
291 PuzzleSetBrick(this->selected_puzzle, x, y, this->selected_action);
292 }
293 if (selected_action == selection_clear) {
294 PuzzleSetBrick(this->selected_puzzle, x, y, -1);
295 }
296 if (selected_action == selection_move_up) {
297 BricksMoveUp(this->selected_puzzle, x, y);
298 }
299 if (selected_action == selection_move_down) {
300 BricksMoveDown(this->selected_puzzle, x, y);
301 }
302 if (selected_action == selection_move_left) {
303 BricksMoveColumnLeft(this->selected_puzzle, x);
304 }
305 if (selected_action == selection_move_right) {
306 BricksMoveColumnRight(this->selected_puzzle, x);
307 }
308 SavePuzzleStages();
309
310 }
311
312 void PuzzleEditorState::Draw(SDL_Renderer* target) {
313 DrawBackground(target);
314
315 ImGui::Begin("Board area");
316 ImGui::BeginChild("Test");
317 ImVec2 p = ImGui::GetCursorScreenPos();
318 ImVec2 size = ImGui::GetContentRegionAvail();
319 const int x_offset = p.x;
320 const int y_offset = p.y;
321 constexpr int height = BOARD_HEIGHT;
322 constexpr int width = BOARD_WIDTH;
323 window_resize.SetPhysicalSize(size.x, size.y);
324
325
326 int brick_size = 50;
327 window_resize.LogicalToPhysical(&brick_size, nullptr);
328 brick_size -= window_resize.GetLeftMargin();
329 for (int i=0; i < 6; ++i) {
330 for (int j=0; j < 12; ++j) {
331 ImVec2 p1(i*50.0f, (11-j)*50.0f);
332 LogicalToPhysical(window_resize, p1);
333 p1.x += x_offset;
334 p1.y += y_offset;
335 const int brick = PuzzleGetBrick(this->selected_puzzle, i, j);
336 DrawBrick(brick, static_cast<int>(p1.x), static_cast<int>(p1.y), brick_size, brick_size);
337 }
338 }
339
340 for (int i=0; i <= 6;++i) {
341 ImVec2 p1(i*50.0f, 0);
342 ImVec2 p2(i*50.0f, height);
343 LogicalToPhysical(window_resize, p1);
344 LogicalToPhysical(window_resize, p2);
345 p1.x += x_offset;
346 p1.y += y_offset;
347 p2.x += x_offset;
348 p2.y += y_offset;
349 ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
350 }
351 for (int i=0; i <= 12;++i) {
352 ImVec2 p1(0,i*50.0f);
353 ImVec2 p2(width, i*50.0f);
354 LogicalToPhysical(window_resize, p1);
355 LogicalToPhysical(window_resize, p2);
356 p1.x += x_offset;
357 p1.y += y_offset;
358 p2.x += x_offset;
359 p2.y += y_offset;
360 ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
361 }
362 const ImGuiIO& io = ImGui::GetIO();
363 if (ImGui::IsMouseReleased(0) && io.MouseDownDurationPrev[0] < 0.3f && !ImGui::IsMouseDragPastThreshold(0)) {
364 ImVec2 pos = io.MousePos;
365 pos.x -= x_offset;
366 pos.y -= y_offset;
367 int logical_pos_x = 0;
368 int logical_pos_y = 0;
369 window_resize.PhysicalToLogical(pos.x, pos.y, logical_pos_x, logical_pos_y);
370 //std::cout << "Clicked at: " << pos.x << "," << pos.y << "\n";
371 //std::cout << "Logical Pos: " << logical_pos_x << "," << logical_pos_y << "\n";
372 const int board_x = logical_pos_x / 50;
373 const int board_y = 11-(logical_pos_y / 50);
374 if (board_x >=0 && board_x < 6 && board_y >= 0 && board_y < 12) {
375 std::cout << "Board Pos: " << board_x << "," << board_y << "\n";
376 BrickClicked(board_x, board_y);
377 }
378
379 }
380 ImGui::EndChild();
381 ImGui::End();
382
383 ImGui::Begin("File list", nullptr, ImGuiWindowFlags_NoCollapse);
384 for (const auto& file : this->puzzle_files) {
385 if (ImGui::Selectable(file.c_str(), this->selected_file == file)) {
386 SelectFile(file);
387 }
388 }
389
390 ImGui::Separator();
391 ImGui::InputText("##newfilename", new_filename_buffer, sizeof(new_filename_buffer));
392 if (ImGui::Button("Create file")) {
393 std::string new_file(new_filename_buffer);
394 if (new_file.size()) {
395 const auto it = std::find(puzzle_files.begin(), puzzle_files.end(), new_file);
396 if (it == puzzle_files.end()) {
397 puzzle_files.push_back(new_file);
398 std::sort(puzzle_files.begin(), puzzle_files.end());
399 sago::WriteFileContent(fmt::format("puzzles/{}", new_file).c_str(), "0"); // Create file with 0 puzzles in it
400 SelectFile(new_file);
401 }
402 }
403 }
404
405 ImGui::End();
406 ImGui::Begin("Puzzles in file");
407 const int puzzle_count = PuzzleGetNumberOfPuzzles();
408 for (int i = 0; i < puzzle_count; ++i) {
409 if (ImGui::Selectable(std::to_string(i).c_str(), this->selected_puzzle == i)) {
410 this->selected_puzzle = i;
411 }
412 }
413 if (ImGui::Button("Add puzzle")) {
414 if (!read_only) {
415 SaveCurrentState();
416 EditorAddPuzzle(this->selected_puzzle);
417 SavePuzzleStages();
418 }
419 }
420 if (ImGui::Button("Remove puzzle")) {
421 if (!read_only) {
422 SaveCurrentState();
423 EditorRemovePuzzle(this->selected_puzzle);
424 SavePuzzleStages();
425 }
426 }
427 ImGui::End();
428
429 ImGui::Begin("Palette");
430 for (size_t i = 0; i < 7; ++i) {
431 if (ImGui::Selectable(fmt::format("##{}", i).c_str(), this->selected_action == static_cast<int>(i), 0, ImVec2(0,50))) {
432 this->selected_action = static_cast<int>(i);
433 }
434 ImGui::SameLine();
435 sago::SagoSprite& sprite = globalData.bricks[i];
436 ImGuiWritePartOfTexture(sprite.GetTextureHandler().get(), sprite.GetImageCord().x, sprite.GetImageCord().y, sprite.GetWidth(), sprite.GetHeight(), 50, 50);
437 }
438 if (ImGui::Selectable("Clear", this->selected_action == selection_clear)) {
439 this->selected_action = selection_clear;
440 }
441 if (ImGui::Selectable("Move Up", this->selected_action == selection_move_up)) {
442 this->selected_action = selection_move_up;
443 }
444 /*if (ImGui::Selectable("Move down", this->selected_action == selection_move_down)) {
445 this->selected_action = selection_move_down;
446 }
447 if (ImGui::Selectable("Move left", this->selected_action == selection_move_left)) {
448 this->selected_action = selection_move_left;
449 }
450 if (ImGui::Selectable("Move right", this->selected_action == selection_move_right)) {
451 this->selected_action = selection_move_right;
452 }*/
453 ImGui::Separator();
454 ImGui::LabelText("moves allowed", "%i", PuzzleNumberOfMovesAllowed(this->selected_puzzle));
455 if (ImGui::Button("+1 move")) {
456 if (!read_only) {
457 SaveCurrentState();
458 PuzzleNumberOfMovesAllowedSet(this->selected_puzzle, PuzzleNumberOfMovesAllowed(this->selected_puzzle)+1);
459 SavePuzzleStages();
460 }
461 }
462 if (ImGui::Button("-1 move")) {
463 if (!read_only) {
464 SaveCurrentState();
465 PuzzleNumberOfMovesAllowedSet(this->selected_puzzle, PuzzleNumberOfMovesAllowed(this->selected_puzzle)-1);
466 SavePuzzleStages();
467 }
468 }
469 ImGui::Separator();
470 if (read_only) {
471 ImGui::LabelText("Read Only", "");
472 } else {
473 const bool undo_available = CanUndo();
474 if (!undo_available) {
475 ImGui::BeginDisabled();
476 }
477 if (ImGui::Button("Undo (Ctrl+Z)")) {
478 Undo();
479 }
480 if (!undo_available) {
481 ImGui::EndDisabled();
482 }
483
484 ImGui::SameLine();
485 const bool redo_available = CanRedo();
486
487 if (!redo_available) {
488 ImGui::BeginDisabled();
489 }
490 if (ImGui::Button("Redo (Ctrl+Y)")) {
491 Redo();
492 }
493 if (!redo_available) {
494 ImGui::EndDisabled();
495 }
496 }
497 ImGui::Separator();
498 if (ImGui::Button("Play")) {
499 SetSinglePuzzleMode(this->selected_puzzle);
500 runGame(Gametype::Puzzle, this->selected_puzzle);
501 }
502 ImGui::End();
503 }
504
505 void PuzzleEditorState::Update() {
506 }
507