-
Notifications
You must be signed in to change notification settings - Fork 6
/
untERDLed.pas
415 lines (357 loc) · 11.3 KB
/
untERDLed.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
{
untERDLed v1.0.0 - a simple led in the style of FL-Studio
for Delphi 2010 - 10.4 by Ernst Reidinga
https://erdesigns.eu
This unit is part of the ERDesigns Audio Toolkit Components Pack.
(c) Copyright 2020 Ernst Reidinga <ernst@erdesigns.eu>
Bugfixes / Updates:
- Initial Release 1.0.0
If you use this unit, please give credits to the original author;
Ernst Reidinga.
}
unit untERDLed;
interface
uses
System.SysUtils, System.Classes, Winapi.Windows, Vcl.Controls, Vcl.Graphics,
Winapi.Messages, System.Types, GDIPlus;
type
TERDLedState = (lsOff, lsOn, lsGrayed);
TERDLedToggleEvent = procedure(Sender: TObject; State: TERDLedState) of object;
TERDLed = class(TCustomControl)
private
{ Private declarations }
FBorderWidth : Integer;
FBorderColor : TColor;
FLedOnColor : TColor;
FLedOffColor : TColor;
FGrayedColor : TColor;
FState : TERDLedState;
FToggleOnClick : Boolean;
{ Buffer - Avoid flickering }
FBuffer : TBitmap;
FUpdateRect : TRect;
FRedraw : Boolean;
FOnLedToggle : TERDLedToggleEvent;
procedure SetBorderWidth(const I: Integer);
procedure SetBorderColor(const C: TColor);
procedure SetOnColor(const C: TColor);
procedure SetOffColor(const C: TColor);
procedure SetGrayedColor(const C: TColor);
procedure SetState(const S: TERDLedState);
procedure WMPaint(var Msg: TWMPaint); message WM_PAINT;
procedure WMEraseBkGnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
protected
{ Protected declarations }
procedure Paint; override;
procedure Resize; override;
procedure CreateParams(var Params: TCreateParams); override;
procedure WndProc(var Message: TMessage); override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property UpdateRect: TRect read FUpdateRect write FUpdateRect;
property NeedsRedrawing: Boolean read FRedraw write FRedraw;
published
{ Published declarations }
property BorderWidth: Integer read FBorderWidth write SetBorderWidth default 1;
property BorderColor: TColor read FBorderColor write SetBorderColor default $008F8A87;
property LedOnColor: TColor read FLedOnColor write SetOnColor default $005C86FF;
property LedOffColor: TColor read FLedOffColor write SetOffColor default $008F8A87;
property LedGrayedColor: TColor read FGrayedColor write SetGrayedColor default $006B6860;
property State: TERDLedState read FState write SetState default lsOff;
property ToggleOnClick: Boolean read FToggleOnClick write FToggleOnClick default False;
property OnLedToggle: TERDLedToggleEvent read FOnLedToggle write FOnLedToggle;
property Align;
property Anchors;
property Color;
property Constraints;
property Enabled;
property ParentColor;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Touch;
property Visible;
property OnClick;
property OnEnter;
property OnExit;
property OnGesture;
property OnMouseActivate;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnResize;
end;
implementation
uses System.Math, untERDMidiCommon;
(******************************************************************************)
(*
(* Led (TLed)
(*
(******************************************************************************)
constructor TERDLed.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
{ If the ControlStyle property includes csOpaque, the control paints itself
directly. We dont want the control to accept controls - but this might
change in the future so we leave it here commented out. offcourse we
like to get click, double click and mouse events. }
ControlStyle := ControlStyle + [csOpaque{, csAcceptsControls},
csCaptureMouse, csClickEvents, csDoubleClicks];
{ We dont want to get focus }
TabStop := False;
{ Create Buffers }
FBuffer := TBitmap.Create;
FBuffer.PixelFormat := pf32bit;
{ Width / Height }
Width := 16;
Height := 16;
{ Defaults }
FBorderWidth := 1;
FState := lsOff;
FBorderColor := $008F8A87;
FLedOnColor := $005C86FF;
FLedOffColor := $008F8A87;
FGrayedColor := $006B6860;
{ Draw for the first time }
NeedsRedrawing := True;
end;
destructor TERDLed.Destroy;
begin
{ Free Buffer }
FBuffer.Free;
inherited Destroy;
end;
procedure TERDLed.SetBorderWidth(const I: Integer);
begin
if BorderWidth <> I then
begin
if I < 0 then
FBorderWidth := 0
else
FBorderWidth := I;
NeedsRedrawing := True;
Invalidate;
end;
end;
procedure TERDLed.SetBorderColor(const C: TColor);
begin
if BorderColor <> C then
begin
FBorderColor := C;
NeedsRedrawing := True;
Invalidate;
end;
end;
procedure TERDLed.SetOnColor(const C: TColor);
begin
if LedOnColor <> C then
begin
FLedOnColor := C;
NeedsRedrawing := True;
Invalidate;
end;
end;
procedure TERDLed.SetOffColor(const C: TColor);
begin
if LedOffColor <> C then
begin
FLedOffColor := C;
NeedsRedrawing := True;
Invalidate;
end;
end;
procedure TERDLed.SetGrayedColor(const C: TColor);
begin
if LedGrayedColor <> C then
begin
FGrayedColor := C;
NeedsRedrawing := True;
Invalidate;
end;
end;
procedure TERDLed.SetState(const S: TERDLedState);
begin
if State <> S then
begin
FState := S;
NeedsRedrawing := True;
Invalidate;
end;
end;
procedure TERDLed.WMPaint(var Msg: TWMPaint);
begin
GetUpdateRect(Handle, FUpdateRect, False);
inherited;
end;
procedure TERDLed.WMEraseBkGnd(var Msg: TWMEraseBkgnd);
begin
{ Draw Buffer to the Control }
BitBlt(Msg.DC, 0, 0, ClientWidth, ClientHeight, FBuffer.Canvas.Handle, 0, 0, SRCCOPY);
Msg.Result := -1;
end;
procedure TERDLed.Paint;
var
WorkRect: TRect;
LedColor: TColor;
procedure DrawBackground;
begin
with FBuffer.Canvas do
begin
Brush.Color := Color;
FillRect(ClipRect);
end;
end;
procedure DrawBorder(var FGraphics: IGPGraphics);
var
FBorderBrush: IGPSolidBrush;
begin
{ Create Solid Border Brush }
FBorderBrush := TGPSolidBrush.Create(TGPColor.CreateFromColorRef(FBorderColor));
{ Draw background and border }
FGraphics.FillEllipse(FBorderBrush, TGPRect.Create(WorkRect));
end;
procedure DrawLedBorder(var FGraphics: IGPGraphics);
var
FLedBorderBrush: IGPSolidBrush;
begin
InflateRect(WorkRect, -BorderWidth, -BorderWidth);
{ Create Solid Border Brush }
FLedBorderBrush := TGPSolidBrush.Create(TGPColor.CreateFromColorRef(Darken(LedColor, 50)));
{ Draw background and border }
FGraphics.FillEllipse(FLedBorderBrush, TGPRect.Create(WorkRect));
InflateRect(WorkRect, -1, -1);
{ Create Solid Border Brush }
FLedBorderBrush := TGPSolidBrush.Create(TGPColor.CreateFromColorRef(Darken(LedColor, 20)));
{ Draw background and border }
FGraphics.FillEllipse(FLedBorderBrush, TGPRect.Create(WorkRect));
InflateRect(WorkRect, -1, -1);
end;
procedure DrawLed(var FGraphics: IGPGraphics);
var
FFromColor : TGPColor;
FToColor : TGPColor;
FLedBrush : IGPLinearGradientBrush;
FLightColor : TGPColor;
FLightToColor : TGPColor;
FLedLight : IGPLinearGradientBrush;
FLightRect : TRect;
begin
{ Create colors for gradient led face }
FFromColor := TGPColor.CreateFromColorRef(LedColor);
FToColor := TGPColor.CreateFromColorRef(Brighten(LedColor, 80));
FLedBrush := TGPLinearGradientBrush.Create(TGPRect.Create(WorkRect), FFromColor, FToColor, 90);
FGraphics.FillEllipse(FLedBrush, TGPRect.Create(WorkRect));
{ Create light overlay on the top of the led face }
FLightColor := TGPColor.CreateFromColorRef(Brighten(LedColor, 65));
FLightColor.Alpha := 125;
FLightToColor := TGPColor.CreateFromColorRef(Brighten(LedColor, 20));
FlightColor.Alpha := 125;
FLightRect := WorkRect;
InflateRect(FLightRect, -((Ceil(WorkRect.Width / 5) * 4) - 4), -1);
FLightRect.Height := Ceil(WorkRect.Height / 4);
FLedLight := TGPLinearGradientBrush.Create(TGPRect.Create(FLightRect), FLightColor, FLightToColor, 90);
FGraphics.FillEllipse(FLedLight, TGPRect.Create(FLightRect));
end;
var
FGraphics : IGPGraphics;
var
X, Y, W, H : Integer;
begin
{ Maintain width / height -ratio }
if (csdesigning in componentstate) then
if Height < Width then Height := Width else Width := Height;
{ Set Buffer size }
FBuffer.SetSize(ClientWidth, ClientHeight);
{ Create GDI+ Graphic }
FGraphics := TGPGraphics.Create(FBuffer.Canvas.Handle);
FGraphics.SmoothingMode := SmoothingModeAntiAlias;
FGraphics.InterpolationMode := InterpolationModeHighQualityBicubic;
{ Redraw Buffer }
if NeedsRedrawing then
begin
case State of
lsOff : LedColor := LedOffColor;
lsOn : LedColor := LedOnColor;
lsGrayed : LedColor := LedGrayedColor;
end;
WorkRect := Rect(
ClientRect.Left,
ClientRect.Top,
ClientRect.Right -1,
ClientRect.Bottom - 1
);
NeedsRedrawing := False;
try
DrawBackground;
DrawBorder(FGraphics);
DrawLedBorder(FGraphics);
DrawLed(FGraphics);
except
NeedsRedrawing := True;
end;
end;
{ Now draw the Buffer to the components surface }
X := UpdateRect.Left;
Y := UpdateRect.Top;
W := UpdateRect.Right - UpdateRect.Left;
H := UpdateRect.Bottom - UpdateRect.Top;
if (W <> 0) and (H <> 0) then
{ Only update part - invalidated }
BitBlt(Canvas.Handle, X, Y, W, H, FBuffer.Canvas.Handle, X, Y, SRCCOPY)
else
{ Repaint the whole buffer to the surface }
BitBlt(Canvas.Handle, 0, 0, ClientWidth, ClientHeight, FBuffer.Canvas.Handle, X, Y, SRCCOPY);
inherited;
end;
procedure TERDLed.Resize;
begin
NeedsRedrawing := True;
inherited;
end;
procedure TERDLed.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
with Params do
Style := Style and not (CS_HREDRAW or CS_VREDRAW);
end;
procedure TERDLed.WndProc(var Message: TMessage);
begin
inherited;
case Message.Msg of
// Capture Keystrokes
WM_GETDLGCODE:
Message.Result := Message.Result or DLGC_WANTARROWS or DLGC_WANTALLKEYS;
{ Enabled/Disabled - Redraw }
CM_ENABLEDCHANGED:
begin
if not Enabled then State := lsGrayed;
Invalidate;
end;
{ The color changed }
CM_COLORCHANGED:
begin
NeedsRedrawing := True;
Invalidate;
end;
end;
end;
procedure TERDLed.MouseDown(Button: TMouseButton; Shift: TShiftState; X: Integer; Y: Integer);
begin
if ToggleOnClick then
begin
if State = lsOff then
State := lsOn
else
State := lsOff;
if Assigned(FOnLedToggle) then FOnLedToggle(Self, State);
end;
inherited;
end;
end.