From e229914a748ea29d2d3ef62be5cbb1fae55316d1 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Sun, 9 May 2021 10:19:20 +0200 Subject: [PATCH] CatDog: Move the main widget into its own file --- Userland/Demos/CatDog/CMakeLists.txt | 1 + Userland/Demos/CatDog/CatDog.cpp | 126 ++++++++++++++++++++ Userland/Demos/CatDog/CatDog.h | 58 ++++++++++ Userland/Demos/CatDog/main.cpp | 167 +-------------------------- 4 files changed, 188 insertions(+), 164 deletions(-) create mode 100644 Userland/Demos/CatDog/CatDog.cpp create mode 100644 Userland/Demos/CatDog/CatDog.h diff --git a/Userland/Demos/CatDog/CMakeLists.txt b/Userland/Demos/CatDog/CMakeLists.txt index d03747eb57..3224787079 100644 --- a/Userland/Demos/CatDog/CMakeLists.txt +++ b/Userland/Demos/CatDog/CMakeLists.txt @@ -1,4 +1,5 @@ set(SOURCES + CatDog.cpp main.cpp ) diff --git a/Userland/Demos/CatDog/CatDog.cpp b/Userland/Demos/CatDog/CatDog.cpp new file mode 100644 index 0000000000..9f71cdb657 --- /dev/null +++ b/Userland/Demos/CatDog/CatDog.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2021, Richard Gráčik + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "CatDog.h" +#include +#include +#include + +void CatDog::timer_event(Core::TimerEvent&) +{ + 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 CatDog::paint_event(GUI::PaintEvent& event) +{ + 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 CatDog::mousemove_event(GUI::MouseEvent& event) +{ + if (m_temp_pos == event.position()) + return; + m_temp_pos = event.position(); + m_timer.start(); + if (m_sleeping) { + m_curr_bmp = m_alert; + update(); + } + m_sleeping = false; +} +void CatDog::track_cursor_globally() +{ + VERIFY(window()); + auto window_id = window()->window_id(); + VERIFY(window_id >= 0); + + set_global_cursor_tracking(true); + GUI::WindowServerConnection::the().set_global_cursor_tracking(window_id, true); +} diff --git a/Userland/Demos/CatDog/CatDog.h b/Userland/Demos/CatDog/CatDog.h new file mode 100644 index 0000000000..8fdeb96e52 --- /dev/null +++ b/Userland/Demos/CatDog/CatDog.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2021, Richard Gráčik + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +#pragma once + +class CatDog final : public GUI::Widget { + C_OBJECT(CatDog); + +public: + virtual void timer_event(Core::TimerEvent&) override; + virtual void paint_event(GUI::PaintEvent& event) override; + virtual void mousemove_event(GUI::MouseEvent& event) override; + + void track_cursor_globally(); + 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; + + AK::NonnullRefPtr m_alert = *Gfx::Bitmap::load_from_file("/res/icons/catdog/alert.png"); + AK::NonnullRefPtr m_erun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/erun1.png"); + AK::NonnullRefPtr m_erun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/erun2.png"); + AK::NonnullRefPtr m_nerun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nerun1.png"); + AK::NonnullRefPtr m_nerun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nerun2.png"); + AK::NonnullRefPtr m_nrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nrun1.png"); + AK::NonnullRefPtr m_nrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nrun2.png"); + AK::NonnullRefPtr m_nwrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nwrun1.png"); + AK::NonnullRefPtr m_nwrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nwrun2.png"); + AK::NonnullRefPtr m_serun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/serun1.png"); + AK::NonnullRefPtr m_serun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/serun2.png"); + AK::NonnullRefPtr m_sleep1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/sleep1.png"); + AK::NonnullRefPtr m_sleep2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/sleep2.png"); + AK::NonnullRefPtr m_srun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/srun1.png"); + AK::NonnullRefPtr m_srun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/srun2.png"); + AK::NonnullRefPtr m_still = *Gfx::Bitmap::load_from_file("/res/icons/catdog/still.png"); + AK::NonnullRefPtr m_swrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/swrun1.png"); + AK::NonnullRefPtr m_swrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/swrun2.png"); + AK::NonnullRefPtr m_wrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/wrun1.png"); + AK::NonnullRefPtr m_wrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/wrun2.png"); + + AK::NonnullRefPtr m_curr_bmp = m_alert; + CatDog() + : m_temp_pos { 0, 0 } + { + } +}; diff --git a/Userland/Demos/CatDog/main.cpp b/Userland/Demos/CatDog/main.cpp index 70c0dd5d4f..521693a288 100644 --- a/Userland/Demos/CatDog/main.cpp +++ b/Userland/Demos/CatDog/main.cpp @@ -4,175 +4,14 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include +#include "CatDog.h" +#include #include #include #include #include #include -#include -#include #include -#include -#include - -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 - { - if (m_temp_pos == event.position()) - return; - m_temp_pos = event.position(); - m_timer.start(); - if (m_sleeping) { - m_curr_bmp = m_alert; - update(); - } - m_sleeping = false; - } - - void track_cursor_globally() - { - VERIFY(window()); - auto window_id = window()->window_id(); - VERIFY(window_id >= 0); - - set_global_cursor_tracking(true); - GUI::WindowServerConnection::the().set_global_cursor_tracking(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 m_alert = *Gfx::Bitmap::load_from_file("/res/icons/catdog/alert.png"); - NonnullRefPtr m_erun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/erun1.png"); - NonnullRefPtr m_erun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/erun2.png"); - NonnullRefPtr m_nerun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nerun1.png"); - NonnullRefPtr m_nerun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nerun2.png"); - NonnullRefPtr m_nrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nrun1.png"); - NonnullRefPtr m_nrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nrun2.png"); - NonnullRefPtr m_nwrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nwrun1.png"); - NonnullRefPtr m_nwrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/nwrun2.png"); - NonnullRefPtr m_serun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/serun1.png"); - NonnullRefPtr m_serun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/serun2.png"); - NonnullRefPtr m_sleep1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/sleep1.png"); - NonnullRefPtr m_sleep2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/sleep2.png"); - NonnullRefPtr m_srun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/srun1.png"); - NonnullRefPtr m_srun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/srun2.png"); - NonnullRefPtr m_still = *Gfx::Bitmap::load_from_file("/res/icons/catdog/still.png"); - NonnullRefPtr m_swrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/swrun1.png"); - NonnullRefPtr m_swrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/swrun2.png"); - NonnullRefPtr m_wrun1 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/wrun1.png"); - NonnullRefPtr m_wrun2 = *Gfx::Bitmap::load_from_file("/res/icons/catdog/wrun2.png"); - - NonnullRefPtr m_curr_bmp = m_alert; - MainFrame() - : m_temp_pos { 0, 0 } - { - } -}; int main(int argc, char** argv) { @@ -208,7 +47,7 @@ int main(int argc, char** argv) window->set_alpha_hit_threshold(1.0f); window->set_icon(app_icon.bitmap_for_size(16)); - auto& root_widget = window->set_main_widget(); + auto& root_widget = window->set_main_widget(); root_widget.set_layout(); root_widget.layout()->set_spacing(0);