Vital
Loading...
Searching...
No Matches
synth_button.h
Go to the documentation of this file.
1
3
4#pragma once
5
6#include "JuceHeader.h"
7
10#include "synth_gui_interface.h"
11
18 public:
20 static constexpr float kHoverInc = 0.2f;
21
24 OpenGlShapeButtonComponent(Button* button) : button_(button), down_(false), hover_(false), hover_amount_(0.0f),
25 use_on_colors_(false), shape_("shape") {
26 shape_.setComponent(button);
27 shape_.setScissor(true);
28 }
29
31 void parentHierarchyChanged() override {
32 if (findParentComponentOfClass<SynthGuiInterface>())
33 setColors();
34 }
35
37 void setColors() {
38 off_normal_color_ = button_->findColour(Skin::kIconButtonOff, true);
39 off_hover_color_ = button_->findColour(Skin::kIconButtonOffHover, true);
40 off_down_color_ = button_->findColour(Skin::kIconButtonOffPressed, true);
41 on_normal_color_ = button_->findColour(Skin::kIconButtonOn, true);
42 on_hover_color_ = button_->findColour(Skin::kIconButtonOnHover, true);
43 on_down_color_ = button_->findColour(Skin::kIconButtonOnPressed, true);
44 }
45
47 void incrementHover();
48
51 virtual void init(OpenGlWrapper& open_gl) override {
52 OpenGlComponent::init(open_gl);
53 shape_.init(open_gl);
54 };
55
59 virtual void render(OpenGlWrapper& open_gl, bool animate) override;
60
63 virtual void destroy(OpenGlWrapper& open_gl) override {
65 shape_.destroy(open_gl);
66 };
67
69 void redoImage() { shape_.redrawImage(true); setColors(); }
70
73 void setShape(const Path& shape) { shape_.setShape(shape); }
74
77 void useOnColors(bool use) { use_on_colors_ = use; }
78
81 void setDown(bool down) { down_ = down; }
82
85 void setHover(bool hover) { hover_ = hover; }
86
87 private:
88 Button* button_;
89 bool down_;
90 bool hover_;
91 float hover_amount_;
92 bool use_on_colors_;
93 PlainShapeComponent shape_;
94
95 // Colors
96 Colour off_normal_color_;
97 Colour off_hover_color_;
98 Colour off_down_color_;
99 Colour on_normal_color_;
100 Colour on_hover_color_;
101 Colour on_down_color_;
102
103 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OpenGlShapeButtonComponent)
104};
105
110class OpenGlShapeButton : public ToggleButton {
111 public:
114 OpenGlShapeButton(String name) : gl_component_(this) {
115 setName(name);
116 }
117
120 OpenGlComponent* getGlComponent() { return &gl_component_; }
121
124 void setShape(const Path& shape) { gl_component_.setShape(shape); }
125
128 void useOnColors(bool use) { gl_component_.useOnColors(use); }
129
131 void resized() override {
132 ToggleButton::resized();
133 gl_component_.redoImage();
134 }
135
138 void mouseEnter(const MouseEvent& e) override {
139 ToggleButton::mouseEnter(e);
140 gl_component_.setHover(true);
141 }
142
145 void mouseExit(const MouseEvent& e) override {
146 ToggleButton::mouseExit(e);
147 gl_component_.setHover(false);
148 }
149
152 void mouseDown(const MouseEvent& e) override {
153 ToggleButton::mouseDown(e);
154 gl_component_.setDown(true);
155 }
156
159 void mouseUp(const MouseEvent& e) override {
160 ToggleButton::mouseUp(e);
161 gl_component_.setDown(false);
162 }
163
164 private:
165 OpenGlShapeButtonComponent gl_component_;
166
167 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OpenGlShapeButton)
168};
169
176 public:
178 static constexpr float kHoverInc = 0.2f;
179
189
192 OpenGlButtonComponent(Button* button) : style_(kTextButton), button_(button),
194 down_(false), hover_(false), hover_amount_(0.0f),
195 background_(Shaders::kRoundedRectangleFragment), text_("text", "") {
197 background_.setColor(Colours::orange);
198 background_.setQuad(0, -1.0f, -1.0f, 2.0f, 2.0f);
199
200 addChildComponent(text_);
201 text_.setActive(false);
202 text_.setScissor(true);
203 text_.setComponent(button);
205 }
206
209 virtual void init(OpenGlWrapper& open_gl) override {
210 if (style_ == kPowerButton)
212
213 background_.init(open_gl);
214 text_.init(open_gl);
215
216 setColors();
217 }
218
220 void setColors();
221
225 void renderTextButton(OpenGlWrapper& open_gl, bool animate);
226
230 void renderPowerButton(OpenGlWrapper& open_gl, bool animate);
231
235 void renderUiButton(OpenGlWrapper& open_gl, bool animate);
236
240 void renderLightenButton(OpenGlWrapper& open_gl, bool animate);
241
243 void incrementHover();
244
248 virtual void render(OpenGlWrapper& open_gl, bool animate) override {
249 if (style_ == kTextButton || style_ == kJustText)
250 renderTextButton(open_gl, animate);
251 else if (style_ == kPowerButton)
252 renderPowerButton(open_gl, animate);
253 else if (style_ == kUiButton)
254 renderUiButton(open_gl, animate);
255 else if (style_ == kLightenButton)
256 renderLightenButton(open_gl, animate);
257 }
258
260 void setText() {
261 String text = button_->getButtonText();
262 if (!text.isEmpty()) {
263 text_.setActive(true);
265 }
266 }
267
270 void setDown(bool down) { down_ = down; }
271
274 void setHover(bool hover) { hover_ = hover; }
275
278 virtual void destroy(OpenGlWrapper& open_gl) override {
279 background_.destroy(open_gl);
280 text_.destroy(open_gl);
281 }
282
285 void setJustification(Justification justification) { text_.setJustification(justification); }
286
290
293 void setShowOnColors(bool show) { show_on_colors_ = show; }
294
297 void setPrimaryUiButton(bool primary) { primary_ui_button_ = primary; }
298
300 void paintBackground(Graphics& g) override { }
301
305
309
313
314 protected:
316 Button* button_;
319 bool down_;
320 bool hover_;
324
325 // Colors
326 Colour on_color_;
334
335 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OpenGlButtonComponent)
336};
337
342class OpenGlToggleButton : public ToggleButton {
343 public:
346 OpenGlToggleButton(String name) : ToggleButton(name), active_(true), button_component_(this) { }
347
350 OpenGlButtonComponent* getGlComponent() { return &button_component_; }
351
354 void setActive(bool active = true) { active_ = active; }
355
358 bool isActive() const { return active_; }
359
361 void resized() override;
362
365 void setText(String text) {
366 setButtonText(text);
367 button_component_.setText();
368 }
369
373 }
374
378 }
379
382 void setJustification(Justification justification) {
383 button_component_.setJustification(justification);
384 }
385
390
393 void setShowOnColors(bool show) {
394 button_component_.setShowOnColors(show);
395 }
396
399 void setUiButton(bool primary) {
401 button_component_.setPrimaryUiButton(primary);
402 }
403
405 virtual void enablementChanged() override {
406 ToggleButton::enablementChanged();
407 button_component_.setColors();
408 }
409
412 void mouseEnter(const MouseEvent& e) override {
413 ToggleButton::mouseEnter(e);
414 button_component_.setHover(true);
415 }
416
419 void mouseExit(const MouseEvent& e) override {
420 ToggleButton::mouseExit(e);
421 button_component_.setHover(false);
422 }
423
426 void mouseDown(const MouseEvent& e) override {
427 ToggleButton::mouseDown(e);
428 button_component_.setDown(true);
429 }
430
433 void mouseUp(const MouseEvent& e) override {
434 ToggleButton::mouseUp(e);
435 button_component_.setDown(false);
436 }
437
438 private:
439 bool active_;
440 OpenGlButtonComponent button_component_;
441
442 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(OpenGlToggleButton)
443};
444
451 public:
458
462 public:
464 virtual ~ButtonListener() { }
465
468 virtual void guiChanged(SynthButton* button) { }
469 };
470
473 SynthButton(String name);
474
477 void setStringLookup(const std::string* lookup) {
478 string_lookup_ = lookup;
479 }
480
483 const std::string* getStringLookup() const { return string_lookup_; }
484
488 String getTextFromValue(bool value);
489
492 void handlePopupResult(int result);
493
496 virtual void mouseDown(const MouseEvent& e) override;
497
498
501 virtual void mouseUp(const MouseEvent& e) override;
502
503
506 void addButtonListener(ButtonListener* listener);
507
509 virtual void clicked() override {
510 OpenGlToggleButton::clicked();
511 if (string_lookup_)
512 setText(string_lookup_[getToggleState() ? 1 : 0]);
513 }
514
515 private:
518 void clicked(const ModifierKeys& modifiers) override;
519
520 const std::string* string_lookup_;
521 std::vector<ButtonListener*> button_listeners_;
522
523 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(SynthButton)
524};
A specialized OpenGL component for rendering various styles of buttons.
Definition synth_button.h:175
void renderUiButton(OpenGlWrapper &open_gl, bool animate)
Definition synth_button.cpp:175
void setDown(bool down)
Definition synth_button.h:270
void setColors()
Sets the colors based on the current style and Skin.
Definition synth_button.cpp:54
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Definition synth_button.h:248
virtual void init(OpenGlWrapper &open_gl) override
Definition synth_button.h:209
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
ButtonStyle
Enumeration of different button styles.
Definition synth_button.h:181
@ kJustText
Definition synth_button.h:183
@ kNumButtonStyles
Definition synth_button.h:187
@ kTextButton
Definition synth_button.h:182
@ kUiButton
Definition synth_button.h:185
@ kPowerButton
Definition synth_button.h:184
@ kLightenButton
Definition synth_button.h:186
void setJustification(Justification justification)
Definition synth_button.h:285
bool show_on_colors_
True if showing on-colors when toggled on.
Definition synth_button.h:317
void setHover(bool hover)
Definition synth_button.h:274
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
void setPrimaryUiButton(bool primary)
Definition synth_button.h:297
ButtonStyle style()
Definition synth_button.h:312
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
virtual void destroy(OpenGlWrapper &open_gl) override
Definition synth_button.h:278
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 setShowOnColors(bool show)
Definition synth_button.h:293
void renderLightenButton(OpenGlWrapper &open_gl, bool animate)
Definition synth_button.cpp:211
void setStyle(ButtonStyle style)
Definition synth_button.h:289
void incrementHover()
Increments or decrements the hover amount, smoothing the hover transitions.
Definition synth_button.cpp:230
OpenGlButtonComponent(Button *button)
Definition synth_button.h:192
void paintBackground(Graphics &g) override
Overrides the default background painting, does nothing as we paint with OpenGL.
Definition synth_button.h:300
ButtonStyle style_
Current button style.
Definition synth_button.h:315
A base component class that integrates JUCE's Component with OpenGL rendering.
Definition open_gl_component.h:20
virtual void destroy(OpenGlWrapper &open_gl)
Destroys any OpenGL-specific resources allocated by this component.
Definition open_gl_component.cpp:168
virtual void init(OpenGlWrapper &open_gl)
Initializes any OpenGL-specific resources needed by the component.
Definition open_gl_component.cpp:148
void setComponent(Component *component)
Sets the component to be drawn into the OpenGL image. If not set, uses this component.
Definition open_gl_image_component.h:83
void setActive(bool active)
Sets whether this component is active (rendered) or not.
Definition open_gl_image_component.h:113
virtual void init(OpenGlWrapper &open_gl) override
Initializes any OpenGL resources for rendering this component.
Definition open_gl_image_component.cpp:57
virtual void destroy(OpenGlWrapper &open_gl) override
Destroys OpenGL-related resources used by this component.
Definition open_gl_image_component.cpp:69
void setScissor(bool scissor)
Enables or disables scissor testing when drawing the image.
Definition open_gl_image_component.h:89
virtual void redrawImage(bool force)
Redraws the image if necessary, creating or updating the internal Image.
Definition open_gl_image_component.cpp:16
virtual void init(OpenGlWrapper &open_gl) override
Initializes OpenGL buffers and shader attributes.
Definition open_gl_multi_quad.cpp:37
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
void setTargetComponent(Component *target_component)
Sets a target component to help position the quads.
Definition open_gl_multi_quad.h:358
void setFragmentShader(Shaders::FragmentShader shader)
Sets the fragment shader used to render the quads.
Definition open_gl_multi_quad.h:86
virtual void destroy(OpenGlWrapper &open_gl) override
Releases OpenGL resources when the component is destroyed.
Definition open_gl_multi_quad.cpp:69
force_inline void setColor(Colour color)
Sets the base color for the quads.
Definition open_gl_multi_quad.h:102
A convenience class for a single quad rendered via OpenGL.
Definition open_gl_multi_quad.h:447
A specialized OpenGL component for rendering a shape-based button.
Definition synth_button.h:17
void setColors()
Sets the colors used for this shape button based on the current Skin.
Definition synth_button.h:37
virtual void render(OpenGlWrapper &open_gl, bool animate) override
Definition synth_button.cpp:16
void setDown(bool down)
Definition synth_button.h:81
virtual void init(OpenGlWrapper &open_gl) override
Definition synth_button.h:51
void setShape(const Path &shape)
Definition synth_button.h:73
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
void setHover(bool hover)
Definition synth_button.h:85
OpenGlShapeButtonComponent(Button *button)
Definition synth_button.h:24
virtual void destroy(OpenGlWrapper &open_gl) override
Definition synth_button.h:63
void redoImage()
Redraws the image of the shape, useful after size or color changes.
Definition synth_button.h:69
void useOnColors(bool use)
Definition synth_button.h:77
void parentHierarchyChanged() override
Called when the parent hierarchy changes, for example when the component is moved in the UI.
Definition synth_button.h:31
A ToggleButton that uses an OpenGlShapeButtonComponent for its rendering.
Definition synth_button.h:110
void mouseExit(const MouseEvent &e) override
Definition synth_button.h:145
OpenGlComponent * getGlComponent()
Definition synth_button.h:120
void mouseEnter(const MouseEvent &e) override
Definition synth_button.h:138
void mouseUp(const MouseEvent &e) override
Definition synth_button.h:159
void resized() override
Called when the button is resized. Updates the internal image.
Definition synth_button.h:131
void setShape(const Path &shape)
Definition synth_button.h:124
void useOnColors(bool use)
Definition synth_button.h:128
void mouseDown(const MouseEvent &e) override
Definition synth_button.h:152
OpenGlShapeButton(String name)
Definition synth_button.h:114
A ToggleButton that uses an OpenGlButtonComponent for its rendering.
Definition synth_button.h:342
void setActive(bool active=true)
Definition synth_button.h:354
bool isActive() const
Definition synth_button.h:358
void mouseEnter(const MouseEvent &e) override
Definition synth_button.h:412
void setNoBackground()
Removes the background, showing just text.
Definition synth_button.h:376
void mouseExit(const MouseEvent &e) override
Definition synth_button.h:419
void setUiButton(bool primary)
Definition synth_button.h:399
virtual void enablementChanged() override
Called when the button enablement changes.
Definition synth_button.h:405
void setShowOnColors(bool show)
Definition synth_button.h:393
OpenGlButtonComponent * getGlComponent()
Definition synth_button.h:350
void setLightenButton()
Configures the button as a lighten button.
Definition synth_button.h:387
void setJustification(Justification justification)
Definition synth_button.h:382
void setPowerButton()
Configures the button as a power button.
Definition synth_button.h:371
void mouseUp(const MouseEvent &e) override
Definition synth_button.h:433
OpenGlToggleButton(String name)
Definition synth_button.h:346
void setText(String text)
Definition synth_button.h:365
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
A component that draws a shape into an OpenGlImageComponent.
Definition open_gl_image_component.h:417
void setShape(Path shape)
Sets the shape to be drawn.
Definition open_gl_image_component.h:441
A text component rendered into an OpenGlImageComponent with configurable font and justification.
Definition open_gl_image_component.h:301
@ kMono
Definition open_gl_image_component.h:311
void setText(String text)
Sets the displayed text and redraws the image.
Definition open_gl_image_component.h:336
void setJustification(Justification justification)
Sets the text justification (e.g., centered, left, right).
Definition open_gl_image_component.h:389
void setFontType(FontType font_type)
Sets the font type (Title, Light, Regular, Mono).
Definition open_gl_image_component.h:381
Manages and provides access to vertex and fragment shaders used by the OpenGL rendering pipeline.
Definition shaders.h:19
@ kCircleFragment
Definition shaders.h:65
@ kIconButtonOff
Definition skin.h:185
@ kIconButtonOn
Definition skin.h:188
@ kIconButtonOffHover
Definition skin.h:186
@ kIconButtonOffPressed
Definition skin.h:187
@ kIconButtonOnPressed
Definition skin.h:190
@ kIconButtonOnHover
Definition skin.h:189
Interface for objects interested in changes to SynthButton state.
Definition synth_button.h:461
virtual void guiChanged(SynthButton *button)
Definition synth_button.h:468
virtual ~ButtonListener()
Virtual destructor.
Definition synth_button.h:464
A specialized OpenGlToggleButton with additional functionality for the Vital synth.
Definition synth_button.h:450
void addButtonListener(ButtonListener *listener)
Definition synth_button.cpp:343
MenuId
Possible menu IDs for popup operations.
Definition synth_button.h:453
@ kCancel
Definition synth_button.h:454
@ 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
void setStringLookup(const std::string *lookup)
Definition synth_button.h:477
const std::string * getStringLookup() const
Definition synth_button.h:483
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
A helper struct containing references to OpenGL context, shaders, and display scale.
Definition shaders.h:174