Luna/wind/Mouse.cpp

99 lines
3.8 KiB
C++
Raw Normal View History

#include "Mouse.h"
#include "Client.h"
2023-08-03 15:38:49 +00:00
#include <os/File.h>
#include <os/IPC.h>
#include <ui/Image.h>
#include <ui/ipc/Client.h>
static SharedPtr<ui::Image> g_mouse_cursor;
Mouse::Mouse(ui::Canvas& screen)
{
m_position.x = screen.width / 2;
m_position.y = screen.height / 2;
m_screen_rect = screen.rect();
g_mouse_cursor = ui::Image::load("/usr/share/cursors/default.tga").value_or({});
}
void Mouse::draw(ui::Canvas& screen)
{
if (!g_mouse_cursor) return;
auto canvas = screen.subcanvas(ui::Rect { m_position, g_mouse_cursor->width(), g_mouse_cursor->height() });
canvas.fill(g_mouse_cursor->pixels(), g_mouse_cursor->width());
}
2023-08-03 15:38:49 +00:00
void Mouse::update(const moon::MousePacket& packet)
{
m_position.x += packet.xdelta;
m_position.y -= packet.ydelta;
m_position = m_screen_rect.normalize(m_position);
2023-08-03 15:38:49 +00:00
2023-08-04 11:08:48 +00:00
if (m_dragging_window && !(packet.buttons & moon::MouseButton::Left))
2023-08-03 15:38:49 +00:00
{
os::println("Stopped drag: window at (%d,%d,%d,%d) with offset (%d,%d)", m_dragging_window->surface.pos.x,
m_dragging_window->surface.pos.y, m_dragging_window->surface.width,
m_dragging_window->surface.height, this->m_initial_drag_position.x,
this->m_initial_drag_position.y);
m_dragging_window = nullptr;
}
if (m_dragging_window)
{
m_dragging_window->surface.pos =
ui::Point { m_position.x - m_initial_drag_position.x, m_position.y - m_initial_drag_position.y };
m_dragging_window->surface = m_dragging_window->surface.normalized();
2023-08-04 11:08:48 +00:00
}
else if ((packet.buttons & moon::MouseButton::Left) && !m_dragging_window)
{
// Iterate from the end of the list, since windows at the beginning are stacked at the bottom and windows at the
// top are at the end.
for (Window* window = g_windows.last().value_or(nullptr); window;
window = g_windows.previous(window).value_or(nullptr))
{
if (window->surface.absolute(window->close_button).contains(m_position))
{
ui::WindowCloseRequest request;
request.window = window->id;
auto& client = *window->client;
os::IPC::send_async(client.conn, request);
break;
}
else if (window->surface.absolute(window->titlebar).contains(m_position))
2023-08-04 11:08:48 +00:00
{
m_dragging_window = window;
m_initial_drag_position = window->surface.relative(m_position);
2023-08-04 11:08:48 +00:00
os::println("Started drag: window at (%d,%d,%d,%d) with offset (%d,%d)", window->surface.pos.x,
window->surface.pos.y, window->surface.width, window->surface.height,
m_initial_drag_position.x, m_initial_drag_position.y);
2023-08-04 11:08:48 +00:00
window->focus();
break;
2023-08-04 11:08:48 +00:00
}
else if (window->surface.absolute(window->contents).contains(m_position))
{
window->focus();
break; // We don't want to continue iterating, otherwise this would take into account windows whose
// titlebar is underneath another window's contents!
}
}
2023-08-03 15:38:49 +00:00
}
for (Window* window = g_windows.last().value_or(nullptr); window;
window = g_windows.previous(window).value_or(nullptr))
{
auto titlebar = window->surface.absolute(window->titlebar);
auto contents = window->surface.absolute(window->contents);
if (titlebar.contains(m_position)) break;
if (contents.contains(m_position))
{
ui::MouseEventRequest request;
request.window = window->id;
request.position = contents.relative(m_position);
request.buttons = packet.buttons;
os::IPC::send_async(window->client->conn, request);
break;
}
}
}