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
1873b078 Poul Sander 2025-02-06 22:01 96
void PuzzleEditorState::Draw(SDL_Renderer* target) {
6046ca3f Poul Sander 2025-04-06 16:14 97
	DrawBackground(target);
1fcc8881 Poul Sander 2025-05-15 18:54 98
1fcc8881 Poul Sander 2025-05-15 18:54 99
	ImGui::Begin("Board area");
1fcc8881 Poul Sander 2025-05-15 18:54 100
	ImGui::BeginChild("Test");
1fcc8881 Poul Sander 2025-05-15 18:54 101
	ImVec2 p = ImGui::GetCursorScreenPos();
636cde3b Poul Sander 2025-05-15 21:40 102
	ImVec2 size = ImGui::GetContentRegionAvail();
636cde3b Poul Sander 2025-05-15 21:40 103
	int xoffset = p.x;
636cde3b Poul Sander 2025-05-15 21:40 104
	int yoffset = p.y;
636cde3b Poul Sander 2025-05-15 21:40 105
	int height = BOARD_HEIGHT;
636cde3b Poul Sander 2025-05-15 21:40 106
	int width = BOARD_WIDTH;
636cde3b Poul Sander 2025-05-15 21:40 107
	window_resize.SetPhysicalSize(size.x, size.y);
636cde3b Poul Sander 2025-05-15 21:40 108
636cde3b Poul Sander 2025-05-15 21:40 109
59365d69 Poul Sander 2025-05-25 12:06 110
	int brick_size = 50;
59365d69 Poul Sander 2025-05-25 12:06 111
	window_resize.LogicalToPhysical(&brick_size, nullptr);
e1a54754 Poul Sander 2025-05-25 13:31 112
	brick_size -= window_resize.GetLeftMargin();
59365d69 Poul Sander 2025-05-25 12:06 113
	for (int i=0; i < 6; ++i) {
59365d69 Poul Sander 2025-05-25 12:06 114
		for (int j=0; j < 12; ++j) {
59365d69 Poul Sander 2025-05-25 12:06 115
			ImVec2 p1(i*50.0f, (11-j)*50.0f);
59365d69 Poul Sander 2025-05-25 12:06 116
			LogicalToPhysical(window_resize, p1);
59365d69 Poul Sander 2025-05-25 12:06 117
			p1.x += xoffset;
59365d69 Poul Sander 2025-05-25 12:06 118
			p1.y += yoffset;
59365d69 Poul Sander 2025-05-25 12:06 119
			int brick = PuzzleGetBrick(this->selected_puzzle, i, j);
59365d69 Poul Sander 2025-05-25 12:06 120
			DrawBrick(brick, static_cast<int>(p1.x), static_cast<int>(p1.y), brick_size, brick_size);
59365d69 Poul Sander 2025-05-25 12:06 121
		}
59365d69 Poul Sander 2025-05-25 12:06 122
	}
59365d69 Poul Sander 2025-05-25 12:06 123
1fcc8881 Poul Sander 2025-05-15 18:54 124
	for (int i=0; i <= 6;++i) {
636cde3b Poul Sander 2025-05-15 21:40 125
		ImVec2 p1(i*50.0f, 0);
636cde3b Poul Sander 2025-05-15 21:40 126
		ImVec2 p2(i*50.0f, height);
636cde3b Poul Sander 2025-05-15 21:40 127
		LogicalToPhysical(window_resize, p1);
636cde3b Poul Sander 2025-05-15 21:40 128
		LogicalToPhysical(window_resize, p2);
636cde3b Poul Sander 2025-05-15 21:40 129
		p1.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 130
		p1.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 131
		p2.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 132
		p2.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 133
		ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
1fcc8881 Poul Sander 2025-05-15 18:54 134
	}
1fcc8881 Poul Sander 2025-05-15 18:54 135
	for (int i=0; i <= 12;++i) {
636cde3b Poul Sander 2025-05-15 21:40 136
		ImVec2 p1(0,i*50.0f);
636cde3b Poul Sander 2025-05-15 21:40 137
		ImVec2 p2(width, i*50.0f);
636cde3b Poul Sander 2025-05-15 21:40 138
		LogicalToPhysical(window_resize, p1);
636cde3b Poul Sander 2025-05-15 21:40 139
		LogicalToPhysical(window_resize, p2);
636cde3b Poul Sander 2025-05-15 21:40 140
		p1.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 141
		p1.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 142
		p2.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 143
		p2.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 144
		ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
6046ca3f Poul Sander 2025-04-06 16:14 145
	}
37278577 Poul Sander 2025-06-21 07:50 146
	ImGuiIO& io = ImGui::GetIO();
37278577 Poul Sander 2025-06-21 07:50 147
	if (ImGui::IsMouseReleased(0) && io.MouseDownDurationPrev[0] < 0.3f && !ImGui::IsMouseDragPastThreshold(0)) {
37278577 Poul Sander 2025-06-21 07:50 148
		ImVec2 pos = io.MousePos;
37278577 Poul Sander 2025-06-21 07:50 149
		pos.x -= xoffset;
37278577 Poul Sander 2025-06-21 07:50 150
		pos.y -= yoffset;
37278577 Poul Sander 2025-06-21 07:50 151
		int logical_pos_x = 0;
37278577 Poul Sander 2025-06-21 07:50 152
		int logical_pos_y = 0;
37278577 Poul Sander 2025-06-21 07:50 153
		window_resize.PhysicalToLogical(pos.x, pos.y, logical_pos_x, logical_pos_y);
37278577 Poul Sander 2025-06-21 07:50 154
		//std::cout << "Clicked at: " << pos.x << "," << pos.y << "\n";
37278577 Poul Sander 2025-06-21 07:50 155
		//std::cout << "Logical Pos: " << logical_pos_x << "," << logical_pos_y << "\n";
37278577 Poul Sander 2025-06-21 07:50 156
		int board_x = logical_pos_x / 50;
37278577 Poul Sander 2025-06-21 07:50 157
		int board_y = logical_pos_y / 50;
37278577 Poul Sander 2025-06-21 07:50 158
		if (board_x >=0 && board_x < 6 && board_y >= 0 && board_y < 12) {
37278577 Poul Sander 2025-06-21 07:50 159
			std::cout << "Board Pos: " << board_x << "," << board_y << "\n";
37278577 Poul Sander 2025-06-21 07:50 160
		}
37278577 Poul Sander 2025-06-21 07:50 161
37278577 Poul Sander 2025-06-21 07:50 162
	}
1fcc8881 Poul Sander 2025-05-15 18:54 163
	ImGui::EndChild();
1fcc8881 Poul Sander 2025-05-15 18:54 164
	ImGui::End();
1fcc8881 Poul Sander 2025-05-15 18:54 165
1873b078 Poul Sander 2025-02-06 22:01 166
	ImGui::Begin("File list", nullptr, ImGuiWindowFlags_NoCollapse);
a80f6a78 Poul Sander 2025-04-04 18:34 167
	for (const auto& file : this->puzzle_files) {
a80f6a78 Poul Sander 2025-04-04 18:34 168
		if (ImGui::Selectable(file.c_str(), this->selected_file == file)) {
a80f6a78 Poul Sander 2025-04-04 18:34 169
			SelectFile(file);
a80f6a78 Poul Sander 2025-04-04 18:34 170
		}
1873b078 Poul Sander 2025-02-06 22:01 171
	}
1873b078 Poul Sander 2025-02-06 22:01 172
a80f6a78 Poul Sander 2025-04-04 18:34 173
a80f6a78 Poul Sander 2025-04-04 18:34 174
	ImGui::End();
a80f6a78 Poul Sander 2025-04-04 18:34 175
	ImGui::Begin("Puzzles in file");
a80f6a78 Poul Sander 2025-04-04 18:34 176
	int puzzle_count = PuzzleGetNumberOfPuzzles();
a80f6a78 Poul Sander 2025-04-04 18:34 177
	for (int i = 0; i < puzzle_count; ++i) {
6046ca3f Poul Sander 2025-04-06 16:14 178
		if (ImGui::Selectable(std::to_string(i).c_str(), this->selected_puzzle == i)) {
6046ca3f Poul Sander 2025-04-06 16:14 179
			this->selected_puzzle = i;
a80f6a78 Poul Sander 2025-04-04 18:34 180
		}
a80f6a78 Poul Sander 2025-04-04 18:34 181
	}
1873b078 Poul Sander 2025-02-06 22:01 182
	ImGui::End();
298835a8 Poul Sander 2025-05-31 20:49 183
298835a8 Poul Sander 2025-05-31 20:49 184
	ImGui::Begin("Palette");
52352a94 Poul Sander 2025-06-03 19:04 185
	for (size_t i = 0; i < 7; ++i) {
52352a94 Poul Sander 2025-06-03 19:04 186
		if (ImGui::Selectable(fmt::format("##{}", i).c_str(), this->selected_action == i, 0, ImVec2(0,50))) {
52352a94 Poul Sander 2025-06-03 19:04 187
			this->selected_action = i;
52352a94 Poul Sander 2025-06-03 19:04 188
		}
52352a94 Poul Sander 2025-06-03 19:04 189
		ImGui::SameLine();
52352a94 Poul Sander 2025-06-03 19:04 190
		sago::SagoSprite& sprite = globalData.bricks[i];
52352a94 Poul Sander 2025-06-03 19:04 191
		ImGuiWritePartOfTexture(sprite.GetTextureHandler().get(), sprite.GetImageCord().x, sprite.GetImageCord().y, sprite.GetWidth(), sprite.GetHeight(), 50, 50);
298835a8 Poul Sander 2025-05-31 20:49 192
	}
298835a8 Poul Sander 2025-05-31 20:49 193
	if (ImGui::Selectable("Clear", this->selected_action == selection_clear)) {
298835a8 Poul Sander 2025-05-31 20:49 194
		this->selected_action = selection_clear;
298835a8 Poul Sander 2025-05-31 20:49 195
	}
298835a8 Poul Sander 2025-05-31 20:49 196
	if (ImGui::Selectable("Move Up", this->selected_action == selection_move_up)) {
298835a8 Poul Sander 2025-05-31 20:49 197
		this->selected_action = selection_move_up;
298835a8 Poul Sander 2025-05-31 20:49 198
	}
298835a8 Poul Sander 2025-05-31 20:49 199
	if (ImGui::Selectable("Move down", this->selected_action == selection_move_down)) {
298835a8 Poul Sander 2025-05-31 20:49 200
		this->selected_action = selection_move_down;
298835a8 Poul Sander 2025-05-31 20:49 201
	}
298835a8 Poul Sander 2025-05-31 20:49 202
	if (ImGui::Selectable("Move left", this->selected_action == selection_move_left)) {
298835a8 Poul Sander 2025-05-31 20:49 203
		this->selected_action = selection_move_left;
298835a8 Poul Sander 2025-05-31 20:49 204
	}
298835a8 Poul Sander 2025-05-31 20:49 205
	if (ImGui::Selectable("Move right", this->selected_action == selection_move_right)) {
298835a8 Poul Sander 2025-05-31 20:49 206
		this->selected_action = selection_move_right;
298835a8 Poul Sander 2025-05-31 20:49 207
	}
298835a8 Poul Sander 2025-05-31 20:49 208
	ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 209
	ImGui::LabelText("moves allowed", "%i", PuzzleNumberOfMovesAllowed(this->selected_puzzle));
298835a8 Poul Sander 2025-05-31 20:49 210
	ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 211
	if (read_only) {
298835a8 Poul Sander 2025-05-31 20:49 212
		ImGui::LabelText("Read Only", "");
298835a8 Poul Sander 2025-05-31 20:49 213
		ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 214
	}
298835a8 Poul Sander 2025-05-31 20:49 215
	ImGui::End();
1873b078 Poul Sander 2025-02-06 22:01 216
}
1873b078 Poul Sander 2025-02-06 22:01 217
1873b078 Poul Sander 2025-02-06 22:01 218
void PuzzleEditorState::Update() {
1873b078 Poul Sander 2025-02-06 22:01 219
}
1873b078 Poul Sander 2025-02-06 22:01 220
1970-01-01 00:00 221