2024-05-29 16:00:54 +03:00
|
|
|
#include <BAN/Debug.h>
|
|
|
|
|
|
|
|
#include <LibGUI/Window.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
void randomize_color(BAN::UniqPtr<LibGUI::Window>& window)
|
|
|
|
{
|
|
|
|
uint32_t color = ((rand() % 255) << 16) | ((rand() % 255) << 8) | ((rand() % 255) << 0);
|
|
|
|
for (uint32_t y = 0; y < window->height(); y++)
|
|
|
|
for (uint32_t x = 0; x < window->width(); x++)
|
2024-09-12 20:53:45 +03:00
|
|
|
window->set_pixel(x, y, 0xFF000000 | color);
|
2024-05-29 16:00:54 +03:00
|
|
|
window->invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
srand(ts.tv_nsec);
|
|
|
|
|
2024-06-02 17:27:09 +03:00
|
|
|
auto window_or_error = LibGUI::Window::create(300, 200, "test-window");
|
2024-05-29 16:00:54 +03:00
|
|
|
if (window_or_error.is_error())
|
|
|
|
{
|
|
|
|
dprintln("{}", window_or_error.error());
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-02 17:27:09 +03:00
|
|
|
bool running = true;
|
|
|
|
|
2024-05-29 16:00:54 +03:00
|
|
|
auto window = window_or_error.release_value();
|
2024-06-02 17:27:09 +03:00
|
|
|
window->set_close_window_event_callback([&] { running = false; });
|
2024-05-29 16:00:54 +03:00
|
|
|
window->set_mouse_button_event_callback(
|
2024-10-17 01:36:59 +03:00
|
|
|
[&](LibGUI::EventPacket::MouseButtonEvent::event_t event)
|
2024-05-29 16:00:54 +03:00
|
|
|
{
|
2024-10-17 01:36:59 +03:00
|
|
|
if (event.pressed && event.button == LibInput::MouseButton::Left)
|
2024-05-29 16:00:54 +03:00
|
|
|
randomize_color(window);
|
|
|
|
|
|
|
|
const char* button;
|
|
|
|
switch (event.button)
|
|
|
|
{
|
2024-10-17 01:36:59 +03:00
|
|
|
case LibInput::MouseButton::Left: button = "left"; break;
|
|
|
|
case LibInput::MouseButton::Right: button = "right"; break;
|
|
|
|
case LibInput::MouseButton::Middle: button = "middle"; break;
|
|
|
|
case LibInput::MouseButton::Extra1: button = "extra1"; break;
|
|
|
|
case LibInput::MouseButton::Extra2: button = "extra2"; break;
|
2024-05-29 16:00:54 +03:00
|
|
|
}
|
|
|
|
dprintln("mouse button '{}' {} at {}, {}", button, event.pressed ? "pressed" : "released", event.x, event.y);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
randomize_color(window);
|
|
|
|
|
2024-06-02 17:27:09 +03:00
|
|
|
while (running)
|
2024-05-29 16:00:54 +03:00
|
|
|
{
|
|
|
|
window->poll_events();
|
|
|
|
|
|
|
|
timespec duration;
|
|
|
|
duration.tv_sec = 0;
|
|
|
|
duration.tv_nsec = 16'666'666;
|
|
|
|
nanosleep(&duration, nullptr);
|
|
|
|
}
|
|
|
|
}
|