diff options
author | TheMorc <r.gracik@gmail.com> | 2021-01-02 23:34:34 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-01-03 12:57:18 +0100 |
commit | 7be2c9864ea05e2d4729af23fadaf32cf29562ae (patch) | |
tree | c0b3f2f8d9fc6b4effd002e60021f36f7020cdbd | |
parent | a63330077fa7fad9b2605558164284338ee77b67 (diff) | |
download | serenity-7be2c9864ea05e2d4729af23fadaf32cf29562ae.zip |
Demos: add CatDog (Neko clone)
26 files changed, 249 insertions, 0 deletions
diff --git a/Base/res/apps/CatDog.af b/Base/res/apps/CatDog.af new file mode 100644 index 0000000000..af94002db2 --- /dev/null +++ b/Base/res/apps/CatDog.af @@ -0,0 +1,4 @@ +[App] +Name=CatDog +Executable=/bin/CatDog +Category=Demos diff --git a/Base/res/icons/16x16/app-catdog.png b/Base/res/icons/16x16/app-catdog.png Binary files differnew file mode 100644 index 0000000000..838df17aa6 --- /dev/null +++ b/Base/res/icons/16x16/app-catdog.png diff --git a/Base/res/icons/32x32/app-catdog.png b/Base/res/icons/32x32/app-catdog.png Binary files differnew file mode 100644 index 0000000000..3b4df05ea4 --- /dev/null +++ b/Base/res/icons/32x32/app-catdog.png diff --git a/Base/res/icons/catdog/alert.png b/Base/res/icons/catdog/alert.png Binary files differnew file mode 100644 index 0000000000..2cb0a53ee3 --- /dev/null +++ b/Base/res/icons/catdog/alert.png diff --git a/Base/res/icons/catdog/erun1.png b/Base/res/icons/catdog/erun1.png Binary files differnew file mode 100644 index 0000000000..f70027a7fc --- /dev/null +++ b/Base/res/icons/catdog/erun1.png diff --git a/Base/res/icons/catdog/erun2.png b/Base/res/icons/catdog/erun2.png Binary files differnew file mode 100644 index 0000000000..f8b24e45db --- /dev/null +++ b/Base/res/icons/catdog/erun2.png diff --git a/Base/res/icons/catdog/nerun1.png b/Base/res/icons/catdog/nerun1.png Binary files differnew file mode 100644 index 0000000000..5b63c4e848 --- /dev/null +++ b/Base/res/icons/catdog/nerun1.png diff --git a/Base/res/icons/catdog/nerun2.png b/Base/res/icons/catdog/nerun2.png Binary files differnew file mode 100644 index 0000000000..6a5ef17432 --- /dev/null +++ b/Base/res/icons/catdog/nerun2.png diff --git a/Base/res/icons/catdog/nrun1.png b/Base/res/icons/catdog/nrun1.png Binary files differnew file mode 100644 index 0000000000..f0fb254172 --- /dev/null +++ b/Base/res/icons/catdog/nrun1.png diff --git a/Base/res/icons/catdog/nrun2.png b/Base/res/icons/catdog/nrun2.png Binary files differnew file mode 100644 index 0000000000..a3e0cd884e --- /dev/null +++ b/Base/res/icons/catdog/nrun2.png diff --git a/Base/res/icons/catdog/nwrun1.png b/Base/res/icons/catdog/nwrun1.png Binary files differnew file mode 100644 index 0000000000..ee233dae11 --- /dev/null +++ b/Base/res/icons/catdog/nwrun1.png diff --git a/Base/res/icons/catdog/nwrun2.png b/Base/res/icons/catdog/nwrun2.png Binary files differnew file mode 100644 index 0000000000..1242326b1f --- /dev/null +++ b/Base/res/icons/catdog/nwrun2.png diff --git a/Base/res/icons/catdog/serun1.png b/Base/res/icons/catdog/serun1.png Binary files differnew file mode 100644 index 0000000000..9acfeba9fe --- /dev/null +++ b/Base/res/icons/catdog/serun1.png diff --git a/Base/res/icons/catdog/serun2.png b/Base/res/icons/catdog/serun2.png Binary files differnew file mode 100644 index 0000000000..4ae8680ba7 --- /dev/null +++ b/Base/res/icons/catdog/serun2.png diff --git a/Base/res/icons/catdog/sleep1.png b/Base/res/icons/catdog/sleep1.png Binary files differnew file mode 100644 index 0000000000..d1d8214fb9 --- /dev/null +++ b/Base/res/icons/catdog/sleep1.png diff --git a/Base/res/icons/catdog/sleep2.png b/Base/res/icons/catdog/sleep2.png Binary files differnew file mode 100644 index 0000000000..88c9c0acef --- /dev/null +++ b/Base/res/icons/catdog/sleep2.png diff --git a/Base/res/icons/catdog/srun1.png b/Base/res/icons/catdog/srun1.png Binary files differnew file mode 100644 index 0000000000..300bb4891d --- /dev/null +++ b/Base/res/icons/catdog/srun1.png diff --git a/Base/res/icons/catdog/srun2.png b/Base/res/icons/catdog/srun2.png Binary files differnew file mode 100644 index 0000000000..49da2400b2 --- /dev/null +++ b/Base/res/icons/catdog/srun2.png diff --git a/Base/res/icons/catdog/still.png b/Base/res/icons/catdog/still.png Binary files differnew file mode 100644 index 0000000000..3b4df05ea4 --- /dev/null +++ b/Base/res/icons/catdog/still.png diff --git a/Base/res/icons/catdog/swrun1.png b/Base/res/icons/catdog/swrun1.png Binary files differnew file mode 100644 index 0000000000..0f29c8e279 --- /dev/null +++ b/Base/res/icons/catdog/swrun1.png diff --git a/Base/res/icons/catdog/swrun2.png b/Base/res/icons/catdog/swrun2.png Binary files differnew file mode 100644 index 0000000000..84df6f1b21 --- /dev/null +++ b/Base/res/icons/catdog/swrun2.png diff --git a/Base/res/icons/catdog/wrun1.png b/Base/res/icons/catdog/wrun1.png Binary files differnew file mode 100644 index 0000000000..8544cde427 --- /dev/null +++ b/Base/res/icons/catdog/wrun1.png diff --git a/Base/res/icons/catdog/wrun2.png b/Base/res/icons/catdog/wrun2.png Binary files differnew file mode 100644 index 0000000000..cf7d7b048c --- /dev/null +++ b/Base/res/icons/catdog/wrun2.png diff --git a/Demos/CMakeLists.txt b/Demos/CMakeLists.txt index f6ddd58db3..bae2a972c1 100644 --- a/Demos/CMakeLists.txt +++ b/Demos/CMakeLists.txt @@ -1,3 +1,4 @@ +add_subdirectory(CatDog) add_subdirectory(Cube) add_subdirectory(DynamicObject) #add_subdirectory(DynamicLink) diff --git a/Demos/CatDog/CMakeLists.txt b/Demos/CatDog/CMakeLists.txt new file mode 100644 index 0000000000..d03747eb57 --- /dev/null +++ b/Demos/CatDog/CMakeLists.txt @@ -0,0 +1,6 @@ +set(SOURCES + main.cpp +) + +serenity_app(CatDog ICON app-catdog) +target_link_libraries(CatDog LibGUI LibGfx) diff --git a/Demos/CatDog/main.cpp b/Demos/CatDog/main.cpp new file mode 100644 index 0000000000..e7a20acef2 --- /dev/null +++ b/Demos/CatDog/main.cpp @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2021, Richard Gráčik <r.gracik@gmail.com> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include <LibCore/ElapsedTimer.h> +#include <LibGUI/AboutDialog.h> +#include <LibGUI/BoxLayout.h> +#include <LibGUI/Icon.h> +#include <LibGUI/Menu.h> +#include <LibGUI/MenuBar.h> +#include <LibGUI/Painter.h> +#include <LibGUI/Widget.h> +#include <LibGUI/Window.h> +#include <LibGUI/WindowServerConnection.h> +#include <LibGfx/Bitmap.h> + +class MainFrame final : public GUI::Widget { + C_OBJECT(MainFrame); + +public: + virtual void timer_event(Core::TimerEvent&) override + { + if (m_temp_pos.x() > 48) { + m_left = false; + m_right = true; + m_moveX = 16; + + m_curr_bmp = m_erun1; + if (m_curr_frame == 2) + m_curr_bmp = m_erun2; + } else if (m_temp_pos.x() < -16) { + m_left = true; + m_right = false; + m_moveX = -16; + + m_curr_bmp = m_wrun1; + if (m_curr_frame == 2) + m_curr_bmp = m_wrun2; + } else { + m_left = false; + m_right = false; + m_moveX = 0; + } + + if (m_temp_pos.y() > 48) { + m_up = false; + m_down = true; + m_moveY = 10; + + m_curr_bmp = m_srun1; + if (m_curr_frame == 2) + m_curr_bmp = m_srun2; + } else if (m_temp_pos.y() < -16) { + m_up = true; + m_down = false; + m_moveY = -10; + + m_curr_bmp = m_nrun1; + if (m_curr_frame == 2) + m_curr_bmp = m_nrun2; + } else { + m_up = false; + m_down = false; + m_moveY = 0; + } + + if (m_up && m_left) { + m_curr_bmp = m_nwrun1; + if (m_curr_frame == 2) + m_curr_bmp = m_nwrun2; + } else if (m_up && m_right) { + m_curr_bmp = m_nerun1; + if (m_curr_frame == 2) + m_curr_bmp = m_nerun2; + } else if (m_down && m_left) { + m_curr_bmp = m_swrun1; + if (m_curr_frame == 2) + m_curr_bmp = m_swrun2; + } else if (m_down && m_right) { + m_curr_bmp = m_serun1; + if (m_curr_frame == 2) + m_curr_bmp = m_serun2; + } + + window()->move_to(window()->position().x() + m_moveX, window()->position().y() + m_moveY); + m_temp_pos.set_x(m_temp_pos.x() + (-m_moveX)); + m_temp_pos.set_y(m_temp_pos.y() + (-m_moveY)); + + if (m_curr_frame == 1) { + m_curr_frame = 2; + } else { + m_curr_frame = 1; + } + + if (!m_up && !m_down && !m_left && !m_right) { + m_curr_bmp = m_still; + if (m_timer.elapsed() > 5000) { + m_curr_bmp = m_sleep1; + if (m_curr_frame == 2) + m_curr_bmp = m_sleep2; + m_sleeping = true; + } + } + + update(); + } + + void paint_event(GUI::PaintEvent& event) override + { + GUI::Painter painter(*this); + painter.clear_rect(event.rect(), Gfx::Color()); + painter.blit(Gfx::IntPoint(0, 0), *m_curr_bmp, m_curr_bmp->rect()); + } + + void mousemove_event(GUI::MouseEvent& event) override + { + m_temp_pos = event.position(); + m_timer.start(); + if (m_sleeping) { + m_curr_bmp = m_alert; + update(); + } + m_sleeping = false; + } + + void track_cursor_globally() + { + ASSERT(window()); + auto window_id = window()->window_id(); + ASSERT(window_id >= 0); + + set_global_cursor_tracking(true); + GUI::WindowServerConnection::the().send_sync<Messages::WindowServer::SetGlobalCursorTracking>(window_id, true); + } + + void start_the_timer() { m_timer.start(); } + +private: + Gfx::IntPoint m_temp_pos; + Core::ElapsedTimer m_timer; + int m_curr_frame = 1; + int m_moveX, m_moveY = 0; + bool m_up, m_down, m_left, m_right, m_sleeping = false; + + NonnullRefPtr<Gfx::Bitmap> m_alert = *Gfx::Bitmap::load_from_file("/res/icons/catdog/alert.png"); + NonnullRefPtr<Gfx::Bitmap> m_erun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/erun1.png"); + NonnullRefPtr<Gfx::Bitmap> m_erun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/erun2.png"); + NonnullRefPtr<Gfx::Bitmap> m_nerun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nerun1.png"); + NonnullRefPtr<Gfx::Bitmap> m_nerun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nerun2.png"); + NonnullRefPtr<Gfx::Bitmap> m_nrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nrun1.png"); + NonnullRefPtr<Gfx::Bitmap> m_nrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nrun2.png"); + NonnullRefPtr<Gfx::Bitmap> m_nwrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nwrun1.png"); + NonnullRefPtr<Gfx::Bitmap> m_nwrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nwrun2.png"); + NonnullRefPtr<Gfx::Bitmap> m_serun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/serun1.png"); + NonnullRefPtr<Gfx::Bitmap> m_serun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/serun2.png"); + NonnullRefPtr<Gfx::Bitmap> m_sleep1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/sleep1.png"); + NonnullRefPtr<Gfx::Bitmap> m_sleep2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/sleep2.png"); + NonnullRefPtr<Gfx::Bitmap> m_srun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/srun1.png"); + NonnullRefPtr<Gfx::Bitmap> m_srun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/srun2.png"); + NonnullRefPtr<Gfx::Bitmap> m_still = *Gfx::Bitmap::load_from_file("/res/icons/catdog/still.png"); + NonnullRefPtr<Gfx::Bitmap> m_swrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/swrun1.png"); + NonnullRefPtr<Gfx::Bitmap> m_swrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/swrun2.png"); + NonnullRefPtr<Gfx::Bitmap> m_wrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/wrun1.png"); + NonnullRefPtr<Gfx::Bitmap> m_wrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/wrun2.png"); + + NonnullRefPtr<Gfx::Bitmap> m_curr_bmp = m_alert; + MainFrame() + : m_temp_pos { 0, 0 } + { + } +}; + +int main(int argc, char** argv) +{ + auto app = GUI::Application::construct(argc, argv); + auto app_icon = GUI::Icon::default_icon("app-catdog"); + + if (pledge("stdio rpath shared_buffer", nullptr) < 0) { + perror("pledge"); + return 1; + } + + if (unveil("/res", "r") < 0) { + perror("unveil"); + return 1; + } + + auto window = GUI::Window::construct(); + 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_icon(app_icon.bitmap_for_size(16)); + + auto& root_widget = window->set_main_widget<MainFrame>(); + root_widget.set_layout<GUI::VerticalBoxLayout>(); + root_widget.layout()->set_spacing(0); + + auto menubar = GUI::MenuBar::construct(); + auto& app_menu = menubar->add_menu("CatDog Demo"); + app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })); + auto& help_menu = menubar->add_menu("Help"); + help_menu.add_action(GUI::Action::create("About", [&](auto&) { + GUI::AboutDialog::show("CatDog Demo", app_icon.bitmap_for_size(32), window); + })); + app->set_menubar(move(menubar)); + + window->show(); + root_widget.track_cursor_globally(); + root_widget.start_timer(250, Core::TimerShouldFireWhenNotVisible::Yes); + root_widget.start_the_timer(); // timer for "mouse sleep detection" + + return app->exec(); +} |