From e91542a3cf863d4f9dc1714e774cda52506aad41 Mon Sep 17 00:00:00 2001 From: Peter Elliott Date: Tue, 11 Aug 2020 18:59:32 -0600 Subject: [PATCH] Chess: Add menu options for setting board theme and piece set --- Base/res/icons/chess/mini-board.png | Bin 0 -> 595 bytes Games/Chess/ChessWidget.cpp | 25 +++++++++- Games/Chess/ChessWidget.h | 15 +++++- Games/Chess/main.cpp | 69 +++++++++++++++++++++++++++- 4 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 Base/res/icons/chess/mini-board.png diff --git a/Base/res/icons/chess/mini-board.png b/Base/res/icons/chess/mini-board.png new file mode 100644 index 0000000000000000000000000000000000000000..c47bcdbab9d044e23c0d614b6edc0a5d8f018cd7 GIT binary patch literal 595 zcmeAS@N?(olHy`uVBq!ia0y~yU=RRd4mJh`2Kmqb6B!s7TQZ%U13aCb6$*;-(=u~X z85lGs)=sqbI2<6->L0w+Rg14oK=5S1T%C?Ci!9Nu0L3d@YprJN`NBWTL`74tuPOWB z!TCp5HFs}b$G5IY;Ro}hCoh(~P*S}&T_ozrj|U&^&Q(7zdSBUaVTHljS5oXIcg-%J zY?*O(ZP&u^7C}aZq=>X>hU%W0Kh~|UX8BPm{(S47`+s*{7}64j?L3w6B*H;dD&;!=Klc5bw{lGuj$7)%^rEhCPQReS7H*T*eV=RJSezAB_`wtN zGPiy!2)O!EtnRX~KmJ1-^P_hNGuA4!(cWdx(L7fx*+&&t;ucLK6U(); + 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"); + } +} diff --git a/Games/Chess/ChessWidget.h b/Games/Chess/ChessWidget.h index e4e77c0991..51608d0061 100644 --- a/Games/Chess/ChessWidget.h +++ b/Games/Chess/ChessWidget.h @@ -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> m_pieces; diff --git a/Games/Chess/main.cpp b/Games/Chess/main.cpp index 29ad23722a..cdf257562f 100644 --- a/Games/Chess/main.cpp +++ b/Games/Chess/main.cpp @@ -25,7 +25,12 @@ */ #include "ChessWidget.h" +#include +#include +#include #include +#include +#include #include int main(int argc, char** argv) @@ -36,10 +41,70 @@ int main(int argc, char** argv) auto& widget = window->set_main_widget(); widget.set_side(Chess::Colour::White); + RefPtr 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();