~tytel/helm/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* Copyright 2013-2017 Matt Tytel
 *
 * helm is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * helm is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with helm.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "synth_slider.h"

#include "default_look_and_feel.h"
#include "full_interface.h"
#include "helm_common.h"
#include "synth_gui_interface.h"
#include "text_look_and_feel.h"

#define DEFAULT_POPUP_BUFFER 10

namespace {
  enum MenuIds {
    kCancel = 0,
    kArmMidiLearn,
    kClearMidiLearn,
    kDefaultValue,
    kClearModulations,
    kModulationList
  };

  static void sliderPopupCallback(int result, SynthSlider* slider) {
    if (slider != nullptr && result != kCancel)
      slider->handlePopupResult(result);
  }
} // namespace

const float SynthSlider::rotary_angle = 0.8f * static_cast<float>(mopo::PI);
const float SynthSlider::linear_rail_width = 2.0f;

SynthSlider::SynthSlider(String name) : Slider(name), bipolar_(false), flip_coloring_(false),
                                        active_(true), snap_to_value_(false), snap_value_(0.0),
                                        string_lookup_(nullptr), parent_(nullptr) {
  popup_placement_ = BubbleComponent::below;
  popup_buffer_ = DEFAULT_POPUP_BUFFER;

  if (!mopo::Parameters::isParameter(name.toStdString()))
    return;

  setRotaryParameters(2.0f * mopo::PI - rotary_angle, 2.0f * mopo::PI + rotary_angle, true);
  details_ = mopo::Parameters::getDetails(name.toStdString());
  if (details_.steps)
    setRange(details_.min, details_.max, (details_.max - details_.min) / (details_.steps - 1));
  else
    setRange(details_.min, details_.max);

  setDoubleClickReturnValue(true, details_.default_value);
  setTextBoxStyle(Slider::NoTextBox, true, 0, 0);

  setBufferedToImage(true);
  setColour(Slider::backgroundColourId, Colour(0xff303030));
  setColour(Slider::textBoxOutlineColourId, Colour(0x00000000));
}

void SynthSlider::resized() {
  if (parent_ == nullptr)
    parent_ = findParentComponentOfClass<FullInterface>();

  setPopupDisplayEnabled(true, parent_);
  Slider::resized();
}

void SynthSlider::mouseDown(const MouseEvent& e) {
  SynthGuiInterface* parent = findParentComponentOfClass<SynthGuiInterface>();
  if (parent == nullptr)
    return;
  SynthBase* synth = parent->getSynth();

  if (e.mods.isPopupMenu()) {
    PopupMenu m;
    m.setLookAndFeel(DefaultLookAndFeel::instance());

    if (isDoubleClickReturnEnabled())
      m.addItem(kDefaultValue, "Set to Default Value");

    std::vector<mopo::ModulationConnection*> connections;
    m.addItem(kArmMidiLearn, "Learn MIDI Assignment");
    if (parent->getSynth()->isMidiMapped(getName().toStdString()))
      m.addItem(kClearMidiLearn, "Clear MIDI Assignment");

    connections = parent->getSynth()->getDestinationConnections(getName().toStdString());

    String disconnect("Disconnect from ");
    for (int i = 0; i < connections.size(); ++i)
      m.addItem(kModulationList + i, disconnect + connections[i]->source);

    if (connections.size() > 1)
      m.addItem(kClearModulations, "Disconnect all modulations");

    m.showMenuAsync(PopupMenu::Options(),
                    ModalCallbackFunction::forComponent(sliderPopupCallback, this));
  }
  else {
    Slider::mouseDown(e);

    if (parent)
      synth->beginChangeGesture(getName().toStdString());
    if (isRotary()) {
      click_position_ = e.getScreenPosition().toFloat();
      setMouseCursor(MouseCursor::NoCursor);
    }
  }
}

void SynthSlider::mouseUp(const MouseEvent& e) {
  if (!e.mods.isPopupMenu()) {
    Slider::mouseUp(e);

    SynthGuiInterface* parent = findParentComponentOfClass<SynthGuiInterface>();
    if (parent)
      parent->getSynth()->endChangeGesture(getName().toStdString());

    if (isRotary()) {
      setMouseCursor(MouseCursor::ParentCursor);
      Desktop::getInstance().getMainMouseSource().setScreenPosition(click_position_);
    }
  }
}

void SynthSlider::mouseEnter(const MouseEvent &e) {
  Slider::mouseEnter(e);
  notifyTooltip();
  for (SynthSlider::SliderListener* listener : slider_listeners_)
    listener->hoverStarted(getName().toStdString());
}

void SynthSlider::mouseExit(const MouseEvent &e) {
  Slider::mouseExit(e);
  for (SynthSlider::SliderListener* listener : slider_listeners_)
    listener->hoverEnded(getName().toStdString());
}

void SynthSlider::valueChanged() {
  Slider::valueChanged();
  notifyTooltip();
  notifyGuis();

  if (popup_placement_ == BubbleComponent::below && popup_buffer_) {
    Component* popup = getCurrentPopupDisplay();
    if (popup) {
      Rectangle<int> bounds = popup->getBounds();
      Rectangle<int> local_bounds = getLocalArea(popup, popup->getLocalBounds());

      int y_diff = getHeight() + popup_buffer_ - local_bounds.getY();
      bounds.setY(bounds.getY() + y_diff);
      popup->setBounds(bounds);
    }
  }
}

String SynthSlider::getTextFromValue(double value) {
  if (string_lookup_) {
    int lookup = mopo::utils::iclamp(value, 0, getMaximum());
    return string_lookup_[lookup];
  }

  float display_value = value;
  switch (details_.display_skew) {
    case mopo::ValueDetails::kQuadratic:
      display_value = powf(display_value, 2.0f);
      break;
    case mopo::ValueDetails::kExponential:
      display_value = powf(2.0f, display_value);
      break;
    case mopo::ValueDetails::kSquareRoot:
      display_value = sqrt(display_value);
      break;
    default:
      break;
  }
  display_value += details_.post_offset;
  if (details_.display_invert)
    display_value = 1.0 / display_value;
  display_value *= details_.display_multiply;

  return formatValue(display_value);
}

double SynthSlider::snapValue(double attempted_value, DragMode drag_mode) {
  const double percent = 0.05;
  if (!snap_to_value_ || drag_mode != DragMode::absoluteDrag)
    return attempted_value;

  double range = getMaximum() - getMinimum();
  double radius = percent * range;
  if (attempted_value - snap_value_ <= radius && attempted_value - snap_value_ >= -radius)
    return snap_value_;
  return attempted_value;
}

void SynthSlider::drawShadow(Graphics &g) {
  if (&getLookAndFeel() == TextLookAndFeel::instance())
    drawRectangularShadow(g);
  else if (isRotary())
    drawRotaryShadow(g);
  else {
    g.setColour(Colour(0xff222222));
    g.fillRect(getBounds());
  }
}

void SynthSlider::drawRotaryShadow(Graphics &g) {
  static const DropShadow shadow(Colour(0xee000000), 3, Point<int>(0, 0));
  static const float stroke_percent = 0.12f;

  g.saveState();
  g.setOrigin(getX(), getY());

  float full_radius = std::min(getWidth() / 2.0f, getHeight() / 2.0f);
  float stroke_width = 2.0f * full_radius * stroke_percent;
  Path shadow_path;
  float outer_radius = full_radius - stroke_width;
  shadow_path.addCentredArc(full_radius, full_radius,
                            0.89f * full_radius, 0.87f * full_radius,
                            0, -rotary_angle, rotary_angle, true);
  shadow.drawForPath(g, shadow_path);

  Path rail_outer;
  rail_outer.addCentredArc(full_radius, full_radius, outer_radius, outer_radius,
                           0.0f, -rotary_angle, rotary_angle, true);

  g.setColour(Colour(0xff333333));

  PathStrokeType outer_stroke =
      PathStrokeType(stroke_width, PathStrokeType::beveled, PathStrokeType::butt);
  g.strokePath(rail_outer, outer_stroke);

  g.restoreState();
}

void SynthSlider::drawRectangularShadow(Graphics &g) {
  static const DropShadow shadow(Colour(0xbb000000), 2, Point<int>(0, 0));

  g.saveState();
  g.setOrigin(getX(), getY());
  shadow.drawForRectangle(g, getLocalBounds());
  g.setColour(Colour(0xff333333));
  g.fillRect(getLocalBounds());

  g.restoreState();
}

void SynthSlider::flipColoring(bool flip_coloring) {
  flip_coloring_ = flip_coloring;
  repaint();
}

void SynthSlider::setBipolar(bool bipolar) {
  bipolar_ = bipolar;
  repaint();
}

void SynthSlider::setActive(bool active) {
  active_ = active;
  repaint();
}

void SynthSlider::addSliderListener(SynthSlider::SliderListener* listener) {
  slider_listeners_.push_back(listener);
}

String SynthSlider::formatValue(float value) {
  static const int number_length = 5;
  static const int max_decimals = 3;

  if (details_.steps)
    return String(value) + " " + details_.display_units;

  String format = String(value, max_decimals);
  format = format.substring(0, number_length);
  int spaces = number_length - format.length();
  
  for (int i = 0; i < spaces; ++i)
    format = " " + format;
  
  return format + " " + details_.display_units;
}

void SynthSlider::notifyGuis() {
  for (SynthSlider::SliderListener* listener : slider_listeners_)
    listener->guiChanged(this);
}

void SynthSlider::handlePopupResult(int result) {
  SynthGuiInterface* parent = findParentComponentOfClass<SynthGuiInterface>();
  if (parent == nullptr)
    return;

  SynthBase* synth = parent->getSynth();
  std::vector<mopo::ModulationConnection*> connections =
      parent->getSynth()->getDestinationConnections(getName().toStdString());

  if (result == kArmMidiLearn)
    synth->armMidiLearn(getName().toStdString());
  else if (result == kClearMidiLearn)
    synth->clearMidiLearn(getName().toStdString());
  else if (result == kDefaultValue)
    setValue(getDoubleClickReturnValue());
  else if (result == kClearModulations) {
    for (mopo::ModulationConnection* connection : connections) {
      std::string source = connection->source;
      synth->disconnectModulation(connection);
    }
    for (SynthSlider::SliderListener* listener : slider_listeners_)
      listener->modulationsChanged(getName().toStdString());
  }
  else if (result >= kModulationList) {
    int connection_index = result - kModulationList;
    std::string source = connections[connection_index]->source;
    synth->disconnectModulation(connections[connection_index]);

    for (SynthSlider::SliderListener* listener : slider_listeners_)
      listener->modulationsChanged(getName().toStdString());
  }
}

void SynthSlider::notifyTooltip() {
  if (parent_ == nullptr)
    parent_ = findParentComponentOfClass<FullInterface>();
  if (parent_) {
    std::string name = getName().toStdString();
    if (mopo::Parameters::isParameter(name))
      name = mopo::Parameters::getDetails(name).display_name;

    parent_->setToolTipText(name, getTextFromValue(getValue()));
  }
}