summaryrefslogtreecommitdiff
path: root/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp
blob: 74d9d86feddca568368dace679736dea3fed6fd6 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "ChessSettingsWidget.h"
#include <Applications/GamesSettings/ChessSettingsWidgetGML.h>
#include <LibChess/Chess.h>
#include <LibConfig/Client.h>
#include <LibCore/Directory.h>
#include <LibGUI/CheckBox.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/Frame.h>
#include <LibGUI/ItemListModel.h>
#include <LibGUI/Painter.h>

namespace GamesSettings {

struct BoardTheme {
    StringView name;
    Color dark_square_color;
    Color light_square_color;
};

// The following colors have been taken from lichess.org, but I'm pretty sure they took them from chess.com.
Array<BoardTheme, 3> s_board_themes {
    BoardTheme { "Beige"sv, Color::from_rgb(0xb58863), Color::from_rgb(0xf0d9b5) },
    BoardTheme { "Blue"sv, Color::from_rgb(0x8ca2ad), Color::from_rgb(0xdee3e6) },
    BoardTheme { "Green"sv, Color::from_rgb(0x86a666), Color::from_rgb(0xffffdd) },
};

static BoardTheme& get_board_theme(StringView name)
{
    for (size_t i = 0; i < s_board_themes.size(); ++i) {
        if (s_board_themes[i].name == name)
            return s_board_themes[i];
    }
    return s_board_themes[0];
}

class BoardThemeModel final : public GUI::Model {
public:
    static ErrorOr<NonnullRefPtr<BoardThemeModel>> create()
    {
        return adopt_nonnull_ref_or_enomem(new (nothrow) BoardThemeModel());
    }

    ~BoardThemeModel() = default;

    virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override
    {
        return s_board_themes.size();
    }

    virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override
    {
        return 1;
    }

    virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role = GUI::ModelRole::Display) const override
    {
        if (role != GUI::ModelRole::Display)
            return {};
        if (!is_within_range(index))
            return {};

        return s_board_themes.at(index.row()).name;
    }

private:
    BoardThemeModel()
    {
    }
};

static ErrorOr<RefPtr<Gfx::Bitmap>> load_piece_image(StringView set, StringView image)
{
    auto path = TRY(String::formatted("/res/graphics/chess/sets/{}/{}", set, image));
    return Gfx::Bitmap::load_from_file(path.bytes_as_string_view());
}

class ChessGamePreview final : public GUI::Frame {
    C_OBJECT_ABSTRACT(ChessGamePreview)

public:
    static ErrorOr<NonnullRefPtr<ChessGamePreview>> try_create()
    {
        auto preview = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ChessGamePreview()));
        return preview;
    }

    virtual ~ChessGamePreview() = default;

    ErrorOr<void> set_piece_set_name(DeprecatedString const& piece_set_name)
    {
        if (m_piece_set_name == piece_set_name.view())
            return {};

        m_piece_set_name = TRY(String::from_utf8(piece_set_name));
        m_piece_images.clear();

        m_piece_images.set({ Chess::Color::White, Chess::Type::Pawn }, TRY(load_piece_image(m_piece_set_name, "white-pawn.png"sv)));
        m_piece_images.set({ Chess::Color::Black, Chess::Type::Pawn }, TRY(load_piece_image(m_piece_set_name, "black-pawn.png"sv)));
        m_piece_images.set({ Chess::Color::White, Chess::Type::Knight }, TRY(load_piece_image(m_piece_set_name, "white-knight.png"sv)));
        m_piece_images.set({ Chess::Color::Black, Chess::Type::Knight }, TRY(load_piece_image(m_piece_set_name, "black-knight.png"sv)));
        m_piece_images.set({ Chess::Color::White, Chess::Type::Bishop }, TRY(load_piece_image(m_piece_set_name, "white-bishop.png"sv)));
        m_piece_images.set({ Chess::Color::Black, Chess::Type::Bishop }, TRY(load_piece_image(m_piece_set_name, "black-bishop.png"sv)));
        m_piece_images.set({ Chess::Color::White, Chess::Type::Rook }, TRY(load_piece_image(m_piece_set_name, "white-rook.png"sv)));
        m_piece_images.set({ Chess::Color::Black, Chess::Type::Rook }, TRY(load_piece_image(m_piece_set_name, "black-rook.png"sv)));
        m_piece_images.set({ Chess::Color::White, Chess::Type::Queen }, TRY(load_piece_image(m_piece_set_name, "white-queen.png"sv)));
        m_piece_images.set({ Chess::Color::Black, Chess::Type::Queen }, TRY(load_piece_image(m_piece_set_name, "black-queen.png"sv)));
        m_piece_images.set({ Chess::Color::White, Chess::Type::King }, TRY(load_piece_image(m_piece_set_name, "white-king.png"sv)));
        m_piece_images.set({ Chess::Color::Black, Chess::Type::King }, TRY(load_piece_image(m_piece_set_name, "black-king.png"sv)));

        update();

        return {};
    }

    void set_dark_square_color(Gfx::Color dark_square_color)
    {
        if (m_dark_square_color == dark_square_color)
            return;

        m_dark_square_color = dark_square_color;
        update();
    }

    void set_light_square_color(Gfx::Color light_square_color)
    {
        if (m_light_square_color == light_square_color)
            return;

        m_light_square_color = light_square_color;
        update();
    }

    void set_show_coordinates(bool show_coordinates)
    {
        if (m_show_coordinates == show_coordinates)
            return;

        m_show_coordinates = show_coordinates;
        update();
    }

