-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathControls.cpp
911 lines (808 loc) · 22.6 KB
/
Controls.cpp
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
//
// Controls.cpp
// Evaluator
//
// Created by Damien Quartz on 10/23/17.
//
#include "Controls.h"
#include "Evaluator.h"
#include "Interface.h"
#include "Presets.h"
#pragma region ITextEdit
ITextEdit::ITextEdit(IPlugBase* pPlug, IRECT pR, int paramIdx, IText* pText, const char* str, ETextEntryOptions textEntryOptions)
: IControl(pPlug, pR)
, mIdx(paramIdx)
, mStr(str, kExpressionLengthMax)
{
mDisablePrompt = true;
mText = *pText;
mTextEntryLength = kExpressionLengthMax;
mTextEntryOptions = textEntryOptions;
}
ITextEdit::~ITextEdit() {}
bool ITextEdit::Draw(IGraphics* pGraphics)
{
pGraphics->FillIRect(&mText.mTextEntryBGColor, &mRECT);
IRECT textRect = mRECT.GetHPadded(-3);
// copy into fixed-size buffer because DrawIText needs a non-const string
char textEditText[kExpressionLengthMax];
strncpy(textEditText, mStr.c_str(), kExpressionLengthMax - 1);
#if defined(OS_OSX)
// line spacing is too large when rendering on High Sierra (and presumably Mojave as well)
// so we render one line at a time so we can control the line spacing to match the native text field.
// it'd be great to be able to use strtok here, but if there are two \n in a row,
// strtok will skip the second one and we won't get an empty line.
char* line = textEditText;
while(line != NULL)
{
char* end = strchr(line, '\n');
if (end != NULL)
{
end[0] = '\0';
}
pGraphics->DrawIText(&mText, line, &textRect);
if (mText.mCached != NULL)
{
textRect.T += mText.mCached->GetLineHeight() + 1;
}
line = end != NULL ? end+1 : NULL;
}
#else
return pGraphics->DrawIText(&mText, textEditText, &textRect);
#endif
return true;
}
void ITextEdit::OnMouseDown(int x, int y, IMouseMod* pMod)
{
// stupid hack to prevent the edit window from displaying 1 pixel wider and taller
IRECT entryRect = mRECT.GetPadded(0,0,-1,-1);
IText entryTextStyle = mText;
const char * entryText = mStr.c_str();
#if defined(OS_OSX)
entryRect.L += 3;
entryTextStyle.mSize -= 4;
#elif defined(OS_WIN)
size_t pos = mStr.find('\n');
while (pos != std::string::npos)
{
mStr.replace(pos, 1, "\r\n");
// start searching from pos+2 because we inserted 2 characters and don't want to find the \n we just inserted
pos = mStr.find('\n', pos + 2);
}
entryText = mStr.c_str();
#endif
mPlug->GetGUI()->CreateTextEntry(this, &entryTextStyle, &entryRect, entryText);
if (mNameDisplayControl)
{
mNameDisplayControl->Hide(false);
}
}
void ITextEdit::TextFromTextEntry(const char* txt)
{
// we always do this because we want to keep our input string clean.
// just because we are on OSX doesn't mean a user can't load a file that contains \r\n
std::string input(txt);
size_t pos = input.find('\r');
while (pos != std::string::npos)
{
input.erase(pos, 1);
pos = input.find('\r');
}
// don't set if it is the same, we don't want the project to become modified in this case
if (mStr != input)
{
// don't assign more characters than we support
mStr.assign(input, 0, kExpressionLengthMax);
SetDirty(false);
mPlug->OnParamChange(mIdx);
// we have to call this or else the host will not mark the project as modified
mPlug->InformHostOfParamChange(mIdx, 0);
}
if (mNameDisplayControl)
{
mNameDisplayControl->Hide(true);
}
}
#pragma endregion ITextEdit
#pragma region TextBox
TextBox::TextBox(IPlugBase* pPlug, IRECT pR, int paramIdx, IText* pText, IRECT textRect, bool showParamUnits)
: ICaptionControl(pPlug, pR, paramIdx, pText, showParamUnits)
, mTextRect(textRect)
{
}
bool TextBox::Draw(IGraphics* pGraphics)
{
pGraphics->FillIRect(&mText.mTextEntryBGColor, &mRECT);
pGraphics->DrawRect(&mText.mTextEntryFGColor, &mRECT);
IRECT ourRect = mRECT;
mRECT = mTextRect;
bool result = ICaptionControl::Draw(pGraphics);
mRECT = ourRect;
return result;
}
void TextBox::OnMouseDown(int x, int y, IMouseMod* pMod)
{
if (pMod->L)
{
IText ourText = mText;
IRECT promptRect = mTextRect;
#if defined(OS_OSX)
mText.mSize -= 2;
promptRect.T -= 1;
#endif
mPlug->GetGUI()->PromptUserInput(this, mPlug->GetParam(mParamIdx), &promptRect);
mText = ourText;
Redraw();
}
}
void TextBox::OnMouseDrag(int x, int y, int dX, int dY, IMouseMod* pMod)
{
if (pMod->R)
{
OnMouseWheel(x, y, pMod, -dY);
}
}
void TextBox::OnMouseWheel(int x, int y, IMouseMod* pMod, int d)
{
#ifdef PROTOOLS
if (pMod->C)
{
mValue += GetParam()->GetStep() * 0.001 * d;
}
#else
if (pMod->C || pMod->S)
{
mValue += GetParam()->GetStep() * 0.001 * d;
}
#endif
else
{
mValue += GetParam()->GetStep() * 0.01 * d;
}
SetDirty();
}
#pragma endregion TextBox
#pragma region TextTable
TextTable::TextTable(IPlugBase* pPlug, IRECT pR, IText* pText, const char** data, int iColumns, int iRows)
: IControl(pPlug, pR)
, tableData(data)
, columns(iColumns)
, rows(iRows)
{
SetText(pText);
}
bool TextTable::Draw(IGraphics* pGraphics)
{
for (int r = 0; r < rows; ++r)
{
IRECT rowRect = mRECT.SubRectVertical(rows, r);
for (int c = 0; c < columns; ++c)
{
IRECT cellRect = rowRect.SubRectHorizontal(columns*2, c);
char* contents = const_cast<char*>(tableData[r*columns + c]);
pGraphics->DrawIText(&mText, contents, &cellRect);
}
}
return true;
}
#pragma endregion TextTable
#pragma region ConsoleText
ConsoleText::ConsoleText(IPlugBase* plug, IRECT pR, IText* textStyle, const IColor* backgroundColor, int margin)
: IControl(plug, pR)
, mPanel(plug, pR, backgroundColor)
, mText(plug, pR.GetPadded(-margin), textStyle, "")
{}
bool ConsoleText::Draw(IGraphics* pGraphics)
{
return mPanel.Draw(pGraphics) && mText.Draw(pGraphics);
}
void ConsoleText::SetTextFromPlug(const char * text)
{
mText.SetTextFromPlug(const_cast<char*>(text));
if (mText.IsDirty())
{
SetDirty(false);
Redraw();
}
}
#pragma endregion ConsoleText
#pragma region EnumControl (used for RunMode)
EnumControl::EnumControl(IPlugBase* pPlug, IRECT rect, int paramIdx, IText* textStyle)
: IControl(pPlug, rect, paramIdx)
{
SetText(textStyle);
mDblAsSingleClick = true;
mDisablePrompt = false;
}
bool EnumControl::Draw(IGraphics* pGraphics)
{
pGraphics->FillIRect(&mText.mTextEntryBGColor, &mRECT);
pGraphics->DrawRect(&mText.mTextEntryFGColor, &mRECT);
static char display[16];
GetParam()->GetDisplayForHost(display);
IRECT textRect = mRECT;
textRect.T += 3;
pGraphics->DrawIText(&mText, display, &textRect);
return true;
}
void EnumControl::OnMouseDown(int x, int y, IMouseMod* pMod)
{
if (pMod->R)
{
PromptUserInput();
}
else
{
int count = GetParam()->GetNDisplayTexts();
if (count > 1)
{
mValue += 1.0 / (double)(count - 1);
}
else
{
mValue += 1.0;
}
if (mValue > 1.001)
{
mValue = 0.0;
}
SetDirty();
}
}
#pragma endregion EnumControl
#pragma region Oscilloscope
static const int gridLineWidth = 4;
Oscilloscope::Oscilloscope(IPlugBase* pPlug, IRECT pR, const IColor* backgroundColor, const IColor* lineColorLeft, const IColor* lineColorRight)
: IControl(pPlug, pR)
, mBackgroundColor(*backgroundColor)
, mLineColorLeft(*lineColorLeft)
, mLineColorRight(*lineColorRight)
{
mBufferSize = pR.W() * 2;
// buffer size for Grid
//mBufferSize = pR.W() * pR.H() / gridLineWidth + pR.H();
mBuffer = new double[mBufferSize];
memset(mBuffer, 0, mBufferSize*sizeof(double));
mBufferBegin = 0;
}
Oscilloscope::~Oscilloscope()
{
delete[] mBuffer;
}
bool Oscilloscope::Draw(IGraphics* pGraphics)
{
pGraphics->FillIRect(&mBackgroundColor, &mRECT, &mBlend);
DrawWaveform(pGraphics);
//DrawGrid(pGraphics);
return true;
}
void Oscilloscope::DrawWaveform(IGraphics* pGraphics)
{
const float midY = mRECT.MH();
const float halfH = mRECT.H()*0.5f;
float px1 = (float)mRECT.L;
float pyl1 = midY;
float pyr1 = midY;
pGraphics->DrawLine(&COLOR_GRAY, (float)mRECT.L, midY, (float)mRECT.R, midY);
IChannelBlend lineBlend = IChannelBlend(IChannelBlend::kBlendAdd);
const float fade = 0.25f;
IColor lineGhostLeft(mLineColorLeft.A,
(int)((float)mLineColorLeft.R*fade),
(int)((float)mLineColorLeft.G*fade),
(int)((float)mLineColorLeft.B*fade));
IColor lineGhostRight(mLineColorRight.A,
(int)((float)mLineColorRight.R*fade),
(int)((float)mLineColorRight.G*fade),
(int)((float)mLineColorRight.B*fade));
for (int x = 0; x < mRECT.W(); x++)
{
const int lidx = (mBufferBegin + x * 2) % mBufferSize;
const int ridx = lidx + 1;
const float px2 = (float)(mRECT.L + x);
const float pyl2 = midY - (float)mBuffer[lidx] * halfH;
const float pyr2 = midY - (float)mBuffer[ridx] * halfH;
pGraphics->DrawLine(&lineGhostLeft, px2, midY, px2, pyl2, &lineBlend);
pGraphics->DrawLine(&lineGhostRight, px2, midY, px2, pyr2, &lineBlend);
pGraphics->DrawLine(&mLineColorLeft, px1, pyl1, px2, pyl2, &lineBlend, true);
pGraphics->DrawLine(&mLineColorRight, px1, pyr1, px2, pyr2, &lineBlend, true);
px1 = px2;
pyl1 = pyl2;
pyr1 = pyr2;
}
}
void Oscilloscope::DrawGrid(IGraphics* pGraphics)
{
const float midY = mRECT.MH();
const int halfH = mRECT.H() / 2;
const int columns = mRECT.W() / gridLineWidth;
const int columnH = halfH*2;
const int readStart = (mBufferBegin + columnH - 1) / columnH * columnH;
for(int x = 0; x < columns; ++x)
{
const int off = x*halfH*2;
const float px = (float)(mRECT.L + x*gridLineWidth);
for(int i = 0; i < halfH; ++i)
{
const int lidx = (readStart + off + i * 2) % mBufferSize;
const int ridx = lidx + 1;
const float pyl = midY - i;
const float pyr = midY + halfH - i;
float fade = (mBuffer[lidx]+1) / 2.0f;
IColor colorLeft(mLineColorLeft.A,
(int)((float)mLineColorLeft.R*fade),
(int)((float)mLineColorLeft.G*fade),
(int)((float)mLineColorLeft.B*fade));
fade = (mBuffer[ridx]+1) / 2.0f;
IColor colorRight(mLineColorRight.A,
(int)((float)mLineColorRight.R*fade),
(int)((float)mLineColorRight.G*fade),
(int)((float)mLineColorRight.B*fade));
pGraphics->DrawLine(&colorLeft, px, pyl, px+gridLineWidth, pyl);
pGraphics->DrawLine(&colorRight, px, pyr, px+gridLineWidth, pyr);
}
}
}
void Oscilloscope::AddSample(double left, double right)
{
mBuffer[mBufferBegin] = left;
mBuffer[mBufferBegin + 1] = right;
mBufferBegin = (mBufferBegin + 2) % mBufferSize;
SetDirty(false);
}
#pragma endregion Oscilloscope
#pragma region LoadButton
static const int kMenuPadding = 5;
static char* kLoadText = "LOAD...";
LoadButton::LoadButton(IPlugBase* pPlug, int x, int y, IBitmap* pButtonBack, IText* pButtonTextStyle, IRECT menuRect, IText* pMenuTextStyle, Interface* pInterface)
: IBitmapControl(pPlug, x, y, -1, pButtonBack)
, mState(kClosed)
, mButtonRect(x,y,pButtonBack)
, mTextRect(mButtonRect)
, mMenuRect(menuRect.GetPadded(kMenuPadding))
, mButtonText(*pButtonTextStyle)
, mMenuText(*pMenuTextStyle)
, mInterface(pInterface)
{
// we make the entire window our rect, otherwise our background fade doesn't render correctly
mRECT = IRECT(0, 0, GUI_WIDTH, GUI_HEIGHT);
// but only our button should get mouse clicks to begin with
SetTargetArea(mButtonRect);
mDisablePrompt = true;
mMenuRect.T += kMenuPadding;
mMenuRect.B += kMenuPadding;
const int selectionCount = Presets::Count() + 1;
const int selectionHeight = menuRect.H() / (selectionCount-1);
for (int i = 0; i < selectionCount; ++i)
{
const int T = mMenuRect.T + kMenuPadding + i*selectionHeight;
IRECT rect = IRECT(menuRect.L + kMenuPadding, T, menuRect.R - kMenuPadding, T + selectionHeight);
mRECTs.Add(rect);
}
mMenuRect.B += kMenuPadding*2;
mInterface->GetGUI()->MeasureIText(&mButtonText, kLoadText, &mTextRect);
mTextRect.T += (mButtonRect.H() - mTextRect.H()) / 2;
}
bool LoadButton::Draw(IGraphics* pGraphics)
{
pGraphics->DrawBitmap(&mBitmap, &mButtonRect, 1, &mBlend);
pGraphics->DrawIText(&mButtonText, kLoadText, &mTextRect);
if (mState == kOpen)
{
IColor fadeColor(128, 0, 0, 0);
pGraphics->FillIRect(&fadeColor, &mTargetRECT);
pGraphics->FillIRect(&COLOR_BLACK, &mMenuRect);
pGraphics->DrawRect(&mMenuText.mColor, &mMenuRect);
IColor selectionColor(255, 30, 30, 30);
char text[128];
for (int i = 0; i < mRECTs.GetSize(); ++i)
{
IRECT itemRect = mRECTs.Get()[i];
if (i == mSelection)
{
IRECT itemBack(itemRect);
itemBack.L -= 2;
itemBack.R += 2;
pGraphics->FillIRect(&selectionColor, &itemBack);
}
if (i < Presets::Count())
{
const auto& preset = Presets::Get(i);
if (preset.runMode == kRunModeMIDI || preset.midiNoteResetsTime)
{
sprintf(text, "%s [MIDI]", preset.name);
}
else
{
sprintf(text, "%s", preset.name);
}
pGraphics->DrawIText(&mMenuText, text, &itemRect);
}
else
{
pGraphics->DrawIText(&mMenuText, "load from file...", &itemRect);
}
}
Redraw();
}
return true;
}
void LoadButton::OnMouseDown(int x, int y, IMouseMod* pMod)
{
switch (mState)
{
case kClosed:
mState = kOpen;
// now we want to capture the mouse in the entire window
SetTargetArea(mRECT);
mSelection = -1;
SetDirty(false);
break;
case kOpen:
mState = kClosed;
SetTargetArea(mButtonRect);
SetDirty(false);
if (mSelection >= 0 )
{
if (mSelection < Presets::Count())
{
mInterface->LoadPreset(mSelection);
}
else
{
WDL_String fileName("");
WDL_String directory("");
mPlug->GetGUI()->PromptForFile(&fileName, kFileOpen, &directory, "txt fxp");
if (fileName.GetLength() > 0)
{
if (strcmp(fileName.get_fileext(), ".fxp") == 0)
{
mPlug->LoadProgramFromFXP(&fileName);
mInterface->SetProgramName(fileName.get_filepart());
}
else
{
FILE* fp = fopen(fileName.Get(), "rb");
if (fp)
{
long fileSize;
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
rewind(fp);
char* contents = new char[fileSize + 1];
fread(contents, fileSize, 1, fp);
fclose(fp);
contents[fileSize] = 0;
mInterface->SetProgramName(fileName.get_filepart());
mInterface->SetProgramText(contents);
// note: for some reason this doesn't set the Reaper project as modified in this case.
mPlug->DirtyParameters();
delete[] contents;
}
}
}
}
}
break;
}
}
void LoadButton::OnMouseOver(int x, int y, IMouseMod* pMod)
{
if (mState == kOpen)
{
mSelection = -1;
for (int i = 0; i < mRECTs.GetSize(); ++i)
{
if (mRECTs.Get()[i].Contains(x, y))
{
mSelection = i;
}
}
}
}
#pragma endregion LoadButton
#pragma region SaveButton
static char* kSaveText = "SAVE...";
SaveButton::SaveButton(IPlugBase* pPlug, int x, int y, IBitmap* pButtonBack, IText* pButtonTextStyle, Interface* pInterface)
: IBitmapControl(pPlug, x, y, -1, pButtonBack)
, mButtonText(*pButtonTextStyle)
, mTextRect(mRECT)
, mInterface(pInterface)
{
mInterface->GetGUI()->MeasureIText(&mButtonText, kSaveText, &mTextRect);
mTextRect.T += (mRECT.H() - mTextRect.H()) / 2;
}
bool SaveButton::Draw(IGraphics* pGraphics)
{
pGraphics->DrawBitmap(&mBitmap, &mRECT, 1, &mBlend);
pGraphics->DrawIText(&mButtonText, kSaveText, &mTextRect);
return true;
}
void SaveButton::OnMouseDown(int x, int y, IMouseMod* pMod)
{
WDL_String fileName("");
WDL_String directory("");
mPlug->GetGUI()->PromptForFile(&fileName, kFileSave, &directory, "fxp");
if (fileName.GetLength() > 0)
{
mPlug->SaveProgramAsFXP(&fileName);
}
}
#pragma endregion SaveButton
#pragma region ManualButton
static char* kManualText = "MANUAL";
ManualButton::ManualButton(IPlugBase* pPlug, int x, int y, IBitmap* pButtonBack, IText* pButtonTextStyle, Interface* pInterface)
: IBitmapControl(pPlug, x, y, -1, pButtonBack)
, mButtonText(*pButtonTextStyle)
, mTextRect(mRECT)
, mInterface(pInterface)
{
mInterface->GetGUI()->MeasureIText(&mButtonText, kManualText, &mTextRect);
mTextRect.T += (mRECT.H() - mTextRect.H()) / 2;
}
bool ManualButton::Draw(IGraphics* pGraphics)
{
pGraphics->DrawBitmap(&mBitmap, &mRECT, 1, &mBlend);
pGraphics->DrawIText(&mButtonText, kManualText, &mTextRect);
return true;
}
void ManualButton::OnMouseDown(int x, int y, IMouseMod* pMod)
{
WDL_String fileName("");
bool success = false;
// check in the same folder as the app first because
// this is where it will be if installed by Itch
// or if the user downloaded the standalone directly.
#if SA_API
mPlug->GetGUI()->HostPath(&fileName);
fileName.Append("/Evaluator_manual.pdf");
success = mPlug->GetGUI()->OpenURL(fileName.Get());
#endif
// if we didn't find it, check in the support path,
// which is where it will be if this was installed
// by the installer.
if ( !success )
{
success = mInterface->GetSupportPath(&fileName);
if (success)
{
fileName.Append("/Evaluator_manual.pdf");
success = mPlug->GetGUI()->OpenURL(fileName.Get());
}
}
if ( !success )
{
static char msg[256];
sprintf(msg, "Sorry, couldn't find the manual at %s.", fileName.Get());
mPlug->GetGUI()->ShowMessageBox(msg, "Error", MB_OK);
}
}
#pragma endregion ManualButton
#pragma region HelpButton
static char* kHelpText = "?";
HelpButton::HelpButton(IPlugBase* pPlug, IRECT rect, IText* pButtonTextStyle, Interface* pInterface)
: IControl(pPlug, rect)
, mButtonText(*pButtonTextStyle)
, mTextRect(mRECT)
, mInterface(pInterface)
{
mInterface->GetGUI()->MeasureIText(&mButtonText, kHelpText, &mTextRect);
mTextRect.T += (mRECT.H() - mTextRect.H()) / 2;
// haxxxxx
#if defined(OS_OSX)
mTextRect.T += 1;
#endif
}
bool HelpButton::Draw(IGraphics* pGraphics)
{
if ( mValue )
{
pGraphics->FillIRect(&mButtonText.mTextEntryFGColor, &mRECT);
pGraphics->DrawRect(&mButtonText.mTextEntryBGColor, &mRECT);
}
else
{
pGraphics->FillIRect(&mButtonText.mTextEntryBGColor, &mRECT);
pGraphics->DrawRect(&mButtonText.mTextEntryFGColor, &mRECT);
}
pGraphics->DrawIText(&mButtonText, kHelpText, &mTextRect);
return true;
}
void HelpButton::OnMouseDown(int x, int y, IMouseMod* pMod)
{
mInterface->ToggleHelp();
mValue = 1 - mValue;
}
#pragma endregion HelpButton
#pragma region TransportButtons
TransportButtons::TransportButtons(IPlugBase* pPlug, IRECT rect, const IColor& backgroundColor, const IColor& foregroundColor)
: IControl(pPlug, rect)
, mState(kTransportStopped)
, mBack(backgroundColor)
, mFore(foregroundColor)
{
mDblAsSingleClick = true;
mPlayRect = rect.SubRectHorizontal(3, 0);
mPauseRect = rect.SubRectHorizontal(3, 1);
mStopRect = rect.SubRectHorizontal(3, 2);
}
bool TransportButtons::Draw(IGraphics* pGraphics)
{
// play button
{
const bool active = (mState == kTransportPlaying || mState == kTransportPaused);
const int cx = (int)mPlayRect.MW();
const int cy = (int)mPlayRect.MH();
IRECT back = mPlayRect.GetPadded(-1);
pGraphics->FillIRect(active ? &mFore : &mBack, &back);
pGraphics->FillTriangle(active ? &mBack : &mFore,
cx - 6, cy - 5,
cx - 6, cy + 5,
cx + 8, cy,
&mBlend
);
}
// pause button
{
bool active = mState == kTransportPaused;
IRECT back = mPauseRect.GetPadded(-1);
pGraphics->FillIRect(active ? &mFore : &mBack, &back);
const IColor* color = active ? &mBack : &mFore;
const int cx = (int)mPauseRect.MW();
const int cy = (int)mPauseRect.MH();
const int w = 4;
const int h = 6;
IRECT slab(cx - (int)(w*1.5f), cy - h, cx - (int)(w*0.5f), cy + h);
pGraphics->FillIRect(color, &slab, &mBlend);
slab.L += w*2;
slab.R += w*2;
pGraphics->FillIRect(color, &slab, &mBlend);
}
// stop button
{
const int cx = (int)mStopRect.MW();
const int cy = (int)mStopRect.MH();
const int r = 6;
IRECT back = mStopRect.GetPadded(-1);
IRECT button(cx - r, cy - r, cx + r, cy + r);
pGraphics->FillIRect(&mBack, &back);
pGraphics->FillIRect(&mFore, &button);
}
return true;
}
void TransportButtons::OnMouseDown(int x, int y, IMouseMod* pMod)
{
if (mPlayRect.Contains(x, y))
{
// if pause we stop and then start
// so that tick will be reset to zero.
// this matches transport behavior in reaper
// where pressing the play button while paused
// will cause it to start playing from the timeline marker
// rather than resuming from the current playhead position
if ( mState == kTransportPaused )
{
SetTransportState(kTransportStopped);
}
SetTransportState(kTransportPlaying);
}
else if (mPauseRect.Contains(x, y))
{
if ( mState == kTransportPaused )
{
SetTransportState(kTransportPlaying);
}
else
{
SetTransportState(kTransportPaused);
}
}
else if (mStopRect.Contains(x, y))
{
SetTransportState(kTransportStopped);
}
}
void TransportButtons::SetTransportState( TransportState state )
{
mState = state;
SetDirty(false);
mPlug->OnParamChange(kTransportState);
}
TransportState TransportButtons::GetTransportState() const
{
return mState;
}
#pragma endregion TransportButtons
#pragma region ToggleControl
ToggleControl::ToggleControl(IPlugBase* pPlug, IRECT rect, int paramIdx, IColor backgroundColor, IColor fillColor)
: IControl(pPlug, rect, paramIdx)
{
mText.mTextEntryBGColor = backgroundColor;
mText.mTextEntryFGColor = fillColor;
}
bool ToggleControl::Draw(IGraphics* pGraphics)
{
pGraphics->FillIRect(&mText.mTextEntryBGColor, &mRECT);
pGraphics->DrawRect(&mText.mTextEntryFGColor, &mRECT);
if (mValue)
{
IRECT fill = mRECT.GetPadded(-2);
fill.T += 1;
fill.L += 1;
pGraphics->FillIRect(&mText.mTextEntryFGColor, &fill);
}
return true;
}
void ToggleControl::OnMouseDown(int x, int y, IMouseMod* pMod)
{
mValue = 1 - mValue;
SetDirty();
}
#pragma endregion ToggleControl
#pragma region MidiControl
int KeyToNote(int key)
{
int note = -1;
if (key >= KEY_DIGIT_0 && key <= KEY_DIGIT_9)
{
key = '0' + key - KEY_DIGIT_0;
}
else if (key >= KEY_ALPHA_A && key <= KEY_ALPHA_Z)
{
key += 50;
}
switch (key)
{
case 'Z': note = 48; break;
case 'S': note = 49; break;
case 'X': note = 50; break;
case 'D': note = 51; break;
case 'C': note = 52; break;
case 'V': note = 53; break;
case 'G': note = 54; break;
case 'B': note = 55; break;
case 'H': note = 56; break;
case 'N': note = 57; break;
case 'J': note = 58; break;
case 'M': note = 59; break;
case 'K': note = 60; break;
case 'Q': note = 60; break;
case '2': note = 61; break;
case 'W': note = 62; break;
case '3': note = 63; break;
case 'E': note = 64; break;
case 'R': note = 65; break;
case '5': note = 66; break;
case 'T': note = 67; break;
case '6': note = 68; break;
case 'Y': note = 69; break;
case '7': note = 70; break;
case 'U': note = 71; break;
case 'I': note = 72; break;
case '9': note = 73; break;
case 'O': note = 74; break;
case '0': note = 75; break;
case 'P': note = 76; break;
default:
break;
}
return note;
}
MidiControl::MidiControl(IPlugBase* pPlug)
: IControl(pPlug, IRECT(0,0,0,0))
{
}
bool MidiControl::OnKeyDown(int x, int y, int key)
{
const int note = KeyToNote(key);
const bool handled = note != -1;
if (handled)
{
IMidiMsg msg;
msg.MakeNoteOnMsg(note, 127, 0);
mPlug->ProcessMidiMsg(&msg);
msg.MakeNoteOffMsg(note, (int)mPlug->GetSampleRate()/8);
mPlug->ProcessMidiMsg(&msg);
}
return handled;
}
#pragma endregion MidiControl