Krotos Modules 3
Loading...
Searching...
No Matches
AttachedSlider.cpp
Go to the documentation of this file.
1#include "AttachedSlider.h"
2
3namespace krotos
4{
5 AttachedSlider::AttachedSlider(const String& labelText, bool setLabelVisible, const String& dragAndDropDescription)
6 : m_labelText(labelText)
7 {
8 setComponentID(labelText);
9
10 addAndMakeVisible(m_slider);
11 setLabelPosition(LabelPosition::Above); // default label position
12 m_slider.setSliderStyle(Slider::SliderStyle::RotaryVerticalDrag); // default slider style
13 m_slider.setTextBoxStyle(Slider::TextEntryBoxPosition::NoTextBox, false, 0, 0);
14 m_slider.setScrollWheelEnabled(false); // stops receiving scrollwheel messages from parent
15
16 addAndMakeVisible(m_label);
17 m_label.setText(labelText, dontSendNotification);
18 m_label.setFont(Font(15.f));
19 m_label.setJustificationType(Justification::centred);
20 m_label.setVisible(setLabelVisible);
21 // Load Images for MouseCursor
23 ImageFileFormat::loadFrom(KrotosBinaryData::HoverHand_png, KrotosBinaryData::HoverHand_pngSize));
25 ImageFileFormat::loadFrom(KrotosBinaryData::GrabbingHand_png, KrotosBinaryData::GrabbingHand_pngSize));
26
27 m_label.setDragAndDropDescription(dragAndDropDescription);
28
29 m_slider.onValueChange = [this]() {
30 if (m_mouseIsOver)
31 {
33 }
34 };
35
36 m_slider.addMouseListener(this, true);
37 }
38
40 {
41 auto value = m_slider.getValue();
42
43 if (getComponentID() == "Pan")
44 {
45 value *= 50; // Adjust the value only for "Pan" componentID (-1, 1 becomes -50,50)
46 m_valueSuffix = panSuffix(value);
47 }
48
49 if (m_valueFormat.isNotEmpty())
50 {
51 String formattedValue = String::formatted(m_valueFormat, value);
52 m_label.setText(formattedValue + " " + m_valueSuffix, dontSendNotification);
53 }
54 else
55 {
56 m_label.setText(String(value) + " " + m_valueSuffix, dontSendNotification);
57 }
58 }
59
60 String AttachedSlider::panSuffix(double value) const
61 {
62 const float tolerance = 1.5f; // Adjust this tolerance level as needed
63
64 if (value < -tolerance)
65 return "L";
66 else if (value > tolerance)
67 return "R";
68 else
69 return "C";
70 }
71
72 void AttachedSlider::mouseMove(const MouseEvent& event)
73 {
74 if (m_slider.getBounds().contains(event.getPosition()))
75 {
76 m_mouseIsOver = true;
78 }
79 }
80
81 void AttachedSlider::mouseExit(const MouseEvent& /*event*/)
82 {
83 m_mouseIsOver = false;
84 m_label.setText(m_labelText, dontSendNotification);
85 }
86
87 void AttachedSlider::paint(Graphics& /*g*/) {}
88
90 {
91 switch (m_labelPosition)
92 {
94 Rectangle<int> sliderBounds = getLocalBounds();
95 Rectangle<int> labelBounds = sliderBounds.removeFromLeft(m_layout.lhLabelWidth);
96 sliderBounds.expand(sliderBounds.getHeight() / 2, 0);
97 sliderBounds.removeFromRight(sliderBounds.getHeight() / 2);
98 m_slider.setTextBoxStyle(Slider::TextEntryBoxPosition::NoTextBox, false, 0, 0);
99 m_label.setJustificationType(Justification::centredLeft);
100 m_label.setBorderSize(juce::BorderSize<int>(0));
101 m_label.setFont(Font((float)m_layout.lhFontHeight));
102 m_label.setBounds(labelBounds);
103 m_slider.setBounds(sliderBounds);
104 break;
105 }
107 auto bounds = getLocalBounds();
108 m_slider.setBounds(bounds);
109 const int padding = 0;
110 Rectangle<int> labelBounds;
111 Rectangle<int> sliderBounds;
112 int labelHeight;
113 float fontSize;
114 labelHeight = static_cast<int>(getHeight() * 0.2f);
115 labelHeight = jmax<int>(labelHeight, 10);
116
117 labelBounds = bounds.removeFromBottom(labelHeight).reduced(padding);
118 sliderBounds = bounds;
119 fontSize = 0.7f * (float)labelHeight;
120 m_slider.setTextBoxStyle(Slider::TextEntryBoxPosition::NoTextBox, false, labelBounds.getWidth(),
121 labelBounds.getHeight());
122
123 if (sliderBounds.getWidth() < 25 || sliderBounds.getHeight() < 50)
124 {
125 sliderBounds = sliderBounds.expanded(5);
126 labelBounds = labelBounds.expanded(3);
127 fontSize *= 1.4f;
128 }
129
130 m_label.setJustificationType(Justification::centred);
131 m_label.setFont(Font(fontSize));
132 m_label.setBounds(labelBounds);
133 break;
134 }
136 auto bounds = getLocalBounds();
137 m_slider.setBounds(bounds);
138
139 Rectangle<int> labelBounds;
140
141 if (m_labelYPositionOverride >= 0) // for custom positioning outside of standard layouts
142 {
143 labelBounds.setY(m_labelYPositionOverride);
144 }
145 else if (m_slider.getSliderStyle() == Slider::SliderStyle::LinearHorizontal)
146 {
148 }
149 else if (m_slider.getSliderStyle() == Slider::SliderStyle::RotaryVerticalDrag)
150 {
152 }
153
154 m_slider.setTextBoxStyle(Slider::TextEntryBoxPosition::NoTextBox, false, labelBounds.getWidth(),
155 labelBounds.getHeight());
156 m_label.setJustificationType(Justification::centred);
157 m_label.setBounds(labelBounds);
158 break;
159 }
160 }
161 }
162
164
166
168
169 std::unique_ptr<SliderParameterAttachment>& AttachedSlider::getAttachment() { return m_attachment; }
170
171 void AttachedSlider::setDebugColour(Colour newColour)
172 {
173 m_slider.setColour(Slider::ColourIds::thumbColourId, newColour);
174 }
175
176 void AttachedSlider::setDragAndDropDescription(const var& description)
177 {
178 m_label.setEnableDrag(description.size() != 0 || description.toString().length() > 0);
180 }
181
183
184} // namespace krotos
Slider m_slider
Definition AttachedSlider.h:91
String m_labelText
Definition AttachedSlider.h:88
void setLabelYPosition(int newPos)
Definition AttachedSlider.cpp:182
Label & getLabel()
Definition AttachedSlider.cpp:163
std::unique_ptr< SliderParameterAttachment > & getAttachment()
Definition AttachedSlider.cpp:169
void setDebugColour(Colour newColour)
Definition AttachedSlider.cpp:171
void resized() override
Definition AttachedSlider.cpp:89
String panSuffix(double value) const
Definition AttachedSlider.cpp:60
void updateLabel()
Definition AttachedSlider.cpp:39
void paint(Graphics &g) override
Definition AttachedSlider.cpp:87
DraggableLabel m_label
Definition AttachedSlider.h:90
int m_labelYPositionOverride
Definition AttachedSlider.h:94
Slider & getControlComponent()
Definition AttachedSlider.cpp:167
void setDragAndDropDescription(const var &description)
Definition AttachedSlider.cpp:176
bool m_mouseIsOver
Definition AttachedSlider.h:87
String m_valueFormat
Definition AttachedSlider.h:85
struct krotos::AttachedSlider::Layout m_layout
LabelPosition m_labelPosition
Definition AttachedSlider.h:89
Slider & getSlider()
Definition AttachedSlider.cpp:165
void mouseMove(const MouseEvent &event) override
Definition AttachedSlider.cpp:72
void mouseExit(const MouseEvent &) override
Definition AttachedSlider.cpp:81
AttachedSlider(const String &labelText, bool setLabelVisible=true, const String &dragAndDropDescription=String())
Definition AttachedSlider.cpp:5
std::unique_ptr< SliderParameterAttachment > m_attachment
Definition AttachedSlider.h:92
void setLabelPosition(LabelPosition position)
Definition AttachedSlider.h:49
String m_valueSuffix
Definition AttachedSlider.h:86
void setEnableDrag(bool dragEnabled)
Definition DraggableLabel.cpp:46
void setHoverHandImage(const Image &image)
Definition DraggableLabel.cpp:96
void setGrabHandImage(const Image &image)
Definition DraggableLabel.cpp:98
void setDragAndDropDescription(const var &newDescription)
Definition DraggableLabel.cpp:41
Definition AirAbsorptionFilter.cpp:2
const Rectangle< int > linearSliderLabelBounds
Definition AttachedSlider.h:77
const Rectangle< int > rotarySliderLabelBounds
Definition AttachedSlider.h:78
const int lhFontHeight
Definition AttachedSlider.h:73
const int lhLabelWidth
Definition AttachedSlider.h:72
struct krotos::AttachedSlider::Layout::Above above