File: PositionPlane.h

package info (click to toggle)
iem-plugin-suite 1.15.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,080 kB
  • sloc: cpp: 58,973; python: 269; sh: 79; makefile: 41
file content (590 lines) | stat: -rw-r--r-- 18,888 bytes parent folder | download | duplicates (2)
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*
 ==============================================================================
 This file is part of the IEM plug-in suite.
 Author: Daniel Rudrich
 Copyright (c) 2017 - Institute of Electronic Music and Acoustics (IEM)
 https://iem.at

 The IEM plug-in suite 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.

 The IEM plug-in suite 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 this software.  If not, see <https://www.gnu.org/licenses/>.
 ==============================================================================
 */

#pragma once

class PositionPlane : public juce::Component
{
public:
    PositionPlane() : drawPlane (xy), autoScale (true), dimensions (1.0f, 1.0f, 1.0f) {};

    ~PositionPlane() { deleteAllChildren(); };

    enum Planes
    {
        xy,
        zy,
        zx
    };

    class Element
    {
    public:
        Element() {}
        Element (juce::String newID) { ID = newID; }
        virtual ~Element() {}

        virtual void startMovement() {};
        virtual void moveElement (const juce::MouseEvent& event,
                                  const juce::Point<float> centre,
                                  const float scale,
                                  Planes plane,
                                  PositionPlane* positionPlane,
                                  int xFactor = 1,
                                  int yFactor = 1,
                                  int zFactor = 1) = 0;
        virtual void stopMovement() {};

        void setActive (bool shouldBeActive) { active = shouldBeActive; }
        bool isActive() { return active; }

        void setColour (juce::Colour newColour) { faceColour = newColour; }
        juce::Colour getColour() { return faceColour; }

        void setLabel (juce::String newLabel) { label = newLabel; }
        void setID (juce::String newID) { ID = newID; }

        virtual juce::Vector3D<float> getPosition() = 0;

        juce::String getLabel() { return label; };
        juce::String getID() { return ID; };

        void addPlane (PositionPlane* positionPlane)
        {
            jassert (positionPlane != nullptr);
            if (positionPlane != nullptr)
                planesImIn.add (positionPlane);
        };
        void removePlane (PositionPlane* positionPlane)
        {
            planesImIn.removeFirstMatchingValue (positionPlane);
        };

        void repaintAllPlanesImIn()
        {
            for (int i = planesImIn.size(); --i >= 0;)
            {
                PositionPlane* handle = planesImIn.getUnchecked (i);
                handle->repaint();
            }
        }

    private:
        bool active = true;

        juce::Colour faceColour = juce::Colours::white;
        juce::String ID = "";
        juce::String label = "";

        juce::Array<PositionPlane*> planesImIn;
    };

    class ParameterElement : public Element
    {
    public:
        ParameterElement (juce::AudioProcessorParameter& xParameter,
                          juce::NormalisableRange<float> xParameterRange,
                          juce::AudioProcessorParameter& yParameter,
                          juce::NormalisableRange<float> yParameterRange,
                          juce::AudioProcessorParameter& zParameter,
                          juce::NormalisableRange<float> zParameterRange) :
            x (xParameter),
            xRange (xParameterRange),
            y (yParameter),
            yRange (yParameterRange),
            z (zParameter),
            zRange (zParameterRange)
        {
        }

        void startMovement() override
        {
            x.beginChangeGesture();
            y.beginChangeGesture();
            z.beginChangeGesture();
        };

