From 04c3cddb1e4bb4bf7bc5f28fe2cd2108e1936250 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 4 May 2021 17:02:36 +0200 Subject: [PATCH] Snake: Spruce up the GUI a tiny bit Give the game window a GUI::Frame appearance, and make sure the menus have Alt shortcuts. :^) --- Userland/Games/Snake/SnakeGame.cpp | 12 +++++++----- Userland/Games/Snake/SnakeGame.h | 7 ++++--- Userland/Games/Snake/main.cpp | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Userland/Games/Snake/SnakeGame.cpp b/Userland/Games/Snake/SnakeGame.cpp index b48a22eed29..c8d8e423fb0 100644 --- a/Userland/Games/Snake/SnakeGame.cpp +++ b/Userland/Games/Snake/SnakeGame.cpp @@ -75,13 +75,13 @@ void SnakeGame::spawn_fruit() Gfx::IntRect SnakeGame::score_rect() const { int score_width = font().width(m_score_text); - return { width() - score_width - 2, height() - font().glyph_height() - 2, score_width, font().glyph_height() }; + return { frame_inner_rect().width() - score_width - 2, frame_inner_rect().height() - font().glyph_height() - 2, score_width, font().glyph_height() }; } Gfx::IntRect SnakeGame::high_score_rect() const { int high_score_width = font().width(m_high_score_text); - return { 2, height() - font().glyph_height() - 2, high_score_width, font().glyph_height() }; + return { frame_thickness() + 2, frame_inner_rect().height() - font().glyph_height() - 2, high_score_width, font().glyph_height() }; } void SnakeGame::timer_event(Core::TimerEvent&) @@ -179,11 +179,11 @@ void SnakeGame::keydown_event(GUI::KeyEvent& event) Gfx::IntRect SnakeGame::cell_rect(const Coordinate& coord) const { - auto game_rect = rect(); + auto game_rect = frame_inner_rect(); auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows); return { - coord.column * cell_size.width(), - coord.row * cell_size.height(), + game_rect.x() + coord.column * cell_size.width(), + game_rect.y() + coord.row * cell_size.height(), cell_size.width(), cell_size.height() }; @@ -191,7 +191,9 @@ Gfx::IntRect SnakeGame::cell_rect(const Coordinate& coord) const void SnakeGame::paint_event(GUI::PaintEvent& event) { + GUI::Frame::paint_event(event); GUI::Painter painter(*this); + painter.add_clip_rect(frame_inner_rect()); painter.add_clip_rect(event.rect()); painter.fill_rect(event.rect(), Color::Black); diff --git a/Userland/Games/Snake/SnakeGame.h b/Userland/Games/Snake/SnakeGame.h index 6f1619c495b..bdddb924c6c 100644 --- a/Userland/Games/Snake/SnakeGame.h +++ b/Userland/Games/Snake/SnakeGame.h @@ -8,10 +8,11 @@ #include #include -#include +#include + +class SnakeGame : public GUI::Frame { + C_OBJECT(SnakeGame); -class SnakeGame : public GUI::Widget { - C_OBJECT(SnakeGame) public: virtual ~SnakeGame() override; diff --git a/Userland/Games/Snake/main.cpp b/Userland/Games/Snake/main.cpp index a4d7d954cb6..0b5e8734407 100644 --- a/Userland/Games/Snake/main.cpp +++ b/Userland/Games/Snake/main.cpp @@ -54,7 +54,7 @@ int main(int argc, char** argv) window->set_double_buffering_enabled(false); window->set_title("Snake"); - window->resize(320, 320); + window->resize(324, 344); auto& game = window->set_main_widget(); @@ -70,7 +70,7 @@ int main(int argc, char** argv) GUI::Application::the()->quit(); })); - auto& help_menu = menubar->add_menu("Help"); + auto& help_menu = menubar->add_menu("&Help"); help_menu.add_action(GUI::CommonActions::make_about_action("Snake", app_icon, window)); window->set_menubar(move(menubar));