-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimages.hpp
86 lines (70 loc) · 2.84 KB
/
images.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include <QCoreApplication>
#include <QGraphicsObject>
#include <QPixmap>
#include <filesystem>
#include <libataxx/piece.hpp>
class PieceImages {
private:
inline static std::optional<std::array<QPixmap, 3>> piece_image_table;
public:
static inline const QPixmap& piece_images(const libataxx::Piece& piece) {
if (!piece_image_table.has_value()) {
load();
}
Q_ASSERT(piece_image_table.has_value());
return piece_image_table.value().at(static_cast<int>(piece));
}
static inline std::filesystem::path path_to_themes() {
return QCoreApplication::applicationDirPath().toStdString() + "/piece_images/";
}
static inline void load(std::string name = "") {
const std::filesystem::path dst_path =
QCoreApplication::applicationDirPath().toStdString() + "/selected_piece_images/";
if (name != "" || !std::filesystem::exists(dst_path)) {
if (name == "") {
name = "default";
}
const std::filesystem::path src_path = path_to_themes() / name;
std::filesystem::create_directories(dst_path);
for (auto f : std::vector{"x.png", "o.png", "-.png"}) {
std::filesystem::copy_file(
src_path / f, dst_path / f, std::filesystem::copy_options::overwrite_existing);
}
}
piece_image_table = {
QPixmap(QString::fromStdString(dst_path.string() + "x.png")),
QPixmap(QString::fromStdString(dst_path.string() + "o.png")),
QPixmap(QString::fromStdString(dst_path.string() + "-.png")),
};
}
};
class BoardImage {
private:
inline static std::optional<QPixmap> board_image_pixmap;
public:
static inline const QPixmap& board_image() {
if (!board_image_pixmap.has_value()) {
load();
}
Q_ASSERT(board_image_pixmap.has_value());
return board_image_pixmap.value();
}
static inline std::filesystem::path path_to_themes() {
return QCoreApplication::applicationDirPath().toStdString() + "/board_images/";
}
static inline void load(std::string name = "") {
const std::filesystem::path dst_path =
QCoreApplication::applicationDirPath().toStdString() + "/selected_board_image/";
if (name != "" || !std::filesystem::exists(dst_path)) {
if (name == "") {
name = "default";
}
const std::filesystem::path src_path = path_to_themes() / name;
std::filesystem::create_directories(dst_path);
std::filesystem::copy_file(
src_path / "board.png", dst_path / "board.png", std::filesystem::copy_options::overwrite_existing);
}
board_image_pixmap = QPixmap(QString::fromStdString(dst_path.string() + "board.png"));
}
};