Vital
Loading...
Searching...
No Matches
line_editor.h
Go to the documentation of this file.
1#pragma once
2
3#include "JuceHeader.h"
4
5#include "line_generator.h"
10#include "synth_lfo.h"
11#include "synth_module.h"
12
27class LineEditor : public OpenGlLineRenderer, public TextEditor::Listener {
28public:
30 static constexpr float kPositionWidth = 9.0f;
32 static constexpr float kPowerWidth = 7.0f;
34 static constexpr float kRingThickness = 0.45f;
36 static constexpr float kGrabRadius = 12.0f;
38 static constexpr float kDragRadius = 20.0f;
39
41 static constexpr int kResolution = 64;
43 static constexpr int kNumWrapPoints = 8;
47 static constexpr int kTotalPoints = kDrawPoints + 2 * kNumWrapPoints;
48
50 static constexpr int kMaxGridSizeX = 32;
51 static constexpr int kMaxGridSizeY = 24;
52
54 static constexpr float kPaddingY = 6.0f;
56 static constexpr float kPaddingX = 0.0f;
57
59 static constexpr float kPowerMouseMultiplier = 9.0f;
61 static constexpr float kMinPointDistanceForPower = 3.0f;
62
81
86 class Listener {
87 public:
88 virtual ~Listener() { }
89
94 virtual void setPhase(float phase) = 0;
95
101 virtual void lineEditorScrolled(const MouseEvent& e, const MouseWheelDetails& wheel) = 0;
102
108 virtual void togglePaintMode(bool enabled, bool temporary_switch) = 0;
109
113 virtual void fileLoaded() = 0;
114
118 virtual void importLfo() = 0;
119
123 virtual void exportLfo() = 0;
124
131 virtual void pointChanged(int index, Point<float> position, bool mouse_up) { }
132
137 virtual void powersChanged(bool mouse_up) { }
138
144 virtual void pointAdded(int index, Point<float> position) { }
145
150 virtual void pointRemoved(int index) { }
151
157 virtual void pointsAdded(int index, int num_points_added) { }
158
164 virtual void pointsRemoved(int index, int num_points_removed) { }
165 };
166
171 LineEditor(LineGenerator* line_source);
172
176 virtual ~LineEditor();
177
181 void resetWavePath();
182
183 void resized() override {
185 drag_circle_.setBounds(getLocalBounds());
186 hover_circle_.setBounds(getLocalBounds());
187 grid_lines_.setBounds(getLocalBounds());
188 position_circle_.setBounds(getLocalBounds());
189 point_circles_.setBounds(getLocalBounds());
190 power_circles_.setBounds(getLocalBounds());
192 }
193
199 float padY(float y);
200
206 float unpadY(float y);
207
213 float padX(float x);
214
220 float unpadX(float x);
221
222 virtual void mouseDown(const MouseEvent& e) override;
223 virtual void mouseDoubleClick(const MouseEvent& e) override;
224 virtual void mouseMove(const MouseEvent& e) override;
225 virtual void mouseDrag(const MouseEvent& e) override;
226 virtual void mouseUp(const MouseEvent& e) override;
227
234 virtual void respondToCallback(int point, int power, int option);
235
241
246 void paintLine(const MouseEvent& e);
247
252 void drawDown(const MouseEvent& e);
253
258 void drawDrag(const MouseEvent& e);
259
264 void drawUp(const MouseEvent& e);
265
266 void mouseWheelMove(const MouseEvent& e, const MouseWheelDetails& wheel) override;
267 void mouseExit(const MouseEvent& e) override;
269
273 void renderGrid(OpenGlWrapper& open_gl, bool animate);
274
278 void renderPoints(OpenGlWrapper& open_gl, bool animate);
279
280 void init(OpenGlWrapper& open_gl) override;
281 void render(OpenGlWrapper& open_gl, bool animate) override;
282 void destroy(OpenGlWrapper& open_gl) override;
283
288 void setSizeRatio(float ratio) { size_ratio_ = ratio; }
289
294 float sizeRatio() const { return size_ratio_; }
295
300 void setLoop(bool loop) { loop_ = loop; }
301
306 void setSmooth(bool smooth) { model_->setSmooth(smooth); resetPositions(); }
307
312 bool getSmooth() const { return model_->smooth(); }
313
318 void setPaint(bool paint);
319
324 void setPaintPattern(std::vector<std::pair<float, float>> pattern) { paint_pattern_ = pattern; }
325
330 virtual void setGridSizeX(int size) { grid_size_x_ = size; setGridPositions(); }
331
336 virtual void setGridSizeY(int size) { grid_size_y_ = size; setGridPositions(); }
337
341 int getGridSizeX() { return grid_size_x_; }
342
346 int getGridSizeY() { return grid_size_y_; }
347
352 void setModel(LineGenerator* model) { model_ = model; resetPositions(); }
353
357 LineGenerator* getModel() { return model_; }
358
362 void showTextEntry();
363
367 void hideTextEntry();
368
369 void textEditorReturnKeyPressed(TextEditor& editor) override;
370 void textEditorFocusLost(TextEditor& editor) override;
371 void textEditorEscapeKeyPressed(TextEditor& editor) override;
372
377
382 void setAllowFileLoading(bool allow) { allow_file_loading_ = allow; }
383
388 void addListener(Listener* listener) { listeners_.push_back(listener); }
389
394 void setActive(bool active) { active_ = active; }
395
399 force_inline void resetPositions() { reset_positions_ = true; }
400
406 if (value_entry_)
407 return value_entry_->getImageComponent();
408 return nullptr;
409 }
410
411protected:
418 void drawPosition(OpenGlWrapper& open_gl, Colour color, float fraction_x);
419
424
428 void setGridPositions();
429
433 void setPointPositions();
434
438 void setGlPositions();
439
444 int getActivePoint() { return active_point_; }
445
450 int getActivePower() { return active_power_; }
451
456 int getActiveGridSection() { return active_grid_section_; }
457
461 bool isPainting() { return paint_ != temporary_paint_toggle_; }
462
466 bool isPaintEnabled() { return paint_; }
467
474
479 virtual void enableTemporaryPaintToggle(bool toggle);
480
482 std::vector<Listener*> listeners_;
483
484private:
485 float adjustBoostPhase(float phase);
486
487 int getHoverPoint(Point<float> position);
488 int getHoverPower(Point<float> position);
489 float getSnapRadiusX();
490 float getSnapRadiusY();
491 float getSnappedX(float x);
492 float getSnappedY(float y);
493
494 void addPointAt(Point<float> position);
495 void movePoint(int index, Point<float> position, bool snap);
496 void movePower(int index, Point<float> position, bool all, bool alternate);
497 void removePoint(int index);
498 float getMinX(int index);
499 float getMaxX(int index);
500
501 Point<float> valuesToOpenGlPosition(float x, float y);
502 Point<float> getPowerPosition(int index);
503 bool powerActive(int index);
504
505 LineGenerator* model_;
506 int active_point_;
507 int active_power_;
508 int active_grid_section_;
509 bool dragging_;
510 bool reset_positions_;
511 bool allow_file_loading_;
512 Point<float> last_mouse_position_;
513 int last_model_render_;
514 bool loop_;
515 int grid_size_x_;
516 int grid_size_y_;
517 bool paint_;
518 bool temporary_paint_toggle_;
519 std::vector<std::pair<float, float>> paint_pattern_;
520 vital::poly_float last_phase_;
521 vital::poly_float last_voice_;
522 vital::poly_float last_last_voice_;
523 float size_ratio_;
524
525 OpenGlQuad drag_circle_;
526 OpenGlQuad hover_circle_;
527 OpenGlMultiQuad grid_lines_;
528 OpenGlQuad position_circle_;
529 OpenGlMultiQuad point_circles_;
530 OpenGlMultiQuad power_circles_;
531 std::unique_ptr<OpenGlTextEditor> value_entry_;
532 bool entering_phase_;
533 int entering_index_;
534
535 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(LineEditor)
536};
Interface for classes that want to receive notifications about line editor changes.
Definition line_editor.h:86
virtual void pointsAdded(int index, int num_points_added)
Called when multiple points are added at once.
Definition line_editor.h:157
virtual void importLfo()=0
Called when the user requests to import an LFO.
virtual void pointRemoved(int index)
Called when a point is removed from the curve.
Definition line_editor.h:150
virtual void powersChanged(bool mouse_up)
Called when the curve powers have changed.
Definition line_editor.h:137
virtual void setPhase(float phase)=0
Called when the LFO phase or start point is changed.
virtual void exportLfo()=0
Called when the user requests to export the current LFO.
virtual void pointAdded(int index, Point< float > position)
Called when a point is added to the curve.
Definition line_editor.h:144
virtual void pointChanged(int index, Point< float > position, bool mouse_up)
Called when a point's position changes.
Definition line_editor.h:131
virtual void fileLoaded()=0
Called when a file (e.g., a saved LFO shape) is loaded into the editor.
virtual ~Listener()
Definition line_editor.h:88
virtual void pointsRemoved(int index, int num_points_removed)
Called when multiple points are removed at once.
Definition line_editor.h:164
virtual void togglePaintMode(bool enabled, bool temporary_switch)=0
Called when paint mode is toggled.
virtual void lineEditorScrolled(const MouseEvent &e, const MouseWheelDetails &wheel)=0
Called when the user scrolls the mouse wheel over the line editor.
A GUI component for editing and visualizing a user-defined line shape (curve or envelope).
Definition line_editor.h:27
bool isPainting()
Checks if painting mode is currently active.
Definition line_editor.h:461
static constexpr float kPaddingX
Horizontal padding in pixels.
Definition line_editor.h:56
void resized() override
Called when the component is resized.
Definition line_editor.h:183
void clearActiveMouseActions()
Definition line_editor.cpp:634
static constexpr int kDrawPoints
Number of points drawn: resolution plus max points from LineGenerator.
Definition line_editor.h:45
static constexpr int kTotalPoints
Total points including wrap-around.
Definition line_editor.h:47
vital::poly_float adjustBoostPhase(vital::poly_float phase)
Adjusts a given phase value for boost calculations.
Definition line_editor.cpp:1061
bool active_
Definition line_editor.h:481
void renderGrid(OpenGlWrapper &open_gl, bool animate)
Renders the grid lines that show snapping lines or painting sections.
Definition line_editor.cpp:763
void setSliderPositionFromText()
Sets the position of the selected point/power from the text field.
Definition line_editor.cpp:1027
virtual void setGridSizeY(int size)
Sets the vertical grid size.
Definition line_editor.h:336
void showTextEntry()
Shows the text editor for entering a precise value or phase.
Definition line_editor.cpp:995
void setPointPositions()
Calculates and sets positions for point and power handle quads.
Definition line_editor.cpp:931
static constexpr float kMinPointDistanceForPower
Minimum horizontal distance (in pixels) between points to show power handles.
Definition line_editor.h:61
void render(OpenGlWrapper &open_gl, bool animate) override
Renders the line using OpenGL.
Definition line_editor.cpp:788
static constexpr int kResolution
Resolution used for intermediate line calculations.
Definition line_editor.h:41
void setLoop(bool loop)
Enables or disables looping of the line at the edges.
Definition line_editor.h:300
static constexpr int kMaxGridSizeY
Definition line_editor.h:51
void init(OpenGlWrapper &open_gl) override
Initializes OpenGL resources for rendering the line.
Definition line_editor.cpp:753
bool getSmooth() const
Checks if smoothing is enabled.
Definition line_editor.h:312
bool isPaintEnabled()
Checks if paint mode is globally enabled.
Definition line_editor.h:466
virtual void mouseUp(const MouseEvent &e) override
Definition line_editor.cpp:603
virtual void setGridSizeX(int size)
Sets the horizontal grid size.
Definition line_editor.h:330
void resetWavePath()
Resets the wave path, recalculating positions based on current points and powers.
Definition line_editor.cpp:643
void setSmooth(bool smooth)
Enables or disables smoothing of the curve.
Definition line_editor.h:306
void addListener(Listener *listener)
Adds a listener for line editor events.
Definition line_editor.h:388
void setEditingCircleBounds()
Sets bounds for the editing circles (hover and drag indicators).
Definition line_editor.cpp:869
void mouseWheelMove(const MouseEvent &e, const MouseWheelDetails &wheel) override
Definition line_editor.cpp:629
virtual void mouseDoubleClick(const MouseEvent &e) override
Definition line_editor.cpp:517
LineGenerator * getModel()
Gets the current LineGenerator model.
Definition line_editor.h:357
bool hasMatchingSystemClipboard()
Checks if the system clipboard contains a compatible line data JSON.
Definition line_editor.cpp:353
void destroy(OpenGlWrapper &open_gl) override
Destroys OpenGL resources allocated by this line renderer.
Definition line_editor.cpp:975
void drawDrag(const MouseEvent &e)
Handles mouse dragging to move points or adjust powers.
Definition line_editor.cpp:591
float padY(float y)
Pads a Y coordinate to fit the drawing area with vertical padding.
Definition line_editor.cpp:729
int getActivePower()
Gets the currently active power handle index.
Definition line_editor.h:450
static constexpr float kRingThickness
Fractional thickness for marker ring rendering.
Definition line_editor.h:34
void drawUp(const MouseEvent &e)
Handles mouse release to finalize point or power positions.
Definition line_editor.cpp:610
virtual void enableTemporaryPaintToggle(bool toggle)
Temporarily enables or disables paint mode using a toggle (e.g., via a modifier key).
Definition line_editor.cpp:1080
static constexpr float kPaddingY
Vertical padding in pixels.
Definition line_editor.h:54
MenuOptions
Context menu options available in the line editor.
Definition line_editor.h:67
@ kFlipHorizontal
Definition line_editor.h:77
@ kRemovePoint
Definition line_editor.h:75
@ kSave
Definition line_editor.h:71
@ kEnterValue
Definition line_editor.h:73
@ kCancel
Definition line_editor.h:68
@ kInit
Definition line_editor.h:76
@ kResetPower
Definition line_editor.h:74
@ kEnterPhase
Definition line_editor.h:72
@ kCopy
Definition line_editor.h:69
@ kNumMenuOptions
Definition line_editor.h:79
@ kPaste
Definition line_editor.h:70
@ kFlipVertical
Definition line_editor.h:78
void paintLine(const MouseEvent &e)
Paints the line by adding points according to a pattern when in paint mode.
Definition line_editor.cpp:364
float sizeRatio() const
Gets the current size ratio.
Definition line_editor.h:294
static constexpr int kNumWrapPoints
Number of wrap points for looping lines.
Definition line_editor.h:43
static constexpr float kDragRadius
Radius in pixels for dragging a point or power handle.
Definition line_editor.h:38
int getGridSizeY()
Gets the current vertical grid size.
Definition line_editor.h:346
static constexpr float kPositionWidth
Width in pixels for main position markers.
Definition line_editor.h:30
void mouseExit(const MouseEvent &e) override
Definition line_editor.cpp:624
float unpadY(float y)
Removes padding from a padded Y coordinate.
Definition line_editor.cpp:734
void setAllowFileLoading(bool allow)
Allows or disallows file loading actions (copy/paste from files).
Definition line_editor.h:382
void setSizeRatio(float ratio)
Sets the size ratio, scaling UI elements accordingly.
Definition line_editor.h:288
void setGlPositions()
Updates OpenGL buffers with the latest positions if needed.
Definition line_editor.cpp:963
force_inline void resetPositions()
Marks positions as needing recalculation on next render.
Definition line_editor.h:399
void setModel(LineGenerator *model)
Sets the LineGenerator model defining the curve.
Definition line_editor.h:352
void drawDown(const MouseEvent &e)
Handles the initial mouse press (not in paint mode) for dragging points or powers.
Definition line_editor.cpp:497
static constexpr float kPowerMouseMultiplier
Multiplier for mouse movements when adjusting power handles.
Definition line_editor.h:59
OpenGlComponent * getTextEditorComponent()
Gets the OpenGlComponent for text editing (if any).
Definition line_editor.h:405
void setGridPositions()
Calculates and sets positions for grid lines.
Definition line_editor.cpp:898
static constexpr float kPowerWidth
Width in pixels for power markers (curve shaping handles).
Definition line_editor.h:32
virtual void mouseMove(const MouseEvent &e) override
Definition line_editor.cpp:557
virtual void respondToCallback(int point, int power, int option)
Responds to a callback triggered by a menu option or action.
Definition line_editor.cpp:287
void setActive(bool active)
Sets whether the line editor is active (enabled).
Definition line_editor.h:394
void setPaint(bool paint)
Enables or disables paint mode.
Definition line_editor.cpp:985
void drawPosition(OpenGlWrapper &open_gl, Colour color, float fraction_x)
Draws a position marker at a specific fraction of the X-axis.
Definition line_editor.cpp:838
int getActivePoint()
Gets the currently active point index.
Definition line_editor.h:444
int getGridSizeX()
Gets the current horizontal grid size.
Definition line_editor.h:341
virtual void mouseDrag(const MouseEvent &e) override
Definition line_editor.cpp:582
void hideTextEntry()
Hides the text entry editor.
Definition line_editor.cpp:991
int getActiveGridSection()
Gets the currently active grid section index for painting.
Definition line_editor.h:456
float padX(float x)
Pads an X coordinate to fit the drawing area with horizontal padding.
Definition line_editor.cpp:739
void textEditorEscapeKeyPressed(TextEditor &editor) override
Definition line_editor.cpp:1023
LineEditor(LineGenerator *line_source)
Constructs the LineEditor.
Definition line_editor.cpp:21
void renderPoints(OpenGlWrapper &open_gl, bool animate)
Renders the points and power handles on the curve.
Definition line_editor.cpp:768
void setPaintPattern(std::vector< std::pair< float, float > > pattern)
Sets a pattern of points used when painting the line.
Definition line_editor.h:324
virtual void mouseDown(const MouseEvent &e) override
Definition line_editor.cpp:455
std::vector< Listener * > listeners_
Definition line_editor.h:482
void textEditorFocusLost(TextEditor &editor) override
Definition line_editor.cpp:1019
virtual ~LineEditor()
Destructor.
Definition line_editor.cpp:87
static constexpr int kMaxGridSizeX
Maximum grid sizes for horizontal and vertical lines.
Definition line_editor.h:50
void textEditorReturnKeyPressed(TextEditor &editor) override
Definition line_editor.cpp:1015
float unpadX(float x)
Removes padding from a padded X coordinate.
Definition line_editor.cpp:746
static constexpr float kGrabRadius
Radius in pixels for detecting grabbing a point or power handle.
Definition line_editor.h:36
A class for generating and storing a line shape, defined by a series of points and associated powers.
Definition line_generator.h:20
force_inline bool smooth() const
Indicates whether smoothing is enabled.
Definition line_generator.h:280
void setSmooth(bool smooth)
Enables or disables smoothing behavior.
Definition line_generator.h:96
static constexpr int kMaxPoints
Maximum number of points that can define the line.
Definition line_generator.h:25
A base component class that integrates JUCE's Component with OpenGL rendering.
Definition open_gl_component.h:20
virtual void resized() override
Called when the component is resized.
Definition open_gl_component.cpp:121
A component for rendering lines with optional filling and boost effects using OpenGL.
Definition open_gl_line_renderer.h:16
force_inline Colour color() const
Gets the current line color.
Definition open_gl_line_renderer.h:189
A component for rendering multiple quads using OpenGL, with customizable colors, rounding,...
Definition open_gl_multi_quad.h:16
A convenience class for a single quad rendered via OpenGL.
Definition open_gl_multi_quad.h:447
#define force_inline
Definition common.h:23
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174
Represents a vector of floating-point values using SIMD instructions.
Definition poly_values.h:600
Defines the SynthModule class which extends ProcessorRouter to form a building block of the Vital syn...