summaryrefslogtreecommitdiff
path: root/Userland/Demos/CatDog/main.cpp
blob: 47b0f0482c41dc95b0f0455be6f694b581e0496f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * Copyright (c) 2021, Richard Gráčik <r.gracik@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/ElapsedTimer.h>
#include <LibGUI/Application.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
    {
        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().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)
{
    if (pledge("stdio recvfd sendfd rpath wpath cpath accept unix fattr", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    auto app = GUI::Application::construct(argc, argv);
    auto app_icon = GUI::Icon::default_icon("app-catdog");

    if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    if (unveil("/res", "r") < 0) {
        perror("unveil");
        return 1;
    }

    if (unveil(nullptr, nullptr) < 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_alpha_hit_threshold(1.0f);
    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("File");
    app_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
    auto& help_menu = menubar->add_menu("Help");
    help_menu.add_action(GUI::CommonActions::make_about_action("CatDog Demo", app_icon, window));
    window->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();
}