Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
reorder_list.hpp
Go to the documentation of this file.
1#pragma once
2
17
21#include <pulp/view/view.hpp>
22
23#include <functional>
24#include <memory>
25#include <vector>
26
27namespace pulp::view {
28
33int reorder_target_index(int from, float offset, float pitch, int count);
34
38class ReorderList : public View {
39public:
41
42 ReorderList() = default;
43
48 void add_item(std::unique_ptr<View> item);
49
50 int item_count() const { return static_cast<int>(items_.size()); }
52 View* item_at(int i) const {
53 return (i >= 0 && i < item_count()) ? items_[i] : nullptr;
54 }
55
56 int index_of(const View* v) const;
57
58 void set_orientation(Orientation o) { orientation_ = o; }
59 Orientation orientation() const { return orientation_; }
60 void set_item_extent(float px) { item_extent_ = px; }
61 void set_gap(float px) { gap_ = px; }
63 float pitch() const { return item_extent_ + gap_; }
64
66 int dragging_index() const { return dragging_; }
68 int drop_target_index() const { return target_; }
69
72 std::function<void(int from, int to)> on_reorder;
73
74 void layout_children() override;
75 void paint(canvas::Canvas& canvas) override;
76 void on_mouse_down(Point pos) override;
77 void on_mouse_drag(Point pos) override;
78 void on_mouse_up(Point pos) override;
79 bool wants_mouse_input() const override { return true; }
80
81private:
82 // Main-axis coordinate of a point in this view's local space.
83 float main_of(Point p) const {
84 return orientation_ == Orientation::vertical ? p.y : p.x;
85 }
86 // Position item `display_index` at its resting slot (accounting for an
87 // in-progress drag that opened a gap), writing its bounds.
88 void position_item(int display_index) const;
89
90 std::vector<View*> items_;
92 float item_extent_ = 40.0f;
93 float gap_ = 6.0f;
94
95 int dragging_ = -1;
96 int target_ = -1;
97 float drag_start_main_ = 0;
98 float drag_offset_ = 0;
99 ValueAnimation settle_{0.0f};
100 ValueAnimation glow_{0.0f};
101 int glow_index_ = -1;
102};
103
104} // namespace pulp::view
Definition canvas.hpp:240
View * item_at(int i) const
Item at DISPLAY index i (reflects reorders), or nullptr out of range.
Definition reorder_list.hpp:52
int item_count() const
Definition reorder_list.hpp:50
float pitch() const
Distance between adjacent item origins along the main axis.
Definition reorder_list.hpp:63
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 reorder_list.hpp:79
int drop_target_index() const
The index the dragged item would land on right now, or -1 when idle.
Definition reorder_list.hpp:68
std::function< void(int from, int to)> on_reorder
Fired once on a committed reorder (drop that changed the index).
Definition reorder_list.hpp:72
Orientation
Definition reorder_list.hpp:40
@ horizontal
Definition reorder_list.hpp:40
@ vertical
Definition reorder_list.hpp:40
int index_of(const View *v) const
Current display index of v, or -1 if not an item.
int dragging_index() const
The item currently being dragged (display index), or -1 when idle.
Definition reorder_list.hpp:66
void set_item_extent(float px)
height (vertical) / width (horizontal)
Definition reorder_list.hpp:60
void on_mouse_down(Point pos) override
void on_mouse_drag(Point pos) override
Orientation orientation() const
Definition reorder_list.hpp:59
void paint(canvas::Canvas &canvas) override
void on_mouse_up(Point pos) override
void add_item(std::unique_ptr< View > item)
Append an item (ownership moves in).
void set_gap(float px)
Definition reorder_list.hpp:61
void layout_children() override
void set_orientation(Orientation o)
Definition reorder_list.hpp:58
Definition affine_transform.hpp:24
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ px
Absolute pixels.
Definition geometry.hpp:393
@ p
Definition input_events.hpp:40
@ i
Definition input_events.hpp:40
@ v
Definition input_events.hpp:41
@ o
Definition input_events.hpp:40
int reorder_target_index(int from, float offset, float pitch, int count)
Target index for a drag that started on item from, moved offset along the main axis,...
Definition geometry.hpp:20