summaryrefslogtreecommitdiff
path: root/Userland/Games/2048/BoardView.cpp
blob: 3608f0aa44b72dd625b36a45bc2a3cfbaf317922 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "BoardView.h"
#include <LibGUI/Painter.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Palette.h>

BoardView::BoardView(Game::Board const* board)
    : m_board(board)
{
}

BoardView::~BoardView()
{
}

void BoardView::set_board(Game::Board const* board)
{
    if (has_timer())
        stop_timer();

    slide_animation_frame = 0;
    pop_in_animation_frame = 0;
    start_timer(frame_duration_ms);

    if (m_board == board)
        return;

    if (!board) {
        m_board = nullptr;
        return;
    }

    bool must_resize = !m_board || m_board->tiles().size() != board->tiles().size();

    m_board = board;

    if (must_resize)
        resize();

    update();
}

void BoardView::pick_font()
{
    String best_font_name;
    int best_font_size = -1;
    auto& font_database = Gfx::FontDatabase::the();
    font_database.for_each_font([&](Gfx::Font const& font) {
        if (font.family() != "Liza" || font.weight() != 700)
            return;
        auto size = font.glyph_height();
        if (size * 2 <= m_cell_size && size > best_font_size) {
            best_font_name = font.qualified_name();
            best_font_size = size;
        }
    });

    auto font = font_database.get_by_name(best_font_name);
    set_font(font);

    m_min_cell_size = best_font_size;
}

size_t BoardView::rows() const
{
    if (!m_board)
        return 0;
    return m_board->tiles().size();
}

size_t BoardView::columns() const
{
    if (!m_board)
        return 0;
    if (m_board->tiles().is_empty())
        return 0;
    return m_board->tiles()[0].size();
}

void BoardView::resize_event(GUI::ResizeEvent&)
{
    resize();
}

void BoardView::resize()
{
    constexpr float padding_ratio = 7;
    m_padding = min(
        width() / (columns() * (padding_ratio + 1) + 1),
        height() / (rows() * (padding_ratio + 1) + 1));
    m_cell_size = m_padding * padding_ratio;

    pick_font();
}

void BoardView::keydown_event(GUI::KeyEvent& event)
{
    if (!on_move)
        return;

    switch (event.key()) {
    case KeyCode::Key_A:
    case KeyCode::Key_Left:
        on_move(Game::Direction::Left);
        break;
    case KeyCode::Key_D:
    case KeyCode::Key_Right:
        on_move(Game::Direction::Right);
        break;
    case KeyCode::Key_W:
    case KeyCode::Key_Up:
        on_move(Game::Direction::Up);
        break;
    case KeyCode::Key_S:
    case KeyCode::Key_Down:
        on_move(Game::Direction::Down);
        break;
    default:
        return;
    }
}

Gfx::Color BoardView::background_color_for_cell(u32 value)
{
    switch (value) {
    case 0:
        return Color::from_rgb(0xcdc1b4);
    case 2:
        return Color::from_rgb(0xeee4da);
    case 4:
        return Color::from_rgb(0xede0c8);
    case 8:
        return Color::from_rgb(0xf2b179);
    case 16:
        return Color::from_rgb(0xf59563);
    case 32:
        return Color::from_rgb(0xf67c5f);
    case 64:
        return Color::from_rgb(0xf65e3b);
    case 128:
        return Color::from_rgb(0xedcf72);
    case 256:
        return Color::from_rgb(0xedcc61);
    case 512:
        return Color::from_rgb(0xedc850);
    case 1024:
        return Color::from_rgb(0xedc53f);
    case 2048:
        return Color::from_rgb(0xedc22e);
    default:
        VERIFY(value > 2048);
        return Color::from_rgb(0x3c3a32);
    }
}

Gfx::Color BoardView::text_color_for_cell(u32 value)
{
    if (value <= 4)
        return Color::from_rgb(0x776e65);
    return Color::from_rgb(0xf9f6f2);
}

void BoardView::timer_event(Core::TimerEvent&)
{
    if (slide_animation_frame < animation_duration) {
        slide_animation_frame++;
        update();
    } else 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)
{
    Frame::paint_event(event);

    Color background_color = Color::from_rgb(0xbbada0);

    GUI::Painter painter(*this);
    painter.add_clip_rect(event.rect());
    painter.add_clip_rect(frame_inner_rect());
    painter.translate(frame_thickness(), frame_thickness());

    if (!m_board) {
        painter.fill_rect(rect(), background_color);
        return;
    }
    auto& tiles = m_board->tiles();

    Gfx::IntRect field_rect {
        0,
        0,
        static_cast<int>(m_padding + (m_cell_size + m_padding) * columns()),
        static_cast<int>(m_padding + (m_cell_size + m_padding) * rows())
    };
    field_rect.center_within(rect());
    painter.fill_rect(field_rect, background_color);

    auto tile_center = [&](size_t row, size_t column) {
        return Gfx::IntPoint {
            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 + m_cell_size / 2,
        };
    };

    if (slide_animation_frame < animation_duration) {
        // background
        for (size_t column = 0; column < columns(); ++column) {
            for (size_t row = 0; row < rows(); ++row) {
                auto center = tile_center(row, column);
                auto tile_size = Gfx::IntSize { m_cell_size, m_cell_size };
                auto rect = Gfx::IntRect::centered_on(center, tile_size);
                painter.fill_rect(rect, background_color_for_cell(0));
            }
        }

        for (auto& sliding_tile : m_board->sliding_tiles()) {
            auto center_from = tile_center(sliding_tile.row_from, sliding_tile.column_from);
            auto center_to = tile_center(sliding_tile.row_to, sliding_tile.column_to);
            auto offset = Gfx::FloatPoint(center_to - center_from);
            auto center = center_from + Gfx::IntPoint(offset * (slide_animation_frame / (float)animation_duration));

            auto tile_size = Gfx::IntSize { m_cell_size, m_cell_size };
            auto rect = Gfx::IntRect::centered_on(center, tile_size);

            painter.fill_rect(rect, background_color_for_cell(sliding_tile.value_from));
            painter.draw_text(rect, String::number(sliding_tile.value_from), font(), Gfx::TextAlignment::Center, text_color_for_cell(sliding_tile.value_from));
        }
    } else {
        for (size_t column = 0; column < columns(); ++column) {
            for (size_t row = 0; row < rows(); ++row) {
                auto center = tile_center(row, column);
                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];
                painter.fill_rect(rect, background_color_for_cell(entry));
                if (entry > 0)
                    painter.draw_text(rect, String::number(entry), font(), Gfx::TextAlignment::Center, text_color_for_cell(entry));
            }
        }
    }
}