summaryrefslogtreecommitdiff
path: root/Games/2048
diff options
context:
space:
mode:
authorSergey Bugaev <bugaevc@serenityos.org>2020-08-18 14:05:44 +0300
committerAndreas Kling <kling@serenityos.org>2020-08-18 17:19:52 +0200
commit99efc01b2efbfe5484141195367490906befbf4b (patch)
tree190bbb699cf917e1725f8271e5b03f68b7e5dab7 /Games/2048
parent50d81f1e1406df6c96cb0316286f8cc7720a56ba (diff)
downloadserenity-99efc01b2efbfe5484141195367490906befbf4b.zip
2048: Automatically pick an appropriate font size
Diffstat (limited to 'Games/2048')
-rw-r--r--Games/2048/2048.cpp27
-rw-r--r--Games/2048/2048.h1
2 files changed, 27 insertions, 1 deletions
diff --git a/Games/2048/2048.cpp b/Games/2048/2048.cpp
index bb91c96367..d408c29c02 100644
--- a/Games/2048/2048.cpp
+++ b/Games/2048/2048.cpp
@@ -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)
diff --git a/Games/2048/2048.h b/Games/2048/2048.h
index 77b9c0f6e2..82deae05b1 100644
--- a/Games/2048/2048.h
+++ b/Games/2048/2048.h
@@ -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;