-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
bgraknob.pas
943 lines (775 loc) · 25.7 KB
/
bgraknob.pas
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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{
Initially written by Circular.
}
{******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with delphi VCL 11/2018)
- Sandy Ganz | [email protected]
(added range, sector, and other features)
***************************** END CONTRIBUTOR(S) *****************************}
unit BGRAKnob;
{$I bgracontrols.inc}
interface
uses
Classes, SysUtils, {$IFDEF FPC}LResources,{$ENDIF} Forms, Controls, Graphics,
{$IFNDEF FPC}BGRAGraphics, GraphType, FPImage, {$ENDIF}
BCBaseCtrls, BGRAGradients, BGRABitmap, BGRABitmapTypes;
type
TBGRAKnobPositionType = (kptLineSquareCap, kptLineRoundCap, kptFilledCircle,
kptHollowCircle);
TKnobType = (ktRange, ktSector);
TBGRAKnobValueChangedEvent = procedure(Sender: TObject; Value: single) of object;
{ TBGRAKnob }
TBGRAKnob = class(TBGRAGraphicCtrl)
private
{ Private declarations }
FPhong: TPhongShading;
FCurveExponent: single;
FKnobBmp: TBGRABitmap;
FKnobColor: TColor;
FAngularPos: single; // In RADIANS
FPositionColor: TColor;
FPositionMargin: single;
FPositionOpacity: byte;
FPositionType: TBGRAKnobPositionType;
FPositionWidth: single;
FSettingAngularPos: boolean;
FUsePhongLighting: boolean;
FMinValue, FMaxValue: single; // Knob Values
FStartAngle, FEndAngle: single; // Knob Angles
FKnobType: TKnobType;
FOnKnobValueChange: TBGRAKnobValueChangedEvent;
FStartFromBottom: boolean;
FWheelSpeed: byte; // 0 : no wheel, 1 slowest, 255 fastest
FWheelWrap: boolean;
FSlowSnap: boolean;
FReverseScale: boolean;
FSectorDivisions: integer; // Computed internally from FMinValue/FMaxValue
procedure CreateKnobBmp;
function GetLightIntensity: integer;
function GetValue: single;
function AngularPosToDeg(RadPos: single): single;
function DegPosToAngular(DegPos: single): single;
procedure SetCurveExponent(const AValue: single);
procedure SetLightIntensity(const AValue: integer);
procedure SetStartFromBottom(const AValue: boolean);
procedure SetValue(AValue: single);
procedure SetMaxValue(AValue: single);
procedure SetMinValue(AValue: single);
procedure SetStartAngle(AValue: single);
procedure SetEndAngle(AValue: single);
procedure SetKnobType(const AValue: TKnobType);
procedure SetPositionColor(const AValue: TColor);
procedure SetPositionMargin(AValue: single);
procedure SetPositionOpacity(const AValue: byte);
procedure SetPositionType(const AValue: TBGRAKnobPositionType);
procedure SetPositionWidth(const AValue: single);
procedure SetUsePhongLighting(const AValue: boolean);
procedure UpdateAngularPos(X, Y: integer);
procedure SetKnobColor(const AValue: TColor);
procedure SetWheelSpeed(AValue: byte);
procedure SetReverseScale(AValue: boolean);
protected
{ Protected declarations }
class function GetControlClassDefaultSize: TSize; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
procedure MouseMove(Shift: TShiftState; X, Y: integer); override;
procedure Paint; override;
procedure Resize; override;
function ValueCorrection(var AValue: single): boolean; overload; virtual;
function ValueCorrection: boolean; overload; virtual;
function DoMouseWheel(Shift: TShiftState; WheelDelta: integer; MousePos: TPoint): boolean; override;
procedure MouseWheelPos({%H-}Shift: TShiftState; WheelDelta: integer); virtual;
function RemapRange(OldValue: single; OldMin, OldMax, NewMin, NewMax: single): single;
function AngularPosSector(AValue: single): single;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
public
{ Streaming }
{$IFDEF FPC}
procedure SaveToFile(AFileName: string);
procedure LoadFromFile(AFileName: string);
{$ENDIF}
procedure OnFindClass({%H-}Reader: TReader; const AClassName: string;
var ComponentClass: TComponentClass);
published
{ Published declarations }
property Anchors;
property CurveExponent: single read FCurveExponent write SetCurveExponent nodefault;
property KnobColor: TColor read FKnobColor write SetKnobColor default clBtnFace;
property LightIntensity: integer read GetLightIntensity write SetLightIntensity default 300;
property PositionColor: TColor read FPositionColor write SetPositionColor default clBtnText;
property PositionWidth: single read FPositionWidth write SetPositionWidth default 4;
property PositionOpacity: byte read FPositionOpacity write SetPositionOpacity default 192;
property PositionMargin: single read FPositionMargin write SetPositionMargin default 4;
property PositionType: TBGRAKnobPositionType
read FPositionType write SetPositionType default kptLineSquareCap;
property UsePhongLighting: boolean read FUsePhongLighting write SetUsePhongLighting default true;
property MinValue: single read FMinValue write SetMinValue nodefault;
property MaxValue: single read FMaxValue write SetMaxValue nodefault;
property StartFromBottom: boolean read FStartFromBottom write SetStartFromBottom default true;
property StartAngle: single read FStartAngle write SetStartAngle default 30;
property EndAngle: single read FEndAngle write SetEndAngle default 330;
property KnobType: TKnobType read FKnobType write SetKnobType default ktRange;
property Value: single read GetValue write SetValue nodefault;
property OnValueChanged: TBGRAKnobValueChangedEvent
read FOnKnobValueChange write FOnKnobValueChange;
property WheelSpeed: byte read FWheelSpeed write SetWheelSpeed default 0;
property WheelWrap: boolean read FWheelWrap write FWheelWrap default false;
property SlowSnap: boolean read FSlowSnap write FSlowSnap default false;
property ReverseScale: boolean read FReverseScale write SetReverseScale default false;
property OnMouseWheel;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseUp;
property OnMouseMove;
property OnMouseEnter;
property OnMouseLeave;
end;
{$IFDEF FPC}
procedure Register;
{$ENDIF}
const
VERSIONSTR = '2.11'; // knob version
implementation
uses Math;
const
WHEELSPEEDFACTOR = 20.0; // used to calculate mouse wheel speed
WHEELSPEEDBASE = 300;
{$IFDEF FPC}
procedure Register;
begin
RegisterComponents('BGRA Controls', [TBGRAKnob]);
end;
{$ENDIF}
{ TBGRAKnob }
// Override the base class which has a rectangular dimension, odd for a knob
class function TBGRAKnob.GetControlClassDefaultSize: TSize;
begin
Result.CX := 50;
Result.CY := 50;
end;
procedure TBGRAKnob.CreateKnobBmp;
var
tx, ty: integer;
h: single;
d2: single;
v: TPointF;
p: PBGRAPixel;
center: TPointF;
yb: integer;
xb: integer;
mask: TBGRABitmap;
Map: TBGRABitmap;
BGRAKnobColor: TBGRAPixel;
begin
tx := ClientWidth;
ty := ClientHeight;
if (tx = 0) or (ty = 0) then
exit;
FreeAndNil(FKnobBmp);
FKnobBmp := TBGRABitmap.Create(tx, ty);
center := PointF((tx - 1) / 2, (ty - 1) / 2);
BGRAKnobColor := KnobColor;
if UsePhongLighting then
begin
//compute knob height map
Map := TBGRABitmap.Create(tx, ty);
for yb := 0 to ty - 1 do
begin
p := map.ScanLine[yb];
for xb := 0 to tx - 1 do
begin
//compute vector between center and current pixel
v := PointF(xb, yb) - center;
//scale down to unit circle (with 1 pixel margin for soft border)
v.x := v.x / (tx / 2 + 1);
v.y := v.y / (ty / 2 + 1);
//compute squared distance with scalar product
d2 := v {$if FPC_FULLVERSION < 30203}*{$ELSE}**{$ENDIF} v;
//interpolate as quadratic curve and apply power function
if d2 > 1 then
h := 0
else
h := power(1 - d2, FCurveExponent);
p^ := MapHeightToBGRA(h, 255);
Inc(p);
end;
end;
//antialiased border
mask := TBGRABitmap.Create(tx, ty, BGRABlack);
Mask.FillEllipseAntialias(center.x, center.y, tx / 2, ty / 2, BGRAWhite);
map.ApplyMask(mask);
Mask.Free;
FPhong.Draw(FKnobBmp, Map, 30, 0, 0, BGRAKnobColor);
Map.Free;
end
else
begin
FKnobBmp.FillEllipseAntialias(center.x, center.y, tx / 2, ty / 2, BGRAKnobColor);
end;
end;
function TBGRAKnob.GetLightIntensity: integer;
begin
Result := round(FPhong.LightSourceIntensity);
end;
function TBGRAKnob.GetValue: single;
begin
// Maintains the correct value range based on knobtype, result in terms of
// FMinValue and FMaxValue
Result := RemapRange(AngularPosToDeg(FAngularPos), FStartAngle,
FEndAngle, FMinValue, FMaxValue);
// Check to Reverse the scale and fix value
if FReverseScale then
Result := FMaxValue + FMinValue - Result;
if FKnobType = ktSector then
Result := Round(Result);
end;
function TBGRAKnob.AngularPosToDeg(RadPos: single): single;
begin
// helper to convert AnglePos in radians to degrees, wraps as needed
Result := RadPos * 180 / Pi;
if Result < 0 then
Result := Result + 360;
Result := 270 - Result;
if Result < 0 then
Result := Result + 360;
end;
function TBGRAKnob.DegPosToAngular(DegPos: single): single;
begin
// helper to convert Angle in degrees to radians
Result := 3 * Pi / 2 - DegPos * Pi / 180;
if Result > Pi then
Result := Result - (2 * Pi);
if Result < -Pi then
Result := Result + (2 * Pi);
end;
function TBGRAKnob.AngularPosSector(AValue: single): single;
var
sector: integer;
begin
// AValue is the degree angle of FAngularPos of where the mouse is
// typically. So no restrictions on values, 0 to < 360
if AValue > FEndAngle then
Avalue := FEndAngle;
if AValue < FStartAngle then
Avalue := FStartAngle;
// from the current angular pos get the value
sector := Round(RemapRange(AValue, FStartAngle, FEndAngle, FMinValue, FMaxValue));
// now get back the FAngularPos after mapping
Result := DegPosToAngular(RemapRange(sector, FMinValue, FMaxValue, FStartAngle, FEndAngle));
end;
function TBGRAKnob.ValueCorrection(var AValue: single): boolean;
begin
if AValue < FStartAngle then
begin
AValue := FStartAngle;
Result := True;
end
else
if AValue > FEndAngle then
begin
AValue := FEndAngle;
Result := True;
end
else
Result := False;
end;
function TBGRAKnob.ValueCorrection: boolean;
var
LValue: single;
begin
LValue := AngularPosToDeg(FAngularPos);
// this always needs to be in Degrees of position (NOT VALUE)
Result := ValueCorrection(LValue); // LValue modified by call
if Result then
FAngularPos := DegPosToAngular(LValue); // Back to Radians
end;
function TBGRAKnob.RemapRange(OldValue: single;
OldMin, OldMax, NewMin, NewMax: single): single;
begin
// Generic mapping of ranges. Value is the number to remap, returns number
// in the new range. Looks for odd div by 0 condition and fixes
if OldMin = OldMax then
begin
if OldValue <= OldMin then
exit(NewMin)
else
exit(NewMax);
end;
Result := (((OldValue - OldMin) * (NewMax - NewMin)) / (OldMax - OldMin)) + NewMin;
end;
procedure TBGRAKnob.SetCurveExponent(const AValue: single);
begin
if FCurveExponent = AValue then
exit;
FCurveExponent := AValue;
FreeAndNil(FKnobBmp);
Invalidate;
end;
procedure TBGRAKnob.SetKnobColor(const AValue: TColor);
begin
if FKnobColor = AValue then
exit;
FKnobColor := AValue;
FreeAndNil(FKnobBmp);
Invalidate;
end;
procedure TBGRAKnob.SetWheelSpeed(AValue: byte);
begin
// Sets the mouse wheel speed
FWheelSpeed := AValue;
end;
procedure TBGRAKnob.SetReverseScale(AValue: boolean);
var
oldVal: single;
begin
// Sets the direction of the scale
if FReverseScale = AValue then
exit;
oldVal := GetValue;
FReverseScale := AValue;
SetValue(oldVal);
end;
procedure TBGRAKnob.SetLightIntensity(const AValue: integer);
begin
if AValue <> FPhong.LightSourceIntensity then
begin
FPhong.LightSourceIntensity := AValue;
FreeAndNil(FKnobBmp);
Invalidate;
end;
end;
procedure TBGRAKnob.SetStartFromBottom(const AValue: boolean);
begin
if FStartFromBottom = AValue then
exit;
FStartFromBottom := AValue;
Invalidate;
end;
procedure TBGRAKnob.SetValue(AValue: single);
var
NewAngularPos: single;
begin
// AValue in the range of FStartAngle and FEndAngles after the mapping
if AValue > FMaxValue then
AValue := FMaxValue;
if AValue < FMinValue then
AValue := FMinValue;
// Get the integeral value from given sector,
if FKnobType = ktSector then
AValue := Round(AValue); // Round to sector
AValue := RemapRange(AValue, FMinValue, FMaxValue, FStartAngle, FEndAngle);
// Reverse the scale if needed
if FReverseScale then
AValue := FEndAngle + FStartAngle - AValue;
ValueCorrection(AValue);
NewAngularPos := 3 * Pi / 2 - AValue * Pi / 180;
if NewAngularPos > Pi then
NewAngularPos := NewAngularPos - (2 * Pi);
if NewAngularPos < -Pi then
NewAngularPos := NewAngularPos + (2 * Pi);
if NewAngularPos <> FAngularPos then
begin
FAngularPos := NewAngularPos;
Invalidate;
end;
end;
procedure TBGRAKnob.SetEndAngle(AValue: single);
var
oldValue: single;
begin
// degrees for position of start position
if (FEndAngle = AValue) or (FStartAngle >= AValue) or (AValue < 0) or
(AValue >= 360) then
exit;
// If we are going to change the angle, we need to save off the current value
// as it will change it if we don't reset it
oldValue := GetValue;
FEndAngle := AValue;
if FStartAngle > FEndAngle then
FStartAngle := FEndAngle;
SetValue(oldValue); // Invalidate the hard way, preserve Value for user
end;
procedure TBGRAKnob.SetStartAngle(AValue: single);
var
oldValue: single;
begin
// Start angle in degrees
if (FStartAngle = AValue) or (FEndAngle <= AValue) or (AValue < 0) or
(AValue >= 360) then
exit;
oldValue := GetValue;
FStartAngle := AValue;
if FEndAngle < FStartAngle then
FEndAngle := FStartAngle;
SetValue(oldValue); // Invalidate the hard way, preserve Value for user
end;
procedure TBGRAKnob.SetMaxValue(AValue: single);
var
oldValue: single;
IntMinVal, IntMaxVal: integer;
begin
// Note : MinValue and MaxValue can span negative ranges and be increasing
// decreasing
// If sector mode do some math, set number of sector divisions
if FKnobType = ktSector then
begin
IntMinVal := Round(FMinValue);
IntMaxVal := Round(AValue);
FSectorDivisions := IntMaxVal - IntMinVal;
// Just to be safe, ensure at least 1 sector division
if FSectorDivisions < 1 then
FSectorDivisions := 1;
FMinValue := IntMinVal; // force to an integeral value if in sector mode
AValue := IntMaxVal;
end;
// Min and Max Can't be the same in any case
if (FMinValue >= AValue) then
exit;
oldValue := GetValue;
FMaxValue := AValue;
SetValue(oldValue);
end;
procedure TBGRAKnob.SetMinValue(AValue: single);
var
oldValue: single;
IntMinVal, IntMaxVal: integer;
begin
// Note : MinValue and MaxValue can span negative ranges and be increasing
// decreasing
// If sector mode do some math, set number of sector divisions
if FKnobType = ktSector then
begin
IntMinVal := Round(AValue);
IntMaxVal := Round(FMaxValue);
FSectorDivisions := IntMaxVal - IntMinVal;
// Just to be safe, ensure at least 1 sector division
if FSectorDivisions < 1 then
FSectorDivisions := 1;
FMaxValue := IntMaxVal; // force to an integeral value if in sector mode
AValue := IntMinVal;
end;
// Min and Max Can't be the same in any case, rounding can also cause this
if (FMaxValue <= AValue) then
exit;
// Save and refresh with proper value
oldValue := GetValue;
FMinValue := AValue;
SetValue(oldValue);
end;
procedure TBGRAKnob.SetKnobType(const AValue: TKnobType);
var
IntMinVal, IntMaxVal: integer;
begin
// Set the knobtype, if ktRange nothing really needed,
// if ktSector then calc and check value for divisions.
FKnobType := AValue;
if FKnobType = ktSector then
begin
IntMinVal := Round(FMinValue);
IntMaxVal := Round(FMaxValue);
FSectorDivisions := IntMaxVal - IntMinVal;
if FSectorDivisions < 1 then
FSectorDivisions := 1;
end;
// No other changes for ktRange mode
end;
procedure TBGRAKnob.SetPositionColor(const AValue: TColor);
begin
if FPositionColor = AValue then
exit;
FPositionColor := AValue;
Invalidate;
end;
procedure TBGRAKnob.SetPositionMargin(AValue: single);
begin
if FPositionMargin = AValue then
exit;
FPositionMargin := AValue;
Invalidate;
end;
procedure TBGRAKnob.SetPositionOpacity(const AValue: byte);
begin
if FPositionOpacity = AValue then
exit;
FPositionOpacity := AValue;
Invalidate;
end;
procedure TBGRAKnob.SetPositionType(const AValue: TBGRAKnobPositionType);
begin
if FPositionType = AValue then
exit;
FPositionType := AValue;
Invalidate;
end;
procedure TBGRAKnob.SetPositionWidth(const AValue: single);
begin
if FPositionWidth = AValue then
exit;
FPositionWidth := AValue;
Invalidate;
end;
procedure TBGRAKnob.SetUsePhongLighting(const AValue: boolean);
begin
if FUsePhongLighting = AValue then
exit;
FUsePhongLighting := AValue;
FreeAndNil(FKnobBmp);
Invalidate;
end;
procedure TBGRAKnob.UpdateAngularPos(X, Y: integer);
var
FPreviousPos, Sign: single;
prevAngle, currAngle: single;
begin
// Saves a previous position for the SlowSnap functionality.
// Uses that to see how far we have moved to see if we should move
FPreviousPos := FAngularPos;
prevAngle := AngularPosToDeg(FAngularPos); // Need these in degrees!
if FStartFromBottom then
Sign := 1
else
Sign := -1;
FAngularPos := ArcTan2((-Sign) * (Y - ClientHeight / 2) / ClientHeight,
Sign * (X - ClientWidth / 2) / ClientWidth);
currAngle := AngularPosToDeg(FAngularPos);
// If sector mode then we need to translate angle to sector and back to simulate each sector
if FKnobType = ktSector then
FAngularPos := AngularPosSector(currAngle);
ValueCorrection;
// If SlowSnap mode make sure past the Min/Max angles before snapping.
// This is less twitchy behavior near endpoints. This may not make sense
// when in ktSector mode so skip if that
if FSlowSnap and (FKnobType <> ktSector) then
if ((currAngle <= FStartAngle) and (prevAngle = FEndAngle)) or
((CurrAngle >= FEndAngle) and (PrevAngle = FStartAngle)) then
FAngularPos := FPreviousPos;
Invalidate;
if (FPreviousPos <> FAngularPos) and Assigned(FOnKnobValueChange) then
FOnKnobValueChange(Self, Value); // Value passes back with data based on knobtype
end;
procedure TBGRAKnob.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: integer);
begin
inherited MouseDown(Button, Shift, X, Y);
if Button = mbLeft then
begin
FSettingAngularPos := True;
UpdateAngularPos(X, Y);
end;
end;
procedure TBGRAKnob.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer);
begin
inherited MouseUp(Button, Shift, X, Y);
if Button = mbLeft then
FSettingAngularPos := False;
end;
procedure TBGRAKnob.MouseMove(Shift: TShiftState; X, Y: integer);
begin
inherited MouseMove(Shift, X, Y);
if FSettingAngularPos then
UpdateAngularPos(X, Y);
end;
procedure TBGRAKnob.Paint;
var
Bmp: TBGRABitmap;
Center, Pos: TPointF;
PosColor: TBGRAPixel;
PosLen: single;
begin
if (ClientWidth = 0) or (ClientHeight = 0) then
exit;
if FKnobBmp = nil then
begin
CreateKnobBmp;
if FKnobBmp = nil then
Exit;
end;
Bmp := TBGRABitmap.Create(ClientWidth, ClientHeight);
Bmp.BlendImage(0, 0, FKnobBmp, boLinearBlend);
// draw current position
PosColor := ColorToBGRA(ColorToRGB(FPositionColor), FPositionOpacity);
Center := PointF(ClientWidth / 2, ClientHeight / 2);
Pos.X := Cos(FAngularPos) * (ClientWidth / 2);
Pos.Y := -Sin(FAngularPos) * (ClientHeight / 2);
if not FStartFromBottom then
Pos := -Pos;
PosLen := VectLen(Pos);
Pos := Pos * ((PosLen - PositionMargin - FPositionWidth) / PosLen);
Pos := Center + Pos;
case PositionType of
kptLineSquareCap:
begin
Bmp.LineCap := pecSquare;
Bmp.DrawLineAntialias(Center.X, Center.Y, Pos.X, Pos.Y,
PosColor, FPositionWidth);
end;
kptLineRoundCap:
begin
Bmp.LineCap := pecRound;
Bmp.DrawLineAntialias(Center.X, Center.Y, Pos.X, Pos.Y,
PosColor, FPositionWidth);
end;
kptFilledCircle:
begin
Bmp.FillEllipseAntialias(Pos.X, Pos.Y, FPositionWidth,
FPositionWidth, PosColor);
end;
kptHollowCircle:
begin
Bmp.EllipseAntialias(Pos.X, Pos.Y, FPositionWidth * 2 / 3,
FPositionWidth * 2 / 3, PosColor, FPositionWidth / 3);
end;
end;
Bmp.Draw(Canvas, 0, 0, False);
Bmp.Free;
end;
procedure TBGRAKnob.Resize;
begin
inherited Resize;
if (FKnobBmp <> nil) and ((ClientWidth <> FKnobBmp.Width) or
(ClientHeight <> FKnobBmp.Height)) then
FreeAndNil(FKnobBmp);
end;
constructor TBGRAKnob.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
FPhong := TPhongShading.Create;
FPhong.LightPositionZ := 100;
FPhong.LightSourceIntensity := 300;
FPhong.NegativeDiffusionFactor := 0.8;
FPhong.AmbientFactor := 0.5;
FPhong.DiffusionFactor := 0.6;
FKnobBmp := nil;
FCurveExponent := 0.2;
FKnobColor := clBtnFace;
FPositionColor := clBtnText;
FPositionOpacity := 192;
FPositionWidth := 4;
FPositionMargin := 4;
FPositionType := kptLineSquareCap;
FUsePhongLighting := True;
FOnKnobValueChange := nil;
FStartFromBottom := True;
FWheelSpeed := 0; // 0, no wheel, 1 slowest, 255 fastest
FWheelWrap := False; // don't allow the mouse wheel to wrap around
FSlowSnap := False; // True : less snap around on min/max
FReverseScale := False; // Flips direction around if True
FSectorDivisions := 1; // Number of divisions for sector knob, computed
FKnobType := ktRange; // Defaults ranges to match orig knob
FStartAngle := 30;
FEndAngle := 330;
FMinValue := 30;
FMaxValue := 330;
SetValue(30);
end;
destructor TBGRAKnob.Destroy;
begin
FPhong.Free;
FKnobBmp.Free;
inherited Destroy;
end;
{$IFDEF FPC}
procedure TBGRAKnob.SaveToFile(AFileName: string);
var
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
try
WriteComponentAsTextToStream(AStream, Self);
AStream.SaveToFile(AFileName);
finally
AStream.Free;
end;
end;
procedure TBGRAKnob.LoadFromFile(AFileName: string);
var
AStream: TMemoryStream;
begin
AStream := TMemoryStream.Create;
try
AStream.LoadFromFile(AFileName);
ReadComponentFromTextStream(AStream, TComponent(Self), OnFindClass);
finally
AStream.Free;
end;
end;
{$ENDIF}
procedure TBGRAKnob.OnFindClass(Reader: TReader; const AClassName: string;
var ComponentClass: TComponentClass);
begin
if CompareText(AClassName, 'TBGRAKnob') = 0 then
ComponentClass := TBGRAKnob;
end;
function TBGRAKnob.DoMouseWheel(Shift: TShiftState; WheelDelta: integer;
MousePos: TPoint): boolean;
begin
Result := inherited DoMouseWheel(Shift, WheelDelta, MousePos);
MouseWheelPos(Shift, WheelDelta);
end;
procedure TBGRAKnob.MouseWheelPos(Shift: TShiftState; WheelDelta: integer);
var
newValue: single;
begin
// WheelSpeed is a Base Value and a factor to slow or speed up the wheel affect.
// FWheelSpeed = 0 then no wheel, 1 slowest movement, 255 fastest movement
if FWheelSpeed > 0 then
begin
if FKnobType = ktRange then
begin
newValue := Value + (FMaxValue - FMinValue) * WheelDelta /
((WHEELSPEEDBASE - FWheelSpeed) * WHEELSPEEDFACTOR);
// Check for wrap in either direction
if FWheelWrap then
begin
if newValue > FMaxValue then
newValue := FMinValue
else
if newValue < FMinValue then
newValue := FMaxValue;
end;
end
else
begin
// ktSector
// Jumps are now always 1 or -1, in terms of sectors, note wheel speed
// does not make any difference in ktSector mode since we can only bump 1/-1
// value or it will rounded back to an integral value an not move
if WheelDelta < 0 then
begin
// Move Backwards, check for wrap
newValue := Value - 1.0;
if newValue < FMinValue then
begin
if FWheelWrap then
newValue := FMaxValue
else
newValue := FMinValue;
end;
end
else
begin
// Move Forward, check for wrap
newValue := Value + 1.0;
if newValue >= FMaxValue then
begin
if FWheelWrap then
newValue := FMinValue
else
newValue := FMaxValue;
end;
end;
end;
SetValue(newValue);
end;
if Assigned(FOnKnobValueChange) then
FOnKnobValueChange(Self, Value);
end;
end.