        void moveElement (const juce::MouseEvent& event,
                          const juce::Point<float> centre,
                          const float scale,
                          Planes plane,
                          PositionPlane* positionPlane,
                          int xFactor = 1,
                          int yFactor = 1,
                          int zFactor = 1) override
        {
            auto mousePos = event.getPosition().toFloat();
            mousePos.x -= centre.x;
            mousePos.y -= centre.y;
            mousePos /= scale;

            juce::Vector3D<float> roomDims = positionPlane->getDimensions();
            juce::Vector3D<float> pos;

            switch (plane)
            {
                case xy:
                    pos.x = -mousePos.y * xFactor;
                    pos.y = -mousePos.x * yFactor;
                    pos.x =
                        juce::Range<float> (-0.5 * roomDims.x, 0.5 * roomDims.x).clipValue (pos.x);
                    pos.y =
                        juce::Range<float> (-0.5 * roomDims.y, 0.5 * roomDims.y).clipValue (pos.y);
                    x.setValueNotifyingHost (xRange.convertTo0to1 (pos.x));
                    y.setValueNotifyingHost (yRange.convertTo0to1 (pos.y));

                    break;
                case zy:
                    pos.z = -mousePos.y * zFactor;
                    pos.y = -mousePos.x * yFactor;
                    pos.z =
                        juce::Range<float> (-0.5 * roomDims.z, 0.5 * roomDims.z).clipValue (pos.z);
                    pos.y =
                        juce::Range<float> (-0.5 * roomDims.y, 0.5 * roomDims.y).clipValue (pos.y);
                    z.setValueNotifyingHost (zRange.convertTo0to1 (pos.z));
                    y.setValueNotifyingHost (yRange.convertTo0to1 (pos.y));
                    break;
                case zx:
                    pos.z = -mousePos.y * zFactor;
                    pos.x = mousePos.x * xFactor;
                    pos.z =
                        juce::Range<float> (-0.5 * roomDims.z, 0.5 * roomDims.z).clipValue (pos.z);
                    pos.x =
                        juce::Range<float> (-0.5 * roomDims.x, 0.5 * roomDims.x).clipValue (pos.x);
                    z.setValueNotifyingHost (zRange.convertTo0to1 (pos.z));
                    x.setValueNotifyingHost (xRange.convertTo0to1 (pos.x));
                    break;
            }
        }

        void stopMovement() override
        {
            x.endChangeGesture();
            y.endChangeGesture();
            z.endChangeGesture();
        };

        /**
         Get cartesian coordinates
         */
        juce::Vector3D<float> getPosition() override
        {
            return juce::Vector3D<float> (xRange.convertFrom0to1 (x.getValue()),
                                          yRange.convertFrom0to1 (y.getValue()),
                                          zRange.convertFrom0to1 (z.getValue()));
        };

    private:
        juce::AudioProcessorParameter& x;
        juce::NormalisableRange<float> xRange;
        juce::AudioProcessorParameter& y;
        juce::NormalisableRange<float> yRange;
        juce::AudioProcessorParameter& z;
        juce::NormalisableRange<float> zRange;
    };

    class PositionPlaneListener
    {
    public:
        virtual ~PositionPlaneListener() {}

        virtual void PositionPlaneElementChanged (PositionPlane* plane, Element* element) = 0;
    };

