mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:47:35 +00:00
2048: Move score to a status bar
See how straightforward this was? That's because, thanks to the separation between the model and the view, we can tweak the view without modifying the model in any way.
This commit is contained in:
parent
05ea144961
commit
8dd5b0af4e
3 changed files with 11 additions and 31 deletions
|
@ -39,16 +39,6 @@ BoardView::~BoardView()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void BoardView::set_score(size_t score)
|
|
||||||
{
|
|
||||||
if (m_score == score && !m_score_text.is_null())
|
|
||||||
return;
|
|
||||||
|
|
||||||
m_score = score;
|
|
||||||
m_score_text = String::format("Score: %d", score);
|
|
||||||
update();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BoardView::set_board(const Game::Board* board)
|
void BoardView::set_board(const Game::Board* board)
|
||||||
{
|
{
|
||||||
if (m_board == board)
|
if (m_board == board)
|
||||||
|
@ -82,12 +72,6 @@ void BoardView::pick_font()
|
||||||
set_font(font);
|
set_font(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
Gfx::IntRect BoardView::score_rect() const
|
|
||||||
{
|
|
||||||
int score_width = font().width(m_score_text);
|
|
||||||
return { 0, 2, score_width, font().glyph_height() };
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t BoardView::rows() const
|
size_t BoardView::rows() const
|
||||||
{
|
{
|
||||||
if (!m_board)
|
if (!m_board)
|
||||||
|
@ -106,12 +90,10 @@ size_t BoardView::columns() const
|
||||||
|
|
||||||
void BoardView::resize_event(GUI::ResizeEvent&)
|
void BoardView::resize_event(GUI::ResizeEvent&)
|
||||||
{
|
{
|
||||||
int score_height = font().glyph_height() + 2;
|
|
||||||
|
|
||||||
constexpr float padding_ratio = 7;
|
constexpr float padding_ratio = 7;
|
||||||
m_padding = min(
|
m_padding = min(
|
||||||
width() / (columns() * (padding_ratio + 1) + 1),
|
width() / (columns() * (padding_ratio + 1) + 1),
|
||||||
(height() - score_height) / (rows() * (padding_ratio + 1) + 1));
|
height() / (rows() * (padding_ratio + 1) + 1));
|
||||||
m_cell_size = m_padding * padding_ratio;
|
m_cell_size = m_padding * padding_ratio;
|
||||||
|
|
||||||
pick_font();
|
pick_font();
|
||||||
|
@ -195,17 +177,13 @@ void BoardView::paint_event(GUI::PaintEvent&)
|
||||||
}
|
}
|
||||||
auto& board = *m_board;
|
auto& board = *m_board;
|
||||||
|
|
||||||
painter.draw_text(score_rect(), m_score_text, font(), Gfx::TextAlignment::TopLeft, palette().color(ColorRole::BaseText));
|
|
||||||
|
|
||||||
int score_height = font().glyph_height() + 2;
|
|
||||||
|
|
||||||
Gfx::IntRect field_rect {
|
Gfx::IntRect field_rect {
|
||||||
0,
|
0,
|
||||||
0,
|
0,
|
||||||
static_cast<int>(m_padding + (m_cell_size + m_padding) * columns()),
|
static_cast<int>(m_padding + (m_cell_size + m_padding) * columns()),
|
||||||
static_cast<int>(m_padding + (m_cell_size + m_padding) * rows())
|
static_cast<int>(m_padding + (m_cell_size + m_padding) * rows())
|
||||||
};
|
};
|
||||||
field_rect.center_within({ 0, score_height, width(), height() - score_height });
|
field_rect.center_within(rect());
|
||||||
painter.fill_rect(field_rect, background_color);
|
painter.fill_rect(field_rect, background_color);
|
||||||
|
|
||||||
for (size_t column = 0; column < columns(); ++column) {
|
for (size_t column = 0; column < columns(); ++column) {
|
||||||
|
|
|
@ -36,7 +36,6 @@ public:
|
||||||
BoardView(const Game::Board*);
|
BoardView(const Game::Board*);
|
||||||
virtual ~BoardView() override;
|
virtual ~BoardView() override;
|
||||||
|
|
||||||
void set_score(size_t score);
|
|
||||||
void set_board(const Game::Board* board);
|
void set_board(const Game::Board* board);
|
||||||
|
|
||||||
Function<void(Game::Direction)> on_move;
|
Function<void(Game::Direction)> on_move;
|
||||||
|
@ -50,7 +49,6 @@ private:
|
||||||
size_t columns() const;
|
size_t columns() const;
|
||||||
|
|
||||||
void pick_font();
|
void pick_font();
|
||||||
Gfx::IntRect score_rect() const;
|
|
||||||
|
|
||||||
Color background_color_for_cell(u32 value);
|
Color background_color_for_cell(u32 value);
|
||||||
Color text_color_for_cell(u32 value);
|
Color text_color_for_cell(u32 value);
|
||||||
|
@ -58,7 +56,5 @@ private:
|
||||||
float m_padding { 0 };
|
float m_padding { 0 };
|
||||||
float m_cell_size { 0 };
|
float m_cell_size { 0 };
|
||||||
|
|
||||||
size_t m_score { 0 };
|
|
||||||
String m_score_text;
|
|
||||||
const Game::Board* m_board { nullptr };
|
const Game::Board* m_board { nullptr };
|
||||||
};
|
};
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
#include <LibGUI/Menu.h>
|
#include <LibGUI/Menu.h>
|
||||||
#include <LibGUI/MenuBar.h>
|
#include <LibGUI/MenuBar.h>
|
||||||
#include <LibGUI/MessageBox.h>
|
#include <LibGUI/MessageBox.h>
|
||||||
|
#include <LibGUI/StatusBar.h>
|
||||||
#include <LibGUI/Window.h>
|
#include <LibGUI/Window.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
@ -70,15 +71,20 @@ int main(int argc, char** argv)
|
||||||
window->set_title("2048");
|
window->set_title("2048");
|
||||||
window->resize(324, 336);
|
window->resize(324, 336);
|
||||||
|
|
||||||
|
auto& main_widget = window->set_main_widget<GUI::Widget>();
|
||||||
|
main_widget.set_layout<GUI::VerticalBoxLayout>();
|
||||||
|
main_widget.set_fill_with_background_color(true);
|
||||||
|
|
||||||
Game game { 4, 4 };
|
Game game { 4, 4 };
|
||||||
|
|
||||||
auto& board_view = window->set_main_widget<BoardView>(&game.board());
|
auto& board_view = main_widget.add<BoardView>(&game.board());
|
||||||
board_view.set_fill_with_background_color(true);
|
board_view.set_focus(true);
|
||||||
|
auto& statusbar = main_widget.add<GUI::StatusBar>();
|
||||||
|
|
||||||
auto update = [&]() {
|
auto update = [&]() {
|
||||||
board_view.set_board(&game.board());
|
board_view.set_board(&game.board());
|
||||||
board_view.set_score(game.score());
|
|
||||||
board_view.update();
|
board_view.update();
|
||||||
|
statusbar.set_text(String::format("Score: %d", game.score()));
|
||||||
};
|
};
|
||||||
|
|
||||||
update();
|
update();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue