git repos / blockattack-game

blame: source/code/puzzle_editor/PuzzleEditorState.cpp

normal view · raw

1873b078 Poul Sander 2025-02-06 22:01 1
/*
1873b078 Poul Sander 2025-02-06 22:01 2
===========================================================================
1873b078 Poul Sander 2025-02-06 22:01 3
blockattack - Block Attack - Rise of the Blocks
1873b078 Poul Sander 2025-02-06 22:01 4
Copyright (C) 2005-2025 Poul Sander
1873b078 Poul Sander 2025-02-06 22:01 5
1873b078 Poul Sander 2025-02-06 22:01 6
This program is free software: you can redistribute it and/or modify
1873b078 Poul Sander 2025-02-06 22:01 7
it under the terms of the GNU General Public License as published by
1873b078 Poul Sander 2025-02-06 22:01 8
the Free Software Foundation, either version 2 of the License, or
1873b078 Poul Sander 2025-02-06 22:01 9
(at your option) any later version.
1873b078 Poul Sander 2025-02-06 22:01 10
1873b078 Poul Sander 2025-02-06 22:01 11
This program is distributed in the hope that it will be useful,
1873b078 Poul Sander 2025-02-06 22:01 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
1873b078 Poul Sander 2025-02-06 22:01 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1873b078 Poul Sander 2025-02-06 22:01 14
GNU General Public License for more details.
1873b078 Poul Sander 2025-02-06 22:01 15
1873b078 Poul Sander 2025-02-06 22:01 16
You should have received a copy of the GNU General Public License
1873b078 Poul Sander 2025-02-06 22:01 17
along with this program.  If not, see http://www.gnu.org/licenses/
1873b078 Poul Sander 2025-02-06 22:01 18
1873b078 Poul Sander 2025-02-06 22:01 19
Source information and contacts persons can be found at
1873b078 Poul Sander 2025-02-06 22:01 20
https://blockattack.net
1873b078 Poul Sander 2025-02-06 22:01 21
===========================================================================
1873b078 Poul Sander 2025-02-06 22:01 22
*/
1873b078 Poul Sander 2025-02-06 22:01 23
1873b078 Poul Sander 2025-02-06 22:01 24
#include "PuzzleEditorState.hpp"
298835a8 Poul Sander 2025-05-31 20:49 25
298835a8 Poul Sander 2025-05-31 20:49 26
#include <unistd.h>
52352a94 Poul Sander 2025-06-03 19:04 27
#include <fmt/core.h>
298835a8 Poul Sander 2025-05-31 20:49 28
1873b078 Poul Sander 2025-02-06 22:01 29
#include "imgui.h"
37278577 Poul Sander 2025-06-21 07:50 30
#include "imgui_internal.h"
1873b078 Poul Sander 2025-02-06 22:01 31
#include "backends/imgui_impl_sdl2.h"
1873b078 Poul Sander 2025-02-06 22:01 32
#include "backends/imgui_impl_sdlrenderer2.h"
a80f6a78 Poul Sander 2025-04-04 18:34 33
#include "../sago/SagoMisc.hpp"
a80f6a78 Poul Sander 2025-04-04 18:34 34
#include "../puzzlehandler.hpp"
1873b078 Poul Sander 2025-02-06 22:01 35
1873b078 Poul Sander 2025-02-06 22:01 36
636cde3b Poul Sander 2025-05-15 21:40 37
PuzzleEditorState::PuzzleEditorState() {
636cde3b Poul Sander 2025-05-15 21:40 38
	this->window_resize = sago::SagoLogicalResize(BOARD_WIDTH+5, BOARD_HEIGHT+5);
636cde3b Poul Sander 2025-05-15 21:40 39
}
636cde3b Poul Sander 2025-05-15 21:40 40
1873b078 Poul Sander 2025-02-06 22:01 41
bool PuzzleEditorState::IsActive() {
1873b078 Poul Sander 2025-02-06 22:01 42
	return isActive;
1873b078 Poul Sander 2025-02-06 22:01 43
}
1873b078 Poul Sander 2025-02-06 22:01 44
1873b078 Poul Sander 2025-02-06 22:01 45
void PuzzleEditorState::ProcessInput(const SDL_Event& event, bool& processed) {
1873b078 Poul Sander 2025-02-06 22:01 46
	ImGui_ImplSDL2_ProcessEvent(&event);
1873b078 Poul Sander 2025-02-06 22:01 47
}
1873b078 Poul Sander 2025-02-06 22:01 48
a80f6a78 Poul Sander 2025-04-04 18:34 49
void PuzzleEditorState::Init() {
a80f6a78 Poul Sander 2025-04-04 18:34 50
	this->selected_file = "";
a80f6a78 Poul Sander 2025-04-04 18:34 51
	this->puzzle_files = sago::GetFileList("puzzles");
a80f6a78 Poul Sander 2025-04-04 18:34 52
}
a80f6a78 Poul Sander 2025-04-04 18:34 53
a80f6a78 Poul Sander 2025-04-04 18:34 54
void PuzzleEditorState::SelectFile(const std::string& file) {
a80f6a78 Poul Sander 2025-04-04 18:34 55
	this->selected_file = file;
a80f6a78 Poul Sander 2025-04-04 18:34 56
	PuzzleSetName(file);
a80f6a78 Poul Sander 2025-04-04 18:34 57
	LoadPuzzleStages();
298835a8 Poul Sander 2025-05-31 20:49 58
	read_only = false;
298835a8 Poul Sander 2025-05-31 20:49 59
	if (file == "puzzle.levels") {
298835a8 Poul Sander 2025-05-31 20:49 60
		read_only = true;
298835a8 Poul Sander 2025-05-31 20:49 61
	}
a80f6a78 Poul Sander 2025-04-04 18:34 62
}
a80f6a78 Poul Sander 2025-04-04 18:34 63
636cde3b Poul Sander 2025-05-15 21:40 64
static void LogicalToPhysical(const sago::SagoLogicalResize& resize, ImVec2& inout) {
636cde3b Poul Sander 2025-05-15 21:40 65
	int inx = inout.x;
636cde3b Poul Sander 2025-05-15 21:40 66
	int iny = inout.y;
636cde3b Poul Sander 2025-05-15 21:40 67
	int outx = inout.x;
636cde3b Poul Sander 2025-05-15 21:40 68
	int outy = inout.y;
636cde3b Poul Sander 2025-05-15 21:40 69
	resize.LogicalToPhysical(inx, iny, outx, outy);
636cde3b Poul Sander 2025-05-15 21:40 70
	inout.x = outx;
636cde3b Poul Sander 2025-05-15 21:40 71
	inout.y = outy;
636cde3b Poul Sander 2025-05-15 21:40 72
}
636cde3b Poul Sander 2025-05-15 21:40 73
59365d69 Poul Sander 2025-05-25 12:06 74
e1a54754 Poul Sander 2025-05-25 13:31 75
static void ImGuiWritePartOfTexture(SDL_Texture* texture, int topx, int topy, int tex_w, int tex_h, int w, int h) {
e1a54754 Poul Sander 2025-05-25 13:31 76
	float sprite_w = tex_w;
e1a54754 Poul Sander 2025-05-25 13:31 77
	float sprite_h = tex_h;
59365d69 Poul Sander 2025-05-25 12:06 78
	float topxf = topx;
59365d69 Poul Sander 2025-05-25 12:06 79
	float topyf = topy;
59365d69 Poul Sander 2025-05-25 12:06 80
	ImVec2 uv0 = ImVec2(topxf / tex_w, topyf / tex_h);
59365d69 Poul Sander 2025-05-25 12:06 81
	ImVec2 uv1 = ImVec2((topxf + sprite_w) / tex_w, (topyf + sprite_h) / tex_h);
59365d69 Poul Sander 2025-05-25 12:06 82
	ImGui::Image((ImTextureID)(intptr_t)texture, ImVec2((float)w, (float)h), uv0, uv1);
59365d69 Poul Sander 2025-05-25 12:06 83
}
59365d69 Poul Sander 2025-05-25 12:06 84
59365d69 Poul Sander 2025-05-25 12:06 85
59365d69 Poul Sander 2025-05-25 12:06 86
static void DrawBrick(int brick, int x, int y, int width, int height) {
59365d69 Poul Sander 2025-05-25 12:06 87
	if (brick < 0 || brick > 6) {
59365d69 Poul Sander 2025-05-25 12:06 88
		return;
59365d69 Poul Sander 2025-05-25 12:06 89
	}
59365d69 Poul Sander 2025-05-25 12:06 90
59365d69 Poul Sander 2025-05-25 12:06 91
	sago::SagoSprite& sprite = globalData.bricks[brick];
59365d69 Poul Sander 2025-05-25 12:06 92
	ImGui::SetCursorScreenPos({ static_cast<float>(x), static_cast<float>(y) });
1ae9f202 Poul Sander 2025-05-30 13:59 93
	ImGuiWritePartOfTexture(sprite.GetTextureHandler().get(), sprite.GetImageCord().x, sprite.GetImageCord().y, sprite.GetWidth(), sprite.GetHeight(), width, height);
59365d69 Poul Sander 2025-05-25 12:06 94
}
59365d69 Poul Sander 2025-05-25 12:06 95
b8a4e6f1 Poul Sander 2025-06-21 10:31 96
b8a4e6f1 Poul Sander 2025-06-21 10:31 97
void PuzzleEditorState::BrickClicked(int x, int y) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 98
	if (read_only) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 99
		return;
b8a4e6f1 Poul Sander 2025-06-21 10:31 100
	}
b8a4e6f1 Poul Sander 2025-06-21 10:31 101
	if (x < 0 || x >= 6) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 102
		return;
b8a4e6f1 Poul Sander 2025-06-21 10:31 103
	}
b8a4e6f1 Poul Sander 2025-06-21 10:31 104
	if (y <0 || y >= 12) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 105
		return;
b8a4e6f1 Poul Sander 2025-06-21 10:31 106
	}
b8a4e6f1 Poul Sander 2025-06-21 10:31 107
	if (selected_action >= 0 && selected_action < 7) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 108
		PuzzleSetBrick(this->selected_puzzle, x, y, this->selected_action);
b8a4e6f1 Poul Sander 2025-06-21 10:31 109
	}
b8a4e6f1 Poul Sander 2025-06-21 10:31 110
	if (selected_action == selection_clear) {
b8a4e6f1 Poul Sander 2025-06-21 10:31 111
		PuzzleSetBrick(this->selected_puzzle, x, y, -1);
b8a4e6f1 Poul Sander 2025-06-21 10:31 112
	}
b8a4e6f1 Poul Sander 2025-06-21 10:31 113
b8a4e6f1 Poul Sander 2025-06-21 10:31 114
}
b8a4e6f1 Poul Sander 2025-06-21 10:31 115
1873b078 Poul Sander 2025-02-06 22:01 116
void PuzzleEditorState::Draw(SDL_Renderer* target) {
6046ca3f Poul Sander 2025-04-06 16:14 117
	DrawBackground(target);
1fcc8881 Poul Sander 2025-05-15 18:54 118
1fcc8881 Poul Sander 2025-05-15 18:54 119
	ImGui::Begin("Board area");
1fcc8881 Poul Sander 2025-05-15 18:54 120
	ImGui::BeginChild("Test");
1fcc8881 Poul Sander 2025-05-15 18:54 121
	ImVec2 p = ImGui::GetCursorScreenPos();
636cde3b Poul Sander 2025-05-15 21:40 122
	ImVec2 size = ImGui::GetContentRegionAvail();
636cde3b Poul Sander 2025-05-15 21:40 123
	int xoffset = p.x;
636cde3b Poul Sander 2025-05-15 21:40 124
	int yoffset = p.y;
636cde3b Poul Sander 2025-05-15 21:40 125
	int height = BOARD_HEIGHT;
636cde3b Poul Sander 2025-05-15 21:40 126
	int width = BOARD_WIDTH;
636cde3b Poul Sander 2025-05-15 21:40 127
	window_resize.SetPhysicalSize(size.x, size.y);
636cde3b Poul Sander 2025-05-15 21:40 128
636cde3b Poul Sander 2025-05-15 21:40 129
59365d69 Poul Sander 2025-05-25 12:06 130
	int brick_size = 50;
59365d69 Poul Sander 2025-05-25 12:06 131
	window_resize.LogicalToPhysical(&brick_size, nullptr);
e1a54754 Poul Sander 2025-05-25 13:31 132
	brick_size -= window_resize.GetLeftMargin();
59365d69 Poul Sander 2025-05-25 12:06 133
	for (int i=0; i < 6; ++i) {
59365d69 Poul Sander 2025-05-25 12:06 134
		for (int j=0; j < 12; ++j) {
59365d69 Poul Sander 2025-05-25 12:06 135
			ImVec2 p1(i*50.0f, (11-j)*50.0f);
59365d69 Poul Sander 2025-05-25 12:06 136
			LogicalToPhysical(window_resize, p1);
59365d69 Poul Sander 2025-05-25 12:06 137
			p1.x += xoffset;
59365d69 Poul Sander 2025-05-25 12:06 138
			p1.y += yoffset;
59365d69 Poul Sander 2025-05-25 12:06 139
			int brick = PuzzleGetBrick(this->selected_puzzle, i, j);
59365d69 Poul Sander 2025-05-25 12:06 140
			DrawBrick(brick, static_cast<int>(p1.x), static_cast<int>(p1.y), brick_size, brick_size);
59365d69 Poul Sander 2025-05-25 12:06 141
		}
59365d69 Poul Sander 2025-05-25 12:06 142
	}
59365d69 Poul Sander 2025-05-25 12:06 143
1fcc8881 Poul Sander 2025-05-15 18:54 144
	for (int i=0; i <= 6;++i) {
636cde3b Poul Sander 2025-05-15 21:40 145
		ImVec2 p1(i*50.0f, 0);
636cde3b Poul Sander 2025-05-15 21:40 146
		ImVec2 p2(i*50.0f, height);
636cde3b Poul Sander 2025-05-15 21:40 147
		LogicalToPhysical(window_resize, p1);
636cde3b Poul Sander 2025-05-15 21:40 148
		LogicalToPhysical(window_resize, p2);
636cde3b Poul Sander 2025-05-15 21:40 149
		p1.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 150
		p1.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 151
		p2.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 152
		p2.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 153
		ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
1fcc8881 Poul Sander 2025-05-15 18:54 154
	}
1fcc8881 Poul Sander 2025-05-15 18:54 155
	for (int i=0; i <= 12;++i) {
636cde3b Poul Sander 2025-05-15 21:40 156
		ImVec2 p1(0,i*50.0f);
636cde3b Poul Sander 2025-05-15 21:40 157
		ImVec2 p2(width, i*50.0f);
636cde3b Poul Sander 2025-05-15 21:40 158
		LogicalToPhysical(window_resize, p1);
636cde3b Poul Sander 2025-05-15 21:40 159
		LogicalToPhysical(window_resize, p2);
636cde3b Poul Sander 2025-05-15 21:40 160
		p1.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 161
		p1.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 162
		p2.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 163
		p2.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 164
		ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
6046ca3f Poul Sander 2025-04-06 16:14 165
	}
37278577 Poul Sander 2025-06-21 07:50 166
	ImGuiIO& io = ImGui::GetIO();
37278577 Poul Sander 2025-06-21 07:50 167
	if (ImGui::IsMouseReleased(0) && io.MouseDownDurationPrev[0] < 0.3f && !ImGui::IsMouseDragPastThreshold(0)) {
37278577 Poul Sander 2025-06-21 07:50 168
		ImVec2 pos = io.MousePos;
37278577 Poul Sander 2025-06-21 07:50 169
		pos.x -= xoffset;
37278577 Poul Sander 2025-06-21 07:50 170
		pos.y -= yoffset;
37278577 Poul Sander 2025-06-21 07:50 171
		int logical_pos_x = 0;
37278577 Poul Sander 2025-06-21 07:50 172
		int logical_pos_y = 0;
37278577 Poul Sander 2025-06-21 07:50 173
		window_resize.PhysicalToLogical(pos.x, pos.y, logical_pos_x, logical_pos_y);
37278577 Poul Sander 2025-06-21 07:50 174
		//std::cout << "Clicked at: " << pos.x << "," << pos.y << "\n";
37278577 Poul Sander 2025-06-21 07:50 175
		//std::cout << "Logical Pos: " << logical_pos_x << "," << logical_pos_y << "\n";
37278577 Poul Sander 2025-06-21 07:50 176
		int board_x = logical_pos_x / 50;
b8a4e6f1 Poul Sander 2025-06-21 10:31 177
		int board_y = 11-(logical_pos_y / 50);
37278577 Poul Sander 2025-06-21 07:50 178
		if (board_x >=0 && board_x < 6 && board_y >= 0 && board_y < 12) {
37278577 Poul Sander 2025-06-21 07:50 179
			std::cout << "Board Pos: " << board_x << "," << board_y << "\n";
b8a4e6f1 Poul Sander 2025-06-21 10:31 180
			BrickClicked(board_x, board_y);
37278577 Poul Sander 2025-06-21 07:50 181
		}
37278577 Poul Sander 2025-06-21 07:50 182
37278577 Poul Sander 2025-06-21 07:50 183
	}
1fcc8881 Poul Sander 2025-05-15 18:54 184
	ImGui::EndChild();
1fcc8881 Poul Sander 2025-05-15 18:54 185
	ImGui::End();
1fcc8881 Poul Sander 2025-05-15 18:54 186
1873b078 Poul Sander 2025-02-06 22:01 187
	ImGui::Begin("File list", nullptr, ImGuiWindowFlags_NoCollapse);
a80f6a78 Poul Sander 2025-04-04 18:34 188
	for (const auto& file : this->puzzle_files) {
a80f6a78 Poul Sander 2025-04-04 18:34 189
		if (ImGui::Selectable(file.c_str(), this->selected_file == file)) {
a80f6a78 Poul Sander 2025-04-04 18:34 190
			SelectFile(file);
a80f6a78 Poul Sander 2025-04-04 18:34 191
		}
1873b078 Poul Sander 2025-02-06 22:01 192
	}
1873b078 Poul Sander 2025-02-06 22:01 193
a80f6a78 Poul Sander 2025-04-04 18:34 194
a80f6a78 Poul Sander 2025-04-04 18:34 195
	ImGui::End();
a80f6a78 Poul Sander 2025-04-04 18:34 196
	ImGui::Begin("Puzzles in file");
a80f6a78 Poul Sander 2025-04-04 18:34 197
	int puzzle_count = PuzzleGetNumberOfPuzzles();
a80f6a78 Poul Sander 2025-04-04 18:34 198
	for (int i = 0; i < puzzle_count; ++i) {
6046ca3f Poul Sander 2025-04-06 16:14 199
		if (ImGui::Selectable(std::to_string(i).c_str(), this->selected_puzzle == i)) {
6046ca3f Poul Sander 2025-04-06 16:14 200
			this->selected_puzzle = i;
a80f6a78 Poul Sander 2025-04-04 18:34 201
		}
a80f6a78 Poul Sander 2025-04-04 18:34 202
	}
1873b078 Poul Sander 2025-02-06 22:01 203
	ImGui::End();
298835a8 Poul Sander 2025-05-31 20:49 204
298835a8 Poul Sander 2025-05-31 20:49 205
	ImGui::Begin("Palette");
52352a94 Poul Sander 2025-06-03 19:04 206
	for (size_t i = 0; i < 7; ++i) {
52352a94 Poul Sander 2025-06-03 19:04 207
		if (ImGui::Selectable(fmt::format("##{}", i).c_str(), this->selected_action == i, 0, ImVec2(0,50))) {
52352a94 Poul Sander 2025-06-03 19:04 208
			this->selected_action = i;
52352a94 Poul Sander 2025-06-03 19:04 209
		}
52352a94 Poul Sander 2025-06-03 19:04 210
		ImGui::SameLine();
52352a94 Poul Sander 2025-06-03 19:04 211
		sago::SagoSprite& sprite = globalData.bricks[i];
52352a94 Poul Sander 2025-06-03 19:04 212
		ImGuiWritePartOfTexture(sprite.GetTextureHandler().get(), sprite.GetImageCord().x, sprite.GetImageCord().y, sprite.GetWidth(), sprite.GetHeight(), 50, 50);
298835a8 Poul Sander 2025-05-31 20:49 213
	}
298835a8 Poul Sander 2025-05-31 20:49 214
	if (ImGui::Selectable("Clear", this->selected_action == selection_clear)) {
298835a8 Poul Sander 2025-05-31 20:49 215
		this->selected_action = selection_clear;
298835a8 Poul Sander 2025-05-31 20:49 216
	}
298835a8 Poul Sander 2025-05-31 20:49 217
	if (ImGui::Selectable("Move Up", this->selected_action == selection_move_up)) {
298835a8 Poul Sander 2025-05-31 20:49 218
		this->selected_action = selection_move_up;
298835a8 Poul Sander 2025-05-31 20:49 219
	}
298835a8 Poul Sander 2025-05-31 20:49 220
	if (ImGui::Selectable("Move down", this->selected_action == selection_move_down)) {
298835a8 Poul Sander 2025-05-31 20:49 221
		this->selected_action = selection_move_down;
298835a8 Poul Sander 2025-05-31 20:49 222
	}
298835a8 Poul Sander 2025-05-31 20:49 223
	if (ImGui::Selectable("Move left", this->selected_action == selection_move_left)) {
298835a8 Poul Sander 2025-05-31 20:49 224
		this->selected_action = selection_move_left;
298835a8 Poul Sander 2025-05-31 20:49 225
	}
298835a8 Poul Sander 2025-05-31 20:49 226
	if (ImGui::Selectable("Move right", this->selected_action == selection_move_right)) {
298835a8 Poul Sander 2025-05-31 20:49 227
		this->selected_action = selection_move_right;
298835a8 Poul Sander 2025-05-31 20:49 228
	}
298835a8 Poul Sander 2025-05-31 20:49 229
	ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 230
	ImGui::LabelText("moves allowed", "%i", PuzzleNumberOfMovesAllowed(this->selected_puzzle));
298835a8 Poul Sander 2025-05-31 20:49 231
	ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 232
	if (read_only) {
298835a8 Poul Sander 2025-05-31 20:49 233
		ImGui::LabelText("Read Only", "");
298835a8 Poul Sander 2025-05-31 20:49 234
		ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 235
	}
298835a8 Poul Sander 2025-05-31 20:49 236
	ImGui::End();
1873b078 Poul Sander 2025-02-06 22:01 237
}
1873b078 Poul Sander 2025-02-06 22:01 238
1873b078 Poul Sander 2025-02-06 22:01 239
void PuzzleEditorState::Update() {
1873b078 Poul Sander 2025-02-06 22:01 240
}
1873b078 Poul Sander 2025-02-06 22:01 241
1970-01-01 00:00 242