summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJookia <166291@gmail.com>2019-07-01 15:57:59 +1000
committerAndreas Kling <awesomekling@gmail.com>2019-07-01 09:51:03 +0200
commiteb4c42bfa8796800a1ab65cc81217fd27ca4bf9b (patch)
treea92e62240155698a9aace29a64fb2ee389104c5e
parent9dbf453015f4d06b054a697b57d289630578ee37 (diff)
downloadserenity-eb4c42bfa8796800a1ab65cc81217fd27ca4bf9b.zip
Minesweeper: Move configuration reading to Field
This makes more sense as it's where configuration writing happens.
-rw-r--r--Games/Minesweeper/Field.cpp16
-rw-r--r--Games/Minesweeper/Field.h11
-rw-r--r--Games/Minesweeper/main.cpp17
3 files changed, 20 insertions, 24 deletions
diff --git a/Games/Minesweeper/Field.cpp b/Games/Minesweeper/Field.cpp
index 47ca0ec41e..9baeb81315 100644
--- a/Games/Minesweeper/Field.cpp
+++ b/Games/Minesweeper/Field.cpp
@@ -92,11 +92,12 @@ public:
bool m_chord { false };
};
-Field::Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, GWidget* parent)
+Field::Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, GWidget* parent, Function<void(Size)> on_size_changed)
: GFrame(parent)
, m_face_button(face_button)
, m_flag_label(flag_label)
, m_time_label(time_label)
+ , m_on_size_changed(move(on_size_changed))
{
srand(time(nullptr));
m_timer.on_timeout = [this] {
@@ -123,6 +124,16 @@ Field::Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, GWidg
m_face_button.on_click = [this](auto&) { reset(); };
set_face(Face::Default);
+
+ {
+ auto config = CConfigFile::get_for_app("Minesweeper");
+ bool single_chording = config->read_num_entry("Minesweeper", "SingleChording", false);
+ int mine_count = config->read_num_entry("Game", "MineCount", 10);
+ int rows = config->read_num_entry("Game", "Rows", 9);
+ int columns = config->read_num_entry("Game", "Columns", 9);
+ set_field_size(rows, columns, mine_count);
+ set_single_chording(single_chording);
+ }
}
Field::~Field()
@@ -458,8 +469,7 @@ void Field::set_field_size(int rows, int columns, int mine_count)
m_mine_count = mine_count;
set_preferred_size({ frame_thickness() * 2 + m_columns * square_size(), frame_thickness() * 2 + m_rows * square_size() });
reset();
- if (on_size_changed)
- on_size_changed();
+ m_on_size_changed(preferred_size());
}
void Field::set_single_chording(bool enabled) {
diff --git a/Games/Minesweeper/Field.h b/Games/Minesweeper/Field.h
index 75ab1334ce..eef12f936c 100644
--- a/Games/Minesweeper/Field.h
+++ b/Games/Minesweeper/Field.h
@@ -36,7 +36,7 @@ class Field final : public GFrame {
friend class SquareLabel;
public:
- Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, GWidget* parent);
+ Field(GLabel& flag_label, GLabel& time_label, GButton& face_button, GWidget* parent, Function<void(Size)> on_size_changed);
virtual ~Field() override;
int rows() const { return m_rows; }
@@ -50,8 +50,6 @@ public:
void reset();
- Function<void()> on_size_changed;
-
private:
virtual void paint_event(GPaintEvent&) override;
@@ -80,9 +78,9 @@ private:
};
void set_face(Face);
- int m_rows { 9 };
- int m_columns { 9 };
- int m_mine_count { 10 };
+ int m_rows { 0 };
+ int m_columns { 0 };
+ int m_mine_count { 0 };
int m_unswept_empties { 0 };
Vector<OwnPtr<Square>> m_squares;
RefPtr<GraphicsBitmap> m_mine_bitmap;
@@ -103,4 +101,5 @@ private:
bool m_chord_preview { false };
bool m_first_click { true };
bool m_single_chording { true };
+ Function<void(Size)> m_on_size_changed;
};
diff --git a/Games/Minesweeper/main.cpp b/Games/Minesweeper/main.cpp
index 81a6926537..0425547fa4 100644
--- a/Games/Minesweeper/main.cpp
+++ b/Games/Minesweeper/main.cpp
@@ -39,23 +39,10 @@ int main(int argc, char** argv)
auto* time_icon_label = new GLabel(container);
time_icon_label->set_icon(GraphicsBitmap::load_from_file("/res/icons/minesweeper/timer.png"));
auto* time_label = new GLabel(container);
- auto* field = new Field(*flag_label, *time_label, *face_button, widget);
-
- field->on_size_changed = [&] {
- auto size = field->preferred_size();
+ auto* field = new Field(*flag_label, *time_label, *face_button, widget, [&](Size size) {
size.set_height(size.height() + container->preferred_size().height());
window->resize(size);
- };
-
- {
- auto config = CConfigFile::get_for_app("Minesweeper");
- bool single_chording = config->read_num_entry("Minesweeper", "SingleChording", false);
- int mine_count = config->read_num_entry("Game", "MineCount", 10);
- int rows = config->read_num_entry("Game", "Rows", 9);
- int columns = config->read_num_entry("Game", "Columns", 9);
- field->set_field_size(rows, columns, mine_count);
- field->set_single_chording(single_chording);
- }
+ });
auto menubar = make<GMenuBar>();