-
Notifications
You must be signed in to change notification settings - Fork 0
/
tansiscreenpaintbox.pas
765 lines (663 loc) · 17.1 KB
/
tansiscreenpaintbox.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
unit tansiscreenpaintbox;
{$mode delphi}
{$H+}
{$packedrecords 1}
interface
// THis is the best part... an object that works similar like a screen.
// you can use writexx commands to write to it and dislpay ansi graphics.
uses Graphics, ExtCtrls, Classes, SysUtils,lconvencoding,strutils;
Const
MaxMsgLines = 5000;
Type
RecSauceInfo = Record
ID : Array[1..5] of Char;
Version : Array[1..2] of Char;
Title : Array[1..35] of Char;
Author : Array[1..20] of Char;
Group : Array[1..20] of Char;
Date : Array[1..8] of Char;
FileSize : Longint;
DataType : Byte;
FileType : Byte;
TInfo1 : Word;
TInfo2 : Word;
TInfo3 : Word;
TInfo4 : Word;
Comments : Byte;
Flags : Byte;
Filler : Array[1..22] of Char;
End;
TDosColor = Record
R:Byte;
G:Byte;
B:Byte;
end;
TDosColors = Array[0..15] of TDosColor ;
RecMessageLine = Array[1..160] of Record
Ch : Char;
Attr : Byte;
End;
//AnsiImage = Array[1..MaxMsgLines] of RecMessageLine;
AnsiImage = Array of RecMessageLine;
Const
DefDosColors : TDosColors = (
(R:000; G:000; B:000; ), //00
(R:000; G:000; B:128; ), //01
(R:000; G:128; B:000; ), //02
(R:000; G:128; B:128; ), //03
(R:170; G:000; B:000; ), //04
(R:128; G:000; B:128; ), //05
(R:128; G:128; B:000; ), //06
(R:192; G:192; B:192; ), //07
(R:128; G:128; B:128; ), //08
(R:000; G:000; B:255; ), //09
(R:000; G:255; B:000; ), //10
(R:000; G:255; B:255; ), //11
(R:255; G:000; B:000; ), //12
(R:255; G:000; B:255; ), //13
(R:255; G:255; B:000; ), //14
(R:255; G:255; B:255; ) //15
);
Type
{ TANSIPaintbox }
TANSIPaintbox = Class
private
FAttr : Byte;
FFont : TFont;
FLine : Integer;
FWhereX : Integer;
FWhereY : Integer;
FX,FY : Integer;
GotAnsi : Boolean;
GotPipe : Boolean;
PipeCode : String[2];
Data : AnsiImage;
Code : String;
Lines : Integer;
Escape : Byte;
SavedX : Integer;
SavedY : Integer;
FCharWidth : Word;
FCharHeight : Word;
DosPal : TDosColors;
FWidth : Integer;
FHeight : Integer;
CurY : Integer;
CurX : Integer;
CWidth : Word;
Protected
Procedure ResetControlCode;
Function ParseNumber (Var Line: String) : Integer;
Function AddChar (Ch: Char) : Boolean;
Procedure MoveXY (X, Y: Word);
Procedure MoveUP;
Procedure MoveDOWN;
Procedure MoveLEFT;
Procedure MoveRIGHT;
Procedure MoveCursor;
Procedure CheckCode (Ch: Char);
Procedure ProcessChar (Ch: Char);
Function ProcessBuf (Var Buf; BufLen: Word) : Boolean;
Procedure SetBFont(Const TF:TFont);
public
Buffer : TBitmap;
FSL : TStringList;
Property DosColors:TDosColors Read DosPal Write DosPal;
Destructor Destroy;
Function GetCharWidth:Byte;
Function GetCharHeight:Byte;
Constructor Create(TF:TFont);
Property WhereX:Integer Read FWhereX;
Property WhereY:Integer Read FWhereY;
Procedure TextColor(FG:Byte);
Procedure TextBackGround(FG:Byte);
Procedure SetTextAttr(Attr:Byte);
Procedure WriteChar(C:Char);
Procedure WriteString(S:String);
Procedure WriteXY(x,y,a:Byte; S:String);
Procedure CenterAt(y,a:byte; S:String);
Procedure WriteLn(S:String);Overload;
Procedure WriteLn; Overload;
Procedure GotoXY(X,Y:Integer);
Function LoadFromFile(FN:String):boolean;
Procedure PrintFile;
Procedure ResetCanvas;
Procedure ClearCanvas;
Procedure LoadXDefaultColors;
Function DosColor(CL:Byte):TColor;
Property Width:Integer Read FWidth;
Property Height:Integer Read FHeight;
Property Font:TFont Read FFont Write SetBFont;
Property ConWidth:Word read CWidth;
end;
Function ReadSauceInfo (FN: String; Var Sauce: RecSauceInfo) : Boolean;
Function Str2DosColor(Str:string):TDosColor;
implementation
{ TANSIPaintbox }
function TANSIPaintbox.DosColor(CL: Byte): TColor;
Begin
Result:=RGBToColor(DosPal[CL].R,DosPal[CL].G,DosPal[CL].B)
End;
function ReadSauceInfo(FN: String; var Sauce: RecSauceInfo): Boolean;
Var
DF : TFilestream;
Str : String;
Res : LongInt;
Begin
Result := False;
if not fileexists(fn) then exit;
fillbyte(sauce,sizeof(sauce),0);
df := TFilestream.Create(FN, fmOpenRead);
try
df.seek(-128,soFromEnd);
res:=df.Read (Sauce, sizeof(sauce));
finally
df.free;
End;
//Writeln(stripc(sauce.title));
Result := copy(sauce.id,1,5) = 'SAUCE';
end;
function Str2DosColor(Str: string): TDosColor;
begin
try
result.r:=strtoint(extractdelimited(1,str,[',',';']));
result.g:=strtoint(extractdelimited(2,str,[',',';']));
result.b:=strtoint(extractdelimited(3,str,[',',';']));
except
result.r:=0;
result.g:=0;
result.b:=0;
end;
end;
constructor TANSIPaintbox.Create(TF:TFont);
begin
Buffer:=TBitmap.Create;
Fx:=0;
Fy:=0;
FWhereX:=1;
CurX:=1;
CurY:=1;
FWhereY:=1;
FAttr:=7;
Fline:=0;
Lines:=1;
GotAnsi := False;
GotPipe := False;
PipeCode := '';
FFont:=TF;
cwidth:=80;
setlength(data,2);
data[1,1].attr:=7;
FillChar (Data, SizeOf(Data), 0);
// Buffer.Width:=80*GetCharWidth;
Buffer.Width:=132*GetCharWidth;
Buffer.Height:=100;
FSl:=TStringList.Create;
DosPal :=DefDosColors;
ResetControlCode;
end;
destructor TANSIPaintbox.Destroy;
begin
Fsl.Free;
Buffer.Free;
end;
procedure TANSIPaintbox.TextColor(FG: Byte);
begin
Buffer.Canvas.Font.Color:=RGBToColor(DosPal[FG].R,DosPal[FG].G,DosPal[FG].B)
end;
procedure TANSIPaintbox.TextBackGround(FG: Byte);
begin
Buffer.Canvas.Brush.Color:=RGBToColor(DosPal[FG].R,DosPal[FG].G,DosPal[FG].B)
end;
procedure TANSIPaintbox.SetTextAttr(Attr: Byte);
begin
FAttr := Attr;
TextColor(Attr Mod 16);
TextBackground(Attr div 16);
end;
procedure TANSIPaintbox.WriteChar(C: Char);
begin
Buffer.Canvas.TextOut(Fx,Fy,cp437ToUtf8(C));
If C<>#13 Then Begin
gotoxy(fwherex+1,fwherey);
end
Else Begin
{ Fx:=0;
Fy:=Fy+FCharHeight;
FWhereX:=1;
FWhereY:=FWhereY+1; }
gotoxy(0,fwherey+1);
end;
//Buffer.Height:=(FWhereY*CharHeight)+30;
// Buffer.Height:=(Lines*CharHeight)+30;
end;
procedure TANSIPaintbox.WriteString(S: String);
Var
i : Integer;
begin
For i:=1 to Length(S) Do WriteChar(S[i]);
end;
procedure TANSIPaintbox.WriteXY(x, y, a: Byte; S: String);
begin
GotoXY(x,y);
SetTextAttr(a);
WriteString(S);
end;
procedure TANSIPaintbox.CenterAt(y, a: byte; S: String);
begin
writexy(40-(length(s) div 2),y,a,s);
end;
procedure TANSIPaintbox.WriteLn(S: String);
begin
WriteString(S);
WriteChar(#13);
end;
procedure TANSIPaintbox.WriteLn;
begin
WriteChar(#13);
end;
procedure TANSIPaintbox.GotoXY(X, Y: Integer);
begin
FWhereX:=X;
FWhereY:=Y;
Fx:=(X-1)*FCharWidth;
Fy:=(Y-1)*FCharHeight;
end;
function TANSIPaintbox.LoadFromFile(FN: String):boolean;
Var
Buf : Array[1..4096] of Char;
BufLen : LongInt;
TopLine : LongInt;
WinSize : LongInt;
AFile : File;
Ch : Char;
Str : String;
Sauce : RecSauceInfo;
Done : Boolean = False;
Per : SmallInt;
Begin
result:=false;
SetTextAttr(7);
If Not FileExists(FN) Then Exit;
FSL.Loadfromfile(fn);
cwidth:=80;
ReadSauceInfo(FN, Sauce);
If Sauce.Datatype=1 then begin
case sauce.filetype of
0: cwidth:=sauce.tinfo1;
1: cwidth:=sauce.tinfo1;
2,3,4,5: begin //ansimation
exit;
end;
end;
end else if sauce.datatype>=80 then cwidth:=sauce.datatype;
AssignFile (AFile, FN);
Reset (AFile, 1);
setlength(data,2);
While Not Eof(AFile) Do Begin
BlockRead (AFile, Buf, SizeOf(Buf), BufLen);
If ProcessBuf (Buf, BufLen) Then Break;
End;
CloseFile (AFile);
result:=true;
// PrintFile;
end;
procedure TANSIPaintbox.PrintFile;
Var
A,B:integer;
Begin
a:=1;
FCharHeight:=GetCharHeight;
FCharWidth:=GetCharWidth;
buffer.width:=ConWidth*FCharWidth;
buffer.height:=(Lines*FCharHeight)+FCharHeight;
while a<=Lines do Begin
GotoXY(1,a);
For b:=1 to ConWidth do begin
SetTextAttr(Data[a][b].Attr);
case Data[a][b].Ch of
#0,#255: WriteChar(' ');
else
WriteChar(Data[a][b].Ch);
End;
end;
a:=a+1;
End;
end;
procedure TANSIPaintbox.ResetCanvas;
begin
Buffer.Canvas.Clear;
setlength(data,2);
FillChar (Data, SizeOf(Data), 0);
FCharHeight:=GetCharHeight;
FCharWidth:=GetCharWidth;
Buffer.Width:=132*FCharWidth;
Buffer.Height:=100;
end;
procedure TANSIPaintbox.ClearCanvas;
begin
Buffer.Canvas.Clear;
Fx:=0;
Fy:=0;
FWhereX:=1;
CurX:=1;
CurY:=1;
FWhereY:=1;
FAttr:=7;
Fline:=0;
Lines:=1;
GotAnsi := False;
GotPipe := False;
PipeCode := '';
setlength(data,2);
FillChar (Data, SizeOf(Data), 0);
FCharHeight:=GetCharHeight;
FCharWidth:=GetCharWidth;
Buffer.Width:=132*FCharWidth;
Buffer.Height:=100;
FSl:=TStringList.Create;
fillbyte(DosPal,0,sizeof(DosPal));
ResetControlCode;
end;
{
procedure TANSIPaintbox.LoadXDefaultColors;
var
tmpdc:tdoscolors;
Begin
fillbyte(DosPal,0,sizeof(DosPal));
// DosColors[0]:=Str2DosColor('000,000,000');
DosPal[0].r:=0;
DosPal[0].g:=0;
DosPal[0].b:=0;
DosPal[1]:=Str2DosColor('000,000,128');
DosPal[2]:=Str2DosColor('000,128,000');
DosPal[3]:=Str2DosColor('000,128,128');
DosPal[4]:=Str2DosColor('170,000,000');
DosPal[5]:=Str2DosColor('128,000,128');
DosPal[6]:=Str2DosColor('128,128,000');
DosPal[7]:=Str2DosColor('192,192,192');
DosPal[8]:=Str2DosColor('128,128,128');
DosPal[9]:=Str2DosColor('000,000,255');
DosPal[10]:=Str2DosColor('000,255,000');
DosPal[11]:=Str2DosColor('000,255,255');
DosPal[12]:=Str2DosColor('255,000,000');
DosPal[13]:=Str2DosColor('255,000,255');
DosPal[14]:=Str2DosColor('255,255,000');
DosPal[15]:=Str2DosColor('255,255,255');
end; }
procedure TANSIPaintbox.LoadXDefaultColors;
var
tmpdc:tdoscolors;
Begin
fillbyte(DosPal,0,sizeof(DosPal));
// DosColors[0]:=Str2DosColor('000,000,000');
DosPal[0].r:=0;
DosPal[0].g:=0;
DosPal[0].b:=0;
DosPal[1]:=Str2DosColor('000,000,173');
DosPal[2]:=Str2DosColor('000,170,000');
DosPal[3]:=Str2DosColor('000,170,173');
DosPal[4]:=Str2DosColor('173,000,000');
DosPal[5]:=Str2DosColor('173,000,173');
DosPal[6]:=Str2DosColor('173,85,000');
DosPal[7]:=Str2DosColor('173,170,173');
DosPal[8]:=Str2DosColor('82,85,82');
DosPal[9]:=Str2DosColor('82,85,255');
DosPal[10]:=Str2DosColor('82,255,82');
DosPal[11]:=Str2DosColor('82,255,255');
DosPal[12]:=Str2DosColor('255,82,85');
DosPal[13]:=Str2DosColor('255,85,255');
DosPal[14]:=Str2DosColor('255,255,82');
DosPal[15]:=Str2DosColor('255,255,255');
end;
function TANSIPaintbox.GetCharWidth: Byte;
begin
Result:=Buffer.Canvas.TextWidth('0');
end;
function TANSIPaintbox.GetCharHeight: Byte;
begin
Result:=Buffer.Canvas.TextHeight('0');
end;
procedure TANSIPaintbox.ResetControlCode;
begin
Escape := 0;
Code := '';
end;
function TANSIPaintbox.ParseNumber(var Line: String): Integer;
Var
A : Integer;
B : LongInt;
Str1 : String;
Str2 : String;
Begin
Str1 := Line;
Val(Str1, A, B);
If B = 0 Then
Str1 := ''
Else Begin
Str2 := Copy(Str1, 1, B - 1);
Delete (Str1, 1, B);
Val (Str2, A, B);
End;
Line := Str1;
ParseNumber := A;
end;
function TANSIPaintbox.AddChar(Ch: Char): Boolean;
begin
AddChar := False;
Data[CurY][CurX].Ch := Ch;
Data[CurY][CurX].Attr := FAttr;
If CurX < ConWidth Then
Inc (CurX)
Else Begin
If CurY = MaxMsgLines Then Begin
AddChar := True;
Exit;
End Else Begin
CurX := 1;
Inc (CurY);
setlength(data,length(data)+1);
End;
End;
end;
procedure TANSIPaintbox.MoveXY(X, Y: Word);
begin
If X > ConWidth Then X := ConWidth;
If Y > Length(Data) Then Y := Length(Data);
CurX := X;
CurY := Y;
end;
procedure TANSIPaintbox.MoveUP;
Var
NewPos : Integer;
Offset : Integer;
Begin
Offset := ParseNumber (Code);
If Offset = 0 Then Offset := 1;
If (CurY - Offset) < 1 Then
NewPos := 1
Else
NewPos := CurY - Offset;
MoveXY (CurX, NewPos);
ResetControlCode;
end;
procedure TANSIPaintbox.MoveDOWN;
Var
NewPos : Byte;
Begin
NewPos := ParseNumber (Code);
If NewPos = 0 Then NewPos := 1;
MoveXY (CurX, CurY + NewPos);
ResetControlCode;
end;
procedure TANSIPaintbox.MoveLEFT;
Var
NewPos : Integer;
Offset : Integer;
Begin
Offset := ParseNumber (Code);
If Offset = 0 Then Offset := 1;
If CurX - Offset < 1 Then
NewPos := 1
Else
NewPos := CurX - Offset;
MoveXY (NewPos, CurY);
ResetControlCode;
end;
procedure TANSIPaintbox.MoveRIGHT;
Var
NewPos : Integer;
Offset : Integer;
Begin
Offset := ParseNumber(Code);
If Offset = 0 Then Offset := 1;
If CurX + Offset > ConWidth Then Begin
NewPos := (CurX + Offset) - ConWidth;
Inc (CurY);
End Else
NewPos := CurX + Offset;
MoveXY (NewPos, CurY);
ResetControlCode;
end;
procedure TANSIPaintbox.MoveCursor;
Var
X : Byte;
Y : Byte;
Begin
X := ParseNumber(Code);
Y := ParseNumber(Code);
If X = 0 Then X := 1;
If Y = 0 Then Y := 1;
MoveXY (X, Y);
ResetControlCode;
end;
procedure TANSIPaintbox.CheckCode(Ch: Char);
Var
Temp1 : Byte;
Temp2 : Byte;
Begin
Case Ch of
'0'..'9', ';', '?' : Code := Code + Ch;
'H', 'f' : MoveCursor;
'A' : MoveUP;
'B' : MoveDOWN;
'C' : MoveRIGHT;
'D' : MoveLEFT;
'J' : Begin
{ClearScreenData;}
ResetControlCode;
End;
'K' : Begin
Temp1 := CurX;
For Temp2 := CurX To ConWidth Do
AddChar(' ');
MoveXY (Temp1, CurY);
ResetControlCode;
End;
'h' : ResetControlCode;
'm' : Begin
While Length(Code) > 0 Do Begin
Case ParseNumber(Code) of
0 : FAttr := 7;
1 : FAttr := FAttr OR $08;
5 : FAttr := FAttr OR $80;
7 : Begin
FAttr := FAttr AND $F7;
FAttr := ((FAttr AND $70) SHR 4) + ((FAttr AND $7) SHL 4) + FAttr AND $80;
End;
30: FAttr := (FAttr AND $F8) + 0;
31: FAttr := (FAttr AND $F8) + 4;
32: FAttr := (FAttr AND $F8) + 2;
33: FAttr := (FAttr AND $F8) + 6;
34: FAttr := (FAttr AND $F8) + 1;
35: FAttr := (FAttr AND $F8) + 5;
36: FAttr := (FAttr AND $F8) + 3;
37: FAttr := (FAttr AND $F8) + 7;
40: TextBackGround (0);
41: TextBackGround (4);
42: TextBackGround (2);
43: TextBackGround (6);
44: TextBackGround (1);
45: TextBackGround (5);
46: TextBackGround (3);
47: TextBackGround (7);
End;
End;
ResetControlCode;
End;
's' : Begin
SavedX := CurX;
SavedY := CurY;
ResetControlCode;
End;
'u' : Begin
MoveXY (SavedX, SavedY);
ResetControlCode;
End;
Else
ResetControlCode;
End;
end;
procedure TANSIPaintbox.ProcessChar(Ch: Char);
Begin
Case Escape of
0 : Begin
Case Ch of
#27 : Escape := 1;
#9 : MoveXY (CurX + 8, CurY);
#12 : {Edit.ClearScreenData};
Else
AddChar (Ch);
ResetControlCode;
End;
End;
1 : If Ch = '[' Then Begin
Escape := 2;
Code := '';
GotAnsi := True;
End Else
Escape := 0;
2 : CheckCode(Ch);
Else
ResetControlCode;
End;
end;
function TANSIPaintbox.ProcessBuf(var Buf; BufLen: Word): Boolean;
Var
Count : Word;
SBuffer : Array[1..4096] of Char Absolute Buf;
Begin
Result := False;
For Count := 1 to BufLen Do Begin
If CurY > Lines Then begin
Lines := CurY;
setlength(data,lines+1);
end;
Case SBuffer[Count] of
#10 : If CurY = MaxMsgLines Then Begin
Result := True;
GotAnsi := False;
Break;
End Else Begin
CurY:=CurY+1;
CurX := 1;
End;
#13 : Begin
CurX := 1;
end;
#26 : Begin
Result := True;
Break;
End;
Else
ProcessChar(SBuffer[Count]);
End;
End;
end;
procedure TANSIPaintbox.SetBFont(const TF: TFont);
begin
Buffer.Canvas.Font:=FFont;
GetCharWidth;
GetCharHeight;
end;
end.