Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
lasso.hpp
Go to the documentation of this file.
1#pragma once
2
3// LassoComponent — rubber-band rectangle selection overlay.
4// Draw a selection rectangle by clicking and dragging on an empty area.
5// Items within the rectangle are added to the selection set.
6// Inspired by PlunderTube's VisageMarqueeOverlay pattern.
7
8#include <pulp/view/view.hpp>
9#include <functional>
10#include <vector>
11#include <cmath>
12#include <algorithm>
13
14namespace pulp::view {
15
18 float x = 0, y = 0, width = 0, height = 0;
19
20 bool contains(float px, float py) const {
21 return px >= x && px < x + width && py >= y && py < y + height;
22 }
23
24 bool intersects(float rx, float ry, float rw, float rh) const {
26 }
27};
28
32class LassoComponent : public View {
33public:
35 set_focusable(false);
36 }
37
40 std::function<void(const SelectionRect&)> on_selection_changed;
41
43 std::function<void(const SelectionRect&)> on_selection_complete;
44
46 void begin_selection(float x, float y) {
47 active_ = true;
48 start_x_ = x;
49 start_y_ = y;
50 current_x_ = x;
51 current_y_ = y;
52 set_visible(true);
53 }
54
56 void update_selection(float x, float y) {
57 if (!active_) return;
58 current_x_ = x;
59 current_y_ = y;
60
63 }
64
67 if (!active_) return;
68
71
72 active_ = false;
73 set_visible(false);
74 }
75
77 bool is_active() const { return active_; }
78
81 float x = std::min(start_x_, current_x_);
82 float y = std::min(start_y_, current_y_);
83 float w = std::abs(current_x_ - start_x_);
84 float h = std::abs(current_y_ - start_y_);
85 return {x, y, w, h};
86 }
87
88 void paint(canvas::Canvas& canvas) override {
89 if (!active_) return;
90
91 auto rect = selection_rect();
92
93 // Draw selection rectangle with semi-transparent fill and border.
94 // Use rgba8 factory — Color has float r/g/b/a fields, no int ctor;
95 // paren-aggregate-init (C++20 P0960) only works on Clang 16+
96 // (macos-15), not macos-14's Clang 15 used by the sanitizer matrix.
97 canvas.set_fill_color(canvas::Color::rgba8(100, 150, 255, 40));
98 canvas.fill_rect(rect.x, rect.y, rect.width, rect.height);
99
100 canvas.set_stroke_color(canvas::Color::rgba8(100, 150, 255, 180));
101 canvas.set_line_width(1.0f);
102 canvas.stroke_rect(rect.x, rect.y, rect.width, rect.height);
103 }
104
105 void on_mouse_down(Point pos) override {
106 begin_selection(pos.x, pos.y);
107 }
108
109 void on_mouse_drag(Point pos) override {
110 update_selection(pos.x, pos.y);
111 }
112
113 void on_mouse_up(Point /*pos*/) override {
115 }
116
117 bool wants_mouse_input() const override { return true; }
118
119private:
120 bool active_ = false;
121 float start_x_ = 0, start_y_ = 0;
122 float current_x_ = 0, current_y_ = 0;
123};
124
125} // namespace pulp::view
Definition canvas.hpp:240
void paint(canvas::Canvas &canvas) override
Definition lasso.hpp:88
LassoComponent()
Definition lasso.hpp:34
void end_selection()
End the selection.
Definition lasso.hpp:66
SelectionRect selection_rect() const
Get the current selection rectangle.
Definition lasso.hpp:80
std::function< void(const SelectionRect &)> on_selection_changed
Called during drag with the current selection rectangle (in parent coords).
Definition lasso.hpp:40
void on_mouse_up(Point) override
Definition lasso.hpp:113
std::function< void(const SelectionRect &)> on_selection_complete
Called when the drag ends with the final selection rectangle.
Definition lasso.hpp:43
bool is_active() const
Whether a lasso selection is currently active.
Definition lasso.hpp:77
void begin_selection(float x, float y)
Start a new lasso selection at the given point.
Definition lasso.hpp:46
void on_mouse_drag(Point pos) override
Definition lasso.hpp:109
void update_selection(float x, float y)
Update the selection rectangle during drag.
Definition lasso.hpp:56
bool wants_mouse_input() const override
True if this widget handles mouse down/drag through virtual methods even when it does not expose JS p...
Definition lasso.hpp:117
void on_mouse_down(Point pos) override
Definition lasso.hpp:105
void set_visible(bool v)
Show or hide this view.
Definition view.hpp:343
void set_focusable(bool f)
Definition view.hpp:669
Definition affine_transform.hpp:24
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ px
Absolute pixels.
Definition geometry.hpp:393
@ h
Definition input_events.hpp:39
@ y
Definition input_events.hpp:42
@ x
Definition input_events.hpp:41
@ w
Definition input_events.hpp:41
static constexpr Color rgba8(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Construct from 8-bit channels [0,255] — convenience for legacy/hex code.
Definition canvas.hpp:61
Definition geometry.hpp:20
float x
Definition geometry.hpp:21
float y
Definition geometry.hpp:21
Rectangle in parent coordinates.
Definition lasso.hpp:17
float height
Definition lasso.hpp:18
float x
Definition lasso.hpp:18
bool contains(float px, float py) const
Definition lasso.hpp:20
float y
Definition lasso.hpp:18
float width
Definition lasso.hpp:18
bool intersects(float rx, float ry, float rw, float rh) const
Definition lasso.hpp:24