Vital
Loading...
Searching...
No Matches
incrementer_buttons.h
Go to the documentation of this file.
1#pragma once
2
3#include "JuceHeader.h"
4#include "skin.h"
5
16class IncrementerButtons : public Component, public Button::Listener {
17public:
23 IncrementerButtons(Slider* slider) : slider_(slider), active_(true) {
24 increment_ = std::make_unique<ShapeButton>("Increment", Colours::black, Colours::black, Colours::black);
25 addAndMakeVisible(increment_.get());
26 increment_->addListener(this);
27
28 // Define the shape for the "increment" arrow (pointing up).
29 Path increment_shape;
30 increment_shape.startNewSubPath(Point<float>(0.5f, 0.1f));
31 increment_shape.lineTo(Point<float>(0.2f, 0.45f));
32 increment_shape.lineTo(Point<float>(0.8f, 0.45f));
33 increment_shape.closeSubPath();
34
35 // Add some dummy segments to ensure proper rendering.
36 increment_shape.startNewSubPath(Point<float>(0.0f, 0.0f));
37 increment_shape.closeSubPath();
38 increment_shape.startNewSubPath(Point<float>(1.0f, 0.5f));
39 increment_shape.closeSubPath();
40
41 increment_shape.addLineSegment(Line<float>(0.0f, 0.0f, 0.0f, 0.0f), 0.2f);
42 increment_shape.addLineSegment(Line<float>(0.5f, 0.5f, 0.5f, 0.5f), 0.2f);
43 increment_->setShape(increment_shape, true, true, false);
44
45 decrement_ = std::make_unique<ShapeButton>("Decrement", Colours::black, Colours::black, Colours::black);
46 addAndMakeVisible(decrement_.get());
47 decrement_->addListener(this);
48
49 // Define the shape for the "decrement" arrow (pointing down).
50 Path decrement_shape;
51 decrement_shape.startNewSubPath(Point<float>(0.5f, 0.4f));
52 decrement_shape.lineTo(Point<float>(0.2f, 0.05f));
53 decrement_shape.lineTo(Point<float>(0.8f, 0.05f));
54 decrement_shape.closeSubPath();
55
56 // Add some dummy segments to ensure proper rendering.
57 decrement_shape.startNewSubPath(Point<float>(0.0f, 0.0f));
58 decrement_shape.closeSubPath();
59 decrement_shape.startNewSubPath(Point<float>(1.0f, 0.5f));
60 decrement_shape.closeSubPath();
61
62 decrement_shape.addLineSegment(Line<float>(0.0f, 0.0f, 0.0f, 0.0f), 0.2f);
63 decrement_shape.addLineSegment(Line<float>(0.5f, 0.5f, 0.5f, 0.5f), 0.2f);
64 decrement_->setShape(decrement_shape, true, true, false);
65 }
66
73 void setActive(bool active) {
74 active_ = active;
75 repaint();
76 }
77
81 void resized() override {
82 Rectangle<int> increment_bounds = getLocalBounds();
83 Rectangle<int> decrement_bounds = increment_bounds.removeFromBottom(getHeight() / 2);
84 increment_->setBounds(increment_bounds);
85 decrement_->setBounds(decrement_bounds);
86 }
87
92 void paint(Graphics& g) override {
93 setColors();
94 }
95
103 void buttonClicked(Button* clicked_button) override {
104 double value = slider_->getValue();
105 if (clicked_button == increment_.get())
106 slider_->setValue(value + 1.0);
107 else if (clicked_button == decrement_.get())
108 slider_->setValue(value - 1.0);
109 }
110
111private:
115 void setColors() {
116 Colour normal = findColour(Skin::kIconButtonOff, true);
117 Colour hover = findColour(Skin::kIconButtonOffHover, true);
118 Colour down = findColour(Skin::kIconButtonOffPressed, true);
119 increment_->setColours(normal, hover, down);
120 decrement_->setColours(normal, hover, down);
121 }
122
123 Slider* slider_;
124 bool active_;
125
126 std::unique_ptr<ShapeButton> increment_;
127 std::unique_ptr<ShapeButton> decrement_;
128
129 JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(IncrementerButtons)
130};
A pair of buttons for incrementing and decrementing a Slider's value.
Definition incrementer_buttons.h:16
IncrementerButtons(Slider *slider)
Constructs the IncrementerButtons object.
Definition incrementer_buttons.h:23
void paint(Graphics &g) override
Paints the component background and updates button colors.
Definition incrementer_buttons.h:92
void setActive(bool active)
Sets whether the incrementer buttons are active.
Definition incrementer_buttons.h:73
void buttonClicked(Button *clicked_button) override
Called when one of the incrementer buttons is clicked.
Definition incrementer_buttons.h:103
void resized() override
Resizes and positions the increment and decrement buttons.
Definition incrementer_buttons.h:81
@ kIconButtonOff
Definition skin.h:185
@ kIconButtonOffHover
Definition skin.h:186
@ kIconButtonOffPressed
Definition skin.h:187