-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
bclealcddisplay.pas
1423 lines (1302 loc) · 44.6 KB
/
bclealcddisplay.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
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in your Lazarus installation
for details about the license.
*****************************************************************************
Based on LCDLine by Yuriy Tereshchenko
Initial Lazarus port and multi-line extension: Boban Spasic ([email protected])
Further optimizations and extensions: Werner Pamler
Published under the name indLCDDisplay as part of the IndustrialStuff package
Conversion to BGRABitmap and published as part of the BComponents package
under the name BLCDDisplay: Boban Spasic 2024
}
unit BCLeaLCDDisplay;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, Controls, fgl, Graphics, LCLIntf,
laz2_dom, laz2_xmlwrite, laz2_xmlread,
BGRABitmapTypes, BGRABitmap, BGRAGradients, BCLeaTheme, BCLeaTypes;
type
TBCLeaLCDDisplay = class;
TDotRow = integer;
TDotRows = array of TDotRow;
TDotMatrixList = specialize TFPGMap<string, TDotRows>;
TBCLeaCharDefs = class(TPersistent)
private
FBCLeaLCDDisplay: TBCLeaLCDDisplay;
FCharList: TDotMatrixList;
FColCount: integer;
FRowCount: integer;
function GetCharByIndex(AIndex: integer): string;
function GetCount: integer;
function GetDotRows(AChar: string): TDotRows;
function GetDotRowsByIndex(AIndex: integer): TDotRows;
procedure SetColCount(AValue: integer);
procedure SetDotRows(AChar: string; const AValue: TDotRows);
procedure SetRowCount(AValue: integer);
function EmptyRows: TDotRows;
procedure ReadCharDefs(Reader: TReader);
procedure WriteCharDefs(Writer: TWriter);
protected
procedure DefineProperties(Filer: TFiler); override;
public
constructor Create(ADisplay: TBCLeaLCDDisplay);
destructor Destroy; override;
procedure Add(AChar: string; const ADotRows: TDotRows);
procedure Assign(ASource: TPersistent); override;
procedure Clear;
procedure Delete(AChar: string);
function DotRowsToString(AChar: string): string;
function Find(const AChar: string): boolean;
procedure LoadFromFile(const AFileName: string);
function SameDotRows(const AChar: string; const ADotRows: TDotRows): boolean;
procedure SaveToFile(const AFileName: string);
property Count: integer read GetCount;
property CharByIndex[AIndex: integer]: string read GetCharByIndex;
property DotRows[AChar: string]: TDotRows read GetDotRows write SetDotRows;
property DotRowsByIndex[AIndex: integer]: TDotRows read GetDotRowsByIndex;
published
property ColCount: integer read FColCount write SetColCount;
property RowCount: integer read FRowCount write SetRowCount;
end;
TBCLeaLCDDisplay = class(TCustomControl)
private
FBitmap: TBGRABitmap;
FTheme: TBCLeaTheme;
{
one char consists of Col x Row of dots
dots have size and space between dots
}
FDotSize: integer; // dot size in pixels
FDotsSpace: integer; // inter-dots space in pixels
FCharCount: integer;
FGlobalDotColsCount: integer;
FCharWidth: integer;
FFrameSize: integer;
FBoardWidth: integer;
FBoardHeight: integer;
FLEDWidth: integer;
FLEDHeight: integer;
FFrameColor: TColor;
FBoardColor: TColor;
FDotColorOn: TColor;
FLenText: integer;
FDisplayLineCount: integer;
FDisplayCharCount: integer;
FLines: TStrings;
FCountOn: integer;
FFrameStyle: TZStyle;
FFrameHeight: integer;
FFrameAltitude: integer;
FDotShape: TDotShape;
FCharDefs: TBCLeaCharDefs;
FOnChange: TNotifyEvent;
FDotBlend: boolean;
FDotBlendOperation: TBlendOperation;
FDotBlur: boolean;
FDotBlurRadius: single;
FBoardShadow: TBoardShadow;
//global intensity of the light
FLightSourceIntensity: single;
//minimum distance always added (positive value)
FLightSourceDistanceTerm: single;
//how much actual distance is taken into account (usually 0 or 1)
FLightSourceDistanceFactor: single;
//how much the location of the lightened pixel is taken into account (usually 0 or 1)
FLightDestFactor: single;
//color of the light reflection
FLightColor: TColor;
//how much light is reflected (0..1)
FSpecularFactor: single;
//how concentrated reflected light is (positive value)
FSpecularIndex: single;
//ambiant lighting whereever the point is (0..1)
FAmbientFactor: single;
//diffusion, i.e. how much pixels are lightened by light source (0..1)
FDiffusionFactor: single;
//how much hidden surface are darkened (0..1)
FNegativeDiffusionFactor: single;
//when diffusion saturates, use light color to show it
FDiffuseSaturation: boolean;
FLightPositionX: integer;
FLightPositionY: integer;
FLightPositionZ: integer;
function GetDotColCount: integer;
function GetDotRowCount: integer;
procedure SetDotColCount(AValue: integer);
procedure SetDotRowCount(AValue: integer);
procedure SetDotShape(const Value: TDotShape);
procedure SetFrameHeight(const Value: integer);
procedure SeTZStyle(const Value: TZStyle);
procedure SetFrameAltitude(const Value: integer);
function GetCharCount: longint;
function GetGlobalDotColsCount: longint;
procedure SetDisplayLineCount(const Value: integer);
procedure SetDisplayCharCount(const Value: integer);
procedure SetLines(const Value: TStrings);
procedure SetDotColorOn(const Value: TColor);
procedure SetBoardColor(const Value: TColor);
procedure SetFrameColor(const Value: TColor);
procedure SetFrameSize(const Value: integer);
procedure SetDotSize(const Value: integer);
procedure SetDotsSpace(const Value: integer);
function CalcCharCount: integer;
procedure InitCharDefs(ACharDefs: TBCLeaCharDefs; AHorDots, AVertDots: integer);
function IsCharDefsStored: boolean;
procedure LinesChanged(Sender: TObject);
procedure SetTheme(AValue: TBCLeaTheme);
//calculate widths and heights of the display matrix, background border and frame
procedure Prepare();
//draw frame
procedure DrawBorder();
//call DrawChar for every char
procedure DrawText();
//call DrawDot for every dot that is on
procedure DrawChar(Row, Col, NChar: integer);
procedure DrawDot(Row, Col: integer; DotColor: TColor);
procedure DrawBitmapToCanvas();
protected
// Basic method of auto-size calculation
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
{%H-}WithThemeSpace: boolean); override;
// Takes care of high-dpi scaling
procedure DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; const AXProportion, AYProportion: double); override;
procedure DoChange; virtual;
// inherited painting routine
procedure Paint; override;
// Recalculates the geometry if a related property has been changed.
procedure UpdateSize;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
// Adds a user-defined character and its dot matrix.
procedure AddCharDef(AChar: string; const Dots: TDotRows);
property CharCount: longint read GetCharCount;
property DotColCount: integer read GetDotColCount;
property DotRowCount: integer read GetDotRowCount;
property GlobalDotColsCount: longint read GetGlobalDotColsCount;
procedure UpdateTheme;
procedure ApplyTheme;
procedure SaveThemeToFile(AFileName: string);
procedure LoadThemeFromFile(AFileName: string);
procedure ApplyDefaultTheme;
published
property AutoSize default True;
property BorderSpacing;
property ShowHint;
property Visible;
property OnClick;
property OnDblClick;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property CharDefs: TBCLeaCharDefs read FCharDefs write FCharDefs stored IsCharDefsStored;
property DotSize: integer read FDotSize write SetDotSize default 4;
property DotsSpace: integer read FDotsSpace write SetDotsSpace default 1;
property FrameSize: integer read FFrameSize write SetFrameSize default 8;
property FrameAltitude: integer read FFrameAltitude write SetFrameAltitude default 2;
property FrameColor: TColor read FFrameColor write SetFrameColor default clBtnFace;
// To use BoardColor, ColorScheme must be set to csCustom
property BoardColor: TColor read FBoardColor write SetBoardColor default clBlack;
// To use DotColorOn, ColorScheme must be set to csCustom
property DotColorOn: TColor read FDotColorOn write SetDotColorOn default clSkyBlue;
// Vertical screen size in chars
// Without AutoSize, if the frame is too small
// a part off the text will not be visible
// e.g. frame is big enough for one line
// and the text contains 3 lines
// - just the middle line will be visible
property DisplayLineCount: integer read FDisplayLineCount write SetDisplayLineCount default 2;
// Horizontal screen size in chars
// Set to <=0 (zero) to have a real AutoSize
// Has no effect without AutoSize
property DisplayCharCount: integer read FDisplayCharCount write SetDisplayCharCount default 10;
// The text to display
// It will be truncated according
// to ScreenRowCount and ScreenColCount
property Lines: TStrings read FLines write SetLines;
property FrameStyle: TZStyle read FFrameStyle write SeTZStyle default zsRaised;
property FrameHeight: integer read FFrameHeight write SetFrameHeight default 8;
property DotShape: TDotShape read FDotShape write SetDotShape default stSquare;
property OnChange: TNotifyEvent read FOnChange write FOnChange;
property Theme: TBCLeaTheme read FTheme write SetTheme;
end;
function CopyDotRows(const ADotRows: array of TDotRow): TDotRows;
procedure Register;
implementation
uses
Dialogs, LazUTF8, LazUnicode;
const
DEFAULT_DOT_COL_COUNT = 5;
DEFAULT_DOT_ROW_COUNT = 7;
procedure Register;
begin
RegisterComponents('BGRA Controls', [TBCLeaLCDDisplay]);
end;
{ Create a "real" copy to avoid reference counter issues. }
function CopyDotRows(const ADotRows: array of TDotRow): TDotRows;
var
i: integer;
begin
Result := nil;
SetLength(Result, Length(ADotRows));
for i := 0 to High(ADotRows) do
Result[i] := ADotRows[i];
end;
{ TBCLeaCharDefs }
constructor TBCLeaCharDefs.Create(ADisplay: TBCLeaLCDDisplay);
begin
inherited Create;
FBCLeaLCDDisplay := ADisplay;
FCharList := TDotMatrixList.Create;
FCharList.Sorted := True;
end;
destructor TBCLeaCharDefs.Destroy;
begin
FCharList.Free;
inherited;
end;
{ Adds a new character dot matrix. }
procedure TBCLeaCharDefs.Add(AChar: string; const ADotRows: TDotRows);
begin
if Length(ADotRows) <> FRowCount then
raise Exception.Create('Incorrect number of rows.');
// Make sure to reset the reference counter --> use a local copy of ADotRows!
FCharList.Add(AChar, CopyDotRows(ADotRows));
end;
procedure TBCLeaCharDefs.Assign(ASource: TPersistent);
var
i: integer;
begin
if (ASource is TBCLeaCharDefs) then
begin
FColCount := TBCLeaCharDefs(ASource).ColCount;
FRowCount := TBCLeaCharDefs(ASource).RowCount;
Clear;
for i := 0 to TBCLeaCharDefs(ASource).Count - 1 do
Add(TBCLeaCharDefs(ASource).CharByIndex[i], TBCLeaCharDefs(ASource).DotRowsByIndex[i]);
end
else
inherited;
end;
{ Clears all characters and their dot matrices. }
procedure TBCLeaCharDefs.Clear;
begin
FCharList.Clear;
end;
{ Prepares streaming of the dot matrices }
procedure TBCLeaCharDefs.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('CharDefs', @ReadCharDefs, @WriteCharDefs, True);
end;
{ Deletes the specified character and its dot matrix from the list. }
procedure TBCLeaCharDefs.Delete(AChar: string);
var
idx: integer;
begin
if FCharList.Find(AChar, idx) then
FCharList.Delete(idx);
end;
{ Display the elements of the RowDots as a string. For debugging purposes. }
function TBCLeaCharDefs.DotRowsToString(AChar: string): string;
var
lDotRows: TDotRows;
i: integer;
begin
lDotRows := DotRows[AChar];
Result := IntToStr(lDotRows[0]);
for i := 1 to High(lDotRows) do
Result := Result + ',' + IntToStr(lDotRows[i]);
end;
{ Creates an empty row in which not dots are set. }
function TBCLeaCharDefs.EmptyRows: TDotRows;
var
row: integer;
begin
Result := nil;
SetLength(Result, FRowCount);
for row := 0 to FRowCount - 1 do
Result[row] := 0;
end;
function TBCLeaCharDefs.GetCharByIndex(AIndex: integer): string;
begin
Result := FCharList.Keys[AIndex];
end;
{ Returns the number of characters contained. }
function TBCLeaCharDefs.GetCount: integer;
begin
Result := FCharList.Count;
end;
{ Returns the dot matrix rows for the specified character. Each row is an
integer in which each bit is interpreted as a dot. }
function TBCLeaCharDefs.GetDotRows(AChar: string): TDotRows;
var
idx: integer;
begin
if FCharList.Find(AChar, idx) then
Result := FCharList.Data[idx]
else
Result := EmptyRows;
end;
function TBCLeaCharDefs.GetDotRowsByIndex(AIndex: integer): TDotRows;
begin
Result := FCharList.Data[AIndex];
end;
function TBCLeaCharDefs.Find(const AChar: string): boolean;
var
idx: integer;
begin
Result := FCharList.Find(AChar, idx);
end;
{ Reads the list of character name and dot matrices from the LFM file. The data
are stored in the LFM as a comma-separated list beginning with the character
name. }
procedure TBCLeaCharDefs.ReadCharDefs(Reader: TReader);
var
i: integer;
s: string;
ch: string;
sa: TStringArray;
rows: TDotRows = nil;
begin
Clear;
Reader.ReadListBegin;
while not Reader.EndOfList do
begin
s := Reader.ReadString;
if s[1] = ',' then
begin
ch := ',';
System.Delete(s, 1, 2);
end
else
begin
i := pos(',', s);
ch := copy(s, 1, i - 1);
System.Delete(s, 1, i);
end;
sa := s.Split(',');
SetLength(rows, Length(sa));
for i := 0 to High(sa) do
rows[i] := StrToInt(sa[i]);
Add(ch, rows);
end;
Reader.ReadListEnd;
end;
function TBCLeaCharDefs.SameDotRows(const AChar: string; const ADotRows: TDotRows): boolean;
var
i: integer;
lDotRows: TDotRows;
begin
Result := False;
lDotRows := DotRows[AChar];
if (Length(lDotRows) <> Length(ADotRows)) then
exit;
for i := 0 to High(lDotRows) do
if lDotRows[i] <> ADotRows[i] then exit;
Result := True;
end;
function DotRowsToStr(ADotRows: TDotRows): string;
var
i: integer;
begin
Result := IntToStr(ADotRows[0]);
for i := 1 to High(ADotRows) do
Result := Result + ',' + IntToStr(ADotRows[i]);
end;
function StrToDotRows(AString: string): TDotRows;
var
sa: TStringArray;
i: integer;
begin
Result := nil;
sa := AString.Split(',');
SetLength(Result, Length(sa));
for i := 0 to High(sa) do
Result[i] := StrToInt(sa[i]);
end;
procedure TBCLeaCharDefs.LoadFromFile(const AFileName: string);
var
doc: TXMLDocument = nil;
rootNode, parentNode, node, childNode: TDOMNode;
nodeName: string;
s: string;
ch: string;
dots: TDotRows;
begin
FCharList.Clear;
try
ReadXMLFile(doc, AFileName);
rootNode := doc.DocumentElement;
parentNode := rootNode.FirstChild;
while Assigned(parentNode) do
begin
nodeName := parentNode.NodeName;
if nodeName = 'DotColCount' then
begin
s := TDOMElement(parentNode).GetAttribute('Value');
if s <> '' then
FColCount := StrToInt(s)
else
raise Exception.Create('DotColCount missing');
end
else
if nodeName = 'DotRowCount' then
begin
s := TDOMElement(parentNode).GetAttribute('Value');
if s <> '' then
FRowCount := StrToInt(s)
else
raise Exception.Create('DotRowCount missing');
end
else
if nodeName = 'Chars' then
begin
node := parentNode.FirstChild;
while Assigned(node) do
begin
childnode := node.FirstChild;
ch := '';
dots := nil;
while Assigned(childnode) do
begin
nodeName := childNode.NodeName;
if nodeName = 'Name' then
ch := childNode.TextContent
else
if nodeName = 'DotRows' then
begin
s := childNode.TextContent;
dots := StrToDotRows(s);
end;
childNode := childNode.NextSibling;
end;
if ch = '' then
raise Exception.Create('Char "Name" missing.');
if dots = nil then
raise Exception.Create('Char "DotRows" missing.');
Add(ch, dots);
node := node.NextSibling;
end;
end;
parentNode := parentNode.NextSibling;
end;
with FBCLeaLCDDisplay do
begin
if AutoSize then
begin
InvalidatePreferredSize;
AdjustSize;
end;
Invalidate;
end;
finally
doc.Free;
end;
end;
{ Saves the list (character plus dot matrix for each) to an xml file. }
procedure TBCLeaCharDefs.SaveToFile(const AFileName: string);
var
doc: TXMLDocument;
rootNode, parentNode, node, childNode, textNode: TDOMNode;
i: integer;
begin
doc := TXMLDocument.Create;
try
rootNode := doc.CreateElement('LCD-CharDefs');
doc.AppendChild(rootNode);
rootNode := doc.DocumentElement;
node := doc.CreateElement('DotColCount');
TDOMElement(node).SetAttribute('Value', IntToStr(ColCount));
rootNode.AppendChild(node);
node := doc.CreateElement('DotRowCount');
TDOMElement(node).SetAttribute('Value', IntToStr(RowCount));
rootNode.AppendChild(node);
parentNode := doc.CreateElement('Chars');
rootNode.AppendChild(parentNode);
for i := 0 to Count - 1 do
begin
node := doc.CreateElement('Char');
parentNode.AppendChild(node);
childNode := doc.CreateElement('Name');
node.AppendChild(childNode);
textNode := doc.CreateTextNode(CharByIndex[i]);
childnode.AppendChild(textNode);
childNode := doc.CreateElement('DotRows');
node.AppendChild(childNode);
textNode := doc.CreateTextNode(DotRowsToStr(DotRowsByIndex[i]));
childNode.Appendchild(textNode);
end;
WriteXMLFile(doc, AFileName);
finally
doc.Free;
end;
end;
{ Returns the number of columns of the dot matrix. }
procedure TBCLeaCharDefs.SetColCount(AValue: integer);
begin
if FColCount = AValue then
exit;
FColCount := AValue;
FCharList.Clear;
end;
{ Sets the dot matrix for the specified character }
procedure TBCLeaCharDefs.SetDotRows(AChar: string; const AValue: TDotRows);
var
idx: integer;
begin
if FCharList.Find(AChar, idx) then
Delete(AChar);
Add(AChar, AValue);
end;
{ Returns the number of rows of the dot matrix. }
procedure TBCLeaCharDefs.SetRowCount(AValue: integer);
begin
if FRowCount = AValue then
exit;
FRowCount := AValue;
FCharList.Clear;
end;
{ Write the character and its dot matrix to the LFM. Each character is stored
as a comma-separated list of character and dotrow values. }
procedure TBCLeaCharDefs.WriteCharDefs(Writer: TWriter);
var
i, j: integer;
ch: string;
rows: TDotRows;
s: string;
begin
Writer.WriteListBegin;
for i := 0 to Count - 1 do
begin
ch := FCharList.Keys[i];
rows := DotRows[ch];
s := ch;
for j := 0 to FRowCount - 1 do
s := s + ',' + IntToStr(rows[j]);
Writer.WriteString(s);
end;
Writer.WriteListEnd;
end;
(*
{ TBCLeaLCDDisplayStrings }
type
TBCLeaLCDDisplayStrings = class(TStrings)
private
FOnChange: TNotifyEvent;
public
property OnChange: TNotifyEvent read FOnChange write FOnChange;
end;
*)
{ TBCLeaLCDDisplay}
constructor TBCLeaLCDDisplay.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csOpaque];
Width := 156;
Height := 76;
FDisplayLineCount := 2;
FDisplayCharCount := 10;
FBoardWidth := 4;
FCharDefs := TBCLeaCharDefs.Create(self);
FCharDefs.ColCount := DEFAULT_DOT_COL_COUNT;
FCharDefs.RowCount := DEFAULT_DOT_ROW_COUNT;
InitCharDefs(FCharDefs, DotColCount, DotRowCount);
FBitmap := TBGRABitmap.Create;
FCountOn := 255;
FLines := TStringList.Create;
TStringList(FLines).OnChange := @LinesChanged;
AutoSize := True;
ApplyDefaultTheme;
end;
destructor TBCLeaLCDDisplay.Destroy;
begin
FreeAndNil(FBitmap);
FCharDefs.Free;
FLines.Free;
inherited Destroy;
end;
procedure TBCLeaLCDDisplay.AddCharDef(AChar: string; const Dots: TDotRows);
begin
FCharDefs.Add(AChar, Dots);
end;
procedure TBCLeaLCDDisplay.CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
var
nDotCols: integer;
nDotRows: integer;
begin
nDotCols := DotColCount;
nDotRows := DotRowCount;
FCharWidth := (DotSize * nDotCols) + (DotsSpace * (nDotCols + 1)); //pixels
FCharCount := CalcCharCount;
FGlobalDotColsCount := (FCharCount * nDotCols) + (FCharCount - 1); //dots
// total matrix width
FLEDWidth := (FGlobalDotColsCount * DotSize) + ((FGlobalDotColsCount - 1) * DotsSpace);
// total matrix height
FLEDHeight := (FDisplayLineCount * nDotRows * (DotSize + DotsSpace)) +
((FDisplayLineCount - 1) * (DotSize + DotsSpace));
//background around text matrix - left/right pixels
FBoardWidth := DotSize + DotsSpace;
//background around text matrix - up/down pixels
FBoardHeight := DotSize + DotsSpace;
//Total size incl. frame
PreferredWidth := FLEDWidth + (2 * FrameSize) + (2 * FBoardWidth);
PreferredHeight := FLEDHeight + (2 * FrameSize) + (2 * FBoardWidth);
end;
procedure TBCLeaLCDDisplay.DoAutoAdjustLayout(const AMode: TLayoutAdjustmentPolicy; const AXProportion, AYProportion: double);
begin
inherited;
if AMode in [lapAutoAdjustWithoutHorizontalScrolling, lapAutoAdjustForDPI] then
begin
FDotSize := round(FDotSize * AXProportion);
FDotsSpace := round(FDotsSpace * AXProportion);
FFrameSize := round(FFrameSize * AXProportion);
end;
end;
procedure TBCLeaLCDDisplay.DoChange;
begin
if Assigned(FOnChange) then FOnChange(Self);
end;
procedure TBCLeaLCDDisplay.Prepare();
var
nDotCols: integer;
nDotRows: integer;
begin
nDotCols := DotColCount;
nDotRows := DotRowCount;
FCharWidth := (DotSize * nDotCols) + (DotsSpace * (nDotCols + 1)); //pixels
FCharCount := ((Width - (2 * FrameSize)) + DotSize) div (FCharWidth + DotSize);
FGlobalDotColsCount := (FCharCount * nDotCols) + (FCharCount - 1); //dots
// total matrix width
FLEDWidth := (FGlobalDotColsCount * DotSize) + ((FGlobalDotColsCount - 1) * DotsSpace);
// total matrix height
FLEDHeight := (FDisplayLineCount * nDotRows * (DotSize + DotsSpace)) +
((FDisplayLineCount - 1) * (DotSize + DotsSpace));
FBoardWidth := (Width - 2 * FrameSize - FLEDWidth) div 2;
FBoardHeight := (Height - 2 * FrameSize - FLEDHeight) div 2;
FBitmap.SetSize(Width, Height);
FBitmap.Fill(FBoardColor);
FBitmap.Canvas2D.resetTransform;
end;
procedure TBCLeaLCDDisplay.DrawBorder();
var
ScaledPhongSize: integer;
EffectiveLineWidth: single;
Blur, Mask, TempBMP, ShadowBoard: TBGRABitmap;
Phong: TPhongShading;
begin
if FFrameHeight = 0 then
EffectiveLineWidth := Height / 12
else
EffectiveLineWidth := FFrameHeight;
FBitmap.Canvas2D.lineWidth := EffectiveLineWidth;
FBitmap.Canvas2D.lineCapLCL := pecRound;
FBitmap.Canvas2D.strokeStyle(FrameColor);
FBitmap.Canvas2D.beginPath;
FBitmap.Canvas2D.rect(FFrameHeight / 2, FFrameHeight / 2, Width - FFrameHeight, Height - FFrameHeight);
FBitmap.Canvas2D.stroke;
if (FFrameStyle = zsLowered) or (FFRameStyle = zsRaised) then
begin
//make a copy for bsOwn
ShadowBoard := TBGRABitmap.Create(Width, Height, BGRABlack);
ShadowBoard.PutImage(0, 0, FBitmap, dmSet);
//draw frame inclusive board
ScaledPhongSize := round(EffectiveLineWidth / 2);
Mask := FBitmap.FilterGrayscale as TBGRABitmap;
if FFRameStyle = zsLowered then
Mask.Negative;
Blur := Mask.FilterBlurRadial(ScaledPhongSize, ScaledPhongSize, rbFast) as TBGRABitmap;
Blur.FillMask(0, 0, Mask, BGRAPixelTransparent, dmSet);
Mask.Free;
//make it 3D
Phong := TPhongShading.Create;
if assigned(FTheme) then
begin
Phong.AmbientFactor := FAmbientFactor;
Phong.SpecularIndex := FSpecularIndex;
Phong.LightDestFactor := FLightDestFactor;
Phong.LightPosition := Point(FLightPositionX, FLightPositionY);
Phong.LightPositionZ := FLightPositionZ;
Phong.LightSourceIntensity := FLightSourceIntensity;
Phong.LightSourceDistanceTerm := FLightSourceDistanceTerm;
Phong.LightSourceDistanceFactor := FLightSourceDistanceFactor;
Phong.NegativeDiffusionFactor := FNegativeDiffusionFactor;
Phong.SpecularFactor := FSpecularFactor;
Phong.DiffusionFactor := FDiffusionFactor;
Phong.DiffuseSaturation := FDiffuseSaturation;
Phong.LightColor := FLightColor;
end;
Phong.Draw(FBitmap, Blur, FFrameAltitude, 0, 0, FBitmap);
Phong.Free;
//now are both the frame and the board processed with phong effect and stored in FBitmap
//re-draw board (without frame)
if FBoardShadow = bsNone then
begin
//create mask for the frame
Mask := TBGRABitmap.Create(Width, Height, BGRABlack);
Mask.Canvas2D.lineWidth := EffectiveLineWidth;
Mask.Canvas2D.lineCapLCL := pecRound;
Mask.Canvas2D.strokeStyle(BGRAWhite);
Mask.Canvas2D.beginPath;
Mask.Canvas2D.rect(FFrameHeight / 2, FFrameHeight / 2, Width - FFrameHeight, Height - FFrameHeight);
Mask.Canvas2D.stroke;
//create new bitmap in BoardColor and add just the frame
TempBMP := TBGRABitmap.Create(Width, Height, ColorToBGRA(ColorToRGB(FBoardColor)));
TempBMP.PutImage(0, 0, FBitmap, dmSet);
TempBMP.ApplyMask(Mask);
Mask.Free;
FBitmap.Fill(FBoardColor);
FBitmap.PutImage(0, 0, TempBMP, dmDrawWithTransparency);
TempBMP.Free;
end;
if FBoardShadow = bsOwn then
begin
//ToDo - not sure about this
Phong := TPhongShading.Create;
if assigned(FTheme) then
begin
Phong.AmbientFactor := FAmbientFactor;
Phong.SpecularIndex := FSpecularIndex;
Phong.LightDestFactor := FLightDestFactor;
Phong.LightPosition := Point(FLightPositionX, FLightPositionY);
Phong.LightPositionZ := FLightPositionZ;
Phong.LightSourceIntensity := FLightSourceIntensity;
Phong.LightSourceDistanceTerm := FLightSourceDistanceTerm;
Phong.LightSourceDistanceFactor := FLightSourceDistanceFactor;
Phong.NegativeDiffusionFactor := FNegativeDiffusionFactor;
Phong.SpecularFactor := FSpecularFactor;
Phong.DiffusionFactor := FDiffusionFactor;
Phong.DiffuseSaturation := FDiffuseSaturation;
Phong.LightColor := FLightColor;
end;
Phong.Draw(ShadowBoard, Blur, 1, 0, 0, ShadowBoard);
Phong.Free;
//create mask for the frame
Mask := TBGRABitmap.Create(Width, Height, BGRAWhite);
Mask.Canvas2D.lineWidth := EffectiveLineWidth;
Mask.Canvas2D.lineCapLCL := pecRound;
Mask.Canvas2D.strokeStyle(BGRABlack);
Mask.Canvas2D.beginPath;
Mask.Canvas2D.rect(FFrameHeight / 2, FFrameHeight / 2, Width - FFrameHeight, Height - FFrameHeight);
Mask.Canvas2D.stroke;
ShadowBoard.ApplyMask(Mask);
Mask.Free;
FBitmap.PutImage(0, 0, ShadowBoard, dmDrawWithTransparency);
end;
ShadowBoard.Free;
Blur.Free;
end;
end;
procedure TBCLeaLCDDisplay.DrawText();
var
x, y, c: integer;
dots: TDotRows;
dotRow: TDotRow;
dotOn: boolean;
i: integer;
line: string;
ch: string;
begin
for i := 0 to FDisplayLineCount - 1 do
begin
if i < FLines.Count then
begin
line := FLines[i];
FLenText := UTF8Length(line);
c := 0;
for ch in line do
begin
Inc(c);
dots := FCharDefs.DotRows[ch];
for y := 0 to DotRowCount - 1 do
begin
DotRow := dots[y];
for x := 0 to 4 do
begin
DotOn := DotRow and (1 shl (5 - x - 1)) > 0;
if DotOn then
DrawChar(y + 8 * i, x, c);
end; // for x
end; // for y
end; // for ch
end;
end;
end;
procedure TBCLeaLCDDisplay.DrawChar(Row, Col, NChar: integer);
begin
Col := Col + ((DotColCount + 1) * (NChar - 1));
if Col > FGlobalDotColsCount - 1 then
Exit;
if Col < 0 then
Exit;
DrawDot(Row, Col, FDotColorOn);
end;
procedure TBCLeaLCDDisplay.DrawDot(Row, Col: integer; DotColor: TColor);
var
DotR: TRect;
Mask: TBGRABitmap;
begin
DotR.Left := FrameSize + FBoardWidth + (DotSize + DotsSpace) * Col;
DotR.Top := FrameSize + FBoardHeight + (DotSize + DotsSpace) * Row;
DotR.Right := DotR.Left + DotSize;
DotR.Bottom := DotR.Top + DotSize;
//todo
if FFrameHeight = 4 then
begin
if (DotR.Top <= FFrameSize) or (DotR.Bottom >= Height - FFrameSize) then
exit;
if (DotR.Left <= FFrameSize) or (DotR.Right >= Width - FFrameSize) then
exit;
end
else
begin
if (DotR.Top <= FFrameSize + 1) or (DotR.Bottom >= Height - FFrameSize - 1) then
exit;
if (DotR.Left <= FFrameSize + 1) or (DotR.Right >= Width - FFrameSize - 1) then
exit;
end;
Mask := TBGRABitmap.Create(Width, Height);
if DotShape = stSquare then
Mask.FillRect(DotR, DotColor)
else
Mask.FillEllipseAntialias(DotR.Left + DotSize / 2, DotR.Top + DotSize / 2, DotSize / 2, DotSize / 2, DotColor);
if FDotBlur then
Mask := Mask.FilterBlurRadial(DotSize * FDotBlurRadius, DotSize * FDotBlurRadius, rbFast);
if FDotBlend then
FBitmap.BlendImageOver(0, 0, Mask, FDotBlendOperation)
else
FBitmap.PutImage(0, 0, Mask, dmDrawWithTransparency);
Mask.Free;
end;
procedure TBCLeaLCDDisplay.DrawBitmapToCanvas();
begin
FBitmap.Draw(Canvas, 0, 0, True);
end;
procedure TBCLeaLCDDisplay.Paint();
begin
Prepare();
DrawBorder();
DrawText();
DrawBitmapToCanvas();
end;
// Find the longest's line length
function TBCLeaLCDDisplay.CalcCharCount: integer;
var
len: integer;
i, tmp: integer;
begin
len := 1;
if FDisplayCharCount > 0 then
Result := FDisplayCharCount
else
begin
for i := 0 to FDisplayLineCount - 1 do
begin
if i < Lines.Count then
begin
tmp := UTF8Length(Lines[i]);
if tmp > Len then Len := tmp;
end;
end;
Result := Len;
end;
end;
procedure TBCLeaLCDDisplay.InitCharDefs(ACharDefs: TBCLeaCharDefs; AHorDots, AVertDots: integer);
begin
ACharDefs.Clear;