private:
    ChessGamePreview() = default;

    virtual void paint_event(GUI::PaintEvent& event) override
    {
        GUI::Frame::paint_event(event);

        GUI::Painter painter(*this);
        painter.add_clip_rect(event.rect());
        painter.add_clip_rect(frame_inner_rect());

        auto& coordinate_font = Gfx::FontDatabase::default_font().bold_variant();

        // To show all the piece graphics, we need at least 12 squares visible.
        // With the same preview size as we use for card games, a nice fit is 2 ranks of 6.
        // There are definitely better ways of doing this, but it'll do. ;^)
        auto square_size = 61;
        auto square_margin = square_size / 10;

        auto rect_for_square = [&](Chess::Square const& square) {
            return Gfx::IntRect {
                frame_inner_rect().left() + square.file * square_size,
                frame_inner_rect().bottom() + 1 - (square.rank + 1) * square_size,
                square_size,
                square_size
            };
        };

        for (int rank = 0; rank < 3; ++rank) {
            for (int file = 0; file < 8; ++file) {
                Chess::Square square { rank, file };
                auto square_rect = rect_for_square(square);
                painter.fill_rect(square_rect, square.is_light() ? m_light_square_color : m_dark_square_color);

                if (m_show_coordinates) {
                    auto text_color = square.is_light() ? m_dark_square_color : m_light_square_color;
                    auto shrunken_rect = square_rect.shrunken(4, 4);

                    if (square.rank == 0) {
                        auto file_char = square.file_char();
                        painter.draw_text(shrunken_rect, { &file_char, 1 }, coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
                    }

                    if (square.file == 0) {
                        auto rank_char = square.rank_char();
                        painter.draw_text(shrunken_rect, { &rank_char, 1 }, coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
                    }
                }
            }
        }

        auto draw_piece = [&](Chess::Piece const& piece, Chess::Square const& square) {
            auto& bitmap = *m_piece_images.get(piece).value();
            painter.draw_scaled_bitmap(
                rect_for_square(square).shrunken(square_margin, square_margin, square_margin, square_margin),
                bitmap,
                bitmap.rect(),
                1.0f,
                Gfx::Painter::ScalingMode::BilinearBlend);
        };

        draw_piece({ Chess::Color::White, Chess::Type::King }, { 0, 0 });
        draw_piece({ Chess::Color::Black, Chess::Type::King }, { 1, 0 });
        draw_piece({ Chess::Color::White, Chess::Type::Queen }, { 0, 1 });
        draw_piece({ Chess::Color::Black, Chess::Type::Queen }, { 1, 1 });
        draw_piece({ Chess::Color::White, Chess::Type::Rook }, { 0, 2 });
        draw_piece({ Chess::Color::Black, Chess::Type::Rook }, { 1, 2 });
        draw_piece({ Chess::Color::White, Chess::Type::Bishop }, { 0, 3 });
        draw_piece({ Chess::Color::Black, Chess::Type::Bishop }, { 1, 3 });
        draw_piece({ Chess::Color::White, Chess::Type::Knight }, { 0, 4 });
        draw_piece({ Chess::Color::Black, Chess::Type::Knight }, { 1, 4 });
        draw_piece({ Chess::Color::White, Chess::Type::Pawn }, { 0, 5 });
        draw_piece({ Chess::Color::Black, Chess::Type::Pawn }, { 1, 5 });
    }

    HashMap<Chess::Piece, RefPtr<Gfx::Bitmap>> m_piece_images;

    Gfx::Color m_dark_square_color { s_board_themes[0].dark_square_color };
    Gfx::Color m_light_square_color { s_board_themes[0].light_square_color };
    bool m_show_coordinates { true };
    String m_piece_set_name;
};

ErrorOr<NonnullRefPtr<ChessSettingsWidget>> ChessSettingsWidget::try_create()
{
    auto chess_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ChessSettingsWidget));
    TRY(chess_settings_widget->initialize());
    return chess_settings_widget;
}

