Vital
Loading...
Searching...
No Matches
bar_editor.cpp
Go to the documentation of this file.
1#include "bar_editor.h"
2
4#include "utils.h"
5
6namespace {
12 void barEditorCallback(int result, BarEditor* bar_editor) {
13 if (bar_editor == nullptr)
14 return;
15
16 if (result == BarEditor::kClear)
17 bar_editor->clear();
18 else if (result == BarEditor::kClearRight)
19 bar_editor->clearRight();
20 else if (result == BarEditor::kClearLeft)
21 bar_editor->clearLeft();
22 else if (result == BarEditor::kClearEven)
23 bar_editor->clearEven();
24 else if (result == BarEditor::kClearOdd)
25 bar_editor->clearOdd();
26 else if (result == BarEditor::kRandomize)
27 bar_editor->randomize();
28 }
29}
30
31void BarEditor::mouseMove(const MouseEvent& e) {
32 current_mouse_position_ = e.getPosition();
33}
34
35void BarEditor::mouseDown(const MouseEvent& e) {
36 current_mouse_position_ = e.getPosition();
38
39 if (e.mods.isPopupMenu()) {
40 PopupItems options;
41 options.addItem(kClear, "Clear");
42 options.addItem(kClearLeft, "Clear Left");
43 options.addItem(kClearRight, "Clear Right");
44 options.addItem(kClearOdd, "Clear Odd");
45 options.addItem(kClearEven, "Clear Even");
46 options.addItem(kRandomize, "Randomize");
47
48 SynthSection* parent = findParentComponentOfClass<SynthSection>();
49 parent->showPopupSelector(this, e.getPosition(), options,
50 [=](int selection) { barEditorCallback(selection, this); });
51 }
52 else {
53 changeValues(e);
54 editing_ = true;
55 }
56}
57
58void BarEditor::mouseUp(const MouseEvent& e) {
59 editing_ = false;
60 current_mouse_position_ = e.getPosition();
61
62 if (!e.mods.isPopupMenu()) {
64
65 for (Listener* listener : listeners_)
66 listener->barsChanged(index, index, true);
67 }
68}
69
70void BarEditor::mouseDrag(const MouseEvent& e) {
71 current_mouse_position_ = e.getPosition();
72 if (!e.mods.isPopupMenu()) {
73 changeValues(e);
75 }
76}
77
78void BarEditor::mouseExit(const MouseEvent& e) {
79 current_mouse_position_ = Point<int>(-10, -10);
80}
81
83 setY(0, -1.0f);
84 for (int i = 1; i < num_points_; ++i)
86
87 for (Listener* listener : listeners_)
88 listener->barsChanged(0, num_points_ - 1, true);
89}
90
92 for (int i = 0; i < num_points_; ++i)
94
95 for (Listener* listener : listeners_)
96 listener->barsChanged(0, num_points_ - 1, true);
97}
98
100 int position = getHoveredIndex(last_edit_position_);
101 for (int i = position + 1; i < num_points_; ++i)
102 setY(i, clear_value_);
103
104 for (Listener* listener : listeners_)
105 listener->barsChanged(position + 1, num_points_ - 1, true);
106}
107
109 int position = getHoveredIndex(last_edit_position_);
110 for (int i = 0; i < position; ++i)
111 setY(i, clear_value_);
112
113 for (Listener* listener : listeners_)
114 listener->barsChanged(0, position - 1, true);
115}
116
118 for (int i = 0; i < num_points_; i += 2)
119 setY(i, clear_value_);
120
121 for (Listener* listener : listeners_)
122 listener->barsChanged(0, num_points_ - 1, true);
123}
124
126 for (int i = 1; i < num_points_; i += 2)
127 setY(i, clear_value_);
128
129 for (Listener* listener : listeners_)
130 listener->barsChanged(0, num_points_ - 1, true);
131}
132
133void BarEditor::changeValues(const MouseEvent& e) {
134 Point<int> mouse_position = e.getPosition();
135
136 int from_index = getHoveredIndex(last_edit_position_);
137 int selected_index = getHoveredIndex(mouse_position);
138
139 float x = mouse_position.x;
140 float y = mouse_position.y;
141 float x_delta = last_edit_position_.x - x;
142 float y_delta = last_edit_position_.y - y;
143 float slope = y_delta == 0 ? 0 : y_delta / x_delta;
144 float next_x = getWidth() * (scale_ * selected_index) / num_points_;
145 int direction = -1;
146
147 if (selected_index < from_index) {
148 direction = 1;
149 next_x += getWidth() * scale_ / num_points_;
150 }
151 float inc_x = next_x - x;
152
153 for (int index = selected_index; index != from_index + direction; index += direction) {
154 if (index >= 0 && index < num_points_) {
155 float new_value = -2.0f * y / getHeight() + 1.0f;
156 setY(index, vital::utils::clamp(new_value, -1.0f, 1.0f));
157 }
158
159 y += inc_x * slope;
160 inc_x = direction * scale_ * getWidth() * 1.0f / num_points_;
161 }
162
163 int min_index = std::min(from_index, selected_index);
164 int max_index = std::max(from_index, selected_index);
165
166 for (Listener* listener : listeners_)
167 listener->barsChanged(min_index, max_index, false);
168
169 dirty_ = true;
170}
171
172int BarEditor::getHoveredIndex(Point<int> position) {
173 int index = floorf(num_points_ * (1.0f * position.x) / getWidth() / scale_);
174 return vital::utils::iclamp(index, 0, num_points_ - 1);
175}
Interface for receiving notifications when bar values are changed.
Definition bar_editor.h:24
An interactive component that allows editing individual bars in a bar graph visually.
Definition bar_editor.h:18
void mouseExit(const MouseEvent &e) override
Handles mouse exit events. Clears hover state.
Definition bar_editor.cpp:78
void clearOdd()
Clears every odd-indexed bar.
Definition bar_editor.cpp:125
void mouseDown(const MouseEvent &e) override
Handles mouse down events. Initiates editing or shows popup menu.
Definition bar_editor.cpp:35
Point< int > current_mouse_position_
Current mouse position.
Definition bar_editor.h:196
void mouseDrag(const MouseEvent &e) override
Handles mouse drag events. Updates bar values while dragging.
Definition bar_editor.cpp:70
void mouseMove(const MouseEvent &e) override
Handles mouse move events to update hovered bar.
Definition bar_editor.cpp:31
void mouseUp(const MouseEvent &e) override
Handles mouse up events. Completes editing operation.
Definition bar_editor.cpp:58
vital::utils::RandomGenerator random_generator_
Random generator for randomizing bars.
Definition bar_editor.h:194
float clear_value_
Value to clear bars to.
Definition bar_editor.h:199
void changeValues(const MouseEvent &e)
Changes values based on mouse drag position.
Definition bar_editor.cpp:133
void clearLeft()
Clears bars to the left of the currently hovered bar.
Definition bar_editor.cpp:108
void clearRight()
Clears bars to the right of the currently hovered bar.
Definition bar_editor.cpp:99
@ kClearEven
Definition bar_editor.h:47
@ kClearLeft
Definition bar_editor.h:46
@ kRandomize
Definition bar_editor.h:49
@ kClearOdd
Definition bar_editor.h:48
@ kClear
Definition bar_editor.h:44
@ kClearRight
Definition bar_editor.h:45
void clearEven()
Clears every even-indexed bar.
Definition bar_editor.cpp:117
bool editing_
Whether the user is currently editing bars.
Definition bar_editor.h:198
int getHoveredIndex(Point< int > position)
Gets the index of the bar under the given position.
Definition bar_editor.cpp:172
void randomize()
Randomizes all bars using a uniform distribution.
Definition bar_editor.cpp:82
void clear()
Clears all bars to the clear value.
Definition bar_editor.cpp:91
std::vector< Listener * > listeners_
List of listeners for bar changes.
Definition bar_editor.h:195
Point< int > last_edit_position_
Last position during editing for interpolation.
Definition bar_editor.h:197
int num_points_
Number of bars to render.
Definition bar_renderer.h:280
bool dirty_
True if bar data needs to be re-uploaded to the GPU.
Definition bar_renderer.h:279
float scale_
Scale factor for bar dimensions.
Definition bar_renderer.h:271
force_inline void setY(int index, float val)
Sets the top y-position of a specific bar.
Definition bar_renderer.h:154
Base class for all synthesizer sections, providing UI layout, painting, and interaction logic.
Definition synth_section.h:193
void showPopupSelector(Component *source, Point< int > position, const PopupItems &options, std::function< void(int)> callback, std::function< void()> cancel={ })
Shows a popup selector with options.
Definition synth_section.cpp:119
force_inline mono_float next()
Returns the next random float in [min, max].
Definition utils.h:85
force_inline int iclamp(int value, int min_val, int max_val)
Clamps an integer between [min_val, max_val].
Definition utils.h:250
force_inline poly_float clamp(poly_float value, mono_float min, mono_float max)
Clamps each lane of a vector to [min, max].
Definition poly_utils.h:306
A hierarchical structure of popup menu items for a selector component.
Definition synth_section.h:29
void addItem(int sub_id, const std::string &sub_name, bool sub_selected=false)
Adds a new item as a submenu entry.
Definition synth_section.h:51
Provides various utility functions, classes, and constants for audio, math, and general-purpose opera...