mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 12:37:44 +00:00
2048: Add pop-in animation for newly added tiles
This commit is contained in:
parent
30c831a3be
commit
9ce5ce3560
3 changed files with 47 additions and 5 deletions
|
@ -21,6 +21,12 @@ BoardView::~BoardView()
|
||||||
|
|
||||||
void BoardView::set_board(Game::Board const* board)
|
void BoardView::set_board(Game::Board const* board)
|
||||||
{
|
{
|
||||||
|
if (has_timer())
|
||||||
|
stop_timer();
|
||||||
|
|
||||||
|
pop_in_animation_frame = 0;
|
||||||
|
start_timer(frame_duration_ms);
|
||||||
|
|
||||||
if (m_board == board)
|
if (m_board == board)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -56,6 +62,8 @@ void BoardView::pick_font()
|
||||||
|
|
||||||
auto font = font_database.get_by_name(best_font_name);
|
auto font = font_database.get_by_name(best_font_name);
|
||||||
set_font(font);
|
set_font(font);
|
||||||
|
|
||||||
|
m_min_cell_size = best_font_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BoardView::rows() const
|
size_t BoardView::rows() const
|
||||||
|
@ -157,6 +165,16 @@ Gfx::Color BoardView::text_color_for_cell(u32 value)
|
||||||
return Color::from_rgb(0xf9f6f2);
|
return Color::from_rgb(0xf9f6f2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BoardView::timer_event(Core::TimerEvent&)
|
||||||
|
{
|
||||||
|
if (pop_in_animation_frame < animation_duration) {
|
||||||
|
pop_in_animation_frame++;
|
||||||
|
update();
|
||||||
|
if (pop_in_animation_frame == animation_duration)
|
||||||
|
stop_timer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void BoardView::paint_event(GUI::PaintEvent& event)
|
void BoardView::paint_event(GUI::PaintEvent& event)
|
||||||
{
|
{
|
||||||
Frame::paint_event(event);
|
Frame::paint_event(event);
|
||||||
|
@ -185,12 +203,16 @@ void BoardView::paint_event(GUI::PaintEvent& event)
|
||||||
|
|
||||||
for (size_t column = 0; column < columns(); ++column) {
|
for (size_t column = 0; column < columns(); ++column) {
|
||||||
for (size_t row = 0; row < rows(); ++row) {
|
for (size_t row = 0; row < rows(); ++row) {
|
||||||
auto rect = Gfx::IntRect {
|
auto center = Gfx::IntPoint {
|
||||||
field_rect.x() + m_padding + (m_cell_size + m_padding) * column,
|
field_rect.x() + m_padding + (m_cell_size + m_padding) * column + m_cell_size / 2,
|
||||||
field_rect.y() + m_padding + (m_cell_size + m_padding) * row,
|
field_rect.y() + m_padding + (m_cell_size + m_padding) * row + m_cell_size / 2,
|
||||||
m_cell_size,
|
|
||||||
m_cell_size,
|
|
||||||
};
|
};
|
||||||
|
auto tile_size = Gfx::IntSize { m_cell_size, m_cell_size };
|
||||||
|
if (pop_in_animation_frame < animation_duration && Game::Board::Position { row, column } == m_board->last_added_position) {
|
||||||
|
float pop_in_size = m_min_cell_size + (m_cell_size - m_min_cell_size) * (pop_in_animation_frame / (float)animation_duration);
|
||||||
|
tile_size = Gfx::IntSize { pop_in_size, pop_in_size };
|
||||||
|
}
|
||||||
|
auto rect = Gfx::IntRect::centered_on(center, tile_size);
|
||||||
auto entry = tiles[row][column];
|
auto entry = tiles[row][column];
|
||||||
painter.fill_rect(rect, background_color_for_cell(entry));
|
painter.fill_rect(rect, background_color_for_cell(entry));
|
||||||
if (entry > 0)
|
if (entry > 0)
|
||||||
|
|
|
@ -24,6 +24,7 @@ private:
|
||||||
virtual void resize_event(GUI::ResizeEvent&) override;
|
virtual void resize_event(GUI::ResizeEvent&) override;
|
||||||
virtual void paint_event(GUI::PaintEvent&) override;
|
virtual void paint_event(GUI::PaintEvent&) override;
|
||||||
virtual void keydown_event(GUI::KeyEvent&) override;
|
virtual void keydown_event(GUI::KeyEvent&) override;
|
||||||
|
virtual void timer_event(Core::TimerEvent&) override;
|
||||||
|
|
||||||
size_t rows() const;
|
size_t rows() const;
|
||||||
size_t columns() const;
|
size_t columns() const;
|
||||||
|
@ -35,7 +36,13 @@ private:
|
||||||
Color text_color_for_cell(u32 value);
|
Color text_color_for_cell(u32 value);
|
||||||
|
|
||||||
float m_padding { 0 };
|
float m_padding { 0 };
|
||||||
|
float m_min_cell_size { 0 };
|
||||||
float m_cell_size { 0 };
|
float m_cell_size { 0 };
|
||||||
|
|
||||||
Game::Board const* m_board { nullptr };
|
Game::Board const* m_board { nullptr };
|
||||||
|
|
||||||
|
static constexpr int frame_duration_ms = 1000 / 60;
|
||||||
|
static constexpr int animation_duration = 5;
|
||||||
|
|
||||||
|
int pop_in_animation_frame = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -41,10 +41,23 @@ public:
|
||||||
|
|
||||||
Tiles tiles;
|
Tiles tiles;
|
||||||
|
|
||||||
|
struct Position {
|
||||||
|
size_t row;
|
||||||
|
size_t column;
|
||||||
|
|
||||||
|
bool operator==(Position const& other) const
|
||||||
|
{
|
||||||
|
return row == other.row && column == other.column;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
void add_tile(size_t row, size_t column, u32 value)
|
void add_tile(size_t row, size_t column, u32 value)
|
||||||
{
|
{
|
||||||
tiles[row][column] = value;
|
tiles[row][column] = value;
|
||||||
|
last_added_position = Position { row, column };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Position last_added_position { 0, 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
Board const& board() const { return m_board; }
|
Board const& board() const { return m_board; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue