File: PuzzleEditorState.cpp

package info (click to toggle)
blockattack 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,292 kB
  • sloc: cpp: 93,645; sh: 143; pascal: 111; xml: 82; makefile: 14
file content (507 lines) | stat: -rw-r--r-- 15,092 bytes parent folder | download
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/*
===========================================================================
blockattack - Block Attack - Rise of the Blocks
Copyright (C) 2005-2025 Poul Sander

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see http://www.gnu.org/licenses/

Source information and contacts persons can be found at
https://blockattack.net
===========================================================================
*/

#include "PuzzleEditorState.hpp"

#include <unistd.h>
#include <fmt/core.h>

#include "imgui.h"
#include "imgui_internal.h"
#include "backends/imgui_impl_sdl2.h"
#include "backends/imgui_impl_sdlrenderer2.h"
#include "../sago/SagoMisc.hpp"
#include "../puzzlehandler.hpp"


void SetSinglePuzzleMode(int level);

PuzzleEditorState::PuzzleEditorState() {
	this->window_resize = sago::SagoLogicalResize(BOARD_WIDTH+5, BOARD_HEIGHT+5);
}

bool PuzzleEditorState::IsActive() {
	return isActive;
}

void PuzzleEditorState::ProcessInput(const SDL_Event& event, bool& processed) {
	if (event.type == SDL_KEYDOWN && !read_only) {
		const Uint8* keystate = SDL_GetKeyboardState(nullptr);
		const bool ctrl_held = keystate[SDL_SCANCODE_LCTRL] || keystate[SDL_SCANCODE_RCTRL];

		if (ctrl_held && event.key.keysym.sym == SDLK_z) {
			if (keystate[SDL_SCANCODE_LSHIFT] || keystate[SDL_SCANCODE_RSHIFT]) {
				Redo();
			} else {
				Undo();
			}
			processed = true;
		} else if (ctrl_held && event.key.keysym.sym == SDLK_y) {
			Redo();
			processed = true;
		}
	}
}

void PuzzleEditorState::Init() {
	this->selected_file = "";
	this->puzzle_files = sago::GetFileList("puzzles");
}

void PuzzleEditorState::SelectFile(const std::string& file) {
	undo_file_stack[this->selected_file] = undo_stack;
	redo_file_stack[this->selected_file] = redo_stack;
	this->selected_file = file;
	PuzzleSetName(file);
	LoadPuzzleStages();
	read_only = false;
	undo_stack = undo_file_stack[this->selected_file];
	redo_stack = redo_file_stack[this->selected_file];
	if (file == "puzzle.levels") {
		read_only = true;
	}
}

static void LogicalToPhysical(const sago::SagoLogicalResize& resize, ImVec2& inout) {
	const int inx = inout.x;
	const int iny = inout.y;
	int out_x = inout.x;
	int out_y = inout.y;
	resize.LogicalToPhysical(inx, iny, out_x, out_y);
	inout.x = out_x;
	inout.y = out_y;
}


static void ImGuiWritePartOfTexture(SDL_Texture* texture, int topx, int topy, int tex_w, int tex_h, int w, int h) {
	const float sprite_w = tex_w;
	const float sprite_h = tex_h;
	const float top_x_f = topx;
	const float top_y_f = topy;
	const ImVec2 uv0 = ImVec2(top_x_f / tex_w, top_y_f / tex_h);
	const ImVec2 uv1 = ImVec2((top_x_f + sprite_w) / tex_w, (top_y_f + sprite_h) / tex_h);
	ImGui::Image((ImTextureID)(intptr_t)texture, ImVec2((float)w, (float)h), uv0, uv1);
}


static void DrawBrick(int brick, int x, int y, int width, int height) {
	if (brick < 0 || brick > 6) {
		return;
	}

	sago::SagoSprite& sprite = globalData.bricks[brick];
	ImGui::SetCursorScreenPos({ static_cast<float>(x), static_cast<float>(y) });
	ImGuiWritePartOfTexture(sprite.GetTextureHandler().get(), sprite.GetImageCord().x, sprite.GetImageCord().y, sprite.GetWidth(), sprite.GetHeight(), width, height);
}

/**
 * Takes the brick pointed to and moves up all bricks above it to leave an empty space.
 * If bricks are pushed out of the board then the disappear.
 * @param level The level to edit
 * @param x
 * @param y
 */
static void BricksMoveUp(int level, int x, int y) {
	for (int i = 11; i > y; i--) {
		PuzzleSetBrick(level, x, i, PuzzleGetBrick(level, x, i-1));
	}
	PuzzleSetBrick(level, x, y, -1);  //Clear the space we started
}

