git repos / blockattack-game

blame: source/code/themes.cpp

normal view · raw

aaba4f1e Poul Sander 2023-07-25 13:34 1
/*
aaba4f1e Poul Sander 2023-07-25 13:34 2
===========================================================================
aaba4f1e Poul Sander 2023-07-25 13:34 3
blockattack - Block Attack - Rise of the Blocks
aaba4f1e Poul Sander 2023-07-25 13:34 4
Copyright (C) 2005-2023 Poul Sander
aaba4f1e Poul Sander 2023-07-25 13:34 5
aaba4f1e Poul Sander 2023-07-25 13:34 6
This program is free software: you can redistribute it and/or modify
aaba4f1e Poul Sander 2023-07-25 13:34 7
it under the terms of the GNU General Public License as published by
aaba4f1e Poul Sander 2023-07-25 13:34 8
the Free Software Foundation, either version 2 of the License, or
aaba4f1e Poul Sander 2023-07-25 13:34 9
(at your option) any later version.
aaba4f1e Poul Sander 2023-07-25 13:34 10
aaba4f1e Poul Sander 2023-07-25 13:34 11
This program is distributed in the hope that it will be useful,
aaba4f1e Poul Sander 2023-07-25 13:34 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
aaba4f1e Poul Sander 2023-07-25 13:34 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
aaba4f1e Poul Sander 2023-07-25 13:34 14
GNU General Public License for more details.
aaba4f1e Poul Sander 2023-07-25 13:34 15
aaba4f1e Poul Sander 2023-07-25 13:34 16
You should have received a copy of the GNU General Public License
aaba4f1e Poul Sander 2023-07-25 13:34 17
along with this program.  If not, see http://www.gnu.org/licenses/
aaba4f1e Poul Sander 2023-07-25 13:34 18
aaba4f1e Poul Sander 2023-07-25 13:34 19
Source information and contacts persons can be found at
aaba4f1e Poul Sander 2023-07-25 13:34 20
https://www.blockattack.net
aaba4f1e Poul Sander 2023-07-25 13:34 21
===========================================================================
aaba4f1e Poul Sander 2023-07-25 13:34 22
*/
aaba4f1e Poul Sander 2023-07-25 13:34 23
aaba4f1e Poul Sander 2023-07-25 13:34 24
aaba4f1e Poul Sander 2023-07-25 13:34 25
#include "themes.hpp"
2660ccea Poul Sander 2023-09-22 19:25 26
#include "sago/SagoMisc.hpp"
aaba4f1e Poul Sander 2023-07-25 13:34 27
#include <vector>
5b5139a9 Poul Sander 2024-02-21 20:32 28
#include <sstream>
00759e5a Poul Sander 2023-09-24 20:01 29
#include <map>
2660ccea Poul Sander 2023-09-22 19:25 30
#include "nlohmann/json.hpp"
b9eda2d4 Poul Sander 2023-10-04 20:16 31
#include <iostream>
b9eda2d4 Poul Sander 2023-10-04 20:16 32
#include "global.hpp"
7d88cb38 Poul Sander 2023-10-05 20:48 33
#include <boost/algorithm/string/predicate.hpp>
68c75938 Poul Sander 2023-11-13 23:03 34
#include <fmt/core.h>
2660ccea Poul Sander 2023-09-22 19:25 35
2660ccea Poul Sander 2023-09-22 19:25 36
using json = nlohmann::json;
2660ccea Poul Sander 2023-09-22 19:25 37
2660ccea Poul Sander 2023-09-22 19:25 38
7602f86d Poul Sander 2024-01-10 21:39 39
const int NUMBER_OF_CUSTOM_SLOTS = 2;
7602f86d Poul Sander 2024-01-10 21:39 40
7602f86d Poul Sander 2024-01-10 21:39 41
b9eda2d4 Poul Sander 2023-10-04 20:16 42
void to_json(json& j, const ImgScale& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 43
	switch (p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 44
	case ImgScale::Stretch:
b9eda2d4 Poul Sander 2023-10-04 20:16 45
		j = "stretch";
b9eda2d4 Poul Sander 2023-10-04 20:16 46
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 47
	case ImgScale::Tile:
b9eda2d4 Poul Sander 2023-10-04 20:16 48
		j = "tile";
b9eda2d4 Poul Sander 2023-10-04 20:16 49
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 50
	case ImgScale::Resize:
b9eda2d4 Poul Sander 2023-10-04 20:16 51
		j = "resize";
b9eda2d4 Poul Sander 2023-10-04 20:16 52
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 53
	case ImgScale::Cut:
b9eda2d4 Poul Sander 2023-10-04 20:16 54
		j = "cut";
b9eda2d4 Poul Sander 2023-10-04 20:16 55
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 56
	default:
b9eda2d4 Poul Sander 2023-10-04 20:16 57
		j = "stretch";
b9eda2d4 Poul Sander 2023-10-04 20:16 58
		break;
b9eda2d4 Poul Sander 2023-10-04 20:16 59
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 60
}
b9eda2d4 Poul Sander 2023-10-04 20:16 61
b9eda2d4 Poul Sander 2023-10-04 20:16 62
void from_json(const json& j, ImgScale& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 63
	std::string s = j;
b9eda2d4 Poul Sander 2023-10-04 20:16 64
	if (s == "stretch") {
b9eda2d4 Poul Sander 2023-10-04 20:16 65
		p = ImgScale::Stretch;
b9eda2d4 Poul Sander 2023-10-04 20:16 66
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 67
	else if (s == "tile") {
b9eda2d4 Poul Sander 2023-10-04 20:16 68
		p = ImgScale::Tile;
b9eda2d4 Poul Sander 2023-10-04 20:16 69
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 70
	else if (s == "resize") {
b9eda2d4 Poul Sander 2023-10-04 20:16 71
		p = ImgScale::Resize;
b9eda2d4 Poul Sander 2023-10-04 20:16 72
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 73
	else if (s == "cut") {
b9eda2d4 Poul Sander 2023-10-04 20:16 74
		p = ImgScale::Cut;
b9eda2d4 Poul Sander 2023-10-04 20:16 75
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 76
	else {
b9eda2d4 Poul Sander 2023-10-04 20:16 77
		p = ImgScale::Stretch;
b9eda2d4 Poul Sander 2023-10-04 20:16 78
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 79
}
b9eda2d4 Poul Sander 2023-10-04 20:16 80
2660ccea Poul Sander 2023-09-22 19:25 81
void to_json(json& j, const BackGroundData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 82
	j = json{ {"background_name", p.name}, {"background_sprite", p.background_sprite}, {"background_sprite_16x9", p.background_sprite_16x9}, {"background_scale", p.background_scale}, {"tileMoveSpeedX", p.tileMoveSpeedX}, {"tileMoveSpeedY", p.tileMoveSpeedY} };
2660ccea Poul Sander 2023-09-22 19:25 83
}
2660ccea Poul Sander 2023-09-22 19:25 84
2660ccea Poul Sander 2023-09-22 19:25 85
void to_json(json& j, const Theme& p) {
173b1d55 Poul Sander 2023-09-23 21:29 86
	j = json{ {"theme_name", p.theme_name}, {"back_board", p.back_board}, {"background_name", p.background.name}, {"decoration_name", p.decoration.name} };
2660ccea Poul Sander 2023-09-22 19:25 87
}
2660ccea Poul Sander 2023-09-22 19:25 88
00759e5a Poul Sander 2023-09-24 20:01 89
void to_json(json& j, const DecorationData& p) {
00759e5a Poul Sander 2023-09-24 20:01 90
	j = json{ {"name", p.name}, {"decoration_sprites", p.decoration_sprites} };
00759e5a Poul Sander 2023-09-24 20:01 91
}
00759e5a Poul Sander 2023-09-24 20:01 92
2660ccea Poul Sander 2023-09-22 19:25 93
void to_json(json& j, const ThemeFileData& p) {
00759e5a Poul Sander 2023-09-24 20:01 94
	j = json{ {"background_data", p.background_data}, {"decoration_data", p.decoration_data}, {"themes", p.themes} };
2660ccea Poul Sander 2023-09-22 19:25 95
}
2660ccea Poul Sander 2023-09-22 19:25 96
b9eda2d4 Poul Sander 2023-10-04 20:16 97
void from_json(const json& j, BackGroundData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 98
	j.at("background_name").get_to(p.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 99
	j.at("background_sprite").get_to(p.background_sprite);
b9eda2d4 Poul Sander 2023-10-04 20:16 100
	j.at("background_sprite_16x9").get_to(p.background_sprite_16x9);
b9eda2d4 Poul Sander 2023-10-04 20:16 101
	j.at("background_scale").get_to(p.background_scale);
b9eda2d4 Poul Sander 2023-10-04 20:16 102
	j.at("tileMoveSpeedX").get_to(p.tileMoveSpeedX);
b9eda2d4 Poul Sander 2023-10-04 20:16 103
	j.at("tileMoveSpeedY").get_to(p.tileMoveSpeedY);
b9eda2d4 Poul Sander 2023-10-04 20:16 104
}
b9eda2d4 Poul Sander 2023-10-04 20:16 105
b9eda2d4 Poul Sander 2023-10-04 20:16 106
void from_json(const json& j, Theme& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 107
	j.at("theme_name").get_to(p.theme_name);
b9eda2d4 Poul Sander 2023-10-04 20:16 108
	j.at("back_board").get_to(p.back_board);
b9eda2d4 Poul Sander 2023-10-04 20:16 109
	j.at("background_name").get_to(p.background.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 110
	j.at("decoration_name").get_to(p.decoration.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 111
}
b9eda2d4 Poul Sander 2023-10-04 20:16 112
b9eda2d4 Poul Sander 2023-10-04 20:16 113
void from_json(const json& j, DecorationData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 114
	j.at("name").get_to(p.name);
b9eda2d4 Poul Sander 2023-10-04 20:16 115
	j.at("decoration_sprites").get_to(p.decoration_sprites);
b9eda2d4 Poul Sander 2023-10-04 20:16 116
}
b9eda2d4 Poul Sander 2023-10-04 20:16 117
b9eda2d4 Poul Sander 2023-10-04 20:16 118
void from_json(const json& j, ThemeFileData& p) {
b9eda2d4 Poul Sander 2023-10-04 20:16 119
	if (j.contains("background_data")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 120
		j.at("background_data").get_to(p.background_data);
b9eda2d4 Poul Sander 2023-10-04 20:16 121
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 122
	if (j.contains("decoration_data")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 123
		j.at("decoration_data").get_to(p.decoration_data);
b9eda2d4 Poul Sander 2023-10-04 20:16 124
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 125
	if (j.contains("themes")) {
b9eda2d4 Poul Sander 2023-10-04 20:16 126
		j.at("themes").get_to(p.themes);
b9eda2d4 Poul Sander 2023-10-04 20:16 127
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 128
}
2660ccea Poul Sander 2023-09-22 19:25 129
2660ccea Poul Sander 2023-09-22 19:25 130
aaba4f1e Poul Sander 2023-07-25 13:34 131
aaba4f1e Poul Sander 2023-07-25 13:34 132
static std::vector<Theme> themes(1);
00759e5a Poul Sander 2023-09-24 20:01 133
static std::map<std::string, BackGroundData> background_data;
00759e5a Poul Sander 2023-09-24 20:01 134
static std::map<std::string, DecorationData> decoration_data;
5d5a5e92 Poul Sander 2024-03-06 21:41 135
static std::map<std::string, ThemeBorderData> border_data;
aaba4f1e Poul Sander 2023-07-25 13:34 136
static bool initialized = false;
aaba4f1e Poul Sander 2023-07-25 13:34 137
static size_t current_theme = 0;
aaba4f1e Poul Sander 2023-07-25 13:34 138
2660ccea Poul Sander 2023-09-22 19:25 139
66d6174b Poul Sander 2024-01-13 22:45 140
void ThemesFillMissingFields(Theme& theme) {
b9eda2d4 Poul Sander 2023-10-04 20:16 141
	if (theme.background.name.empty()) {
b9eda2d4 Poul Sander 2023-10-04 20:16 142
		//If the theme does not define a background then use the standard.
b9eda2d4 Poul Sander 2023-10-04 20:16 143
		theme.background.name = "standard";
b9eda2d4 Poul Sander 2023-10-04 20:16 144
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 145
	theme.background = background_data[theme.background.name];
b9eda2d4 Poul Sander 2023-10-04 20:16 146
}
b9eda2d4 Poul Sander 2023-10-04 20:16 147
51c4029b Poul Sander 2023-11-15 16:11 148
static void ThemesReadDataFromFile(const std::string& filename) {
b9eda2d4 Poul Sander 2023-10-04 20:16 149
	if (globalData.verboseLevel) {
b9eda2d4 Poul Sander 2023-10-04 20:16 150
		std::cout << "Reading theme data from " << filename << "\n";
b9eda2d4 Poul Sander 2023-10-04 20:16 151
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 152
	std::string s = sago::GetFileContent(filename);
68c75938 Poul Sander 2023-11-13 23:03 153
	if (s.empty()) {
68c75938 Poul Sander 2023-11-13 23:03 154
		std::cout << "File \"" << filename << "\" does not exit!\n";
68c75938 Poul Sander 2023-11-13 23:03 155
		return;
68c75938 Poul Sander 2023-11-13 23:03 156
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 157
	json j = json::parse(s);
b9eda2d4 Poul Sander 2023-10-04 20:16 158
	ThemeFileData tfd = j;
b9eda2d4 Poul Sander 2023-10-04 20:16 159
	for (auto& bg : tfd.background_data) {
b9eda2d4 Poul Sander 2023-10-04 20:16 160
		background_data[bg.name] = bg;
b9eda2d4 Poul Sander 2023-10-04 20:16 161
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 162
	for (auto& dec : tfd.decoration_data) {
b9eda2d4 Poul Sander 2023-10-04 20:16 163
		decoration_data[dec.name] = dec;
b9eda2d4 Poul Sander 2023-10-04 20:16 164
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 165
	for (const Theme& theme : tfd.themes) {
b9eda2d4 Poul Sander 2023-10-04 20:16 166
		if (globalData.verboseLevel) {
b9eda2d4 Poul Sander 2023-10-04 20:16 167
			std::cout << "Adding theme " << theme.theme_name << "\n";
b9eda2d4 Poul Sander 2023-10-04 20:16 168
		}
b9eda2d4 Poul Sander 2023-10-04 20:16 169
		themes.push_back(theme);
51c4029b Poul Sander 2023-11-15 16:11 170
		ThemesFillMissingFields(themes.back());
b9eda2d4 Poul Sander 2023-10-04 20:16 171
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 172
}
b9eda2d4 Poul Sander 2023-10-04 20:16 173
b9eda2d4 Poul Sander 2023-10-04 20:16 174
51c4029b Poul Sander 2023-11-15 16:11 175
static void ThemesInitBackGroundData() {
081d847a Poul Sander 2023-07-29 19:59 176
	BackGroundData standard;
173b1d55 Poul Sander 2023-09-23 21:29 177
	standard.name = "standard";
081d847a Poul Sander 2023-07-29 19:59 178
	standard.background_sprite = "background";
081d847a Poul Sander 2023-07-29 19:59 179
	standard.background_sprite_16x9 = "background_sixteen_nine";
081d847a Poul Sander 2023-07-29 19:59 180
	standard.background_scale = ImgScale::Stretch;
00759e5a Poul Sander 2023-09-24 20:01 181
	background_data[standard.name] = standard;
00759e5a Poul Sander 2023-09-24 20:01 182
	DecorationData smileys;
00759e5a Poul Sander 2023-09-24 20:01 183
	decoration_data[smileys.name] = smileys;
081d847a Poul Sander 2023-07-29 19:59 184
}
081d847a Poul Sander 2023-07-29 19:59 185
5d5a5e92 Poul Sander 2024-03-06 21:41 186
static void ThemesInitBorderData() {
5d5a5e92 Poul Sander 2024-03-06 21:41 187
	ThemeBorderData standard;
5d5a5e92 Poul Sander 2024-03-06 21:41 188
	standard.name = "standard";
5d5a5e92 Poul Sander 2024-03-06 21:41 189
	standard.border_sprite = "board_back_back";
5d5a5e92 Poul Sander 2024-03-06 21:41 190
	standard.border_sprite_offset = {-60, -68};
5d5a5e92 Poul Sander 2024-03-06 21:41 191
	border_data[standard.name] = standard;
5d5a5e92 Poul Sander 2024-03-06 21:41 192
	ThemeBorderData mirror = standard;
5d5a5e92 Poul Sander 2024-03-06 21:41 193
	mirror.name = "mirror";
5d5a5e92 Poul Sander 2024-03-06 21:41 194
	mirror.border_sprite = "board_back_back_mirror";
5d5a5e92 Poul Sander 2024-03-06 21:41 195
	mirror.border_sprite_offset = {-116, -68};
5d5a5e92 Poul Sander 2024-03-06 21:41 196
	mirror.score_label_offset = {-100, 80};
5d5a5e92 Poul Sander 2024-03-06 21:41 197
	mirror.time_label_offset = {-100,129};
5d5a5e92 Poul Sander 2024-03-06 21:41 198
	mirror.chain_label_offset = {-100, 178};
5d5a5e92 Poul Sander 2024-03-06 21:41 199
	mirror.speed_label_offset = {-100, 227};
5d5a5e92 Poul Sander 2024-03-06 21:41 200
	border_data[mirror.name] = mirror;
5d5a5e92 Poul Sander 2024-03-06 21:41 201
}
5d5a5e92 Poul Sander 2024-03-06 21:41 202
68c75938 Poul Sander 2023-11-13 23:03 203
static bool str_startswith(const std::string& s, const char* prefix) {
68c75938 Poul Sander 2023-11-13 23:03 204
	return (s.rfind(prefix, 0) == 0);
68c75938 Poul Sander 2023-11-13 23:03 205
}
2660ccea Poul Sander 2023-09-22 19:25 206
51c4029b Poul Sander 2023-11-15 16:11 207
static void ThemesDumpData() {
2660ccea Poul Sander 2023-09-22 19:25 208
	ThemeFileData tfd;
2660ccea Poul Sander 2023-09-22 19:25 209
	tfd.themes = themes;
6d46a5a3 Poul Sander 2023-09-23 14:42 210
	for (auto& pair : background_data) {
2660ccea Poul Sander 2023-09-22 19:25 211
		tfd.background_data.push_back(pair.second);
081d847a Poul Sander 2023-07-29 19:59 212
	}
00759e5a Poul Sander 2023-09-24 20:01 213
	for (auto& pair : decoration_data) {
00759e5a Poul Sander 2023-09-24 20:01 214
		tfd.decoration_data.push_back(pair.second);
00759e5a Poul Sander 2023-09-24 20:01 215
	}
2660ccea Poul Sander 2023-09-22 19:25 216
	json j = tfd;
2660ccea Poul Sander 2023-09-22 19:25 217
	std::string s = j.dump(4);
2660ccea Poul Sander 2023-09-22 19:25 218
	sago::WriteFileContent("themes_dump.json", s);
081d847a Poul Sander 2023-07-29 19:59 219
}
081d847a Poul Sander 2023-07-29 19:59 220
68c75938 Poul Sander 2023-11-13 23:03 221
void ThemesSaveCustomSlots() {
68c75938 Poul Sander 2023-11-13 23:03 222
	ThemeFileData tfd;
68c75938 Poul Sander 2023-11-13 23:03 223
	for (const Theme& theme : themes) {
68c75938 Poul Sander 2023-11-13 23:03 224
		if ( str_startswith(theme.theme_name,"custom_slot_")) {
68c75938 Poul Sander 2023-11-13 23:03 225
			tfd.themes.push_back(theme);
68c75938 Poul Sander 2023-11-13 23:03 226
		}
68c75938 Poul Sander 2023-11-13 23:03 227
	}
68c75938 Poul Sander 2023-11-13 23:03 228
	json j = tfd;
68c75938 Poul Sander 2023-11-13 23:03 229
	std::string s = j.dump(4);
68c75938 Poul Sander 2023-11-13 23:03 230
	sago::WriteFileContent("custom_themes.json", s);
68c75938 Poul Sander 2023-11-13 23:03 231
}
68c75938 Poul Sander 2023-11-13 23:03 232
68c75938 Poul Sander 2023-11-13 23:03 233
static void ThemesAddCustomSlot(std::vector<Theme>& themes, int slot_number) {
68c75938 Poul Sander 2023-11-13 23:03 234
	std::string slot_name = fmt::format("custom_slot_{}", slot_number);
68c75938 Poul Sander 2023-11-13 23:03 235
	for (const Theme& theme : themes) {
68c75938 Poul Sander 2023-11-13 23:03 236
		if (theme.theme_name == slot_name) {
68c75938 Poul Sander 2023-11-13 23:03 237
			//Custom slot already exists
68c75938 Poul Sander 2023-11-13 23:03 238
			return;
68c75938 Poul Sander 2023-11-13 23:03 239
		}
68c75938 Poul Sander 2023-11-13 23:03 240
	}
68c75938 Poul Sander 2023-11-13 23:03 241
	Theme new_theme;
68c75938 Poul Sander 2023-11-13 23:03 242
	new_theme.theme_name = slot_name;
68c75938 Poul Sander 2023-11-13 23:03 243
	themes.push_back(new_theme);
51c4029b Poul Sander 2023-11-15 16:11 244
	ThemesFillMissingFields(themes.back());
68c75938 Poul Sander 2023-11-13 23:03 245
}
68c75938 Poul Sander 2023-11-13 23:03 246
51c4029b Poul Sander 2023-11-15 16:11 247
void ThemesInit() {
aaba4f1e Poul Sander 2023-07-25 13:34 248
	if (initialized) {
aaba4f1e Poul Sander 2023-07-25 13:34 249
		return;
aaba4f1e Poul Sander 2023-07-25 13:34 250
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 251
	initialized = true;
51c4029b Poul Sander 2023-11-15 16:11 252
	ThemesInitBackGroundData();
5b5139a9 Poul Sander 2024-02-21 20:32 253
	ThemesInitCustomBackgrounds();
5d5a5e92 Poul Sander 2024-03-06 21:41 254
	ThemesInitBorderData();
aaba4f1e Poul Sander 2023-07-25 13:34 255
	themes.resize(1);  //Add the default theme
51c4029b Poul Sander 2023-11-15 16:11 256
	ThemesFillMissingFields(themes[0]);
7d88cb38 Poul Sander 2023-10-05 20:48 257
	const std::vector<std::string>& theme_files = sago::GetFileList("themes");
7d88cb38 Poul Sander 2023-10-05 20:48 258
	for (const std::string& filename : theme_files) {
7d88cb38 Poul Sander 2023-10-05 20:48 259
		if (boost::algorithm::ends_with(filename,".json")) {
51c4029b Poul Sander 2023-11-15 16:11 260
			ThemesReadDataFromFile("themes/"+filename);
7d88cb38 Poul Sander 2023-10-05 20:48 261
		}
7d88cb38 Poul Sander 2023-10-05 20:48 262
	}
51c4029b Poul Sander 2023-11-15 16:11 263
	ThemesReadDataFromFile("custom_themes.json");
7602f86d Poul Sander 2024-01-10 21:39 264
	for (int i=1; i <= NUMBER_OF_CUSTOM_SLOTS; ++i) {
68c75938 Poul Sander 2023-11-13 23:03 265
		ThemesAddCustomSlot(themes, i);
68c75938 Poul Sander 2023-11-13 23:03 266
	}
51c4029b Poul Sander 2023-11-15 16:11 267
	ThemesDumpData();
aaba4f1e Poul Sander 2023-07-25 13:34 268
}
aaba4f1e Poul Sander 2023-07-25 13:34 269
51c4029b Poul Sander 2023-11-15 16:11 270
Theme ThemesGetNext() {
51c4029b Poul Sander 2023-11-15 16:11 271
	ThemesInit();
aaba4f1e Poul Sander 2023-07-25 13:34 272
	current_theme++;
aaba4f1e Poul Sander 2023-07-25 13:34 273
	current_theme = current_theme % themes.size();
aaba4f1e Poul Sander 2023-07-25 13:34 274
	return themes.at(current_theme);
aaba4f1e Poul Sander 2023-07-25 13:34 275
}
081d847a Poul Sander 2023-07-29 19:59 276
7602f86d Poul Sander 2024-01-10 21:39 277
Theme& ThemesGet(size_t theme_number) {
51c4029b Poul Sander 2023-11-15 16:11 278
	ThemesInit();
081d847a Poul Sander 2023-07-29 19:59 279
	return themes.at(theme_number % themes.size());
081d847a Poul Sander 2023-07-29 19:59 280
}
7602f86d Poul Sander 2024-01-10 21:39 281
7602f86d Poul Sander 2024-01-10 21:39 282
size_t ThemeGetNumber(const std::string& name) {
7602f86d Poul Sander 2024-01-10 21:39 283
	size_t ret = 0;  // Default to 0
7602f86d Poul Sander 2024-01-10 21:39 284
	ThemesInit();
7602f86d Poul Sander 2024-01-10 21:39 285
	for (size_t i = 0; i < themes.size(); ++i) {
7602f86d Poul Sander 2024-01-10 21:39 286
		if (themes[i].theme_name == name) {
7602f86d Poul Sander 2024-01-10 21:39 287
			ret = i;
7602f86d Poul Sander 2024-01-10 21:39 288
		}
7602f86d Poul Sander 2024-01-10 21:39 289
	}
7602f86d Poul Sander 2024-01-10 21:39 290
	return ret;
7602f86d Poul Sander 2024-01-10 21:39 291
}
66d6174b Poul Sander 2024-01-13 22:45 292
66d6174b Poul Sander 2024-01-13 22:45 293
void ThemesAddOrReplace(const Theme& theme) {
66d6174b Poul Sander 2024-01-13 22:45 294
	ThemesInit();
66d6174b Poul Sander 2024-01-13 22:45 295
	for (size_t i = 0; i < themes.size(); ++i) {
66d6174b Poul Sander 2024-01-13 22:45 296
		if (themes[i].theme_name == theme.theme_name) {
66d6174b Poul Sander 2024-01-13 22:45 297
			themes[i] = theme;
66d6174b Poul Sander 2024-01-13 22:45 298
			return;
66d6174b Poul Sander 2024-01-13 22:45 299
		}
66d6174b Poul Sander 2024-01-13 22:45 300
	}
66d6174b Poul Sander 2024-01-13 22:45 301
	themes.push_back(theme);
66d6174b Poul Sander 2024-01-13 22:45 302
}
66d6174b Poul Sander 2024-01-13 22:45 303
66d6174b Poul Sander 2024-01-13 22:45 304
BackGroundData ThemesGetNextBackground(const std::string& current) {
66d6174b Poul Sander 2024-01-13 22:45 305
	ThemesInit();
66d6174b Poul Sander 2024-01-13 22:45 306
	BackGroundData ret = background_data["standard"];
66d6174b Poul Sander 2024-01-13 22:45 307
	for (auto& pair : background_data) {
66d6174b Poul Sander 2024-01-13 22:45 308
		if (pair.first == current) {
66d6174b Poul Sander 2024-01-13 22:45 309
			auto it = background_data.find(current);
66d6174b Poul Sander 2024-01-13 22:45 310
			++it;
66d6174b Poul Sander 2024-01-13 22:45 311
			if (it == background_data.end()) {
66d6174b Poul Sander 2024-01-13 22:45 312
				it = background_data.begin();
66d6174b Poul Sander 2024-01-13 22:45 313
			}
66d6174b Poul Sander 2024-01-13 22:45 314
			ret = it->second;
66d6174b Poul Sander 2024-01-13 22:45 315
			break;
66d6174b Poul Sander 2024-01-13 22:45 316
		}
66d6174b Poul Sander 2024-01-13 22:45 317
	}
66d6174b Poul Sander 2024-01-13 22:45 318
	return ret;
66d6174b Poul Sander 2024-01-13 22:45 319
}
de41f458 Poul Sander 2024-01-29 20:53 320
de41f458 Poul Sander 2024-01-29 20:53 321
std::string ThemesGetNextBoardBackground(const std::string& current) {
de41f458 Poul Sander 2024-01-29 20:53 322
	ThemesInit();
de41f458 Poul Sander 2024-01-29 20:53 323
	std::vector<std::string> back_boards = {"back_board", "back_board_sample_snow", "trans_cover"};
de41f458 Poul Sander 2024-01-29 20:53 324
	std::string ret = "back_board";
de41f458 Poul Sander 2024-01-29 20:53 325
	for (size_t i = 0; i < back_boards.size(); ++i) {
de41f458 Poul Sander 2024-01-29 20:53 326
		if (back_boards[i] == current) {
de41f458 Poul Sander 2024-01-29 20:53 327
			ret = back_boards[(i + 1) % back_boards.size()];
de41f458 Poul Sander 2024-01-29 20:53 328
			break;
de41f458 Poul Sander 2024-01-29 20:53 329
		}
de41f458 Poul Sander 2024-01-29 20:53 330
	}
de41f458 Poul Sander 2024-01-29 20:53 331
	return ret;
de41f458 Poul Sander 2024-01-29 20:53 332
}
5b5139a9 Poul Sander 2024-02-21 20:32 333
5d5a5e92 Poul Sander 2024-03-06 21:41 334
ThemeBorderData ThemesGetBorder(const std::string& name) {
5d5a5e92 Poul Sander 2024-03-06 21:41 335
	ThemesInit();
5d5a5e92 Poul Sander 2024-03-06 21:41 336
	auto it = border_data.find(name);
5d5a5e92 Poul Sander 2024-03-06 21:41 337
	if (it == border_data.end()) {
5d5a5e92 Poul Sander 2024-03-06 21:41 338
		return border_data["standard"];
5d5a5e92 Poul Sander 2024-03-06 21:41 339
	}
5d5a5e92 Poul Sander 2024-03-06 21:41 340
	return it->second;
5d5a5e92 Poul Sander 2024-03-06 21:41 341
}
5d5a5e92 Poul Sander 2024-03-06 21:41 342
5d5a5e92 Poul Sander 2024-03-06 21:41 343
ThemeBorderData ThemesGetNextBorder(const std::string& current) {
5d5a5e92 Poul Sander 2024-03-06 21:41 344
	ThemesInit();
5d5a5e92 Poul Sander 2024-03-06 21:41 345
	ThemeBorderData ret = border_data["standard"];
5d5a5e92 Poul Sander 2024-03-06 21:41 346
	for (auto& pair : border_data) {
5d5a5e92 Poul Sander 2024-03-06 21:41 347
		if (pair.first == current) {
5d5a5e92 Poul Sander 2024-03-06 21:41 348
			auto it = border_data.find(current);
5d5a5e92 Poul Sander 2024-03-06 21:41 349
			++it;
5d5a5e92 Poul Sander 2024-03-06 21:41 350
			if (it == border_data.end()) {
5d5a5e92 Poul Sander 2024-03-06 21:41 351
				it = border_data.begin();
5d5a5e92 Poul Sander 2024-03-06 21:41 352
			}
5d5a5e92 Poul Sander 2024-03-06 21:41 353
			ret = it->second;
5d5a5e92 Poul Sander 2024-03-06 21:41 354
			break;
5d5a5e92 Poul Sander 2024-03-06 21:41 355
		}
5d5a5e92 Poul Sander 2024-03-06 21:41 356
	}
5d5a5e92 Poul Sander 2024-03-06 21:41 357
	return ret;
5d5a5e92 Poul Sander 2024-03-06 21:41 358
}
5d5a5e92 Poul Sander 2024-03-06 21:41 359
5d5a5e92 Poul Sander 2024-03-06 21:41 360
5b5139a9 Poul Sander 2024-02-21 20:32 361
void ThemesInitCustomBackgrounds() {
5b5139a9 Poul Sander 2024-02-21 20:32 362
	std::vector<std::string> custom_backgrounds = sago::GetFileList("textures/backgrounds");
5b5139a9 Poul Sander 2024-02-21 20:32 363
	std::stringstream sprite_stream;
5b5139a9 Poul Sander 2024-02-21 20:32 364
	sprite_stream << "{\n";
5b5139a9 Poul Sander 2024-02-21 20:32 365
	bool first = true;
5b5139a9 Poul Sander 2024-02-21 20:32 366
	for (const std::string& filename : custom_backgrounds) {
5b5139a9 Poul Sander 2024-02-21 20:32 367
		std::cout << "Found custom background " << filename << "\n";
5b5139a9 Poul Sander 2024-02-21 20:32 368
		if (boost::algorithm::ends_with(filename, ".png") || boost::algorithm::ends_with(filename, ".jpg")) {
5b5139a9 Poul Sander 2024-02-21 20:32 369
			BackGroundData bg;
5b5139a9 Poul Sander 2024-02-21 20:32 370
			bg.name = filename;
5b5139a9 Poul Sander 2024-02-21 20:32 371
			std::string texture_name = fmt::format("custom_background_{}",filename);;
5b5139a9 Poul Sander 2024-02-21 20:32 372
			bg.background_sprite = texture_name;
5b5139a9 Poul Sander 2024-02-21 20:32 373
			bg.background_sprite_16x9 = texture_name;
5b5139a9 Poul Sander 2024-02-21 20:32 374
			bg.background_scale = ImgScale::Resize;
5b5139a9 Poul Sander 2024-02-21 20:32 375
			background_data[bg.name] = bg;
5b5139a9 Poul Sander 2024-02-21 20:32 376
			if (!first) {
5b5139a9 Poul Sander 2024-02-21 20:32 377
				sprite_stream << ",\n";
5b5139a9 Poul Sander 2024-02-21 20:32 378
			}
5b5139a9 Poul Sander 2024-02-21 20:32 379
			sprite_stream << "\"" << texture_name << "\": {\n";
5b5139a9 Poul Sander 2024-02-21 20:32 380
			sprite_stream << "  \"texture\": \"" << "backgrounds/" << filename.substr(0, filename.size()-4) << "\",\n";
5b5139a9 Poul Sander 2024-02-21 20:32 381
			sprite_stream << "  \"topx\": 0,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 382
			sprite_stream << "  \"topy\": 0,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 383
			sprite_stream << "  \"height\": 7680,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 384
			sprite_stream << "  \"width\": 10240,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 385
			sprite_stream << "  \"number_of_frames\": 1,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 386
			sprite_stream << "  \"frame_time\": 1\n";
5b5139a9 Poul Sander 2024-02-21 20:32 387
			sprite_stream << "}\n";
5b5139a9 Poul Sander 2024-02-21 20:32 388
			first = false;
5b5139a9 Poul Sander 2024-02-21 20:32 389
5b5139a9 Poul Sander 2024-02-21 20:32 390
		}
5b5139a9 Poul Sander 2024-02-21 20:32 391
	}
5b5139a9 Poul Sander 2024-02-21 20:32 392
	sprite_stream << "}\n";
5b5139a9 Poul Sander 2024-02-21 20:32 393
	sago::WriteFileContent("sprites/custom_backgrounds.sprite", sprite_stream.str());
5b5139a9 Poul Sander 2024-02-21 20:32 394
}