From 3960d5972452b8326a1cdb9d86fc879a43edba03 Mon Sep 17 00:00:00 2001 From: VAN BOSSUYT Nicolas Date: Sun, 5 Jan 2025 14:34:31 +0100 Subject: [PATCH] grund-shell: Refactored cursor code. --- src/apps/hideo-shell/cursor.path | 2 +- src/srvs/grund-shell/input.cpp | 110 +++++++++++++++++++------------ 2 files changed, 69 insertions(+), 43 deletions(-) diff --git a/src/apps/hideo-shell/cursor.path b/src/apps/hideo-shell/cursor.path index c8c99b91b9..481546bd87 100644 --- a/src/apps/hideo-shell/cursor.path +++ b/src/apps/hideo-shell/cursor.path @@ -1 +1 @@ -"M11.7 11.8 0 0 0 16.9 3.6 13.1 6.9 20.7 9.6 19.5 6.3 11.9Z" +"m7.3 13.9 3.3 7.6-2.7 1.2-3.3-7.6-3.6 3.8v-16.9l11.7 11.8z" diff --git a/src/srvs/grund-shell/input.cpp b/src/srvs/grund-shell/input.cpp index 458f228518..528726d102 100644 --- a/src/srvs/grund-shell/input.cpp +++ b/src/srvs/grund-shell/input.cpp @@ -5,35 +5,84 @@ namespace Grund::Shell { -struct InputTranslator : public Ui::ProxyNode { - App::MouseEvent _mousePrev = {}; - Math::Vec2i _mousePos = {}; - bool _mouseDirty = false; +struct Cursor { + virtual ~Cursor() = default; - InputTranslator(Ui::Child child) - : Ui::ProxyNode(std::move(child)) { - } + virtual Math::Recti bound(Math::Vec2i pos) const = 0; + + virtual void paint(Math::Vec2i pos, Gfx::Canvas &g) const = 0; +}; - Math::Ellipsef _cursor() const { +struct BlobCursor : public Cursor { + Gfx::Color _fill = Gfx::WHITE; + f64 _opacity = 0.25; + + Math::Ellipsef _blob(Math::Vec2i pos) const { return { - _mousePos.cast(), + pos.cast(), {16, 16}, }; } - Math::Recti _cursorDamage() const { - if (false) { - return _cursor().bound().grow(1).cast(); - } else { - return _cursor().bound().grow(16).cast(); + Math::Recti bound(Math::Vec2i pos) const override { + return _blob(pos).bound().grow(1).cast(); + } + + void paint(Math::Vec2i pos, Gfx::Canvas &g) const override { + g.push(); + g.beginPath(); + g.fillStyle(_fill.withOpacity(_opacity)); + g.ellipse(_blob(pos)); + g.fill(Gfx::FillRule::EVENODD); + g.pop(); + } +}; + +struct ClassicCursor : public Cursor { + Gfx::Color _fill = Gfx::BLACK; + Gfx::Color _stroke = Gfx::WHITE; + Math::Path _path = Math::Path::fromSvg( +#include "hideo-shell/cursor.path" + ); + + Math::Recti bound(Math::Vec2i pos) const override { + return Math::Recti{ + pos, + {16, 26}, } + .grow(1); } + void paint(Math::Vec2i pos, Gfx::Canvas &g) const override { + g.push(); + g.translate(pos.cast()); + g.beginPath(); + g.path(_path); + g.fill(_fill); + g.stroke({ + .fill = _stroke, + .width = 1, + .align = Gfx::OUTSIDE_ALIGN, + .join = Gfx::MITER_JOIN, + }); + g.pop(); + } +}; + +struct InputTranslator : public Ui::ProxyNode { + App::MouseEvent _mousePrev = {}; + Math::Vec2i _mousePos = {}; + bool _mouseDirty = false; + Box _cursor; + + InputTranslator(Ui::Child child, Box cursor) + : Ui::ProxyNode(std::move(child)), _cursor(std::move(cursor)) {} + void event(App::Event &e) override { if (auto m = e.is()) { if (not _mouseDirty) { _mouseDirty = true; - Ui::shouldRepaint(*this, _cursorDamage()); + Ui::shouldRepaint(*this, _cursor->bound(_mousePos)); Ui::shouldAnimate(*this); } @@ -71,7 +120,7 @@ struct InputTranslator : public Ui::ProxyNode { } else if (auto k = e.is()) { if (_mouseDirty) { _mouseDirty = false; - Ui::shouldRepaint(*this, _cursorDamage()); + Ui::shouldRepaint(*this, _cursor->bound(_mousePos)); } } @@ -81,37 +130,14 @@ struct InputTranslator : public Ui::ProxyNode { void paint(Gfx::Canvas &g, Math::Recti r) override { child().paint(g, r); - if (_cursorDamage().colide(r)) { - if (false) { - g.push(); - g.beginPath(); - g.fillStyle(Gfx::WHITE.withOpacity(0.25)); - g.ellipse(_cursor()); - g.fill(Gfx::FillRule::EVENODD); - g.pop(); - } else { - g.push(); - g.translate(_mousePos.cast()); - g.beginPath(); - g.path(Math::Path::fromSvg( -#include "hideo-shell/cursor.path" - )); - g.fillStyle(Gfx::BLACK); - g.fill(Gfx::FillRule::EVENODD); - g.stroke(Gfx::Stroke{ - .fill = Gfx::WHITE, - .width = 1, - .align = Gfx::INSIDE_ALIGN, - .join = Gfx::MITER_JOIN, - }); - g.pop(); - } + if (_cursor->bound(_mousePos).colide(r)) { + _cursor->paint(_mousePos, g); } } }; Ui::Child inputTranslator(Ui::Child child) { - return makeStrong(std::move(child)); + return makeStrong(std::move(child), makeBox()); } } // namespace Grund::Shell