summaryrefslogtreecommitdiff
path: root/Userland/Games/2048/BoardView.h
blob: 9b97632f670620e0e950cbeb321859a43254f9c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include "Game.h"
#include <LibGUI/Frame.h>

class BoardView final : public GUI::Frame {
    C_OBJECT(BoardView);

public:
    virtual ~BoardView() override;
    void set_board(Game::Board const* board);

    Function<void(Game::Direction)> on_move;

private:
    explicit BoardView(Game::Board const*);

    virtual void resize_event(GUI::ResizeEvent&) override;
    virtual void paint_event(GUI::PaintEvent&) override;
    virtual void keydown_event(GUI::KeyEvent&) override;
    virtual void timer_event(Core::TimerEvent&) override;

    size_t rows() const;
    size_t columns() const;

    void pick_font();
    void resize();

    Color background_color_for_cell(u32 value);
    Color text_color_for_cell(u32 value);

    float m_padding { 0 };
    float m_min_cell_size { 0 };
    float m_cell_size { 0 };

    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;
    int slide_animation_frame = 0;
};