1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 09:38:11 +00:00
serenity/Userland/Demos/CatDog/main.cpp
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

102 lines
3.6 KiB
C++

/*
* Copyright (c) 2021, Richard Gráčik <r.gracik@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "CatDog.h"
#include "SpeechBubble.h"
#include <LibCore/System.h>
#include <LibCore/Timer.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/Window.h>
#include <LibMain/Main.h>
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio recvfd sendfd rpath wpath cpath unix"));
auto app = TRY(GUI::Application::try_create(arguments));
auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-catdog"sv));
TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
TRY(Core::System::unveil("/res", "r"));
TRY(Core::System::unveil("/sys/kernel/processes", "r"));
// FIXME: For some reason, this is needed in the /sys/kernel/processes shenanigans.
TRY(Core::System::unveil("/etc/passwd", "r"));
TRY(Core::System::unveil(nullptr, nullptr));
auto window = TRY(GUI::Window::try_create());
window->set_title("CatDog Demo");
window->resize(32, 32);
window->set_frameless(true);
window->set_resizable(false);
window->set_has_alpha_channel(true);
window->set_alpha_hit_threshold(1.0f);
window->set_icon(app_icon.bitmap_for_size(16));
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);
auto context_menu = TRY(GUI::Menu::try_create());
TRY(context_menu->try_add_action(GUI::CommonActions::make_about_action("CatDog Demo", app_icon, window)));
TRY(context_menu->try_add_separator());
TRY(context_menu->try_add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })));
window->show();
window->set_always_on_top();
catdog_widget->start_timer(250, Core::TimerShouldFireWhenNotVisible::Yes);
auto advice_window = TRY(GUI::Window::try_create());
advice_window->set_title("CatDog Advice");
advice_window->resize(225, 50);
advice_window->set_frameless(true);
advice_window->set_resizable(false);
advice_window->set_has_alpha_channel(true);
advice_window->set_alpha_hit_threshold(1.0f);
auto advice_widget = TRY(advice_window->try_set_main_widget<SpeechBubble>(catdog_widget));
(void)TRY(advice_widget->try_set_layout<GUI::VerticalBoxLayout>());
advice_widget->layout()->set_spacing(0);
auto advice_timer = TRY(Core::Timer::try_create());
advice_timer->set_interval(15'000);
advice_timer->set_single_shot(true);
advice_timer->on_timeout = [&] {
window->move_to_front();
advice_window->move_to_front();
catdog_widget->set_roaming(false);
advice_window->move_to(window->x() - advice_window->width() / 2, window->y() - advice_window->height());
advice_window->show();
advice_window->set_always_on_top();
};
advice_timer->start();
advice_widget->on_dismiss = [&] {
catdog_widget->set_roaming(true);
advice_window->hide();
advice_timer->start();
};
// Let users toggle the advice functionality by clicking on catdog.
catdog_widget->on_click = [&] {
if (advice_timer->is_active())
advice_timer->stop();
else
advice_timer->start();
};
catdog_widget->on_context_menu_request = [&](GUI::ContextMenuEvent& event) {
if (catdog_widget->rect().contains(event.position()))
context_menu->popup(event.screen_position());
};
return app->exec();
}