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;
aaba4f1e Poul Sander 2023-07-25 13:34 135
static bool initialized = false;
aaba4f1e Poul Sander 2023-07-25 13:34 136
static size_t current_theme = 0;
aaba4f1e Poul Sander 2023-07-25 13:34 137
2660ccea Poul Sander 2023-09-22 19:25 138
66d6174b Poul Sander 2024-01-13 22:45 139
void ThemesFillMissingFields(Theme& theme) {
b9eda2d4 Poul Sander 2023-10-04 20:16 140
	if (theme.background.name.empty()) {
b9eda2d4 Poul Sander 2023-10-04 20:16 141
		//If the theme does not define a background then use the standard.
b9eda2d4 Poul Sander 2023-10-04 20:16 142
		theme.background.name = "standard";
b9eda2d4 Poul Sander 2023-10-04 20:16 143
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 144
	theme.background = background_data[theme.background.name];
b9eda2d4 Poul Sander 2023-10-04 20:16 145
}
b9eda2d4 Poul Sander 2023-10-04 20:16 146
51c4029b Poul Sander 2023-11-15 16:11 147
static void ThemesReadDataFromFile(const std::string& filename) {
b9eda2d4 Poul Sander 2023-10-04 20:16 148
	if (globalData.verboseLevel) {
b9eda2d4 Poul Sander 2023-10-04 20:16 149
		std::cout << "Reading theme data from " << filename << "\n";
b9eda2d4 Poul Sander 2023-10-04 20:16 150
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 151
	std::string s = sago::GetFileContent(filename);
68c75938 Poul Sander 2023-11-13 23:03 152
	if (s.empty()) {
68c75938 Poul Sander 2023-11-13 23:03 153
		std::cout << "File \"" << filename << "\" does not exit!\n";
68c75938 Poul Sander 2023-11-13 23:03 154
		return;
68c75938 Poul Sander 2023-11-13 23:03 155
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 156
	json j = json::parse(s);
b9eda2d4 Poul Sander 2023-10-04 20:16 157
	ThemeFileData tfd = j;
b9eda2d4 Poul Sander 2023-10-04 20:16 158
	for (auto& bg : tfd.background_data) {
b9eda2d4 Poul Sander 2023-10-04 20:16 159
		background_data[bg.name] = bg;
b9eda2d4 Poul Sander 2023-10-04 20:16 160
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 161
	for (auto& dec : tfd.decoration_data) {
b9eda2d4 Poul Sander 2023-10-04 20:16 162
		decoration_data[dec.name] = dec;
b9eda2d4 Poul Sander 2023-10-04 20:16 163
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 164
	for (const Theme& theme : tfd.themes) {
b9eda2d4 Poul Sander 2023-10-04 20:16 165
		if (globalData.verboseLevel) {
b9eda2d4 Poul Sander 2023-10-04 20:16 166
			std::cout << "Adding theme " << theme.theme_name << "\n";
b9eda2d4 Poul Sander 2023-10-04 20:16 167
		}
b9eda2d4 Poul Sander 2023-10-04 20:16 168
		themes.push_back(theme);
51c4029b Poul Sander 2023-11-15 16:11 169
		ThemesFillMissingFields(themes.back());
b9eda2d4 Poul Sander 2023-10-04 20:16 170
	}
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
51c4029b Poul Sander 2023-11-15 16:11 174
static void ThemesInitBackGroundData() {
081d847a Poul Sander 2023-07-29 19:59 175
	BackGroundData standard;
173b1d55 Poul Sander 2023-09-23 21:29 176
	standard.name = "standard";
081d847a Poul Sander 2023-07-29 19:59 177
	standard.background_sprite = "background";
081d847a Poul Sander 2023-07-29 19:59 178
	standard.background_sprite_16x9 = "background_sixteen_nine";
081d847a Poul Sander 2023-07-29 19:59 179
	standard.background_scale = ImgScale::Stretch;
00759e5a Poul Sander 2023-09-24 20:01 180
	background_data[standard.name] = standard;
00759e5a Poul Sander 2023-09-24 20:01 181
	DecorationData smileys;
00759e5a Poul Sander 2023-09-24 20:01 182
	decoration_data[smileys.name] = smileys;
081d847a Poul Sander 2023-07-29 19:59 183
}
081d847a Poul Sander 2023-07-29 19:59 184
68c75938 Poul Sander 2023-11-13 23:03 185
static bool str_startswith(const std::string& s, const char* prefix) {
68c75938 Poul Sander 2023-11-13 23:03 186
	return (s.rfind(prefix, 0) == 0);
68c75938 Poul Sander 2023-11-13 23:03 187
}
2660ccea Poul Sander 2023-09-22 19:25 188
51c4029b Poul Sander 2023-11-15 16:11 189
static void ThemesDumpData() {
2660ccea Poul Sander 2023-09-22 19:25 190
	ThemeFileData tfd;
2660ccea Poul Sander 2023-09-22 19:25 191
	tfd.themes = themes;
6d46a5a3 Poul Sander 2023-09-23 14:42 192
	for (auto& pair : background_data) {
2660ccea Poul Sander 2023-09-22 19:25 193
		tfd.background_data.push_back(pair.second);
081d847a Poul Sander 2023-07-29 19:59 194
	}
00759e5a Poul Sander 2023-09-24 20:01 195
	for (auto& pair : decoration_data) {
00759e5a Poul Sander 2023-09-24 20:01 196
		tfd.decoration_data.push_back(pair.second);
00759e5a Poul Sander 2023-09-24 20:01 197
	}
2660ccea Poul Sander 2023-09-22 19:25 198
	json j = tfd;
2660ccea Poul Sander 2023-09-22 19:25 199
	std::string s = j.dump(4);
2660ccea Poul Sander 2023-09-22 19:25 200
	sago::WriteFileContent("themes_dump.json", s);
081d847a Poul Sander 2023-07-29 19:59 201
}
081d847a Poul Sander 2023-07-29 19:59 202
68c75938 Poul Sander 2023-11-13 23:03 203
void ThemesSaveCustomSlots() {
68c75938 Poul Sander 2023-11-13 23:03 204
	ThemeFileData tfd;
68c75938 Poul Sander 2023-11-13 23:03 205
	for (const Theme& theme : themes) {
68c75938 Poul Sander 2023-11-13 23:03 206
		if ( str_startswith(theme.theme_name,"custom_slot_")) {
68c75938 Poul Sander 2023-11-13 23:03 207
			tfd.themes.push_back(theme);
68c75938 Poul Sander 2023-11-13 23:03 208
		}
68c75938 Poul Sander 2023-11-13 23:03 209
	}
68c75938 Poul Sander 2023-11-13 23:03 210
	json j = tfd;
68c75938 Poul Sander 2023-11-13 23:03 211
	std::string s = j.dump(4);
68c75938 Poul Sander 2023-11-13 23:03 212
	sago::WriteFileContent("custom_themes.json", s);
68c75938 Poul Sander 2023-11-13 23:03 213
}
68c75938 Poul Sander 2023-11-13 23:03 214
68c75938 Poul Sander 2023-11-13 23:03 215
static void ThemesAddCustomSlot(std::vector<Theme>& themes, int slot_number) {
68c75938 Poul Sander 2023-11-13 23:03 216
	std::string slot_name = fmt::format("custom_slot_{}", slot_number);
68c75938 Poul Sander 2023-11-13 23:03 217
	for (const Theme& theme : themes) {
68c75938 Poul Sander 2023-11-13 23:03 218
		if (theme.theme_name == slot_name) {
68c75938 Poul Sander 2023-11-13 23:03 219
			//Custom slot already exists
68c75938 Poul Sander 2023-11-13 23:03 220
			return;
68c75938 Poul Sander 2023-11-13 23:03 221
		}
68c75938 Poul Sander 2023-11-13 23:03 222
	}
68c75938 Poul Sander 2023-11-13 23:03 223
	Theme new_theme;
68c75938 Poul Sander 2023-11-13 23:03 224
	new_theme.theme_name = slot_name;
68c75938 Poul Sander 2023-11-13 23:03 225
	themes.push_back(new_theme);
51c4029b Poul Sander 2023-11-15 16:11 226
	ThemesFillMissingFields(themes.back());
68c75938 Poul Sander 2023-11-13 23:03 227
}
68c75938 Poul Sander 2023-11-13 23:03 228
51c4029b Poul Sander 2023-11-15 16:11 229
void ThemesInit() {
aaba4f1e Poul Sander 2023-07-25 13:34 230
	if (initialized) {
aaba4f1e Poul Sander 2023-07-25 13:34 231
		return;
aaba4f1e Poul Sander 2023-07-25 13:34 232
	}
b9eda2d4 Poul Sander 2023-10-04 20:16 233
	initialized = true;
51c4029b Poul Sander 2023-11-15 16:11 234
	ThemesInitBackGroundData();
5b5139a9 Poul Sander 2024-02-21 20:32 235
	ThemesInitCustomBackgrounds();
aaba4f1e Poul Sander 2023-07-25 13:34 236
	themes.resize(1);  //Add the default theme
51c4029b Poul Sander 2023-11-15 16:11 237
	ThemesFillMissingFields(themes[0]);
7d88cb38 Poul Sander 2023-10-05 20:48 238
	const std::vector<std::string>& theme_files = sago::GetFileList("themes");
7d88cb38 Poul Sander 2023-10-05 20:48 239
	for (const std::string& filename : theme_files) {
7d88cb38 Poul Sander 2023-10-05 20:48 240
		if (boost::algorithm::ends_with(filename,".json")) {
51c4029b Poul Sander 2023-11-15 16:11 241
			ThemesReadDataFromFile("themes/"+filename);
7d88cb38 Poul Sander 2023-10-05 20:48 242
		}
7d88cb38 Poul Sander 2023-10-05 20:48 243
	}
51c4029b Poul Sander 2023-11-15 16:11 244
	ThemesReadDataFromFile("custom_themes.json");
7602f86d Poul Sander 2024-01-10 21:39 245
	for (int i=1; i <= NUMBER_OF_CUSTOM_SLOTS; ++i) {
68c75938 Poul Sander 2023-11-13 23:03 246
		ThemesAddCustomSlot(themes, i);
68c75938 Poul Sander 2023-11-13 23:03 247
	}
51c4029b Poul Sander 2023-11-15 16:11 248
	ThemesDumpData();
aaba4f1e Poul Sander 2023-07-25 13:34 249
}
aaba4f1e Poul Sander 2023-07-25 13:34 250
51c4029b Poul Sander 2023-11-15 16:11 251
Theme ThemesGetNext() {
51c4029b Poul Sander 2023-11-15 16:11 252
	ThemesInit();
aaba4f1e Poul Sander 2023-07-25 13:34 253
	current_theme++;
aaba4f1e Poul Sander 2023-07-25 13:34 254
	current_theme = current_theme % themes.size();
aaba4f1e Poul Sander 2023-07-25 13:34 255
	return themes.at(current_theme);
aaba4f1e Poul Sander 2023-07-25 13:34 256
}
081d847a Poul Sander 2023-07-29 19:59 257
7602f86d Poul Sander 2024-01-10 21:39 258
Theme& ThemesGet(size_t theme_number) {
51c4029b Poul Sander 2023-11-15 16:11 259
	ThemesInit();
081d847a Poul Sander 2023-07-29 19:59 260
	return themes.at(theme_number % themes.size());
081d847a Poul Sander 2023-07-29 19:59 261
}
7602f86d Poul Sander 2024-01-10 21:39 262
7602f86d Poul Sander 2024-01-10 21:39 263
size_t ThemeGetNumber(const std::string& name) {
7602f86d Poul Sander 2024-01-10 21:39 264
	size_t ret = 0;  // Default to 0
7602f86d Poul Sander 2024-01-10 21:39 265
	ThemesInit();
7602f86d Poul Sander 2024-01-10 21:39 266
	for (size_t i = 0; i < themes.size(); ++i) {
7602f86d Poul Sander 2024-01-10 21:39 267
		if (themes[i].theme_name == name) {
7602f86d Poul Sander 2024-01-10 21:39 268
			ret = i;
7602f86d Poul Sander 2024-01-10 21:39 269
		}
7602f86d Poul Sander 2024-01-10 21:39 270
	}
7602f86d Poul Sander 2024-01-10 21:39 271
	return ret;
7602f86d Poul Sander 2024-01-10 21:39 272
}
66d6174b Poul Sander 2024-01-13 22:45 273
66d6174b Poul Sander 2024-01-13 22:45 274
void ThemesAddOrReplace(const Theme& theme) {
66d6174b Poul Sander 2024-01-13 22:45 275
	ThemesInit();
66d6174b Poul Sander 2024-01-13 22:45 276
	for (size_t i = 0; i < themes.size(); ++i) {
66d6174b Poul Sander 2024-01-13 22:45 277
		if (themes[i].theme_name == theme.theme_name) {
66d6174b Poul Sander 2024-01-13 22:45 278
			themes[i] = theme;
66d6174b Poul Sander 2024-01-13 22:45 279
			return;
66d6174b Poul Sander 2024-01-13 22:45 280
		}
66d6174b Poul Sander 2024-01-13 22:45 281
	}
66d6174b Poul Sander 2024-01-13 22:45 282
	themes.push_back(theme);
66d6174b Poul Sander 2024-01-13 22:45 283
}
66d6174b Poul Sander 2024-01-13 22:45 284
66d6174b Poul Sander 2024-01-13 22:45 285
BackGroundData ThemesGetNextBackground(const std::string& current) {
66d6174b Poul Sander 2024-01-13 22:45 286
	ThemesInit();
66d6174b Poul Sander 2024-01-13 22:45 287
	BackGroundData ret = background_data["standard"];
66d6174b Poul Sander 2024-01-13 22:45 288
	for (auto& pair : background_data) {
66d6174b Poul Sander 2024-01-13 22:45 289
		if (pair.first == current) {
66d6174b Poul Sander 2024-01-13 22:45 290
			auto it = background_data.find(current);
66d6174b Poul Sander 2024-01-13 22:45 291
			++it;
66d6174b Poul Sander 2024-01-13 22:45 292
			if (it == background_data.end()) {
66d6174b Poul Sander 2024-01-13 22:45 293
				it = background_data.begin();
66d6174b Poul Sander 2024-01-13 22:45 294
			}
66d6174b Poul Sander 2024-01-13 22:45 295
			ret = it->second;
66d6174b Poul Sander 2024-01-13 22:45 296
			break;
66d6174b Poul Sander 2024-01-13 22:45 297
		}
66d6174b Poul Sander 2024-01-13 22:45 298
	}
66d6174b Poul Sander 2024-01-13 22:45 299
	return ret;
66d6174b Poul Sander 2024-01-13 22:45 300
}
de41f458 Poul Sander 2024-01-29 20:53 301
de41f458 Poul Sander 2024-01-29 20:53 302
std::string ThemesGetNextBoardBackground(const std::string& current) {
de41f458 Poul Sander 2024-01-29 20:53 303
	ThemesInit();
de41f458 Poul Sander 2024-01-29 20:53 304
	std::vector<std::string> back_boards = {"back_board", "back_board_sample_snow", "trans_cover"};
de41f458 Poul Sander 2024-01-29 20:53 305
	std::string ret = "back_board";
de41f458 Poul Sander 2024-01-29 20:53 306
	for (size_t i = 0; i < back_boards.size(); ++i) {
de41f458 Poul Sander 2024-01-29 20:53 307
		if (back_boards[i] == current) {
de41f458 Poul Sander 2024-01-29 20:53 308
			ret = back_boards[(i + 1) % back_boards.size()];
de41f458 Poul Sander 2024-01-29 20:53 309
			break;
de41f458 Poul Sander 2024-01-29 20:53 310
		}
de41f458 Poul Sander 2024-01-29 20:53 311
	}
de41f458 Poul Sander 2024-01-29 20:53 312
	return ret;
de41f458 Poul Sander 2024-01-29 20:53 313
}
5b5139a9 Poul Sander 2024-02-21 20:32 314
5b5139a9 Poul Sander 2024-02-21 20:32 315
void ThemesInitCustomBackgrounds() {
5b5139a9 Poul Sander 2024-02-21 20:32 316
	std::vector<std::string> custom_backgrounds = sago::GetFileList("textures/backgrounds");
5b5139a9 Poul Sander 2024-02-21 20:32 317
	std::stringstream sprite_stream;
5b5139a9 Poul Sander 2024-02-21 20:32 318
	sprite_stream << "{\n";
5b5139a9 Poul Sander 2024-02-21 20:32 319
	bool first = true;
5b5139a9 Poul Sander 2024-02-21 20:32 320
	for (const std::string& filename : custom_backgrounds) {
5b5139a9 Poul Sander 2024-02-21 20:32 321
		std::cout << "Found custom background " << filename << "\n";
5b5139a9 Poul Sander 2024-02-21 20:32 322
		if (boost::algorithm::ends_with(filename, ".png") || boost::algorithm::ends_with(filename, ".jpg")) {
5b5139a9 Poul Sander 2024-02-21 20:32 323
			BackGroundData bg;
5b5139a9 Poul Sander 2024-02-21 20:32 324
			bg.name = filename;
5b5139a9 Poul Sander 2024-02-21 20:32 325
			std::string texture_name = fmt::format("custom_background_{}",filename);;
5b5139a9 Poul Sander 2024-02-21 20:32 326
			bg.background_sprite = texture_name;
5b5139a9 Poul Sander 2024-02-21 20:32 327
			bg.background_sprite_16x9 = texture_name;
5b5139a9 Poul Sander 2024-02-21 20:32 328
			bg.background_scale = ImgScale::Resize;
5b5139a9 Poul Sander 2024-02-21 20:32 329
			background_data[bg.name] = bg;
5b5139a9 Poul Sander 2024-02-21 20:32 330
			if (!first) {
5b5139a9 Poul Sander 2024-02-21 20:32 331
				sprite_stream << ",\n";
5b5139a9 Poul Sander 2024-02-21 20:32 332
			}
5b5139a9 Poul Sander 2024-02-21 20:32 333
			sprite_stream << "\"" << texture_name << "\": {\n";
5b5139a9 Poul Sander 2024-02-21 20:32 334
			sprite_stream << "  \"texture\": \"" << "backgrounds/" << filename.substr(0, filename.size()-4) << "\",\n";
5b5139a9 Poul Sander 2024-02-21 20:32 335
			sprite_stream << "  \"topx\": 0,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 336
			sprite_stream << "  \"topy\": 0,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 337
			sprite_stream << "  \"height\": 7680,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 338
			sprite_stream << "  \"width\": 10240,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 339
			sprite_stream << "  \"number_of_frames\": 1,\n";
5b5139a9 Poul Sander 2024-02-21 20:32 340
			sprite_stream << "  \"frame_time\": 1\n";
5b5139a9 Poul Sander 2024-02-21 20:32 341
			sprite_stream << "}\n";
5b5139a9 Poul Sander 2024-02-21 20:32 342
			first = false;
5b5139a9 Poul Sander 2024-02-21 20:32 343
5b5139a9 Poul Sander 2024-02-21 20:32 344
		}
5b5139a9 Poul Sander 2024-02-21 20:32 345
	}
5b5139a9 Poul Sander 2024-02-21 20:32 346
	sprite_stream << "}\n";
5b5139a9 Poul Sander 2024-02-21 20:32 347
	sago::WriteFileContent("sprites/custom_backgrounds.sprite", sprite_stream.str());
5b5139a9 Poul Sander 2024-02-21 20:32 348
}