    void paint (juce::Graphics& g) override
    {
        juce::Rectangle<float> bounds (0, 0, getBounds().getWidth(), getBounds().getHeight());
        float innerSpacing = 3.0f;
        bounds.reduce (innerSpacing, innerSpacing);

        const float width = bounds.getWidth();
        const float height = bounds.getHeight();

        const float centreX = bounds.getCentreX();
        const float centreY = bounds.getCentreY();

        float drawH;
        float drawW;

        const int xFactor = xFlip ? -1 : 1;
        const int yFactor = yFlip ? -1 : 1;
        const int zFactor = zFlip ? -1 : 1;

        switch (drawPlane)
        {
            default:
            case xy:
                drawH = dimensions.x;
                drawW = dimensions.y;
                break;
            case zy:
                drawH = dimensions.z;
                drawW = dimensions.y;
                break;
            case zx:
                drawH = dimensions.z;
                drawW = dimensions.x;
                break;
        }

        if (autoScale)
        {
            float dimRatio = drawH / drawW;

            if (dimRatio >= height / width)
                scale = height / drawH;
            else
                scale = width / drawW;

            if (dimRatio >= height / width)
                scale = height / drawH;
            else
                scale = width / drawW;
        }
        drawW *= scale;
        drawH *= scale;

        juce::Rectangle<float> room (innerSpacing + 0.5f * (width - drawW),
                                     innerSpacing + 0.5f * (height - drawH),
                                     drawW,
                                     drawH);

        g.setColour (juce::Colours::white.withMultipliedSaturation (0.9f));
        g.setFont (10.0f);
        switch (drawPlane)
        {
            default:
            case xy:
                g.drawArrow (
                    juce::Line<float> (centreX, centreY, centreX, centreY - 20.0f * xFactor),
                    1.0f,
                    4.0f,
                    4.0f);
                g.drawArrow (
                    juce::Line<float> (centreX, centreY, centreX - 20.0f * yFactor, centreY),
                    1.0f,
                    4.0f,
                    4.0f);
                g.drawSingleLineText ("x", centreX + 2.0f, centreY + 2.0f - 9.0f * xFactor);
                g.drawSingleLineText ("y", centreX - 2.0f - 10.0f * yFactor, centreY + 7.0f);
                break;
            case zy:
                g.drawArrow (
                    juce::Line<float> (centreX, centreY, centreX, centreY - 20.0f * zFactor),
                    1.0f,
                    4.0f,
                    4.0f);
                g.drawArrow (
                    juce::Line<float> (centreX, centreY, centreX - 20.0f * yFactor, centreY),
                    1.0f,
                    4.0f,
                    4.0f);
                g.drawSingleLineText ("z", centreX + 2.0f, centreY + 2.0f - 9.0f * zFactor);
                g.drawSingleLineText ("y", centreX - 2.0f - 10.0f * yFactor, centreY + 7.0f);
                ;
                break;
            case zx:
                g.drawArrow (
                    juce::Line<float> (centreX, centreY, centreX, centreY - 20.0f * zFactor),
                    1.0f,
                    4.0f,
                    4.0f);
                g.drawArrow (
                    juce::Line<float> (centreX, centreY, centreX + 20.0f * xFactor, centreY),
                    1.0f,
                    4.0f,
                    4.0f);
                g.drawSingleLineText ("z", centreX + 2.0f, centreY + 2.0f - 9.0f * zFactor);
                g.drawSingleLineText ("x", centreX + 2.0f, centreY + 2.0f - 9.0f * xFactor);
                break;
        }

        g.setColour (juce::Colours::steelblue.withMultipliedAlpha (0.3f));
        g.fillRect (room);

        g.setColour (juce::Colours::white);
        g.drawRect (room, 1.0f);

        for (int i = elements.size(); --i >= 0;)
        {
            Element* handle = (Element*) elements.getUnchecked (i);

            juce::Vector3D<float> position = handle->getPosition();
            g.setColour (handle->isActive() ? handle->getColour() : juce::Colours::grey);

            juce::Path path;
            float posH, posW;
            switch (drawPlane)
            {
                default:
                case xy:
                    posH = position.x * xFactor;
                    posW = position.y * yFactor;
                    break;
                case zy:
                    posH = position.z * zFactor;
                    posW = position.y * yFactor;
                    break;
                case zx:
                    posH = position.z * zFactor;
                    posW = -position.x * xFactor;
                    break;
            }
            juce::Rectangle<float> temp (centreX - posW * scale - 10 / 2,
                                         centreY - posH * scale - 10 / 2,
                                         11,
                                         11);
            path.addEllipse (temp);
            g.fillPath (path);
        }
    };

    float setDimensions (juce::Vector3D<float> newDimensions)
    {
        dimensions = newDimensions;
        repaint();

        float width = getBounds().getWidth();
        float height = getBounds().getHeight();

        float drawH, drawW;
        float tempScale;
        switch (drawPlane)
        {
            default:
            case xy:
                drawH = dimensions.x;
                drawW = dimensions.y;
                break;
            case zy:
                drawH = dimensions.z;
                drawW = dimensions.y;
                break;
            case zx:
                drawH = dimensions.z;
                drawW = dimensions.x;
                break;
        }

        float dimRatio = drawH / drawW;

        if (dimRatio >= height / width)
            tempScale = height / drawH;
        else
            tempScale = width / drawW;

        return tempScale;
    }

    void useAutoScale (bool shouldUseAutoScale) { autoScale = shouldUseAutoScale; }
    bool usingAutoScale() { return autoScale; }

    void setScale (float newScale)
    {
        if (! autoScale)
            scale = newScale;
    }

    juce::Vector3D<float> getDimensions() { return dimensions; }

