WE Core
Loading...
Searching...
No Matches
MidAnchoredRotarySlider.h
Go to the documentation of this file.
1/*
2 * File: MidAnchoredRotarySlider.h
3 *
4 * Version: 1.0.0
5 *
6 * Created: 21/11/2020
7 *
8 * This file is part of WECore.
9 *
10 * WECore is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * WECore is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with WECore. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#pragma once
26
27#include "../JuceLibraryCode/JuceHeader.h"
28#include "General/CoreMath.h"
29
31
40 template <typename BASE>
41 class MidAnchoredRotarySlider : public BASE {
42
43 public:
45 virtual ~MidAnchoredRotarySlider() = default;
46
48 inline virtual void drawRotarySlider(juce::Graphics& g,
49 int x,
50 int y,
51 int width,
52 int height,
53 float sliderPosProportional,
54 float rotaryStartAngle,
55 float rotaryEndAngle,
56 juce::Slider &slider) override;
58 };
59
60 template <typename BASE>
62 int /*x*/,
63 int /*y*/,
64 int width,
65 int height,
66 float /*sliderPosProportional*/,
67 float /*rotaryStartAngle*/,
68 float /*rotaryEndAngle*/,
69 juce::Slider &slider) {
70
71 // Calculate useful constants
72 constexpr double arcGap {CoreMath::DOUBLE_TAU / 4};
73 constexpr double rangeOfMotion {CoreMath::DOUBLE_TAU - arcGap};
74
75 const double sliderNormalisedValue {(slider.getValue() - slider.getMinimum()) /
76 (slider.getMaximum() - slider.getMinimum())};
77
78 double arcStartPoint {0};
79 double arcEndPoint {0};
80 if (sliderNormalisedValue > 0.5) {
81 arcStartPoint = CoreMath::DOUBLE_PI;
82 arcEndPoint = CoreMath::DOUBLE_PI + (sliderNormalisedValue - 0.5) * rangeOfMotion;
83 } else {
84 arcStartPoint = CoreMath::DOUBLE_PI + (sliderNormalisedValue - 0.5) * rangeOfMotion;
85 arcEndPoint = CoreMath::DOUBLE_PI;
86 }
87
88 constexpr int margin {2};
89 juce::Rectangle<int> area = slider.getBounds();
90 area.reduce(margin, margin);
91 const int diameter {std::min(area.getWidth(), area.getHeight())};
92
93 if (slider.isEnabled()) {
94 g.setColour(slider.findColour(juce::Slider::rotarySliderFillColourId));
95 } else {
96 g.setColour(slider.findColour(juce::Slider::rotarySliderOutlineColourId));
97 }
98
99 juce::Path p;
100
101 // Draw inner ring
102 constexpr int arcSpacing {3};
103 p.addCentredArc(width / 2,
104 height / 2,
105 diameter / 2 - arcSpacing,
106 diameter / 2 - arcSpacing,
108 arcGap / 2,
109 CoreMath::DOUBLE_TAU - (arcGap / 2),
110 true);
111
112 g.strokePath(p, juce::PathStrokeType(0.7f));
113
114 // Draw outer ring
115 p.clear();
116 p.addCentredArc(width / 2,
117 height / 2,
118 diameter / 2,
119 diameter / 2,
121 arcStartPoint,
122 arcEndPoint,
123 true);
124 g.strokePath(p, juce::PathStrokeType(3.0f));
125 }
126}
virtual void drawRotarySlider(juce::Graphics &g, int x, int y, int width, int height, float sliderPosProportional, float rotaryStartAngle, float rotaryEndAngle, juce::Slider &slider) override
constexpr double DOUBLE_PI
Definition CoreMath.h:36
constexpr double DOUBLE_TAU
Definition CoreMath.h:39