Chess: Add menu options for setting board theme and piece set

This commit is contained in:
Peter Elliott 2020-08-11 18:59:32 -06:00 committed by Andreas Kling
parent 7b71f4759f
commit e91542a3cf
Notes: sideshowbarker 2024-07-19 03:35:39 +09:00
4 changed files with 104 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 595 B

View file

@ -61,7 +61,7 @@ void ChessWidget::paint_event(GUI::PaintEvent& event)
tile_rect = { (7 - sq.file) * tile_width, sq.rank * tile_height, tile_width, tile_height };
}
painter.fill_rect(tile_rect, ((sq.rank % 2) == (sq.file % 2)) ? m_dark_square_color : m_light_square_color);
painter.fill_rect(tile_rect, ((sq.rank % 2) == (sq.file % 2)) ? board_theme().dark_square_color : board_theme().light_square_color);
if (m_last_move.has_value() && (m_last_move.value().to == sq || m_last_move.value().from == sq)) {
painter.fill_rect(tile_rect, m_move_highlight_color);
@ -199,3 +199,26 @@ Chess::Square ChessWidget::mouse_to_square(GUI::MouseEvent& event) const
return { event.y() / tile_height, 7 - (event.x() / tile_width) };
}
}
void ChessWidget::reset()
{
m_board = Chess();
m_drag_enabled = true;
m_last_move = Optional<Chess::Move>();
update();
}
void ChessWidget::set_board_theme(const StringView& name)
{
// FIXME: Add some kind of themes.json
// The following Colours have been taken from lichess.org, but i'm pretty sure they took them from chess.com.
if (name == "Beige") {
m_board_theme = { "Beige", Color::from_rgb(0xb58863), Color::from_rgb(0xf0d9b5) };
} else if (name == "Green") {
m_board_theme = { "Green", Color::from_rgb(0x86a666), Color::from_rgb(0xffffdd) };
} else if (name == "Blue") {
m_board_theme = { "Blue", Color::from_rgb(0x8ca2ad), Color::from_rgb(0xdee3e6) };
} else {
set_board_theme("Beige");
}
}

View file

@ -60,10 +60,21 @@ public:
bool drag_enabled() const { return m_drag_enabled; }
void set_drag_enabled(bool e) { m_drag_enabled = e; }
void reset();
struct BoardTheme {
String name;
Color dark_square_color;
Color light_square_color;
};
const BoardTheme& board_theme() const { return m_board_theme; }
void set_board_theme(const BoardTheme& theme) { m_board_theme = theme; }
void set_board_theme(const StringView& name);
private:
Chess m_board;
Color m_dark_square_color { Color::from_rgb(0xb58863) };
Color m_light_square_color { Color::from_rgb(0xf0d9b5) };
BoardTheme m_board_theme { "Beige", Color::from_rgb(0xb58863), Color::from_rgb(0xf0d9b5) };
Color m_move_highlight_color { Color::from_rgba(0x66ccee00) };
Chess::Colour m_side { Chess::Colour::White };
HashMap<Chess::Piece, RefPtr<Gfx::Bitmap>> m_pieces;

View file

@ -25,7 +25,12 @@
*/
#include "ChessWidget.h"
#include <LibCore/ConfigFile.h>
#include <LibCore/DirIterator.h>
#include <LibGUI/ActionGroup.h>
#include <LibGUI/Application.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MenuBar.h>
#include <LibGUI/Window.h>
int main(int argc, char** argv)
@ -36,10 +41,70 @@ int main(int argc, char** argv)
auto& widget = window->set_main_widget<ChessWidget>();
widget.set_side(Chess::Colour::White);
RefPtr<Core::ConfigFile> config = Core::ConfigFile::get_for_app("Chess");
auto size = config->read_num_entry("Display", "size", 512);
window->set_title("Chess");
window->resize(512, 512);
window->resize(size, size);
window->set_resizable(false);
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-chess.png"));
auto icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/app-chess.png");
window->set_icon(icon);
widget.set_piece_set(config->read_entry("Style", "PieceSet", "test"));
widget.set_board_theme(config->read_entry("Style", "BoardTheme", "Beige"));
auto menubar = GUI::MenuBar::construct();
auto& app_menu = menubar->add_menu("Chess");
app_menu.add_action(GUI::Action::create("New game", { Mod_None, Key_F2 }, [&](auto&) {
widget.reset();
}));
app_menu.add_action(GUI::CommonActions::make_quit_action([](auto&) {
GUI::Application::the()->quit();
}));
auto& style_menu = menubar->add_menu("Style");
GUI::ActionGroup piece_set_action_group;
piece_set_action_group.set_exclusive(true);
auto& piece_set_menu = style_menu.add_submenu("Piece Set");
piece_set_menu.set_icon(icon);
Core::DirIterator di("/res/icons/chess/sets/", Core::DirIterator::SkipParentAndBaseDir);
while (di.has_next()) {
auto set = di.next_path();
auto action = GUI::Action::create_checkable(set, [&](auto& action) {
widget.set_piece_set(action.text());
widget.update();
config->write_entry("Style", "PieceSet", action.text());
config->sync();
});
piece_set_action_group.add_action(*action);
if (widget.piece_set() == set)
action->set_checked(true);
piece_set_menu.add_action(*action);
}
GUI::ActionGroup board_theme_action_group;
board_theme_action_group.set_exclusive(true);
auto& board_theme_menu = style_menu.add_submenu("Board Theme");
board_theme_menu.set_icon(Gfx::Bitmap::load_from_file("/res/icons/chess/mini-board.png"));
for (auto& theme : Vector({ "Beige", "Green", "Blue" })) {
auto action = GUI::Action::create_checkable(theme, [&](auto& action) {
widget.set_board_theme(action.text());
widget.update();
config->write_entry("Style", "BoardTheme", action.text());
config->sync();
});
board_theme_action_group.add_action(*action);
if (widget.board_theme().name == theme)
action->set_checked(true);
board_theme_menu.add_action(*action);
}
app->set_menubar(move(menubar));
window->show();