1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 02:38:11 +00:00
serenity/Userland/Demos/CatDog/CatDog.h
Andre Eisenbach e80f0b23e3 CatDog: Simplify animation frame logic and fix minor bugs
Moved all images into a Vector instead of storing every animation frame
in its own member variable. This greatly simplifies the bitmap selection
logic and removes redundant if() checks.

Also fixes minor state bugs. For example, CatDog woudld go to sleep
immediately after actively moving for > 5 seconds. Also fixes arbitrary
hardcoded values for mouse offset movement tresholds as well as
inconsistent movement increments. This allows clicking anywhere on the
CatDog window without moving CatDog.

In addition to removing many member variables, the API interface is
also cleaned up a bit to expose less CatDog internals. Nobody likes
exposed CatDog internals ;).

Variables and function are also renamed where necessary to (hopefully)
improve readability.
2022-12-16 08:50:35 -07:00

87 lines
2.1 KiB
C++

/*
* Copyright (c) 2021, Richard Gráčik <r.gracik@gmail.com>
* Copyright (c) 2022, kleines Filmröllchen <filmroellchen@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/EnumBits.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefPtr.h>
#include <AK/Types.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/File.h>
#include <LibCore/Stream.h>
#include <LibGUI/Menu.h>
#include <LibGUI/MouseTracker.h>
#include <LibGUI/Widget.h>
#include <LibGfx/Bitmap.h>
#include <unistd.h>
class CatDog final : public GUI::Widget
, GUI::MouseTracker {
C_OBJECT(CatDog);
public:
static ErrorOr<NonnullRefPtr<CatDog>> create();
virtual void timer_event(Core::TimerEvent&) override;
virtual void paint_event(GUI::PaintEvent& event) override;
virtual void track_mouse_move(Gfx::IntPoint point) override;
virtual void mousedown_event(GUI::MouseEvent& event) override;
virtual void context_menu_event(GUI::ContextMenuEvent& event) override;
Function<void()> on_click;
Function<void(GUI::ContextMenuEvent&)> on_context_menu_request;
void set_roaming(bool roaming);
[[nodiscard]] bool is_artist() const;
[[nodiscard]] bool is_inspector() const;
private:
enum class State : u16 {
Frame1 = 0x0,
Frame2 = 0x1,
Up = 0x10,
Down = 0x20,
Left = 0x40,
Right = 0x80,
Directions = Up | Down | Left | Right,
Roaming = 0x0100,
Idle = 0x0200,
Sleeping = 0x0400,
Alert = 0x0800,
GenericCatDog = 0x0000,
Inspector = 0x1000,
Artist = 0x2000
};
AK_ENUM_BITWISE_FRIEND_OPERATORS(State);
struct ImageForState {
State state;
NonnullRefPtr<Gfx::Bitmap> bitmap;
};
Vector<ImageForState> m_images;
Gfx::IntPoint m_mouse_offset {};
Core::ElapsedTimer m_idle_sleep_timer;
NonnullOwnPtr<Core::Stream::File> m_proc_all;
State m_state { State::Roaming };
State m_frame { State::Frame1 };
CatDog();
[[nodiscard]] Gfx::Bitmap& bitmap_for_state() const;
[[nodiscard]] State special_application_states() const;
};