Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
component_dragger.hpp
Go to the documentation of this file.
1#pragma once
2
36
37#include <algorithm>
39#include <pulp/view/view.hpp>
40
41namespace pulp::view {
42
44public:
45 ComponentDragger() = default;
46
50 void start_dragging(View& view, Point mouse_local) {
51 mouse_start_ = mouse_local;
52 bounds_start_ = view.bounds();
53 active_ = true;
54 }
55
59 void drag_view(View& view, Point mouse_local) {
60 if (!active_) return;
61 const Point delta{mouse_local.x - mouse_start_.x,
62 mouse_local.y - mouse_start_.y};
63 Rect target = bounds_start_;
64 target.x += delta.x;
65 target.y += delta.y;
66
67 if (constrain_to_parent_) {
68 if (auto* p = view.parent()) {
69 const Rect parent_local = p->local_bounds();
70 target.x = std::clamp(target.x,
71 parent_local.x,
72 std::max(parent_local.x,
73 parent_local.right() - target.width));
74 target.y = std::clamp(target.y,
75 parent_local.y,
76 std::max(parent_local.y,
77 parent_local.bottom() - target.height));
78 }
79 }
80 view.set_bounds(target);
81 if (on_drag) on_drag(target);
82 }
83
86 void end_dragging() {
87 active_ = false;
88 if (on_end) on_end();
89 }
90
93 void set_constrain_to_parent(bool yes) { constrain_to_parent_ = yes; }
94 bool constrain_to_parent() const { return constrain_to_parent_; }
95
96 bool is_dragging() const { return active_; }
97
99 Point mouse_start() const { return mouse_start_; }
101 Rect bounds_start() const { return bounds_start_; }
102
104 std::function<void(Rect)> on_drag;
105 std::function<void()> on_end;
106
107private:
108 bool active_ = false;
109 bool constrain_to_parent_ = true;
110 Point mouse_start_{};
111 Rect bounds_start_{};
112};
113
114} // namespace pulp::view
std::function< void(Rect)> on_drag
Optional callbacks for tests / app glue.
Definition component_dragger.hpp:104
Rect bounds_start() const
Dragged view's bounds captured at start_dragging.
Definition component_dragger.hpp:101
void start_dragging(View &view, Point mouse_local)
Begin a drag gesture.
Definition component_dragger.hpp:50
bool is_dragging() const
Definition component_dragger.hpp:96
bool constrain_to_parent() const
Definition component_dragger.hpp:94
std::function< void()> on_end
Definition component_dragger.hpp:105
void drag_view(View &view, Point mouse_local)
Continue a drag gesture.
Definition component_dragger.hpp:59
Point mouse_start() const
Mouse-local press position captured at start_dragging.
Definition component_dragger.hpp:99
void end_dragging()
End the gesture.
Definition component_dragger.hpp:86
void set_constrain_to_parent(bool yes)
When true (the default), drag_view clamps the dragged view's origin so it stays fully inside the pare...
Definition component_dragger.hpp:93
Definition view.hpp:73
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ p
Definition input_events.hpp:40
Definition geometry.hpp:20
float x
Definition geometry.hpp:21
float y
Definition geometry.hpp:21
Axis-aligned rectangle in continuous (float) coordinates.
Definition geometry.hpp:52
float x
Definition geometry.hpp:53
constexpr float right() const
Definition geometry.hpp:55
float height
Definition geometry.hpp:53
float width
Definition geometry.hpp:53
float y
Definition geometry.hpp:53
constexpr float bottom() const
Definition geometry.hpp:56