Luna/wind/Mouse.cpp

55 lines
2.1 KiB
C++
Raw Normal View History

#include "Mouse.h"
2023-08-03 15:38:49 +00:00
#include <os/File.h>
Mouse::Mouse(ui::Canvas& screen)
{
m_position.x = screen.width / 2;
m_position.y = screen.height / 2;
m_screen_rect = screen.rect();
}
void Mouse::draw(ui::Canvas& screen)
{
2023-08-03 15:38:49 +00:00
auto canvas = screen.subcanvas(ui::Rect { m_position, 10, 10 });
canvas.fill(ui::WHITE);
}
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.absolute();
2023-08-04 11:08:48 +00:00
}
else if ((packet.buttons & moon::MouseButton::Left) && !m_dragging_window)
{
g_windows.for_each_reversed([this](Window* window) {
if (!this->m_dragging_window && window->surface.contains(this->m_position))
{
this->m_dragging_window = window;
this->m_initial_drag_position = ui::Point { this->m_position.x - window->surface.pos.x,
this->m_position.y - window->surface.pos.y };
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,
this->m_initial_drag_position.x, this->m_initial_drag_position.y);
window->focus();
}
});
2023-08-03 15:38:49 +00:00
}
}