forked from ericlangedijk/Lemmix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.SkillPanel.pas
540 lines (461 loc) · 15.5 KB
/
Game.SkillPanel.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
unit Game.SkillPanel;
{$include lem_directives.inc}
interface
uses
Types, Classes, Controls, SysUtils, Math,
GR32, GR32_Image, GR32_Layers,
Dos.Compression,
Dos.Structures,
Base.Utils, Base.Bitmaps,
Prog.Strings, Prog.Base, Prog.Data,
Dos.Bitmaps,
Dos.Consts,
Level.Base,
Styles.Base;
{-------------------------------------------------------------------------------
maybe this must be handled by lemgame (just bitmap writing)
// info positions types above the toolbar
// 1. BUILDER(23) 1/14
// 2. OUT 28 15/23
// 3. IN 99% 24/31
// 4. TIME 2-31 32/40
-------------------------------------------------------------------------------}
type
TMinimapClickEvent = procedure(Sender: TObject; const P: TPoint) of object;
TSkillButtonsMouseDownEvent = procedure(aButton: TSkillPanelButton; isDoubleClick: Boolean) of object;
TSkillButtonsMouseUpEvent = procedure of object;
type
TSkillPanelToolbar = class(TCustomControl)
private
fGraph : TGraphicSet;
fImg : TImage32;
fOriginal : TBitmap32;
fSkillFont : array['0'..'9', 0..1] of TBitmap32; // todo make one bitmap
fInfoFont : array[0..37] of TBitmap32; {%} {0..9} {A..Z} // todo make one bitmap
fButtonRects : array[TSkillPanelButton] of TRect;
fRectColor : TColor32;
fOnMinimapClick : TMinimapClickEvent; // event handler for minimap
fOnSkillButtonsMouseDown : TSkillButtonsMouseDownEvent;
fOnSkillButtonsMouseUp : TSkillButtonsMouseUpEvent;
// imageview events
procedure ImgMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
procedure ImgMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
procedure ImgMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
protected
procedure ReadBitmapFromStyle(aStyle: TStyle);
procedure SetButtonRects;
public
fLastDrawnStr: string;
fNewDrawStr: string;
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
procedure DrawNewStr;
property Img: TImage32 read fImg;
// paint methods (called directly from game and gamescreen)
procedure DrawSkillCount(aButton: TSkillPanelButton; aNumber: Integer);
procedure DrawButtonSelector(aButton: TSkillPanelButton; Highlight: Boolean);
procedure DrawMinimap(Map: TBitmap32);
procedure SetInfoAlternative(const info: string);
procedure SetInfoCursorLemming(const Lem: string; Num: Integer);
procedure SetInfoLemmingsOut(Num: Integer);
procedure SetInfoLemmingsIn(Num, Max: Integer);
procedure SetInfoMinutes(Num: Integer);
procedure SetInfoSeconds(Num: Integer);
procedure SetStyleAndGraph(aStyle: TStyle; aGraph: TGraphicSet; aScale: Integer);
procedure RefreshInfo;
// events (for gamescreen, who is responsible for game instructions)
property OnMinimapClick: TMinimapClickEvent read fOnMinimapClick write fOnMinimapClick;
property OnSkillButtonsMouseDown: TSkillButtonsMouseDownEvent read fOnSkillButtonsMouseDown write fOnSkillButtonsMouseDown;
property OnSkillButtonsMouseUp: TSkillButtonsMouseUpEvent read fOnSkillButtonsMouseUp write fOnSkillButtonsMouseUp;
end;
implementation
uses
GameScreen.Player;
function PtInRectEx(const Rect: TRect; const P: TPoint): Boolean;
begin
Result := (P.X >= Rect.Left) and (P.X < Rect.Right) and (P.Y >= Rect.Top) and (P.Y < Rect.Bottom);
end;
{ TSkillPanelToolbar }
constructor TSkillPanelToolbar.Create(aOwner: TComponent);
var
c: Char;
i: Integer;
begin
inherited;
fImg := TImage32.Create(Self);
fImg.Parent := Self;
fImg.RepaintMode := rmOptimizer;
fImg.OnMouseDown := ImgMouseDown;
fImg.OnMouseMove := ImgMouseMove;
fImg.OnMouseUp := ImgMouseUp;
fRectColor := DosVgaColorToColor32(DosInLevelPalettes[Consts.ChristmasPalette][3]);
fOriginal := TBitmap32.Create;
for i := 0 to 37 do begin
fInfoFont[i] := TBitmap32.Create;
end;
for c := '0' to '9' do
for i := 0 to 1 do
fSkillFont[c, i] := TBitmap32.Create;
// info positions types:
// strings (40 characters) = cursor + out + in + time = 1,15,24,32
// 1. BUILDER(23) 1/14 0..13 14
// 2. OUT 28 15/23 14..22 9
// 3. IN 99% 24/31 23..30 8
// 4. TIME 2-31 32/40 31..39 9
fLastDrawnStr := StringOfChar(' ', 40);
fNewDrawStr := StringOfChar(' ', 40);
fNewDrawStr := SSkillPanelTemplate;
{$if defined(paranoid)}
if fNewDrawStr.Length <> 40 then
Throw('Create length error in infostring');
{$ifend}
end;
destructor TSkillPanelToolbar.Destroy;
var
c: Char;
i: Integer;
begin
for i := 0 to 37 do
fInfoFont[i].Free;
for c := '0' to '9' do
for i := 0 to 1 do
fSkillFont[c, i].Free;
fOriginal.Free;
inherited;
end;
procedure TSkillPanelToolbar.DrawButtonSelector(aButton: TSkillPanelButton; Highlight: Boolean);
var
R: TRect;
C: TColor32;
A: TRect;
begin
if aButton = TSkillPanelButton.None then
Exit;
case Highlight of
False:
begin
R := fButtonRects[aButton];
Inc(R.Right);
Inc(R.Bottom, 2);
// top
A := R;
A.Bottom := A.Top + 1;
fOriginal.DrawTo(fImg.Bitmap, A, A);
// left
A := R;
A.Right := A.Left + 1;
fOriginal.DrawTo(fImg.Bitmap, A, A);
// right
A := R;
A.Left := A.Right - 1;
fOriginal.DrawTo(fImg.Bitmap, A, A);
// bottom
A := R;
A.Top := A.Bottom - 1;
fOriginal.DrawTo(fImg.Bitmap, A, A);
end;
True:
begin
R := fButtonRects[aButton];
Inc(R.Right);
Inc(R.Bottom, 2);
C := fRectColor;
fImg.Bitmap.FrameRectS(R, C);
end;
end;
end;
procedure TSkillPanelToolbar.DrawNewStr;
var
O, N: char;
i, x, y, idx: integer;
begin
// info positions types:
// 1. BUILDER(23) 1/14 0..13
// 2. OUT 28 15/23 14..22
// 3. IN 99% 24/31 23..30
// 4. TIME 2-31 32/40 31..39
y := 0;
x := 0;
for i := 1 to 40 do begin
idx := -1;
O := UpCase(fLastDrawnStr[i]);
N := UpCase(fNewDrawStr[i]);
// only draw changed letters
if O <> N then begin
// get index of bitmap
case N of
'%' : idx := 0;
'0'..'9' : idx := ord(n) - ord('0') + 1;
'-' : idx := 11;
'A'..'Z' : idx := ord(n) - ord('A') + 12;
end;
if (idx >= 0) and (idx <= 37)
then fInfoFont[idx].DrawTo(fimg.Bitmap, x, 0)
else fimg.Bitmap.FillRectS(x, y, x + 8, y + 16, 0); // empty
end;
Inc(x, 8);
end;
end;
procedure TSkillPanelToolbar.DrawSkillCount(aButton: TSkillPanelButton; aNumber: Integer);
// draw the number of skills left in the top of the buttons
var
S: string;
LeftDigit, RightDigit: Char;
BtnIdx: Integer;
DstRect, SrcRect: TRect;
c: TColor32;
const
FontYPos = 17;
begin
// x = 3, 19, 35 etc. are the "black holes" for the numbers inside the image
// y = 17
Restrict(aNumber, 0, 99);
S := LeadZeroStr(aNumber, 2);
LeftDigit := S[1];
RightDigit := S[2];
BtnIdx := Ord(aButton) - 1; // "ignore" the 'None' button enumeration
// white nothingness if number is zero
if aNumber = 0 then begin
DstRect := Rect(BtnIdx * 16 + 4, 17, BtnIdx * 16 + 4 + 8, 17 + 8);
c := Color32(60 * 4, 52 * 4, 52 * 4);
fImg.Bitmap.FillRect(DstRect.Left, DstRect.Top, DstRect.Right, DstRect.Bottom, c);
Exit;
end;
// left
DstRect := Rect(BtnIdx * 16 + 4, 17, BtnIdx * 16 + 4 + 4, 17 + 8);
SrcRect := Rect(0, 0, 4, 8);
fSkillFont[LeftDigit, 1].DrawTo(fImg.Bitmap, DstRect, SrcRect); // 1 is left
// right
DstRect.Offset(4, 0);
SrcRect := Rect(4, 0, 8, 8);
fSkillFont[RightDigit, 0].DrawTo(fImg.Bitmap, DstRect, SrcRect); // 0 is right
end;
procedure TSkillPanelToolbar.RefreshInfo;
// called by game or gamescreen after a frame is finished
begin
DrawNewStr;
fLastDrawnStr := fNewDrawStr;
end;
procedure TSkillPanelToolbar.ImgMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
{-------------------------------------------------------------------------------
Mouse behaviour of toolbar.
Minimap scrolling
button clicks
-------------------------------------------------------------------------------}
var
P: TPoint;
btn: TSkillPanelButton;
BtnRect: PRect;
begin
P := Img.ControlToBitmap(Point(X, Y));
// check minimap scroll
if DosMiniMapCorners.Contains(P) then begin
Dec(P.X, DosMinimapCorners.Left);
Dec(P.Y, DosMiniMapCorners.Top);
P.X := P.X * 16;
P.Y := P.Y * 8;
if Assigned(fOnMiniMapClick) then
fOnMinimapClick(Self, P);
Exit;
end;
// button click
for btn := Succ(Low(TSkillPanelButton)) to High(TSkillPanelButton) do // "ignore" spbNone
begin
BtnRect := @fButtonRects[btn];
if PtInRectEx(BtnRect^, P) then begin
if Assigned(fOnSkillButtonsMouseDown) then
fOnSkillButtonsMouseDown(btn, ssDouble in Shift);
Exit;
end;
end;
end;
procedure TSkillPanelToolbar.ImgMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
var
P: TPoint;
begin
if ssLeft in Shift then begin
P := Img.ControlToBitmap(Point(X, Y));
if PtInRectEx(DosMiniMapCorners, P) then begin
Dec(P.X, DosMinimapCorners.Left);
Dec(P.Y, DosMiniMapCorners.Top);
P.X := P.X * 16;
P.Y := P.Y * 8;
if Assigned(fOnMiniMapClick) then
fOnMinimapClick(Self, P);
end;
end
end;
procedure TSkillPanelToolbar.ImgMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer; Layer: TCustomLayer);
// ment for gamescreen
begin
if Assigned(fOnSkillButtonsMouseUp) then
fOnSkillButtonsMouseUp;
end;
procedure TSkillPanelToolbar.ReadBitmapFromStyle(aStyle: TStyle);
var
c: char; i: Integer;
Sections: TDosDatSectionList;
Decompressor: TDosDatDecompressor;
DataStream: TStream;
Fn: string;
LemmixPal: TArrayOfColor32;
HiPal: TArrayOfColor32;
begin
// try and concat palettes
LemmixPal := DosPaletteToArrayOfColor32(DosInLevelPalettes[Consts.ChristmasPalette]);
HiPal := fGraph.PaletteCustom;
{TODO: how o how is the palette constructed ??}
Assert(Length(HiPal) = 8, 'hipal error');
SetLength(LemmixPal, 16);
for i := 8 to 15 do
LemmixPal[i] := HiPal[i - 8];
LemmixPal[7] := LemmixPal[8];
Fn := aStyle.MainDatFileName;
SetButtonRects; // todo: move (just once needed)
Sections := TDosDatSectionList.Create;
Decompressor := TDosDatDecompressor.Create;
try
DataStream := TData.CreateDataStream(aStyle.Name, Fn, TDataType.LemmingData);
try
Decompressor.LoadSectionList(DataStream, Sections, False);
finally
DataStream.Free;
end;
// skillpanel
Decompressor.DecompressSection(Sections[6].CompressedData, Sections[6].DecompressedData);
Sections[6].DecompressedData.seek(0, sofrombeginning);
TDosPlanarBitmap.LoadFromStream(Sections[6].DecompressedData, fOriginal, 0, 320, 40, 4, LemmixPal);
fImg.Bitmap.Assign(fOriginal);
//fOriginal.SaveToPng(Consts.PathToDebugFiles + 'skillpanel.png');
// info fonts
Sections[6].DecompressedData.Seek($1900, soFromBeginning);
for i := 0 to 37 do begin
TDosPlanarBitmap.LoadFromStream(Sections[6].DecompressedData, fInfofont[i], -1, 8, 16, 3, LemmixPal);
//replacecolor(fInfoFont[i], Color32(0,176,0), clred32);
end;
//for i := 0 to 37 do
//fInfoFont[i].SaveToPng(AppPath + 'bmpdebug\skillfont' + i.ToString.PadLeft(2, '0') + '.png');
//skillfont RGB = 0,176,0
// skill fonts
{ TODO : christmas lemmings, fix it }
// pal := DosInLevelPalette;// fGraph.palettestandard; {DosInLevelPalette;}
// pal[1] := pal[3]; // WHITE
LemmixPal[1] := LemmixPal[3]; // WHITE
Decompressor.DecompressSection(Sections[2].CompressedData, Sections[2].DecompressedData);
Sections[2].decompresseddata.seek($1900, sofrombeginning);
for c := '0' to '9' do
for i := 0 to 1 do
TDosPlanarBitmap.LoadFromStream(sections[2].DecompressedData, fSkillFont[c, i], -1, 8, 8, 1, LemmixPal);
finally
Decompressor.free;
sections.free;
end;
end;
procedure TSkillPanelToolbar.SetButtonRects;
var
Org, R: TRect;
iButton: TSkillPanelButton;
begin
// Sca := 3;
Org := Rect(1, 16, 15, 38); // exact position of first button
R := Org;
{R.Left := R.Left * Sca;
R.Right := R.Right * Sca;
R.Top := R.Top * Sca;
R.Bottom := R.Bottom * Sca; }
for iButton := Succ(Low(TSkillPanelButton)) to High(TSkillPanelButton) do
begin
fButtonRects[iButton] := R;
R.Offset(16, 0);
end;
end;
procedure TSkillPanelToolbar.SetInfoCursorLemming(const Lem: string; Num: Integer);
var
S: string;
i: Integer;
begin
if Lem <> '' then begin
S := (Lem + ' ' + IntToStr(Num)).PadRight(14);
for i := 1 to 14 do
fNewDrawStr[i] := S[i];
end
else begin
for i := 1 to 14 do
fNewDrawStr[i] := ' ';
end;
end;
procedure TSkillPanelToolbar.SetInfoAlternative(const info: string);
var
S: string;
i: Integer;
begin
S := info.PadRight(14);
for i := 1 to 14 do
fNewDrawStr[i] := S[i];
end;
procedure TSkillPanelToolbar.SetInfoLemmingsOut(Num: Integer); // todo: rename
var
S: string;
i: Integer;
begin
S := Num.ToString.PadRight(5);
for i := 1 to 5 do
fNewDrawStr[18 + i] := S[i]
end;
procedure TSkillPanelToolbar.SetInfoLemmingsIn(Num, Max: Integer); // todo: rename
var
S: string;
i: Integer;
begin
S := Percentage(Max, Num).ToString + '%';
S := S.PadRight(5);
// todo: optional real number lemstrings
for i := 1 to 5 do
fNewDrawStr[26 + i] := S[i];
end;
procedure TSkillPanelToolbar.SetInfoMinutes(Num: Integer);
var
S: string;
i: Integer;
begin
S := Num.ToString.PadLeft(2);
for i := 1 to 2 do
fNewDrawStr[35 + i] := S[i];
end;
procedure TSkillPanelToolbar.SetInfoSeconds(Num: Integer);
var
S: string;
i: Integer;
begin
S := Num.ToString.PadLeft(2, '0');
for i := 1 to 2 do
fNewDrawStr[38 + i] := S[i];
end;
procedure TSkillPanelToolbar.SetStyleAndGraph(aStyle: TStyle; aGraph: TGraphicSet; aScale: Integer);
begin
fImg.BeginUpdate;
fGraph := aGraph;
ReadBitmapFromStyle(aStyle);
fImg.Scale := aScale;
fImg.ScaleMode := smScale;
fImg.Height := fOriginal.Height * aScale;
fImg.Width := fOriginal.Width * aScale;
Width := fImg.Width;
Height := fImg.Height;
fImg.EndUpdate;
fImg.Changed;
Invalidate;
end;
procedure TSkillPanelToolbar.DrawMinimap(Map: TBitmap32);
// o wow: todo: keep it simple
var
X: Integer;
begin
Map.DrawTo(Img.Bitmap, 208, 18);
if Parent <> nil then
begin
X := -Round(TGameScreenPlayer(Parent).ScreenImg.OffsetHorz/(16 * fImg.Scale));
Img.Bitmap.FrameRectS(208 + X, 18, 208 + X + 20 + 5, 38, fRectColor);
end;
end;
end.