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