1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:47:35 +00:00

2048: Automatically pick an appropriate font size

This commit is contained in:
Sergey Bugaev 2020-08-18 14:05:44 +03:00 committed by Andreas Kling
parent 50d81f1e14
commit 99efc01b2e
2 changed files with 27 additions and 1 deletions

View file

@ -37,7 +37,6 @@
TwentyFortyEightGame::TwentyFortyEightGame()
{
set_font(GUI::FontDatabase::the().get_by_name("Liza Regular"));
srand(time(nullptr));
reset();
}
@ -61,6 +60,30 @@ void TwentyFortyEightGame::add_tile(Board& board, int max_tile_value)
board[row][column] = max(2, value);
}
void TwentyFortyEightGame::pick_font()
{
constexpr static auto liza_regular = "Liza Regular";
String best_font_name = liza_regular;
int best_font_size = -1;
auto& font_database = GUI::FontDatabase::the();
font_database.for_each_font([&](const StringView& font_name) {
// Only consider variations of Liza Regular.
if (!font_name.starts_with(liza_regular))
return;
auto metadata = font_database.get_metadata_by_name(font_name);
if (!metadata.has_value())
return;
auto size = metadata.value().glyph_height;
if (size * 2 <= m_cell_size && size > best_font_size) {
best_font_name = font_name;
best_font_size = size;
}
});
auto font = font_database.get_by_name(best_font_name);
set_font(font);
}
void TwentyFortyEightGame::reset()
{
auto initial_state = [&]() -> State {
@ -222,6 +245,8 @@ void TwentyFortyEightGame::resize_event(GUI::ResizeEvent&)
width() / (m_columns * (padding_ratio + 1) + 1),
(height() - score_height) / (m_rows * (padding_ratio + 1) + 1));
m_cell_size = m_padding * padding_ratio;
pick_font();
}
void TwentyFortyEightGame::keydown_event(GUI::KeyEvent& event)

View file

@ -50,6 +50,7 @@ private:
virtual void paint_event(GUI::PaintEvent&) override;
virtual void keydown_event(GUI::KeyEvent&) override;
void pick_font();
void game_over();
Gfx::IntRect score_rect() const;