mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 21:27:35 +00:00
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
3 changed files with 20 additions and 24 deletions
|
@ -92,11 +92,12 @@ public:
|
||||||
bool m_chord { false };
|
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)
|
: GFrame(parent)
|
||||||
, m_face_button(face_button)
|
, m_face_button(face_button)
|
||||||
, m_flag_label(flag_label)
|
, m_flag_label(flag_label)
|
||||||
, m_time_label(time_label)
|
, m_time_label(time_label)
|
||||||
|
, m_on_size_changed(move(on_size_changed))
|
||||||
{
|
{
|
||||||
srand(time(nullptr));
|
srand(time(nullptr));
|
||||||
m_timer.on_timeout = [this] {
|
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(); };
|
m_face_button.on_click = [this](auto&) { reset(); };
|
||||||
set_face(Face::Default);
|
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()
|
Field::~Field()
|
||||||
|
@ -458,8 +469,7 @@ void Field::set_field_size(int rows, int columns, int mine_count)
|
||||||
m_mine_count = mine_count;
|
m_mine_count = mine_count;
|
||||||
set_preferred_size({ frame_thickness() * 2 + m_columns * square_size(), frame_thickness() * 2 + m_rows * square_size() });
|
set_preferred_size({ frame_thickness() * 2 + m_columns * square_size(), frame_thickness() * 2 + m_rows * square_size() });
|
||||||
reset();
|
reset();
|
||||||
if (on_size_changed)
|
m_on_size_changed(preferred_size());
|
||||||
on_size_changed();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Field::set_single_chording(bool enabled) {
|
void Field::set_single_chording(bool enabled) {
|
||||||
|
|
|
@ -36,7 +36,7 @@ class Field final : public GFrame {
|
||||||
friend class SquareLabel;
|
friend class SquareLabel;
|
||||||
|
|
||||||
public:
|
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;
|
virtual ~Field() override;
|
||||||
|
|
||||||
int rows() const { return m_rows; }
|
int rows() const { return m_rows; }
|
||||||
|
@ -50,8 +50,6 @@ public:
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
Function<void()> on_size_changed;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void paint_event(GPaintEvent&) override;
|
virtual void paint_event(GPaintEvent&) override;
|
||||||
|
|
||||||
|
@ -80,9 +78,9 @@ private:
|
||||||
};
|
};
|
||||||
void set_face(Face);
|
void set_face(Face);
|
||||||
|
|
||||||
int m_rows { 9 };
|
int m_rows { 0 };
|
||||||
int m_columns { 9 };
|
int m_columns { 0 };
|
||||||
int m_mine_count { 10 };
|
int m_mine_count { 0 };
|
||||||
int m_unswept_empties { 0 };
|
int m_unswept_empties { 0 };
|
||||||
Vector<OwnPtr<Square>> m_squares;
|
Vector<OwnPtr<Square>> m_squares;
|
||||||
RefPtr<GraphicsBitmap> m_mine_bitmap;
|
RefPtr<GraphicsBitmap> m_mine_bitmap;
|
||||||
|
@ -103,4 +101,5 @@ private:
|
||||||
bool m_chord_preview { false };
|
bool m_chord_preview { false };
|
||||||
bool m_first_click { true };
|
bool m_first_click { true };
|
||||||
bool m_single_chording { 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);
|
auto* time_icon_label = new GLabel(container);
|
||||||
time_icon_label->set_icon(GraphicsBitmap::load_from_file("/res/icons/minesweeper/timer.png"));
|
time_icon_label->set_icon(GraphicsBitmap::load_from_file("/res/icons/minesweeper/timer.png"));
|
||||||
auto* time_label = new GLabel(container);
|
auto* time_label = new GLabel(container);
|
||||||
auto* field = new Field(*flag_label, *time_label, *face_button, widget);
|
auto* field = new Field(*flag_label, *time_label, *face_button, widget, [&](Size size) {
|
||||||
|
|
||||||
field->on_size_changed = [&] {
|
|
||||||
auto size = field->preferred_size();
|
|
||||||
size.set_height(size.height() + container->preferred_size().height());
|
size.set_height(size.height() + container->preferred_size().height());
|
||||||
window->resize(size);
|
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>();
|
auto menubar = make<GMenuBar>();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue