diff options
author | Tim Ledbetter <timledbetter@gmail.com> | 2023-05-09 20:01:47 +0100 |
---|---|---|
committer | Sam Atkins <atkinssj@gmail.com> | 2023-05-10 12:13:35 +0100 |
commit | cf4a43e4c083591480ac78172324ce0fa4a3e1ae (patch) | |
tree | 299b317762cef94509c68f229cf8abd0c0f6be65 | |
parent | 0c26717ba3317e769f64c605668603a79e909518 (diff) | |
download | serenity-cf4a43e4c083591480ac78172324ce0fa4a3e1ae.zip |
Chess+GameSettings: Optionally highlight the king when in check
When either king is in check, its square is now highlighted with a red
background. This behavior can be toggled in GameSettings.
-rw-r--r-- | Userland/Applications/GamesSettings/ChessSettingsWidget.cpp | 8 | ||||
-rw-r--r-- | Userland/Applications/GamesSettings/ChessSettingsWidget.gml | 6 | ||||
-rw-r--r-- | Userland/Applications/GamesSettings/ChessSettingsWidget.h | 1 | ||||
-rw-r--r-- | Userland/Games/Chess/ChessWidget.cpp | 19 | ||||
-rw-r--r-- | Userland/Games/Chess/ChessWidget.h | 4 | ||||
-rw-r--r-- | Userland/Games/Chess/main.cpp | 1 |
6 files changed, 37 insertions, 2 deletions
diff --git a/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp b/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp index 81d4c836e3..74d9d86fed 100644 --- a/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp +++ b/Userland/Applications/GamesSettings/ChessSettingsWidget.cpp @@ -276,6 +276,12 @@ ErrorOr<void> ChessSettingsWidget::initialize() 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); @@ -289,6 +295,7 @@ 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() @@ -302,6 +309,7 @@ void ChessSettingsWidget::reset_default_values() 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); } } diff --git a/Userland/Applications/GamesSettings/ChessSettingsWidget.gml b/Userland/Applications/GamesSettings/ChessSettingsWidget.gml index edb914b1f9..3d3326182d 100644 --- a/Userland/Applications/GamesSettings/ChessSettingsWidget.gml +++ b/Userland/Applications/GamesSettings/ChessSettingsWidget.gml @@ -54,5 +54,11 @@ text: "Show coordinates" checkbox_position: "Right" } + + @GUI::CheckBox { + name: "highlight_checks" + text: "Highlight checks" + checkbox_position: "Right" + } } } diff --git a/Userland/Applications/GamesSettings/ChessSettingsWidget.h b/Userland/Applications/GamesSettings/ChessSettingsWidget.h index df55705b04..be2fcc6ae6 100644 --- a/Userland/Applications/GamesSettings/ChessSettingsWidget.h +++ b/Userland/Applications/GamesSettings/ChessSettingsWidget.h @@ -34,6 +34,7 @@ private: RefPtr<GUI::ComboBox> m_piece_set_combobox; RefPtr<GUI::ComboBox> m_board_theme_combobox; RefPtr<GUI::CheckBox> m_show_coordinates_checkbox; + RefPtr<GUI::CheckBox> m_highlight_checks_checkbox; }; } diff --git a/Userland/Games/Chess/ChessWidget.cpp b/Userland/Games/Chess/ChessWidget.cpp index 4ce5458728..aeb952c79e 100644 --- a/Userland/Games/Chess/ChessWidget.cpp +++ b/Userland/Games/Chess/ChessWidget.cpp @@ -62,8 +62,20 @@ void ChessWidget::paint_event(GUI::PaintEvent& event) painter.fill_rect(tile_rect, (sq.is_light()) ? board_theme().light_square_color : board_theme().dark_square_color); - if (active_board.last_move().has_value() && (active_board.last_move().value().to == sq || active_board.last_move().value().from == sq)) { - painter.fill_rect(tile_rect, m_move_highlight_color); + if (active_board.last_move().has_value()) { + auto const last_move = active_board.last_move().value(); + if (last_move.to == sq || last_move.from == sq) + painter.fill_rect(tile_rect, m_move_highlight_color); + + auto const piece = active_board.get_piece(sq); + if (m_highlight_checks && last_move.is_check && piece.type == Chess::Type::King && piece.color == active_board.turn()) { + Array<Gfx::ColorStop, 2> colors = { + Gfx::ColorStop { .color = Color::Red, .position = 0.16f }, + Gfx::ColorStop { .color = Color::Transparent, .position = .66f } + }; + + painter.fill_rect_with_radial_gradient(tile_rect, colors, tile_rect.center() - tile_rect.top_left(), tile_rect.size()); + } } if (m_coordinates) { @@ -750,5 +762,8 @@ void ChessWidget::config_bool_did_change(DeprecatedString const& domain, Depreca if (key == "ShowCoordinates"sv) { set_coordinates(value); update(); + } else if (key == "HighlightChecks"sv) { + set_highlight_checks(value); + update(); } } diff --git a/Userland/Games/Chess/ChessWidget.h b/Userland/Games/Chess/ChessWidget.h index 7beb6a23bc..95731c5adb 100644 --- a/Userland/Games/Chess/ChessWidget.h +++ b/Userland/Games/Chess/ChessWidget.h @@ -89,6 +89,9 @@ public: void set_coordinates(bool coordinates) { m_coordinates = coordinates; } bool coordinates() const { return m_coordinates; } + void set_highlight_checks(bool highlight_checks) { m_highlight_checks = highlight_checks; } + bool highlight_checks() const { return m_highlight_checks; } + struct BoardMarking { Chess::Square from { 50, 50 }; Chess::Square to { 50, 50 }; @@ -146,4 +149,5 @@ private: Vector<Chess::Square> m_available_moves; RefPtr<Engine> m_engine; bool m_coordinates { true }; + bool m_highlight_checks { true }; }; diff --git a/Userland/Games/Chess/main.cpp b/Userland/Games/Chess/main.cpp index ba19020741..e591c1cfc6 100644 --- a/Userland/Games/Chess/main.cpp +++ b/Userland/Games/Chess/main.cpp @@ -87,6 +87,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments) widget->set_board_theme(Config::read_string("Games"sv, "Chess"sv, "BoardTheme"sv, "Beige"sv)); widget->set_coordinates(Config::read_bool("Games"sv, "Chess"sv, "ShowCoordinates"sv, true)); widget->set_show_available_moves(Config::read_bool("Games"sv, "Chess"sv, "ShowAvailableMoves"sv, true)); + widget->set_highlight_checks(Config::read_bool("Games"sv, "Chess"sv, "HighlightChecks"sv, true)); auto game_menu = TRY(window->try_add_menu("&Game"_short_string)); |