File: IUIControl.cpp

package info (click to toggle)
bespokesynth 1.3.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,716 kB
  • sloc: cpp: 117,136; ansic: 18,752; python: 593; xml: 74; makefile: 4
file content (353 lines) | stat: -rw-r--r-- 10,754 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
/**
    bespoke synth, a software modular synthesizer
    Copyright (C) 2021 Ryan Challinor (contact: awwbees@gmail.com)

    This program 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.

    This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
**/
//
//  IUIControl.cpp
//  modularSynth
//
//  Created by Ryan Challinor on 6/13/13.
//
//

#include "IUIControl.h"
#include "Snapshots.h"
#include "SynthGlobals.h"
#include "ModularSynth.h"
#include "PatchCable.h"
#include "Push2Control.h"

//static
IUIControl* IUIControl::sLastHoveredUIControl = nullptr;
//static
bool IUIControl::sLastUIHoverWasSetManually = false;

IUIControl::~IUIControl()
{
   if (gHoveredUIControl == this)
      gHoveredUIControl = nullptr;
   if (gBindToUIControl == this)
      gBindToUIControl = nullptr;
}

bool IUIControl::IsPreset()
{
   return VectorContains(this, Snapshots::sSnapshotHighlightControls);
}

bool IUIControl::TestHover(int x, int y)
{
   if (mNoHover)
      return false;

   if (!mShowing)
      return false;

   float w, h;
   GetDimensions(w, h);
   if (x >= 0 && x < w && y >= 0 && y < h) //make sure we're hovered over the control
   {
      IDrawableModule* moduleParent = GetModuleParent();
      float thisX, thisY;
      GetPosition(thisX, thisY);
      x += thisX;
      y += thisY;
      if (moduleParent->GetOwningContainer()->GetModuleAt(x, y) == moduleParent)
      {
         float localX, localY;
         GetPosition(localX, localY, K(localOnly));
         float parentW, parentH;
         GetParent()->GetDimensions(parentW, parentH);
         if (localX < parentW && localY < parentH)
            return true;
      }
   }

   return false;
}

void IUIControl::CheckHover(int x, int y)
{
   static long sLastHoveredUIControlFrame = 0;
   if (TheSynth->GetFrameCount() != sLastHoveredUIControlFrame &&
       !TheSynth->IsGroupSelecting() && PatchCable::sActivePatchCable == nullptr &&
       TestHover(x, y) &&
       (gHoveredUIControl == nullptr || !gHoveredUIControl->IsMouseDown()) &&
       GetModuleParent() == TheSynth->GetModuleAtCursor())
   {
      gHoveredUIControl = this;
      sLastHoveredUIControl = this;
      sLastUIHoverWasSetManually = false;
      sLastHoveredUIControlFrame = TheSynth->GetFrameCount();
   }
}

void IUIControl::DrawHover(float x, float y, float w, float h)
{
   if (Push2Control::sDrawingPush2Display)
      return;

   if (gHoveredUIControl == this && IKeyboardFocusListener::GetActiveKeyboardFocus() == nullptr && TheSynth->GetGroupSelectedModules().empty())
   {
      ofPushStyle();
      ofNoFill();
      ofSetColor(0, 255, 255, 255);
      ofRect(x, y, w, h, 4);
      ofPopStyle();
   }

   if (mRemoteControlCount > 0 && TheSynth->InMidiMapMode())
   {
      ofPushStyle();
      ofFill();
      ofSetColor(255, 0, 255, 100);
      ofRect(x, y, w, h);
      ofPopStyle();
   }

   if (gBindToUIControl == this)
   {
      ofPushStyle();
      ofNoFill();
      ofSetLineWidth(5);
      ofSetColor(255, 0, 255, 200);
      ofRect(x, y, w, h);
      ofPopStyle();
   }

   DrawPatchCableHover();
}

void IUIControl::DrawPatchCableHover()
{
   if (PatchCable::sActivePatchCable &&
       (PatchCable::sActivePatchCable->GetConnectionType() == kConnectionType_Pulse ||
        PatchCable::sActivePatchCable->GetConnectionType() == kConnectionType_Modulator ||
        PatchCable::sActivePatchCable->GetConnectionType() == kConnectionType_ValueSetter ||
        PatchCable::sActivePatchCable->GetConnectionType() == kConnectionType_UIControl ||
        PatchCable::sActivePatchCable->GetConnectionType() == kConnectionType_Grid) &&
       PatchCable::sActivePatchCable->IsValidTarget(this))
   {
      float w, h;
      GetDimensions(w, h);
      ofPushStyle();
      ofNoFill();
      ofSetLineWidth(1.5f);
      ofSetColor(255, 0, 255, 200);
      ofRect(mX, mY, w, h);
      ofPopStyle();
   }
}

bool IUIControl::CanBeTargetedBy(PatchCableSource* source) const
{
   if (!mCableTargetable)
      return false;
   if (GetNoHover())
      return false;
   return source->GetConnectionType() == kConnectionType_Modulator || source->GetConnectionType() == kConnectionType_ValueSetter || source->GetConnectionType() == kConnectionType_UIControl;
}

void IUIControl::StartBeacon()
{
   IClickable::StartBeacon();
   IClickable* moduleParent = GetModuleParent();
   if (moduleParent)
      moduleParent->StartBeacon();
}

void IUIControl::PositionTo(IUIControl* anchor, AnchorDirection direction)
{
   ofRectangle rect = anchor->GetRect(true);
   if (direction == kAnchor_Below)
   {
      mX = rect.x;
      mY = rect.y + rect.height + 2;
   }
   else if (direction == kAnchor_Right)
   {
      mX = rect.x + rect.width + 3;
      mY = rect.y;
   }
   else if (direction == kAnchor_Right_Padded)
   {
      mX = rect.x + rect.width + 10;
      mY = rect.y;
   }
}

void IUIControl::GetColors(ofColor& color, ofColor& textColor)
{
   IDrawableModule* module = dynamic_cast<IDrawableModule*>(GetParent());
   if (module)
      color = IDrawableModule::GetColor(module->GetModuleCategory());
   else
      color = ofColor::white;
   float h, s, b;
   color.getHsb(h, s, b);
   color.setHsb(h, s * .4f, ofLerp(b, 0, .6f));
   if (IsPreset())
   {
      color.getHsb(h, s, b);
      color.setHsb(85, s, b);
      textColor.set(0, 255, 0, gModuleDrawAlpha);
   }
   else
   {
      textColor.set(255, 255, 255, gModuleDrawAlpha);
   }
}

void IUIControl::RemoveFromOwner()
{
   IDrawableModule* owner = dynamic_cast<IDrawableModule*>(GetParent());
   assert(owner);
   if (owner)
      owner->RemoveUIControl(this);
}

//static
void IUIControl::SetNewManualHoverViaTab(int direction)
{
   if (gHoveredUIControl == nullptr)
   {
      gHoveredUIControl = sLastHoveredUIControl;
      sLastUIHoverWasSetManually = true;
   }
   else
   {
      IDrawableModule* uiControlModule = gHoveredUIControl->GetModuleParent();
      if (uiControlModule != nullptr)
      {
         const auto& controls = uiControlModule->GetUIControls();
         int controlIndex = 0;
         for (int i = 0; i < (int)controls.size(); ++i)
         {
            if (controls[i] == gHoveredUIControl)
            {
               controlIndex = i;
               break;
            }
         }

         for (int i = 1; i < (int)controls.size(); ++i)
         {
            int newControlIndex = (controlIndex + i * direction + (int)controls.size()) % (int)controls.size();
            if (controls[newControlIndex]->IsShowing() && !controls[newControlIndex]->GetNoHover())
            {
               gHoveredUIControl = controls[newControlIndex];
               sLastHoveredUIControl = gHoveredUIControl;
               sLastUIHoverWasSetManually = true;
               break;
            }
         }
      }
   }
}

namespace
{
   //only supports cardinal directions
   float GetDistanceScore(ofVec2f direction, ofRectangle rectA, ofRectangle rectB)
   {
      float score = 0;
      ofVec2f edgeA = rectA.getCenter() + ofVec2f(rectA.width * .5f * direction.x, rectA.height * .5f * direction.y);
      ofVec2f edgeB = rectB.getCenter() + ofVec2f(rectB.width * -.5f * direction.x, rectB.height * -.5f * direction.y);
      ofVec2f toRect = edgeB - edgeA;
      float dot = direction.dot(toRect);
      if (dot > 0)
      {
         ofVec2f perpendicularDirection(direction.y, direction.x);
         float minExtentA = fabsf(rectA.getMinX() * perpendicularDirection.x + rectA.getMinY() * perpendicularDirection.y);
         float maxExtentA = fabsf(rectA.getMaxX() * perpendicularDirection.x + rectA.getMaxY() * perpendicularDirection.y);
         float minExtentB = fabsf(rectB.getMinX() * perpendicularDirection.x + rectB.getMinY() * perpendicularDirection.y);
         float maxExtentB = fabsf(rectB.getMaxX() * perpendicularDirection.x + rectB.getMaxY() * perpendicularDirection.y);
         if (minExtentA <= maxExtentB && maxExtentA >= minExtentB) //overlap, score based upon closest in the specified direction
            score = 1 / direction.dot(toRect) + 1000; //bonus points so that overlapping ones win
         else //no overlap,but still in the requested direction. score based upon overall distance
            score = 1 / toRect.distanceSquared();
      }

      return score;
   }
}

//static
void IUIControl::SetNewManualHoverViaArrow(ofVec2f direction)
{
   if (gHoveredUIControl == nullptr)
   {
      gHoveredUIControl = sLastHoveredUIControl;
      sLastUIHoverWasSetManually = true;
   }
   else
   {
      IDrawableModule* uiControlModule = gHoveredUIControl->GetModuleParent();
      if (uiControlModule != nullptr)
      {
         const auto& controls = uiControlModule->GetUIControls();
         ofRectangle currentControlRect = gHoveredUIControl->GetRect();
         float bestScore = 0;
         int bestScoreIndex = -1;
         for (int i = 0; i < (int)controls.size(); ++i)
         {
            if (controls[i]->IsShowing() && !controls[i]->GetNoHover() && controls[i] != gHoveredUIControl)
            {
               float score = GetDistanceScore(direction, currentControlRect, controls[i]->GetRect());
               if (score > bestScore)
               {
                  bestScore = score;
                  bestScoreIndex = i;
               }
            }
         }

         if (bestScoreIndex != -1)
         {
            gHoveredUIControl = controls[bestScoreIndex];
            sLastHoveredUIControl = gHoveredUIControl;
            sLastUIHoverWasSetManually = true;
         }
      }
   }
}

//static
void IUIControl::DestroyCablesTargetingControls(std::vector<IUIControl*> controls)
{
   std::vector<IDrawableModule*> modules;
   TheSynth->GetAllModules(modules);
   std::vector<PatchCable*> cablesToDestroy;
   for (const auto module_iter : modules)
   {
      for (const auto source : module_iter->GetPatchCableSources())
      {
         for (const auto cable : source->GetPatchCables())
         {
            for (const auto control : controls)
            {
               if (cable->GetTarget() == control)
               {
                  cablesToDestroy.push_back(cable);
                  break;
               }
            }
         }
      }
   }
   for (const auto cable : cablesToDestroy)
      cable->Destroy(false);
}