    void mouseDown (const juce::MouseEvent& event) override
    {
        Element* handle;

        auto bounds = getLocalBounds().toType<float>();
        const float centreX = bounds.getCentreX();
        const float centreY = bounds.getCentreY();

        int nElem = elements.size();
        activeElem = -1;
        float activeDSquared = 80.0f; //dummy value
        if (nElem > 0)
        {
            const int xFactor = xFlip ? -1 : 1;
            const int yFactor = yFlip ? -1 : 1;
            const int zFactor = zFlip ? -1 : 1;

            auto pos = event.getPosition();

            float mouseX = (centreY - pos.getY());
            float mouseY = (centreX - pos.getX());

            if (drawPlane == zx)
                mouseY *= -1;

            for (int i = elements.size(); --i >= 0;)
            {
                handle = elements.getUnchecked (i);

                float posH, posW;
                juce::Vector3D<float> position = handle->getPosition();
                switch (drawPlane)
                {
                    default:
                    case xy:
                        posH = position.x * xFactor;
                        posW = position.y * yFactor;
                        break;
                    case zy:
                        posH = position.z * zFactor;
                        posW = position.y * yFactor;
                        break;
                    case zx:
                        posH = position.z * zFactor;
                        posW = position.x * xFactor;
                        break;
                }

                float tx = (mouseX - posH * scale);
                float ty = (mouseY - posW * scale);

                float dSquared = tx * tx + ty * ty;
                if (dSquared <= 80.0f && dSquared < activeDSquared)
                {
                    activeElem = i;
                    activeDSquared = dSquared;
                }
            }
        }
        if (activeElem != -1)
        {
            elements.getUnchecked (activeElem)->startMovement();
        }
    }

    void mouseDrag (const juce::MouseEvent& event) override
    {
        auto bounds = getLocalBounds().toType<float>();
        const auto centre = bounds.getCentre();
        const int xFactor = xFlip ? -1 : 1;
        const int yFactor = yFlip ? -1 : 1;
        const int zFactor = zFlip ? -1 : 1;

        if (activeElem != -1)
        {
            Element* handle = elements.getUnchecked (activeElem);
            handle->moveElement (event, centre, scale, drawPlane, this, xFactor, yFactor, zFactor);
            handle->repaintAllPlanesImIn();
            sendChanges (handle);
        }
        repaint();
    }

    void mouseUp (const juce::MouseEvent& event) override
    {
        if (activeElem != -1)
        {
            elements.getUnchecked (activeElem)->stopMovement();
        }
    }

    void setPlane (Planes PlaneToDraw) { drawPlane = PlaneToDraw; }

    void addListener (PositionPlaneListener* const listener)
    {
        jassert (listener != 0);
        if (listener != 0)
            listeners.add (listener);
    };
    void removeListener (PositionPlaneListener* const listener)
    {
        listeners.removeFirstMatchingValue (listener);
    };

    void sendChanges (Element* element)
    {
        for (int i = listeners.size(); --i >= 0;)
            ((PositionPlaneListener*) listeners.getUnchecked (i))
                ->PositionPlaneElementChanged (this, element);
    }

    void addElement (Element* const element)
    {
        jassert (element != 0);
        if (element != 0)
        {
            elements.add (element);
            element->addPlane (this);
        }
    };
    void removeElement (Element* const element)
    {
        element->removePlane (this);
        elements.removeFirstMatchingValue (element);
    };

    int indexofSmallestElement (float* array, int size)
    {
        int index = 0;

        for (int i = 1; i < size; i++)
        {
            if (array[i] < array[index])
                index = i;
        }

        return index;
    }

    void setXFlip (const bool flipped)
    {
        if (xFlip != flipped)
        {
            xFlip = flipped;
            repaint();
        }
    }

    void setYFlip (const bool flipped)
    {
        if (yFlip != flipped)
        {
            yFlip = flipped;
            repaint();
        }
    }

    void setZFlip (const bool flipped)
    {
        if (zFlip != flipped)
        {
            zFlip = flipped;
            repaint();
        }
    }

private:
    Planes drawPlane;
    juce::String suffix;

    bool xFlip = false;
    bool yFlip = false;
    bool zFlip = false;

    bool autoScale;
    juce::Vector3D<float> dimensions;
    float scale;
    int activeElem;
    bool activeElemWasUpBeforeDrag;
    juce::Array<void*> listeners;
    juce::Array<Element*> elements;
};