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 /Userland/Games/Chess | |
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.
Diffstat (limited to 'Userland/Games/Chess')
-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 |
3 files changed, 22 insertions, 2 deletions
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)); |