Chess: Optionaly display coordinates at edge of board

This commit is contained in:
Peter Elliott 2020-08-21 08:53:04 -06:00 committed by Andreas Kling
parent 28db3cd5ef
commit b0ffd4e946
Notes: sideshowbarker 2024-07-19 03:16:44 +09:00
3 changed files with 29 additions and 0 deletions

View file

@ -29,6 +29,7 @@
#include <AK/String.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Font.h>
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()) {

View file

@ -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<Engine> m_engine;
bool m_coordinates { true };
};

View file

@ -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;