Vital
Loading...
Searching...
No Matches
synth_button.cpp
Go to the documentation of this file.
1
3
4#include "synth_button.h"
5
7#include "full_interface.h"
8#include "synth_strings.h"
10#include "synth_section.h"
11#include "text_look_and_feel.h"
12
18
19 Colour active_color;
20 Colour hover_color;
21 if (button_->getToggleState() && use_on_colors_) {
22 if (down_)
23 active_color = on_down_color_;
24 else
25 active_color = on_normal_color_;
26
27 hover_color = on_hover_color_;
28 }
29 else {
30 if (down_)
31 active_color = off_down_color_;
32 else
33 active_color = off_normal_color_;
34
35 hover_color = off_hover_color_;
36 }
37
38 if (!down_)
39 active_color = active_color.interpolatedWith(hover_color, hover_amount_);
40
41 shape_.setColor(active_color);
42 shape_.render(open_gl, animate);
43};
44
47 if (hover_)
48 hover_amount_ = std::min(1.0f, hover_amount_ + kHoverInc);
49 else
50 hover_amount_ = std::max(0.0f, hover_amount_ - kHoverInc);
51}
52
55 if (button_->findParentComponentOfClass<SynthGuiInterface>() == nullptr)
56 return;
57
58 body_color_ = button_->findColour(Skin::kBody, true);
59 if (style_ == kTextButton || style_ == kJustText) {
60 on_color_ = button_->findColour(Skin::kIconButtonOn, true);
63 off_color_ = button_->findColour(Skin::kIconButtonOff, true);
67 }
68 else if (style_ == kPowerButton) {
69 on_color_ = button_->findColour(Skin::kPowerButtonOn, true);
71 on_hover_color_ = button_->findColour(Skin::kLightenScreen, true);
72 off_color_ = button_->findColour(Skin::kPowerButtonOff, true);
76 }
77 else if (style_ == kUiButton) {
79 on_color_ = button_->findColour(Skin::kUiActionButton, true);
82 }
83 else {
84 on_color_ = button_->findColour(Skin::kUiButton, true);
86 on_hover_color_ = button_->findColour(Skin::kUiButtonHover, true);
87 }
89 }
90 else if (style_ == kLightenButton) {
91 on_color_ = Colours::transparentWhite;
93 on_hover_color_ = button_->findColour(Skin::kLightenScreen, true);
98 }
99}
100
106
107 Colour active_color;
108 Colour hover_color;
109 if (button_->getToggleState() && show_on_colors_) {
110 if (down_)
111 active_color = on_pressed_color_;
112 else
113 active_color = on_color_;
114
115 hover_color = on_hover_color_;
116 }
117 else {
118 if (down_)
119 active_color = off_pressed_color_;
120 else
121 active_color = off_color_;
122
123 hover_color = off_hover_color_;
124 }
125
126 if (!down_)
127 active_color = active_color.interpolatedWith(hover_color, hover_amount_);
128
130 if (!text_.isActive()) {
131 background_.setColor(active_color);
132 background_.render(open_gl, animate);
133 return;
134 }
135
136 if (style_ != kJustText) {
138 background_.render(open_gl, animate);
139 }
140 text_.setColor(active_color);
141 text_.render(open_gl, animate);
142}
143
148 static constexpr float kPowerRadius = 0.45f;
149 static constexpr float kPowerHoverRadius = 0.65f;
150
151 if (button_->getToggleState())
153 else
155
156 background_.setQuad(0, -kPowerRadius, -kPowerRadius, 2.0f * kPowerRadius, 2.0f * kPowerRadius);
157 background_.render(open_gl, animate);
158
160
161 background_.setQuad(0, -kPowerHoverRadius, -kPowerHoverRadius, 2.0f * kPowerHoverRadius, 2.0f * kPowerHoverRadius);
162 if (down_) {
164 background_.render(open_gl, animate);
165 }
166 else if (hover_amount_) {
168 background_.render(open_gl, animate);
169 }
170}
171
176 bool enabled = button_->isEnabled();
178
179 Colour active_color;
180 if (down_)
181 active_color = on_pressed_color_;
182 else
183 active_color = on_color_;
184
185 if (!down_ && enabled)
186 active_color = active_color.interpolatedWith(on_hover_color_, hover_amount_);
187
189 background_.setColor(active_color);
190 background_.render(open_gl, animate);
191
193 if (!enabled) {
195
196 float border_x = 4.0f / button_->getWidth();
197 float border_y = 4.0f / button_->getHeight();
198 background_.setQuad(0, -1.0f + border_x, -1.0f + border_y, 2.0f - 2.0f * border_x, 2.0f - 2.0f * border_y);
200 background_.render(open_gl, animate);
201
202 background_.setQuad(0, -1.0f, -1.0f, 2.0f, 2.0f);
203 }
204
205 text_.render(open_gl, animate);
206}
207
212 bool enabled = button_->isEnabled();
214
215 Colour active_color;
216 if (down_)
217 active_color = on_pressed_color_;
218 else
219 active_color = on_color_;
220
221 if (!down_ && enabled)
222 active_color = active_color.interpolatedWith(on_hover_color_, hover_amount_);
223
225 background_.setColor(active_color);
226 background_.render(open_gl, animate);
227}
228
231 if (hover_)
232 hover_amount_ = std::min(1.0f, hover_amount_ + kHoverInc);
233 else
234 hover_amount_ = std::max(0.0f, hover_amount_ - kHoverInc);
235}
236
239 static constexpr float kUiButtonSizeMult = 0.45f;
240
241 ToggleButton::resized();
242 SynthSection* section = findParentComponentOfClass<SynthSection>();
245 if (section) {
248 getGlComponent()->text().setTextSize(kUiButtonSizeMult * getHeight());
249 }
250 else
252 button_component_.setColors();
253 }
254}
255
258SynthButton::SynthButton(String name) : OpenGlToggleButton(name), string_lookup_(nullptr) {
259 if (!vital::Parameters::isParameter(name.toStdString()))
260 return;
261}
262
265void SynthButton::clicked(const ModifierKeys& modifiers) {
266 OpenGlToggleButton::clicked(modifiers);
267
268 if (!modifiers.isPopupMenu()) {
269 for (SynthButton::ButtonListener* listener : button_listeners_)
270 listener->guiChanged(this);
271 }
272}
273
278 int lookup = on ? 1 : 0;
279
280 if (string_lookup_)
281 return string_lookup_[lookup];
282
283 return strings::kOffOnNames[lookup];
284}
285
289 SynthGuiInterface* parent = findParentComponentOfClass<SynthGuiInterface>();
290 if (parent == nullptr)
291 return;
292
293 SynthBase* synth = parent->getSynth();
294
295 if (result == kArmMidiLearn)
296 synth->armMidiLearn(getName().toStdString());
297 else if (result == kClearMidiLearn)
298 synth->clearMidiLearn(getName().toStdString());
299}
300
304void SynthButton::mouseDown(const MouseEvent& e) {
305 SynthGuiInterface* parent = findParentComponentOfClass<SynthGuiInterface>();
306 if (parent == nullptr)
307 return;
308
309 SynthBase* synth = parent->getSynth();
310
311 if (e.mods.isPopupMenu()) {
313
314 PopupItems options;
315 options.addItem(kArmMidiLearn, "Learn MIDI Assignment");
316 if (parent->getSynth()->isMidiMapped(getName().toStdString()))
317 options.addItem(kClearMidiLearn, "Clear MIDI Assignment");
318
319 SynthSection* parent = findParentComponentOfClass<SynthSection>();
320 parent->showPopupSelector(this, e.getPosition(), options, [=](int selection) { handlePopupResult(selection); });
321 }
322 else {
324 synth->beginChangeGesture(getName().toStdString());
325 }
326}
327
331void SynthButton::mouseUp(const MouseEvent& e) {
332 if (!e.mods.isPopupMenu()) {
334
335 SynthGuiInterface* parent = findParentComponentOfClass<SynthGuiInterface>();
336 if (parent)
337 parent->getSynth()->endChangeGesture(getName().toStdString());
338 }
339}
340
344 button_listeners_.push_back(listener);
345}
void renderUiButton(OpenGlWrapper &open_gl, bool animate)
Definition synth_button.cpp:175
void setColors()
Sets the colors based on the current style and Skin.
Definition synth_button.cpp:54
Button * button_
Associated JUCE Button.
Definition synth_button.h:316
Colour off_pressed_color_
Definition synth_button.h:330
PlainTextComponent text_
Text component for rendering button label.
Definition synth_button.h:323
bool hover_
True if the mouse is hovering over the button.
Definition synth_button.h:320
@ kJustText
Definition synth_button.h:183
@ kTextButton
Definition synth_button.h:182
@ kUiButton
Definition synth_button.h:185
@ kPowerButton
Definition synth_button.h:184
@ kLightenButton
Definition synth_button.h:186
bool show_on_colors_
True if showing on-colors when toggled on.
Definition synth_button.h:317
PlainTextComponent & text()
Definition synth_button.h:308
Colour off_hover_color_
Definition synth_button.h:331
Colour on_color_
Definition synth_button.h:326
void renderTextButton(OpenGlWrapper &open_gl, bool animate)
Definition synth_button.cpp:104
OpenGlQuad & background()
Definition synth_button.h:304
Colour off_color_
Definition synth_button.h:329
bool primary_ui_button_
True if this is a primary UI button.
Definition synth_button.h:318
Colour body_color_
Definition synth_button.h:333
Colour on_hover_color_
Definition synth_button.h:328
Colour on_pressed_color_
Definition synth_button.h:327
static constexpr float kHoverInc
The amount of change in hover transition per frame.
Definition synth_button.h:178
bool down_
True if the button is pressed.
Definition synth_button.h:319
void renderPowerButton(OpenGlWrapper &open_gl, bool animate)
Definition synth_button.cpp:147
Colour background_color_
Definition synth_button.h:332
OpenGlQuad background_
Background quad for rendering button body.
Definition synth_button.h:322
void setText()
Sets the text displayed on the button.
Definition synth_button.h:260
float hover_amount_
A smoothed value for hover transitions.
Definition synth_button.h:321
void renderLightenButton(OpenGlWrapper &open_gl, bool animate)
Definition synth_button.cpp:211
void incrementHover()
Increments or decrements the hover amount, smoothing the hover transitions.
Definition synth_button.cpp:230
ButtonStyle style_
Current button style.
Definition synth_button.h:315
float findValue(Skin::ValueId value_id)
Finds a float value from the skin associated with this component's parent.
Definition open_gl_component.cpp:173
bool isActive() const
Checks if this component is currently active.
Definition open_gl_image_component.h:131
void setColor(Colour color)
Sets a color tint for the image.
Definition open_gl_image_component.h:101
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Renders the image using OpenGL.
Definition open_gl_image_component.cpp:61
void markDirty()
Marks all vertex data as dirty, prompting a refresh on the next render.
Definition open_gl_multi_quad.h:78
void setQuad(int i, float x, float y, float w, float h)
Sets the position and size of a quad in normalized device space.
Definition open_gl_multi_quad.h:313
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Renders the quads using OpenGL.
Definition open_gl_multi_quad.cpp:92
force_inline void setColor(Colour color)
Sets the base color for the quads.
Definition open_gl_multi_quad.h:102
void setRounding(float rounding)
Sets the rounding radius of the quads.
Definition open_gl_multi_quad.h:347
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Definition synth_button.cpp:16
void incrementHover()
Increments or decrements the hover amount, smoothing the hover transitions.
Definition synth_button.cpp:46
static constexpr float kHoverInc
The amount of change in hover transition per frame.
Definition synth_button.h:20
A ToggleButton that uses an OpenGlButtonComponent for its rendering.
Definition synth_button.h:342
void mouseExit(const MouseEvent &e) override
Definition synth_button.h:419
OpenGlButtonComponent * getGlComponent()
Definition synth_button.h:350
void mouseUp(const MouseEvent &e) override
Definition synth_button.h:433
void mouseDown(const MouseEvent &e) override
Definition synth_button.h:426
void resized() override
Called when the button is resized, adjusts text size and colors accordingly.
Definition synth_button.cpp:238
@ kLight
Definition open_gl_image_component.h:309
void setTextSize(float size)
Sets the size of the text in points.
Definition open_gl_image_component.h:372
void setFontType(FontType font_type)
Sets the font type (Title, Light, Regular, Mono).
Definition open_gl_image_component.h:381
@ kButtonFontSize
Definition skin.h:88
@ kLabelBackgroundRounding
Definition skin.h:74
@ kUiButtonPressed
Definition skin.h:195
@ kUiButtonText
Definition skin.h:193
@ kIconButtonOff
Definition skin.h:185
@ kIconButtonOn
Definition skin.h:188
@ kUiActionButton
Definition skin.h:196
@ kPowerButtonOff
Definition skin.h:138
@ kUiButtonHover
Definition skin.h:194
@ kPowerButtonOn
Definition skin.h:137
@ kIconButtonOffHover
Definition skin.h:186
@ kLightenScreen
Definition skin.h:141
@ kUiActionButtonHover
Definition skin.h:197
@ kUiActionButtonPressed
Definition skin.h:198
@ kUiButton
Definition skin.h:192
@ kOverlayScreen
Definition skin.h:140
@ kIconButtonOffPressed
Definition skin.h:187
@ kIconButtonOnPressed
Definition skin.h:190
@ kTextComponentBackground
Definition skin.h:147
@ kIconButtonOnHover
Definition skin.h:189
@ kBody
Definition skin.h:129
A base class providing foundational functionality for the Vital synthesizer’s engine,...
Definition synth_base.h:42
virtual void beginChangeGesture(const std::string &name)
Called when a parameter change gesture begins. Typically not implemented in this base class.
Definition synth_base.h:356
bool isMidiMapped(const std::string &name)
Checks if a given parameter name is MIDI mapped.
Definition synth_base.cpp:677
virtual void endChangeGesture(const std::string &name)
Called when a parameter change gesture ends. Typically not implemented in this base class.
Definition synth_base.h:363
void clearMidiLearn(const std::string &name)
Clears the MIDI mapping for a given parameter name.
Definition synth_base.cpp:673
void armMidiLearn(const std::string &name)
Arms the given parameter name for MIDI learn, associating the next received MIDI control with it.
Definition synth_base.cpp:665
Interface for objects interested in changes to SynthButton state.
Definition synth_button.h:461
void addButtonListener(ButtonListener *listener)
Definition synth_button.cpp:343
@ kClearMidiLearn
Definition synth_button.h:456
@ kArmMidiLearn
Definition synth_button.h:455
void handlePopupResult(int result)
Definition synth_button.cpp:288
SynthButton(String name)
Definition synth_button.cpp:258
virtual void mouseUp(const MouseEvent &e) override
Definition synth_button.cpp:331
String getTextFromValue(bool value)
Definition synth_button.cpp:277
virtual void mouseDown(const MouseEvent &e) override
Definition synth_button.cpp:304
virtual void clicked() override
Called when the button is clicked.
Definition synth_button.h:509
An interface class linking the Vital synthesizer backend (SynthBase) with a GUI.
Definition synth_gui_interface.h:56
SynthBase * getSynth()
Returns the SynthBase instance this interface is managing.
Definition synth_gui_interface.h:85
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
float findValue(Skin::ValueId value_id) const
Finds a value in the skin overrides or from the parent if not found locally.
Definition synth_section.cpp:18
static const bool isParameter(const std::string &name)
Definition synth_parameters.h:220
const std::string kOffOnNames[]
Off/On state names.
Definition synth_strings.h:18
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174
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
Declares classes for OpenGL-based buttons used in the Vital synth UI.