Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
live_constant_editor.hpp
Go to the documentation of this file.
1#pragma once
2
3// LiveConstantEditor — debug overlay for tweaking numeric constants at runtime.
4// Register constants with PULP_LIVE_CONSTANT(value, min, max), then open the
5// editor overlay to adjust them without recompiling.
6
7#include <pulp/view/view.hpp>
8#include <string>
9#include <vector>
10#include <map>
11#include <functional>
12#include <mutex>
13
14namespace pulp::view {
15
18 std::string name;
19 std::string file;
20 int line = 0;
21 float value = 0;
22 float min_value = 0;
23 float max_value = 1;
24 float default_value = 0;
25};
26
28class LiveConstantRegistry {
29public:
30 static LiveConstantRegistry& instance();
31
33 float& register_constant(std::string_view name, std::string_view file, int line,
34 float default_value, float min_val, float max_val);
35
37 std::vector<LiveConstant> all() const;
38
40 void reset_all();
41
43 void reset(std::string_view name);
44
46 void set(std::string_view name, float value);
47
49 float get(std::string_view name) const;
50
52 int count() const;
53
55 std::function<void(const LiveConstant&)> on_change;
56
57private:
58 LiveConstantRegistry() = default;
59 mutable std::mutex mutex_;
60 std::map<std::string, LiveConstant, std::less<>> constants_;
61};
62
64class LiveConstantEditor : public View {
65public:
67
69 void toggle() { set_visible(!visible()); }
70
72 bool is_showing() const { return visible(); }
73
75 void set_row_height(float h) { row_height_ = h; }
76
77 void paint(canvas::Canvas& canvas) override;
78 void on_mouse_down(Point pos) override;
79 void on_mouse_drag(Point pos) override;
80 bool wants_mouse_input() const override { return true; }
81
82private:
83 float row_height_ = 28.0f;
84 int dragging_index_ = -1;
85
86 int hit_test_row(float y) const;
87};
88
89} // namespace pulp::view
90
91// Macro for declaring live constants
92// Usage: float radius = PULP_LIVE_CONSTANT(8.0f, 0.0f, 50.0f);
93#define PULP_LIVE_CONSTANT(default_val, min_val, max_val) \
94 (::pulp::view::LiveConstantRegistry::instance().register_constant( \
95 #default_val, __FILE__, __LINE__, default_val, min_val, max_val))
Definition canvas.hpp:240
void on_mouse_drag(Point pos) override
bool is_showing() const
Whether the editor is showing.
Definition live_constant_editor.hpp:72
void set_row_height(float h)
Slider height per constant.
Definition live_constant_editor.hpp:75
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 live_constant_editor.hpp:80
void toggle()
Toggle visibility.
Definition live_constant_editor.hpp:69
void on_mouse_down(Point pos) override
void paint(canvas::Canvas &canvas) override
std::function< void(const LiveConstant &)> on_change
Called when any constant changes.
Definition live_constant_editor.hpp:55
void set(std::string_view name, float value)
Set a constant value by name.
float & register_constant(std::string_view name, std::string_view file, int line, float default_value, float min_val, float max_val)
Register a constant. Returns a reference to the stored value.
void reset(std::string_view name)
Reset a specific constant.
float get(std::string_view name) const
Get a constant value by name.
int count() const
Number of registered constants.
std::vector< LiveConstant > all() const
Get all registered constants.
static LiveConstantRegistry & instance()
void reset_all()
Reset all constants to defaults.
void set_visible(bool v)
Show or hide this view.
Definition view.hpp:343
bool visible() const
Definition view.hpp:333
Definition affine_transform.hpp:24
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
@ value
the widget's primary value (Knob, Fader, RangeSlider, Toggle, ProgressBar)
Definition binding_diagnostics.hpp:19
@ h
Definition input_events.hpp:39
@ y
Definition input_events.hpp:42
A single live-editable constant.
Definition live_constant_editor.hpp:17
float value
Definition live_constant_editor.hpp:21
int line
Definition live_constant_editor.hpp:20
std::string file
Definition live_constant_editor.hpp:19
float max_value
Definition live_constant_editor.hpp:23
float default_value
Definition live_constant_editor.hpp:24
float min_value
Definition live_constant_editor.hpp:22
std::string name
Definition live_constant_editor.hpp:18
Definition geometry.hpp:20