From 67ed5805324a8a317cb516ddb7e752bdc7634365 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 17 May 2021 12:02:38 +0200 Subject: [PATCH] Chess: Make the main widget a GUI::Frame for a nicer look :^) --- Userland/Games/Chess/ChessWidget.cpp | 29 ++++++++++++++-------------- Userland/Games/Chess/ChessWidget.h | 12 ++++++------ Userland/Games/Chess/main.cpp | 3 ++- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Userland/Games/Chess/ChessWidget.cpp b/Userland/Games/Chess/ChessWidget.cpp index d501a320050..eb174576824 100644 --- a/Userland/Games/Chess/ChessWidget.cpp +++ b/Userland/Games/Chess/ChessWidget.cpp @@ -17,14 +17,9 @@ #include #include -ChessWidget::ChessWidget(const StringView& set) -{ - set_piece_set(set); -} - ChessWidget::ChessWidget() - : ChessWidget("stelar7") { + set_piece_set("stelar7"); } ChessWidget::~ChessWidget() @@ -33,13 +28,15 @@ ChessWidget::~ChessWidget() void ChessWidget::paint_event(GUI::PaintEvent& event) { - GUI::Widget::paint_event(event); + GUI::Frame::paint_event(event); GUI::Painter painter(*this); painter.add_clip_rect(event.rect()); - size_t tile_width = width() / 8; - size_t tile_height = height() / 8; + painter.translate(frame_thickness(), frame_thickness()); + + size_t tile_width = frame_inner_rect().width() / 8; + size_t tile_height = frame_inner_rect().height() / 8; unsigned coord_rank_file = (side() == Chess::Color::White) ? 0 : 7; Chess::Board& active_board = (m_playback ? board_playback() : board()); @@ -164,7 +161,8 @@ void ChessWidget::paint_event(GUI::PaintEvent& event) void ChessWidget::mousedown_event(GUI::MouseEvent& event) { - GUI::Widget::mousedown_event(event); + if (!frame_inner_rect().contains(event.position())) + return; if (event.button() == GUI::MouseButton::Right) { if (m_dragging_piece) { @@ -197,7 +195,8 @@ void ChessWidget::mousedown_event(GUI::MouseEvent& event) void ChessWidget::mouseup_event(GUI::MouseEvent& event) { - GUI::Widget::mouseup_event(event); + if (!frame_inner_rect().contains(event.position())) + return; if (event.button() == GUI::MouseButton::Right) { m_current_marking.secondary_color = event.shift(); @@ -295,7 +294,9 @@ void ChessWidget::mouseup_event(GUI::MouseEvent& event) void ChessWidget::mousemove_event(GUI::MouseEvent& event) { - GUI::Widget::mousemove_event(event); + if (!frame_inner_rect().contains(event.position())) + return; + if (!m_dragging_piece) return; @@ -361,8 +362,8 @@ void ChessWidget::set_piece_set(const StringView& set) Chess::Square ChessWidget::mouse_to_square(GUI::MouseEvent& event) const { - unsigned tile_width = width() / 8; - unsigned tile_height = height() / 8; + unsigned tile_width = frame_inner_rect().width() / 8; + unsigned tile_height = frame_inner_rect().height() / 8; if (side() == Chess::Color::White) { return { (unsigned)(7 - (event.y() / tile_height)), (unsigned)(event.x() / tile_width) }; diff --git a/Userland/Games/Chess/ChessWidget.h b/Userland/Games/Chess/ChessWidget.h index 83ea497cc37..4705dc4ed6b 100644 --- a/Userland/Games/Chess/ChessWidget.h +++ b/Userland/Games/Chess/ChessWidget.h @@ -10,16 +10,14 @@ #include #include #include -#include #include -#include +#include #include -class ChessWidget final : public GUI::Widget { - C_OBJECT(ChessWidget) +class ChessWidget final : public GUI::Frame { + C_OBJECT(ChessWidget); + public: - ChessWidget(); - ChessWidget(const StringView& set); virtual ~ChessWidget() override; virtual void paint_event(GUI::PaintEvent&) override; @@ -107,6 +105,8 @@ public: }; private: + ChessWidget(); + Chess::Board m_board; Chess::Board m_board_playback; bool m_playback { false }; diff --git a/Userland/Games/Chess/main.cpp b/Userland/Games/Chess/main.cpp index ada369fb992..1a9ad9ac236 100644 --- a/Userland/Games/Chess/main.cpp +++ b/Userland/Games/Chess/main.cpp @@ -65,9 +65,10 @@ int main(int argc, char** argv) auto size = config->read_num_entry("Display", "size", 512); window->set_title("Chess"); - window->resize(size, size); + window->set_base_size({ 4, 4 }); window->set_size_increment({ 8, 8 }); window->set_resize_aspect_ratio(1, 1); + window->resize(size - 4, size - 4); window->set_icon(app_icon.bitmap_for_size(16));