Snake: Spruce up the GUI a tiny bit

Give the game window a GUI::Frame appearance, and make sure the
menus have Alt shortcuts. :^)
This commit is contained in:
Andreas Kling 2021-05-04 17:02:36 +02:00
parent d136fafde7
commit 04c3cddb1e
Notes: sideshowbarker 2024-07-18 18:43:15 +09:00
3 changed files with 13 additions and 10 deletions

View file

@ -75,13 +75,13 @@ void SnakeGame::spawn_fruit()
Gfx::IntRect SnakeGame::score_rect() const Gfx::IntRect SnakeGame::score_rect() const
{ {
int score_width = font().width(m_score_text); 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 Gfx::IntRect SnakeGame::high_score_rect() const
{ {
int high_score_width = font().width(m_high_score_text); 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&) 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 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); auto cell_size = Gfx::IntSize(game_rect.width() / m_columns, game_rect.height() / m_rows);
return { return {
coord.column * cell_size.width(), game_rect.x() + coord.column * cell_size.width(),
coord.row * cell_size.height(), game_rect.y() + coord.row * cell_size.height(),
cell_size.width(), cell_size.width(),
cell_size.height() cell_size.height()
}; };
@ -191,7 +191,9 @@ Gfx::IntRect SnakeGame::cell_rect(const Coordinate& coord) const
void SnakeGame::paint_event(GUI::PaintEvent& event) void SnakeGame::paint_event(GUI::PaintEvent& event)
{ {
GUI::Frame::paint_event(event);
GUI::Painter painter(*this); GUI::Painter painter(*this);
painter.add_clip_rect(frame_inner_rect());
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
painter.fill_rect(event.rect(), Color::Black); painter.fill_rect(event.rect(), Color::Black);

View file

@ -8,10 +8,11 @@
#include <AK/CircularQueue.h> #include <AK/CircularQueue.h>
#include <AK/NonnullRefPtrVector.h> #include <AK/NonnullRefPtrVector.h>
#include <LibGUI/Widget.h> #include <LibGUI/Frame.h>
class SnakeGame : public GUI::Frame {
C_OBJECT(SnakeGame);
class SnakeGame : public GUI::Widget {
C_OBJECT(SnakeGame)
public: public:
virtual ~SnakeGame() override; virtual ~SnakeGame() override;

View file

@ -54,7 +54,7 @@ int main(int argc, char** argv)
window->set_double_buffering_enabled(false); window->set_double_buffering_enabled(false);
window->set_title("Snake"); window->set_title("Snake");
window->resize(320, 320); window->resize(324, 344);
auto& game = window->set_main_widget<SnakeGame>(); auto& game = window->set_main_widget<SnakeGame>();
@ -70,7 +70,7 @@ int main(int argc, char** argv)
GUI::Application::the()->quit(); 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)); help_menu.add_action(GUI::CommonActions::make_about_action("Snake", app_icon, window));
window->set_menubar(move(menubar)); window->set_menubar(move(menubar));