Minesweeper: Move configuration reading to Field
This makes more sense as it's where configuration writing happens.
This commit is contained in:
parent
9dbf453015
commit
eb4c42bfa8
Notes:
sideshowbarker
2024-07-19 13:25:07 +09:00
Author: https://github.com/Jookia Commit: https://github.com/SerenityOS/serenity/commit/eb4c42bfa87 Pull-request: https://github.com/SerenityOS/serenity/pull/264 Reviewed-by: https://github.com/awesomekling
3 changed files with 20 additions and 24 deletions
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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>();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue