1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 18:28:12 +00:00

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
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()) {