diff --git a/Games/Chess/ChessWidget.cpp b/Games/Chess/ChessWidget.cpp index cc936cb21e..51c07d3ba1 100644 --- a/Games/Chess/ChessWidget.cpp +++ b/Games/Chess/ChessWidget.cpp @@ -29,6 +29,7 @@ #include #include #include +#include ChessWidget::ChessWidget(const StringView& set) { @@ -53,6 +54,7 @@ void ChessWidget::paint_event(GUI::PaintEvent& event) size_t tile_width = width() / 8; size_t tile_height = height() / 8; + unsigned coord_rank_file = (side() == Chess::Colour::White) ? 0 : 7; Chess::Square::for_each([&](Chess::Square sq) { Gfx::IntRect tile_rect; @@ -68,6 +70,19 @@ void ChessWidget::paint_event(GUI::PaintEvent& event) painter.fill_rect(tile_rect, m_move_highlight_color); } + if (m_coordinates) { + auto coord = sq.to_algebraic(); + auto text_color = (sq.is_light()) ? board_theme().dark_square_color : board_theme().light_square_color; + + auto shrunken_rect = tile_rect; + shrunken_rect.shrink(4, 4); + if (sq.rank == coord_rank_file) + painter.draw_text(shrunken_rect, coord.substring_view(0, 1), Gfx::Font::default_bold_font(), Gfx::TextAlignment::BottomRight, text_color); + + if (sq.file == coord_rank_file) + painter.draw_text(shrunken_rect, coord.substring_view(1, 1), Gfx::Font::default_bold_font(), Gfx::TextAlignment::TopLeft, text_color); + } + if (!(m_dragging_piece && sq == m_moving_square)) { auto bmp = m_pieces.get(board().get_piece(sq)); if (bmp.has_value()) { diff --git a/Games/Chess/ChessWidget.h b/Games/Chess/ChessWidget.h index da881277b6..ed3065a9d4 100644 --- a/Games/Chess/ChessWidget.h +++ b/Games/Chess/ChessWidget.h @@ -78,6 +78,9 @@ public: void maybe_input_engine_move(); + void set_coordinates(bool coordinates) { m_coordinates = coordinates; } + bool coordinates() const { return m_coordinates; } + private: Chess::Board m_board; BoardTheme m_board_theme { "Beige", Color::from_rgb(0xb58863), Color::from_rgb(0xf0d9b5) }; @@ -90,4 +93,5 @@ private: bool m_dragging_piece { false }; bool m_drag_enabled { true }; RefPtr m_engine; + bool m_coordinates { true }; }; diff --git a/Games/Chess/main.cpp b/Games/Chess/main.cpp index 7ee2536719..b4ff6bea1d 100644 --- a/Games/Chess/main.cpp +++ b/Games/Chess/main.cpp @@ -53,6 +53,7 @@ int main(int argc, char** argv) widget.set_piece_set(config->read_entry("Style", "PieceSet", "test")); widget.set_board_theme(config->read_entry("Style", "BoardTheme", "Beige")); + widget.set_coordinates(config->read_bool_entry("Style", "Coordinates", true)); auto menubar = GUI::MenuBar::construct(); auto& app_menu = menubar->add_menu("Chess"); @@ -105,6 +106,15 @@ int main(int argc, char** argv) board_theme_menu.add_action(*action); } + auto coordinates_action = GUI::Action::create_checkable("Coordinates", [&](auto& action) { + widget.set_coordinates(action.is_checked()); + widget.update(); + config->write_bool_entry("Style", "Coordinates", action.is_checked()); + config->sync(); + }); + coordinates_action->set_checked(widget.coordinates()); + style_menu.add_action(coordinates_action); + auto& engine_menu = menubar->add_menu("Engine"); GUI::ActionGroup engines_action_group;