#include "Mouse.h" #include 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) { auto canvas = screen.subcanvas(ui::Rect { m_position, 10, 10 }); canvas.fill(ui::WHITE); } 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); if (m_dragging_window && !(packet.buttons & moon::MouseButton::Left)) { 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(); } 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)) { // Close button pressed g_windows.remove(window); delete window; break; } else if (window->surface.absolute(window->titlebar).contains(m_position)) { m_dragging_window = window; m_initial_drag_position = window->surface.relative(m_position); 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); window->focus(); break; } 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! } } } }