1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:24:57 +00:00

LibChess: Add and use Square::{file,rank}_char() methods

This saves us having to build and allocate a String, just to then use
one character of it.
This commit is contained in:
Sam Atkins 2023-04-24 12:29:31 +01:00 committed by Andreas Kling
parent c73c697f94
commit 5f6dd87163
4 changed files with 35 additions and 13 deletions

View file

@ -180,15 +180,18 @@ private:
painter.fill_rect(square_rect, square.is_light() ? m_light_square_color : m_dark_square_color);
if (m_show_coordinates) {
auto coord = square.to_algebraic();
auto text_color = square.is_light() ? m_dark_square_color : m_light_square_color;
auto shrunken_rect = square_rect.shrunken(4, 4);
if (square.rank == 0)
painter.draw_text(shrunken_rect, coord.substring_view(0, 1), coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
if (square.rank == 0) {
auto file_char = square.file_char();
painter.draw_text(shrunken_rect, { &file_char, 1 }, coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
}
if (square.file == 0)
painter.draw_text(shrunken_rect, coord.substring_view(1, 1), coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
if (square.file == 0) {
auto rank_char = square.rank_char();
painter.draw_text(shrunken_rect, { &rank_char, 1 }, coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
}
}
}
}

View file

@ -66,16 +66,20 @@ void ChessWidget::paint_event(GUI::PaintEvent& event)
}
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), coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
if (sq.file == coord_rank_file)
painter.draw_text(shrunken_rect, coord.substring_view(1, 1), coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
if (sq.rank == coord_rank_file) {
auto file_char = sq.file_char();
painter.draw_text(shrunken_rect, { &file_char, 1 }, coordinate_font, Gfx::TextAlignment::BottomRight, text_color);
}
if (sq.file == coord_rank_file) {
auto rank_char = sq.rank_char();
painter.draw_text(shrunken_rect, { &rank_char, 1 }, coordinate_font, Gfx::TextAlignment::TopLeft, text_color);
}
}
for (auto& m : m_board_markings) {

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -83,6 +84,16 @@ Square::Square(StringView name)
}
}
char Square::file_char() const
{
return file + 'a';
}
char Square::rank_char() const
{
return rank + '1';
}
DeprecatedString Square::to_algebraic() const
{
StringBuilder builder;
@ -200,16 +211,16 @@ DeprecatedString Move::to_algebraic() const
if (is_ambiguous) {
if (from.file != ambiguous.file)
builder.append(from.to_algebraic().substring(0, 1));
builder.append(from.file_char());
else if (from.rank != ambiguous.rank)
builder.append(from.to_algebraic().substring(1, 1));
builder.append(from.rank_char());
else
builder.append(from.to_algebraic());
}
if (is_capture) {
if (piece.type == Type::Pawn && !is_ambiguous)
builder.append(from.to_algebraic().substring(0, 1));
builder.append(from.file_char());
builder.append('x');
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -89,6 +90,9 @@ struct Square {
bool in_bounds() const { return rank >= 0 && file >= 0 && rank < 8 && file < 8; }
bool is_light() const { return (rank % 2) != (file % 2); }
char file_char() const;
char rank_char() const;
DeprecatedString to_algebraic() const;
};