static void BricksMoveDown(int level, int x, int y) {
	for (int i = 0; i < y; i++) {
		PuzzleSetBrick(level, x, i, PuzzleGetBrick(level, x, i+1));
	}
	PuzzleSetBrick(level, x, y, -1);  //Clear the space we started
}

static void BricksMoveColumnLeft(int level, int x) {
	for (int i=0; i < x; i++) {
		for (int j=0; j<12 ; j++) {
			PuzzleSetBrick(level, i, j, PuzzleGetBrick(level, i+1, j));
		}
	}
	for (int j=0; j<12 ; j++) {
		PuzzleSetBrick(level, x, j, -1);
	}
}

static void BricksMoveColumnRight(int level, int x) {
	for (int i=5; i > x; i--) {
		for (int j=0; j<12 ; j++) {
			PuzzleSetBrick(level, i, j, PuzzleGetBrick(level, i-1, j));
		}
	}
	for (int j=0; j<12 ; j++) {
		PuzzleSetBrick(level, x, j, -1);
	}
}


void PuzzleEditorState::SaveCurrentState() {
	if (undo_stack.size() >= MAX_UNDO_STATES) {
		std::vector<PuzzleState> temp;
		for (size_t i = 0; i < MAX_UNDO_STATES - 1; ++i) {
			temp.push_back(undo_stack.back());
			undo_stack.pop_back();
		}
		undo_stack = std::vector<PuzzleState>();
		while (temp.size()) {
			undo_stack.push_back(temp.back());
			temp.pop_back();
		}
	}

	PuzzleState current_state;
	current_state.puzzle_count = PuzzleGetNumberOfPuzzles();
	current_state.current_puzzle = this->selected_puzzle;
	current_state.moves_allowed.resize(current_state.puzzle_count);

	for (int level = 0; level < current_state.puzzle_count; ++level) {
		current_state.moves_allowed[level] = PuzzleNumberOfMovesAllowed(level);
		for (int x = 0; x < 6; ++x) {
			for (int y = 0; y < 12; ++y) {
				current_state.puzzle_data[level][x][y] = PuzzleGetBrick(level, x, y);
			}
		}
	}

	undo_stack.push_back(current_state);
	while (redo_stack.size()) {
		redo_stack.pop_back();
	}
}

void PuzzleEditorState::Undo() {
	if (!CanUndo()) {
		return;
	}

	PuzzleState current_state;
	current_state.puzzle_count = PuzzleGetNumberOfPuzzles();
	current_state.current_puzzle = this->selected_puzzle;
	current_state.moves_allowed.resize(current_state.puzzle_count);

	for (int level = 0; level < current_state.puzzle_count; ++level) {
		current_state.moves_allowed[level] = PuzzleNumberOfMovesAllowed(level);
		for (int x = 0; x < 6; ++x) {
			for (int y = 0; y < 12; ++y) {
				current_state.puzzle_data[level][x][y] = PuzzleGetBrick(level, x, y);
			}
		}
	}
	redo_stack.push_back(current_state);

	PuzzleState previous_state = undo_stack.back();
	undo_stack.pop_back();

	EditorResizePuzzleNumber(previous_state.puzzle_count);
	for (int level = 0; level < previous_state.puzzle_count; ++level) {
		PuzzleNumberOfMovesAllowedSet(level, previous_state.moves_allowed[level]);
		for (int x = 0; x < 6; ++x) {
			for (int y = 0; y < 12; ++y) {
				PuzzleSetBrick(level, x, y, previous_state.puzzle_data[level][x][y]);
			}
		}
	}

	this->selected_puzzle = previous_state.current_puzzle;
	SavePuzzleStages();
}

void PuzzleEditorState::Redo() {
	if (!CanRedo()) {
		return;
	}

	PuzzleState current_state;
	current_state.puzzle_count = PuzzleGetNumberOfPuzzles();
	current_state.current_puzzle = this->selected_puzzle;
	current_state.moves_allowed.resize(current_state.puzzle_count);

	for (int level = 0; level < current_state.puzzle_count; ++level) {
		current_state.moves_allowed[level] = PuzzleNumberOfMovesAllowed(level);
		for (int x = 0; x < 6; ++x) {
			for (int y = 0; y < 12; ++y) {
				current_state.puzzle_data[level][x][y] = PuzzleGetBrick(level, x, y);
			}
		}
	}
	undo_stack.push_back(current_state);

	PuzzleState next_state = redo_stack.back();
	redo_stack.pop_back();

	EditorResizePuzzleNumber(next_state.puzzle_count);
	for (int level = 0; level < next_state.puzzle_count; ++level) {
		PuzzleNumberOfMovesAllowedSet(level, next_state.moves_allowed[level]);
		for (int x = 0; x < 6; ++x) {
			for (int y = 0; y < 12; ++y) {
				PuzzleSetBrick(level, x, y, next_state.puzzle_data[level][x][y]);
			}
		}
	}

	this->selected_puzzle = next_state.current_puzzle;
	SavePuzzleStages();
}

bool PuzzleEditorState::CanUndo() const {
	return undo_stack.size();
}

bool PuzzleEditorState::CanRedo() const {
	return redo_stack.size();
}

void PuzzleEditorState::BrickClicked(int x, int y) {
	if (read_only) {
		return;
	}
	if (x < 0 || x >= 6) {
		return;
	}
	if (y <0 || y >= 12) {
		return;
	}

	SaveCurrentState();

	if (selected_action >= 0 && selected_action < 7) {
		PuzzleSetBrick(this->selected_puzzle, x, y, this->selected_action);
	}
	if (selected_action == selection_clear) {
		PuzzleSetBrick(this->selected_puzzle, x, y, -1);
	}
	if (selected_action == selection_move_up) {
		BricksMoveUp(this->selected_puzzle, x, y);
	}
	if (selected_action == selection_move_down) {
		BricksMoveDown(this->selected_puzzle, x, y);
	}
	if (selected_action == selection_move_left) {
		BricksMoveColumnLeft(this->selected_puzzle, x);
	}
	if (selected_action == selection_move_right) {
		BricksMoveColumnRight(this->selected_puzzle, x);
	}
	SavePuzzleStages();

}

void PuzzleEditorState::Draw(SDL_Renderer* target) {
	DrawBackground(target);

	ImGui::Begin("Board area");
	ImGui::BeginChild("Test");
	ImVec2 p = ImGui::GetCursorScreenPos();
	ImVec2 size = ImGui::GetContentRegionAvail();
	const int x_offset = p.x;
	const int y_offset = p.y;
	constexpr int height = BOARD_HEIGHT;
	constexpr int width = BOARD_WIDTH;
	window_resize.SetPhysicalSize(size.x, size.y);


	int brick_size = 50;
	window_resize.LogicalToPhysical(&brick_size, nullptr);
	brick_size -= window_resize.GetLeftMargin();
	for (int i=0; i < 6; ++i) {
		for (int j=0; j < 12; ++j) {
			ImVec2 p1(i*50.0f, (11-j)*50.0f);
			LogicalToPhysical(window_resize, p1);
			p1.x += x_offset;
			p1.y += y_offset;
			const int brick = PuzzleGetBrick(this->selected_puzzle, i, j);
			DrawBrick(brick, static_cast<int>(p1.x), static_cast<int>(p1.y), brick_size, brick_size);
		}
	}

	for (int i=0; i <= 6;++i) {
		ImVec2 p1(i*50.0f, 0);
		ImVec2 p2(i*50.0f, height);
		LogicalToPhysical(window_resize, p1);
		LogicalToPhysical(window_resize, p2);
		p1.x += x_offset;
		p1.y += y_offset;
		p2.x += x_offset;
		p2.y += y_offset;
		ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
	}
	for (int i=0; i <= 12;++i) {
		ImVec2 p1(0,i*50.0f);
		ImVec2 p2(width, i*50.0f);
		LogicalToPhysical(window_resize, p1);
		LogicalToPhysical(window_resize, p2);
		p1.x += x_offset;
		p1.y += y_offset;
		p2.x += x_offset;
		p2.y += y_offset;
		ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
	}
	const ImGuiIO& io = ImGui::GetIO();
	if (ImGui::IsMouseReleased(0) && io.MouseDownDurationPrev[0] < 0.3f && !ImGui::IsMouseDragPastThreshold(0)) {
		ImVec2 pos = io.MousePos;
		pos.x -= x_offset;
		pos.y -= y_offset;
		int logical_pos_x = 0;
		int logical_pos_y = 0;
		window_resize.PhysicalToLogical(pos.x, pos.y, logical_pos_x, logical_pos_y);
		//std::cout << "Clicked at: " << pos.x << "," << pos.y << "\n";
		//std::cout << "Logical Pos: " << logical_pos_x << "," << logical_pos_y << "\n";
		const int board_x = logical_pos_x / 50;
		const int board_y = 11-(logical_pos_y / 50);
		if (board_x >=0 && board_x < 6 && board_y >= 0 && board_y < 12) {
			std::cout << "Board Pos: " << board_x << "," << board_y << "\n";
			BrickClicked(board_x, board_y);
		}

	}
	ImGui::EndChild();
	ImGui::End();

	ImGui::Begin("File list", nullptr, ImGuiWindowFlags_NoCollapse);
	for (const auto& file : this->puzzle_files) {
		if (ImGui::Selectable(file.c_str(), this->selected_file == file)) {
			SelectFile(file);
		}
	}

	ImGui::Separator();
	ImGui::InputText("##newfilename", new_filename_buffer, sizeof(new_filename_buffer));
	if (ImGui::Button("Create file")) {
		std::string new_file(new_filename_buffer);
		if (new_file.size()) {
			const auto it = std::find(puzzle_files.begin(), puzzle_files.end(), new_file);
			if (it == puzzle_files.end()) {
				puzzle_files.push_back(new_file);
				std::sort(puzzle_files.begin(), puzzle_files.end());
				sago::WriteFileContent(fmt::format("puzzles/{}", new_file).c_str(), "0"); // Create file with 0 puzzles in it
				SelectFile(new_file);
			}
		}
	}

	ImGui::End();
	ImGui::Begin("Puzzles in file");
	const int puzzle_count = PuzzleGetNumberOfPuzzles();
	for (int i = 0; i < puzzle_count; ++i) {
		if (ImGui::Selectable(std::to_string(i).c_str(), this->selected_puzzle == i)) {
			this->selected_puzzle = i;
		}
	}
	if (ImGui::Button("Add puzzle")) {
		if (!read_only) {
			SaveCurrentState();
			EditorAddPuzzle(this->selected_puzzle);
			SavePuzzleStages();
		}
	}
	if (ImGui::Button("Remove puzzle")) {
		if (!read_only) {
			SaveCurrentState();
			EditorRemovePuzzle(this->selected_puzzle);
			SavePuzzleStages();
		}
	}
	ImGui::End();

	ImGui::Begin("Palette");
	for (size_t i = 0; i < 7; ++i) {
		if (ImGui::Selectable(fmt::format("##{}", i).c_str(), this->selected_action == static_cast<int>(i), 0, ImVec2(0,50))) {
			this->selected_action = static_cast<int>(i);
		}
		ImGui::SameLine();
		sago::SagoSprite& sprite = globalData.bricks[i];
		ImGuiWritePartOfTexture(sprite.GetTextureHandler().get(), sprite.GetImageCord().x, sprite.GetImageCord().y, sprite.GetWidth(), sprite.GetHeight(), 50, 50);
	}
	if (ImGui::Selectable("Clear", this->selected_action == selection_clear)) {
		this->selected_action = selection_clear;
	}
	if (ImGui::Selectable("Move Up", this->selected_action == selection_move_up)) {
		this->selected_action = selection_move_up;
	}
	/*if (ImGui::Selectable("Move down", this->selected_action == selection_move_down)) {
		this->selected_action = selection_move_down;
	}
	if (ImGui::Selectable("Move left", this->selected_action == selection_move_left)) {
		this->selected_action = selection_move_left;
	}
	if (ImGui::Selectable("Move right", this->selected_action == selection_move_right)) {
		this->selected_action = selection_move_right;
	}*/
	ImGui::Separator();
	ImGui::LabelText("moves allowed", "%i", PuzzleNumberOfMovesAllowed(this->selected_puzzle));
	if (ImGui::Button("+1 move")) {
		if (!read_only) {
			SaveCurrentState();
			PuzzleNumberOfMovesAllowedSet(this->selected_puzzle, PuzzleNumberOfMovesAllowed(this->selected_puzzle)+1);
			SavePuzzleStages();
		}
	}
	if (ImGui::Button("-1 move")) {
		if (!read_only) {
			SaveCurrentState();
			PuzzleNumberOfMovesAllowedSet(this->selected_puzzle, PuzzleNumberOfMovesAllowed(this->selected_puzzle)-1);
			SavePuzzleStages();
		}
	}
	ImGui::Separator();
	if (read_only) {
		ImGui::LabelText("Read Only", "");
	} else {
		const bool undo_available = CanUndo();
		if (!undo_available) {
			ImGui::BeginDisabled();
		}
		if (ImGui::Button("Undo (Ctrl+Z)")) {
			Undo();
		}
		if (!undo_available) {
			ImGui::EndDisabled();
		}

		ImGui::SameLine();
		const bool redo_available = CanRedo();

		if (!redo_available) {
			ImGui::BeginDisabled();
		}
		if (ImGui::Button("Redo (Ctrl+Y)")) {
			Redo();
		}
		if (!redo_available) {
			ImGui::EndDisabled();
		}
	}
	ImGui::Separator();
	if (ImGui::Button("Play")) {
		SetSinglePuzzleMode(this->selected_puzzle);
		runGame(Gametype::Puzzle, this->selected_puzzle);
	}
	ImGui::End();
}

void PuzzleEditorState::Update() {
}