ErrorOr<void> ChessSettingsWidget::initialize()
{
    TRY(load_from_gml(chess_settings_widget_gml));

    auto piece_set_name = Config::read_string("Games"sv, "Chess"sv, "PieceSet"sv, "stelar7"sv);
    auto board_theme = get_board_theme(Config::read_string("Games"sv, "Chess"sv, "BoardTheme"sv, "Beige"sv));
    auto show_coordinates = Config::read_bool("Games"sv, "Chess"sv, "ShowCoordinates"sv, true);

    m_preview = find_descendant_of_type_named<ChessGamePreview>("chess_preview");

    m_piece_set_combobox = find_descendant_of_type_named<GUI::ComboBox>("piece_set");
    TRY(Core::Directory::for_each_entry("/res/graphics/chess/sets/"sv, Core::DirIterator::SkipParentAndBaseDir, [&](auto const& entry, auto&) -> ErrorOr<IterationDecision> {
        TRY(m_piece_sets.try_append(entry.name));
        return IterationDecision::Continue;
    }));
    auto piece_set_model = GUI::ItemListModel<DeprecatedString>::create(m_piece_sets);
    m_piece_set_combobox->set_model(piece_set_model);
    m_piece_set_combobox->set_text(piece_set_name, GUI::AllowCallback::No);
    m_piece_set_combobox->on_change = [&](auto& value, auto&) {
        set_modified(true);
        m_preview->set_piece_set_name(value).release_value_but_fixme_should_propagate_errors();
    };

    m_board_theme_combobox = find_descendant_of_type_named<GUI::ComboBox>("board_theme");
    m_board_theme_combobox->set_model(TRY(BoardThemeModel::create()));
    m_board_theme_combobox->set_text(board_theme.name, GUI::AllowCallback::No);
    m_board_theme_combobox->on_change = [&](auto&, auto& index) {
        set_modified(true);

        auto& theme = s_board_themes[index.row()];
        m_preview->set_dark_square_color(theme.dark_square_color);
        m_preview->set_light_square_color(theme.light_square_color);
    };

    m_show_coordinates_checkbox = find_descendant_of_type_named<GUI::CheckBox>("show_coordinates");
    m_show_coordinates_checkbox->set_checked(show_coordinates, GUI::AllowCallback::No);
    m_show_coordinates_checkbox->on_checked = [&](bool checked) {
        set_modified(true);
        m_preview->set_show_coordinates(checked);
    };

    m_highlight_checks_checkbox = find_descendant_of_type_named<GUI::CheckBox>("highlight_checks");
    m_highlight_checks_checkbox->set_checked(show_coordinates, GUI::AllowCallback::No);
    m_highlight_checks_checkbox->on_checked = [&](bool) {
        set_modified(true);
    };

    TRY(m_preview->set_piece_set_name(piece_set_name));
    m_preview->set_dark_square_color(board_theme.dark_square_color);
    m_preview->set_light_square_color(board_theme.light_square_color);
    m_preview->set_show_coordinates(show_coordinates);

    return {};
}

void ChessSettingsWidget::apply_settings()
{
    Config::write_string("Games"sv, "Chess"sv, "PieceSet"sv, m_piece_set_combobox->text());
    Config::write_string("Games"sv, "Chess"sv, "BoardTheme"sv, m_board_theme_combobox->text());
    Config::write_bool("Games"sv, "Chess"sv, "ShowCoordinates"sv, m_show_coordinates_checkbox->is_checked());
    Config::write_bool("Games"sv, "Chess"sv, "HighlightChecks"sv, m_highlight_checks_checkbox->is_checked());
}

void ChessSettingsWidget::reset_default_values()
{
    // FIXME: `set_text()` on a combobox doesn't trigger the `on_change` callback, but it probably should!
    //        Until then, we have to manually tell the preview to update.
    m_piece_set_combobox->set_text("stelar7");
    (void)m_preview->set_piece_set_name("stelar7");
    auto& board_theme = get_board_theme("Beige"sv);
    m_board_theme_combobox->set_text(board_theme.name);
    m_preview->set_dark_square_color(board_theme.dark_square_color);
    m_preview->set_light_square_color(board_theme.light_square_color);
    m_show_coordinates_checkbox->set_checked(true);
    m_highlight_checks_checkbox->set_checked(true);
}

}

REGISTER_WIDGET(GamesSettings, ChessGamePreview);