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>
298835a8 Poul Sander 2025-05-31 20:49 27
1873b078 Poul Sander 2025-02-06 22:01 28
#include "imgui.h"
1873b078 Poul Sander 2025-02-06 22:01 29
#include "backends/imgui_impl_sdl2.h"
1873b078 Poul Sander 2025-02-06 22:01 30
#include "backends/imgui_impl_sdlrenderer2.h"
a80f6a78 Poul Sander 2025-04-04 18:34 31
#include "../sago/SagoMisc.hpp"
a80f6a78 Poul Sander 2025-04-04 18:34 32
#include "../puzzlehandler.hpp"
1873b078 Poul Sander 2025-02-06 22:01 33
1873b078 Poul Sander 2025-02-06 22:01 34
636cde3b Poul Sander 2025-05-15 21:40 35
PuzzleEditorState::PuzzleEditorState() {
636cde3b Poul Sander 2025-05-15 21:40 36
	this->window_resize = sago::SagoLogicalResize(BOARD_WIDTH+5, BOARD_HEIGHT+5);
636cde3b Poul Sander 2025-05-15 21:40 37
}
636cde3b Poul Sander 2025-05-15 21:40 38
1873b078 Poul Sander 2025-02-06 22:01 39
bool PuzzleEditorState::IsActive() {
1873b078 Poul Sander 2025-02-06 22:01 40
	return isActive;
1873b078 Poul Sander 2025-02-06 22:01 41
}
1873b078 Poul Sander 2025-02-06 22:01 42
1873b078 Poul Sander 2025-02-06 22:01 43
void PuzzleEditorState::ProcessInput(const SDL_Event& event, bool& processed) {
1873b078 Poul Sander 2025-02-06 22:01 44
	ImGui_ImplSDL2_ProcessEvent(&event);
1873b078 Poul Sander 2025-02-06 22:01 45
}
1873b078 Poul Sander 2025-02-06 22:01 46
a80f6a78 Poul Sander 2025-04-04 18:34 47
void PuzzleEditorState::Init() {
a80f6a78 Poul Sander 2025-04-04 18:34 48
	this->selected_file = "";
a80f6a78 Poul Sander 2025-04-04 18:34 49
	this->puzzle_files = sago::GetFileList("puzzles");
a80f6a78 Poul Sander 2025-04-04 18:34 50
}
a80f6a78 Poul Sander 2025-04-04 18:34 51
a80f6a78 Poul Sander 2025-04-04 18:34 52
void PuzzleEditorState::SelectFile(const std::string& file) {
a80f6a78 Poul Sander 2025-04-04 18:34 53
	this->selected_file = file;
a80f6a78 Poul Sander 2025-04-04 18:34 54
	PuzzleSetName(file);
a80f6a78 Poul Sander 2025-04-04 18:34 55
	LoadPuzzleStages();
298835a8 Poul Sander 2025-05-31 20:49 56
	read_only = false;
298835a8 Poul Sander 2025-05-31 20:49 57
	if (file == "puzzle.levels") {
298835a8 Poul Sander 2025-05-31 20:49 58
		read_only = true;
298835a8 Poul Sander 2025-05-31 20:49 59
	}
a80f6a78 Poul Sander 2025-04-04 18:34 60
}
a80f6a78 Poul Sander 2025-04-04 18:34 61
636cde3b Poul Sander 2025-05-15 21:40 62
static void LogicalToPhysical(const sago::SagoLogicalResize& resize, ImVec2& inout) {
636cde3b Poul Sander 2025-05-15 21:40 63
	int inx = inout.x;
636cde3b Poul Sander 2025-05-15 21:40 64
	int iny = inout.y;
636cde3b Poul Sander 2025-05-15 21:40 65
	int outx = inout.x;
636cde3b Poul Sander 2025-05-15 21:40 66
	int outy = inout.y;
636cde3b Poul Sander 2025-05-15 21:40 67
	resize.LogicalToPhysical(inx, iny, outx, outy);
636cde3b Poul Sander 2025-05-15 21:40 68
	inout.x = outx;
636cde3b Poul Sander 2025-05-15 21:40 69
	inout.y = outy;
636cde3b Poul Sander 2025-05-15 21:40 70
}
636cde3b Poul Sander 2025-05-15 21:40 71
59365d69 Poul Sander 2025-05-25 12:06 72
e1a54754 Poul Sander 2025-05-25 13:31 73
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 74
	float sprite_w = tex_w;
e1a54754 Poul Sander 2025-05-25 13:31 75
	float sprite_h = tex_h;
59365d69 Poul Sander 2025-05-25 12:06 76
	float topxf = topx;
59365d69 Poul Sander 2025-05-25 12:06 77
	float topyf = topy;
59365d69 Poul Sander 2025-05-25 12:06 78
	ImVec2 uv0 = ImVec2(topxf / tex_w, topyf / tex_h);
59365d69 Poul Sander 2025-05-25 12:06 79
	ImVec2 uv1 = ImVec2((topxf + sprite_w) / tex_w, (topyf + sprite_h) / tex_h);
59365d69 Poul Sander 2025-05-25 12:06 80
	ImGui::Image((ImTextureID)(intptr_t)texture, ImVec2((float)w, (float)h), uv0, uv1);
59365d69 Poul Sander 2025-05-25 12:06 81
}
59365d69 Poul Sander 2025-05-25 12:06 82
59365d69 Poul Sander 2025-05-25 12:06 83
59365d69 Poul Sander 2025-05-25 12:06 84
static void DrawBrick(int brick, int x, int y, int width, int height) {
59365d69 Poul Sander 2025-05-25 12:06 85
	if (brick < 0 || brick > 6) {
59365d69 Poul Sander 2025-05-25 12:06 86
		return;
59365d69 Poul Sander 2025-05-25 12:06 87
	}
59365d69 Poul Sander 2025-05-25 12:06 88
59365d69 Poul Sander 2025-05-25 12:06 89
	sago::SagoSprite& sprite = globalData.bricks[brick];
59365d69 Poul Sander 2025-05-25 12:06 90
	ImGui::SetCursorScreenPos({ static_cast<float>(x), static_cast<float>(y) });
1ae9f202 Poul Sander 2025-05-30 13:59 91
	ImGuiWritePartOfTexture(sprite.GetTextureHandler().get(), sprite.GetImageCord().x, sprite.GetImageCord().y, sprite.GetWidth(), sprite.GetHeight(), width, height);
59365d69 Poul Sander 2025-05-25 12:06 92
}
59365d69 Poul Sander 2025-05-25 12:06 93
1873b078 Poul Sander 2025-02-06 22:01 94
void PuzzleEditorState::Draw(SDL_Renderer* target) {
6046ca3f Poul Sander 2025-04-06 16:14 95
	DrawBackground(target);
1fcc8881 Poul Sander 2025-05-15 18:54 96
1fcc8881 Poul Sander 2025-05-15 18:54 97
	ImGui::Begin("Board area");
1fcc8881 Poul Sander 2025-05-15 18:54 98
	ImGui::BeginChild("Test");
1fcc8881 Poul Sander 2025-05-15 18:54 99
	ImVec2 p = ImGui::GetCursorScreenPos();
636cde3b Poul Sander 2025-05-15 21:40 100
	ImVec2 size = ImGui::GetContentRegionAvail();
636cde3b Poul Sander 2025-05-15 21:40 101
	int xoffset = p.x;
636cde3b Poul Sander 2025-05-15 21:40 102
	int yoffset = p.y;
636cde3b Poul Sander 2025-05-15 21:40 103
	int height = BOARD_HEIGHT;
636cde3b Poul Sander 2025-05-15 21:40 104
	int width = BOARD_WIDTH;
636cde3b Poul Sander 2025-05-15 21:40 105
	window_resize.SetPhysicalSize(size.x, size.y);
636cde3b Poul Sander 2025-05-15 21:40 106
636cde3b Poul Sander 2025-05-15 21:40 107
59365d69 Poul Sander 2025-05-25 12:06 108
	int brick_size = 50;
59365d69 Poul Sander 2025-05-25 12:06 109
	window_resize.LogicalToPhysical(&brick_size, nullptr);
e1a54754 Poul Sander 2025-05-25 13:31 110
	brick_size -= window_resize.GetLeftMargin();
59365d69 Poul Sander 2025-05-25 12:06 111
	for (int i=0; i < 6; ++i) {
59365d69 Poul Sander 2025-05-25 12:06 112
		for (int j=0; j < 12; ++j) {
59365d69 Poul Sander 2025-05-25 12:06 113
			ImVec2 p1(i*50.0f, (11-j)*50.0f);
59365d69 Poul Sander 2025-05-25 12:06 114
			LogicalToPhysical(window_resize, p1);
59365d69 Poul Sander 2025-05-25 12:06 115
			p1.x += xoffset;
59365d69 Poul Sander 2025-05-25 12:06 116
			p1.y += yoffset;
59365d69 Poul Sander 2025-05-25 12:06 117
			int brick = PuzzleGetBrick(this->selected_puzzle, i, j);
59365d69 Poul Sander 2025-05-25 12:06 118
			DrawBrick(brick, static_cast<int>(p1.x), static_cast<int>(p1.y), brick_size, brick_size);
59365d69 Poul Sander 2025-05-25 12:06 119
		}
59365d69 Poul Sander 2025-05-25 12:06 120
	}
59365d69 Poul Sander 2025-05-25 12:06 121
1fcc8881 Poul Sander 2025-05-15 18:54 122
	for (int i=0; i <= 6;++i) {
636cde3b Poul Sander 2025-05-15 21:40 123
		ImVec2 p1(i*50.0f, 0);
636cde3b Poul Sander 2025-05-15 21:40 124
		ImVec2 p2(i*50.0f, height);
636cde3b Poul Sander 2025-05-15 21:40 125
		LogicalToPhysical(window_resize, p1);
636cde3b Poul Sander 2025-05-15 21:40 126
		LogicalToPhysical(window_resize, p2);
636cde3b Poul Sander 2025-05-15 21:40 127
		p1.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 128
		p1.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 129
		p2.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 130
		p2.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 131
		ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
1fcc8881 Poul Sander 2025-05-15 18:54 132
	}
1fcc8881 Poul Sander 2025-05-15 18:54 133
	for (int i=0; i <= 12;++i) {
636cde3b Poul Sander 2025-05-15 21:40 134
		ImVec2 p1(0,i*50.0f);
636cde3b Poul Sander 2025-05-15 21:40 135
		ImVec2 p2(width, i*50.0f);
636cde3b Poul Sander 2025-05-15 21:40 136
		LogicalToPhysical(window_resize, p1);
636cde3b Poul Sander 2025-05-15 21:40 137
		LogicalToPhysical(window_resize, p2);
636cde3b Poul Sander 2025-05-15 21:40 138
		p1.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 139
		p1.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 140
		p2.x += xoffset;
636cde3b Poul Sander 2025-05-15 21:40 141
		p2.y += yoffset;
636cde3b Poul Sander 2025-05-15 21:40 142
		ImGui::GetWindowDrawList()->AddLine(p1, p2, IM_COL32(255, 0, 0, 100));
6046ca3f Poul Sander 2025-04-06 16:14 143
	}
1fcc8881 Poul Sander 2025-05-15 18:54 144
	ImGui::EndChild();
1fcc8881 Poul Sander 2025-05-15 18:54 145
	ImGui::End();
1fcc8881 Poul Sander 2025-05-15 18:54 146
1873b078 Poul Sander 2025-02-06 22:01 147
	ImGui::Begin("File list", nullptr, ImGuiWindowFlags_NoCollapse);
a80f6a78 Poul Sander 2025-04-04 18:34 148
	for (const auto& file : this->puzzle_files) {
a80f6a78 Poul Sander 2025-04-04 18:34 149
		if (ImGui::Selectable(file.c_str(), this->selected_file == file)) {
a80f6a78 Poul Sander 2025-04-04 18:34 150
			SelectFile(file);
a80f6a78 Poul Sander 2025-04-04 18:34 151
		}
1873b078 Poul Sander 2025-02-06 22:01 152
	}
1873b078 Poul Sander 2025-02-06 22:01 153
a80f6a78 Poul Sander 2025-04-04 18:34 154
a80f6a78 Poul Sander 2025-04-04 18:34 155
	ImGui::End();
a80f6a78 Poul Sander 2025-04-04 18:34 156
	ImGui::Begin("Puzzles in file");
a80f6a78 Poul Sander 2025-04-04 18:34 157
	int puzzle_count = PuzzleGetNumberOfPuzzles();
a80f6a78 Poul Sander 2025-04-04 18:34 158
	for (int i = 0; i < puzzle_count; ++i) {
6046ca3f Poul Sander 2025-04-06 16:14 159
		if (ImGui::Selectable(std::to_string(i).c_str(), this->selected_puzzle == i)) {
6046ca3f Poul Sander 2025-04-06 16:14 160
			this->selected_puzzle = i;
a80f6a78 Poul Sander 2025-04-04 18:34 161
		}
a80f6a78 Poul Sander 2025-04-04 18:34 162
	}
1873b078 Poul Sander 2025-02-06 22:01 163
	ImGui::End();
298835a8 Poul Sander 2025-05-31 20:49 164
298835a8 Poul Sander 2025-05-31 20:49 165
	ImGui::Begin("Palette");
298835a8 Poul Sander 2025-05-31 20:49 166
	if (ImGui::Selectable("Red", this->selected_action == 0)) {
298835a8 Poul Sander 2025-05-31 20:49 167
		this->selected_action = 0;
298835a8 Poul Sander 2025-05-31 20:49 168
	}
298835a8 Poul Sander 2025-05-31 20:49 169
	if (ImGui::Selectable("Blue", this->selected_action == 1)) {
298835a8 Poul Sander 2025-05-31 20:49 170
		this->selected_action = 1;
298835a8 Poul Sander 2025-05-31 20:49 171
	}
298835a8 Poul Sander 2025-05-31 20:49 172
	if (ImGui::Selectable("Clear", this->selected_action == selection_clear)) {
298835a8 Poul Sander 2025-05-31 20:49 173
		this->selected_action = selection_clear;
298835a8 Poul Sander 2025-05-31 20:49 174
	}
298835a8 Poul Sander 2025-05-31 20:49 175
	if (ImGui::Selectable("Move Up", this->selected_action == selection_move_up)) {
298835a8 Poul Sander 2025-05-31 20:49 176
		this->selected_action = selection_move_up;
298835a8 Poul Sander 2025-05-31 20:49 177
	}
298835a8 Poul Sander 2025-05-31 20:49 178
	if (ImGui::Selectable("Move down", this->selected_action == selection_move_down)) {
298835a8 Poul Sander 2025-05-31 20:49 179
		this->selected_action = selection_move_down;
298835a8 Poul Sander 2025-05-31 20:49 180
	}
298835a8 Poul Sander 2025-05-31 20:49 181
	if (ImGui::Selectable("Move left", this->selected_action == selection_move_left)) {
298835a8 Poul Sander 2025-05-31 20:49 182
		this->selected_action = selection_move_left;
298835a8 Poul Sander 2025-05-31 20:49 183
	}
298835a8 Poul Sander 2025-05-31 20:49 184
	if (ImGui::Selectable("Move right", this->selected_action == selection_move_right)) {
298835a8 Poul Sander 2025-05-31 20:49 185
		this->selected_action = selection_move_right;
298835a8 Poul Sander 2025-05-31 20:49 186
	}
298835a8 Poul Sander 2025-05-31 20:49 187
	ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 188
	ImGui::LabelText("moves allowed", "%i", PuzzleNumberOfMovesAllowed(this->selected_puzzle));
298835a8 Poul Sander 2025-05-31 20:49 189
	ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 190
	if (read_only) {
298835a8 Poul Sander 2025-05-31 20:49 191
		ImGui::LabelText("Read Only", "");
298835a8 Poul Sander 2025-05-31 20:49 192
		ImGui::Separator();
298835a8 Poul Sander 2025-05-31 20:49 193
	}
298835a8 Poul Sander 2025-05-31 20:49 194
	ImGui::End();
1873b078 Poul Sander 2025-02-06 22:01 195
}
1873b078 Poul Sander 2025-02-06 22:01 196
1873b078 Poul Sander 2025-02-06 22:01 197
void PuzzleEditorState::Update() {
1873b078 Poul Sander 2025-02-06 22:01 198
}
1873b078 Poul Sander 2025-02-06 22:01 199
1970-01-01 00:00 200