mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 04:17:34 +00:00
Minesweeper: Make a factory function for Field and propagate errors
This fixes a pleasant 8 FIXMEs. :^)
This commit is contained in:
parent
cba9df1c53
commit
6ddc358a2b
3 changed files with 26 additions and 11 deletions
|
@ -108,12 +108,32 @@ private:
|
||||||
bool m_chord { false };
|
bool m_chord { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ErrorOr<NonnullRefPtr<Field>> Field::create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed)
|
||||||
|
{
|
||||||
|
auto field = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) Field(flag_label, time_label, face_button, move(on_size_changed))));
|
||||||
|
field->m_mine_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/mine.png"sv));
|
||||||
|
field->m_flag_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/flag.png"sv));
|
||||||
|
field->m_badflag_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/badflag.png"sv));
|
||||||
|
field->m_consider_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/consider.png"sv));
|
||||||
|
field->m_default_face_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/face-default.png"sv));
|
||||||
|
field->m_good_face_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/face-good.png"sv));
|
||||||
|
field->m_bad_face_bitmap = TRY(Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/face-bad.png"sv));
|
||||||
|
for (int i = 0; i < 8; ++i)
|
||||||
|
field->m_number_bitmap[i] = TRY(Gfx::Bitmap::try_load_from_file(DeprecatedString::formatted("/res/icons/minesweeper/{}.png", i + 1)));
|
||||||
|
field->initialize();
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
|
||||||
Field::Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed)
|
Field::Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed)
|
||||||
: m_mine_palette(GUI::Application::the()->palette().impl().clone())
|
: m_mine_palette(GUI::Application::the()->palette().impl().clone())
|
||||||
, 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))
|
, m_on_size_changed(move(on_size_changed))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Field::initialize()
|
||||||
{
|
{
|
||||||
m_timer = Core::Timer::create_repeating(
|
m_timer = Core::Timer::create_repeating(
|
||||||
1000, [this] {
|
1000, [this] {
|
||||||
|
@ -121,20 +141,11 @@ Field::Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_b
|
||||||
m_time_label.set_text(human_readable_digital_time(m_time_elapsed));
|
m_time_label.set_text(human_readable_digital_time(m_time_elapsed));
|
||||||
},
|
},
|
||||||
this);
|
this);
|
||||||
m_mine_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/mine.png"sv).release_value_but_fixme_should_propagate_errors();
|
|
||||||
m_flag_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/flag.png"sv).release_value_but_fixme_should_propagate_errors();
|
|
||||||
m_badflag_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/badflag.png"sv).release_value_but_fixme_should_propagate_errors();
|
|
||||||
m_consider_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/consider.png"sv).release_value_but_fixme_should_propagate_errors();
|
|
||||||
m_default_face_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/face-default.png"sv).release_value_but_fixme_should_propagate_errors();
|
|
||||||
m_good_face_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/face-good.png"sv).release_value_but_fixme_should_propagate_errors();
|
|
||||||
m_bad_face_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/minesweeper/face-bad.png"sv).release_value_but_fixme_should_propagate_errors();
|
|
||||||
for (int i = 0; i < 8; ++i)
|
|
||||||
m_number_bitmap[i] = Gfx::Bitmap::try_load_from_file(DeprecatedString::formatted("/res/icons/minesweeper/{}.png", i + 1)).release_value_but_fixme_should_propagate_errors();
|
|
||||||
// Square with mine will be filled with background color later, i.e. red
|
// Square with mine will be filled with background color later, i.e. red
|
||||||
m_mine_palette.set_color(Gfx::ColorRole::Base, Color::from_rgb(0xff4040));
|
m_mine_palette.set_color(Gfx::ColorRole::Base, Color::from_rgb(0xff4040));
|
||||||
|
|
||||||
set_fill_with_background_color(true);
|
set_fill_with_background_color(true);
|
||||||
reset();
|
|
||||||
|
|
||||||
m_face_button.on_click = [this](auto) { reset(); };
|
m_face_button.on_click = [this](auto) { reset(); };
|
||||||
set_face(Face::Default);
|
set_face(Face::Default);
|
||||||
|
|
|
@ -44,6 +44,7 @@ class Field final : public GUI::Frame {
|
||||||
friend class SquareLabel;
|
friend class SquareLabel;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
static ErrorOr<NonnullRefPtr<Field>> create(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed);
|
||||||
virtual ~Field() override = default;
|
virtual ~Field() override = default;
|
||||||
|
|
||||||
enum class Difficulty {
|
enum class Difficulty {
|
||||||
|
@ -110,6 +111,8 @@ public:
|
||||||
private:
|
private:
|
||||||
Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed);
|
Field(GUI::Label& flag_label, GUI::Label& time_label, GUI::Button& face_button, Function<void(Gfx::IntSize)> on_size_changed);
|
||||||
|
|
||||||
|
void initialize();
|
||||||
|
|
||||||
virtual void paint_event(GUI::PaintEvent&) override;
|
virtual void paint_event(GUI::PaintEvent&) override;
|
||||||
|
|
||||||
void on_square_clicked(Square&);
|
void on_square_clicked(Square&);
|
||||||
|
|
|
@ -57,10 +57,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||||
auto& flag_label = *widget->find_descendant_of_type_named<GUI::Label>("flag_label");
|
auto& flag_label = *widget->find_descendant_of_type_named<GUI::Label>("flag_label");
|
||||||
auto& time_label = *widget->find_descendant_of_type_named<GUI::Label>("time_label");
|
auto& time_label = *widget->find_descendant_of_type_named<GUI::Label>("time_label");
|
||||||
auto& face_button = *widget->find_descendant_of_type_named<GUI::Button>("face_button");
|
auto& face_button = *widget->find_descendant_of_type_named<GUI::Button>("face_button");
|
||||||
auto field = TRY(widget->try_add<Field>(flag_label, time_label, face_button, [&](auto size) {
|
auto field = TRY(Field::create(flag_label, time_label, face_button, [&](auto size) {
|
||||||
size.set_height(size.height() + separator.height() + container.height());
|
size.set_height(size.height() + separator.height() + container.height());
|
||||||
window->resize(size);
|
window->resize(size);
|
||||||
}));
|
}));
|
||||||
|
TRY(widget->try_add_child(field));
|
||||||
|
|
||||||
auto game_menu = TRY(window->try_add_menu("&Game"));
|
auto game_menu = TRY(window->try_add_menu("&Game"));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue