CatDog: Make a factory function for CatDog and propagate errors

This fixes an astonishing 22 FIXMEs. :^)
This commit is contained in:
Andreas Kling 2022-12-13 16:38:09 +01:00 committed by Linus Groh
parent ddb22cf10d
commit cba9df1c53
3 changed files with 56 additions and 24 deletions

View file

@ -10,6 +10,35 @@
#include <LibGUI/Painter.h>
#include <LibGUI/Window.h>
ErrorOr<NonnullRefPtr<CatDog>> CatDog::create()
{
auto catdog = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) CatDog));
catdog->m_alert = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/alert.png"sv));
catdog->m_artist = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/artist.png"sv));
catdog->m_erun1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/erun1.png"sv));
catdog->m_erun2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/erun2.png"sv));
catdog->m_inspector = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/inspector.png"sv));
catdog->m_nerun1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nerun1.png"sv));
catdog->m_nerun2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nerun2.png"sv));
catdog->m_nrun1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nrun1.png"sv));
catdog->m_nrun2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nrun2.png"sv));
catdog->m_nwrun1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nwrun1.png"sv));
catdog->m_nwrun2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nwrun2.png"sv));
catdog->m_serun1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/serun1.png"sv));
catdog->m_serun2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/serun2.png"sv));
catdog->m_sleep1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/sleep1.png"sv));
catdog->m_sleep2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/sleep2.png"sv));
catdog->m_srun1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/srun1.png"sv));
catdog->m_srun2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/srun2.png"sv));
catdog->m_still = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/still.png"sv));
catdog->m_swrun1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/swrun1.png"sv));
catdog->m_swrun2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/swrun2.png"sv));
catdog->m_wrun1 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/wrun1.png"sv));
catdog->m_wrun2 = *TRY(Gfx::Bitmap::try_load_from_file("/res/icons/catdog/wrun2.png"sv));
catdog->m_curr_bmp = catdog->m_alert;
return catdog;
}
void CatDog::timer_event(Core::TimerEvent&)
{
auto maybe_proc_info = Core::ProcessStatisticsReader::get_all(*m_proc_all);

View file

@ -23,6 +23,8 @@ class CatDog final : public GUI::Widget
C_OBJECT(CatDog);
public:
static ErrorOr<NonnullRefPtr<CatDog>> create();
// The general state, does not contain movement direction or whether CatDog is roaming.
enum class MainState {
Idle, // default state
@ -75,30 +77,30 @@ private:
NonnullOwnPtr<Core::Stream::File> m_proc_all;
NonnullRefPtr<Gfx::Bitmap> m_alert = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/alert.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_artist = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/artist.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_erun1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/erun1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_erun2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/erun2.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_inspector = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/inspector.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_nerun1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nerun1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_nerun2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nerun2.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_nrun1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nrun1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_nrun2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nrun2.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_nwrun1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nwrun1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_nwrun2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/nwrun2.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_serun1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/serun1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_serun2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/serun2.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_sleep1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/sleep1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_sleep2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/sleep2.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_srun1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/srun1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_srun2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/srun2.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_still = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/still.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_swrun1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/swrun1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_swrun2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/swrun2.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_wrun1 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/wrun1.png"sv).release_value_but_fixme_should_propagate_errors();
NonnullRefPtr<Gfx::Bitmap> m_wrun2 = *Gfx::Bitmap::try_load_from_file("/res/icons/catdog/wrun2.png"sv).release_value_but_fixme_should_propagate_errors();
RefPtr<Gfx::Bitmap> m_alert;
RefPtr<Gfx::Bitmap> m_artist;
RefPtr<Gfx::Bitmap> m_erun1;
RefPtr<Gfx::Bitmap> m_erun2;
RefPtr<Gfx::Bitmap> m_inspector;
RefPtr<Gfx::Bitmap> m_nerun1;
RefPtr<Gfx::Bitmap> m_nerun2;
RefPtr<Gfx::Bitmap> m_nrun1;
RefPtr<Gfx::Bitmap> m_nrun2;
RefPtr<Gfx::Bitmap> m_nwrun1;
RefPtr<Gfx::Bitmap> m_nwrun2;
RefPtr<Gfx::Bitmap> m_serun1;
RefPtr<Gfx::Bitmap> m_serun2;
RefPtr<Gfx::Bitmap> m_sleep1;
RefPtr<Gfx::Bitmap> m_sleep2;
RefPtr<Gfx::Bitmap> m_srun1;
RefPtr<Gfx::Bitmap> m_srun2;
RefPtr<Gfx::Bitmap> m_still;
RefPtr<Gfx::Bitmap> m_swrun1;
RefPtr<Gfx::Bitmap> m_swrun2;
RefPtr<Gfx::Bitmap> m_wrun1;
RefPtr<Gfx::Bitmap> m_wrun2;
NonnullRefPtr<Gfx::Bitmap> m_curr_bmp = m_alert;
RefPtr<Gfx::Bitmap> m_curr_bmp;
// Used if CatDog is still; may also account for animation frames.
void set_image_by_main_state()

View file

@ -40,7 +40,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
window->set_alpha_hit_threshold(1.0f);
window->set_icon(app_icon.bitmap_for_size(16));
auto catdog_widget = TRY(window->try_set_main_widget<CatDog>());
auto catdog_widget = TRY(CatDog::create());
window->set_main_widget(catdog_widget);
(void)TRY(catdog_widget->try_set_layout<GUI::VerticalBoxLayout>());
catdog_widget->layout()->set_spacing(0);