-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathItemShopTableForm.pas
507 lines (457 loc) · 17.1 KB
/
ItemShopTableForm.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
unit ItemShopTableForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, Menus,
CountedDynArrayString,
InflatablesList_ItemShop,
InflatablesList_Item,
InflatablesList_Manager;
const
IL_ITEMSHOPTABLE_ITEMCELL_WIDTH = 300;
IL_ITEMSHOPTABLE_ITEMCELL_HEIGHT = 35;
type
TILItemShopTableRow = record
Item: TILItem;
Shops: array of TILItemShop;
end;
TILItemShopTable = array of TILItemShopTableRow;
TfItemShopTableForm = class(TForm)
dgTable: TDrawGrid;
lblNoTable: TLabel;
cbCompactView: TCheckBox;
lblSelectedInfo: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure dgTableDblClick(Sender: TObject);
procedure dgTableSelectCell(Sender: TObject; ACol, ARow: Integer;
var CanSelect: Boolean);
procedure dgTableDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure dgTableMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure cbCompactViewClick(Sender: TObject);
private
{ Private declarations }
fDrawBuffer: TBitmap;
fILManager: TILManager;
fKnownShops: TCountedDynArrayString;
fTable: TILItemShopTable;
fTracking: Boolean;
fTrackPoint: TPoint;
protected
// table building
procedure EnumShops;
procedure BuildTable;
procedure FillTable;
procedure AdjustTable;
// other methods
procedure UpdateInfo(SelCol,SelRow: Integer);
public
{ Public declarations }
procedure Initialize(ILManager: TILManager);
procedure Finalize;
procedure ShowTable;
end;
var
fItemShopTableForm: TfItemShopTableForm;
implementation
{$R *.dfm}
uses
InflatablesList_Utils,
InflatablesList_LocalStrings;
procedure TfItemShopTableForm.EnumShops;
var
i,j: Integer;
begin
// enumerate shops
CDA_Clear(fKnownShops);
For i := fILManager.ItemLowIndex to fILManager.ItemHighIndex do
If fILManager[i].DataAccessible then
For j := fILManager[i].ShopLowIndex to fILManager[i].ShopHighIndex do
If not CDA_CheckIndex(fKnownShops,CDA_IndexOf(fKnownShops,fILManager[i][j].Name)) then
CDA_Add(fKnownShops,fILManager[i][j].Name);
// sort the shop list
CDA_Sort(fKnownShops);
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.BuildTable;
var
i,j: Integer;
Index: Integer;
begin
SetLength(fTable,fILManager.ItemCount);
For i := fILManager.ItemLowIndex to fILManager.ItemHighIndex do
begin
fTable[i].Item := fILManager.Items[i];
SetLength(fTable[i].Shops,CDA_Count(fKnownShops));
// make sure the shops array contains only nil references
For j := Low(fTable[i].Shops) to High(fTable[i].Shops) do
fTable[i].Shops[j] := nil;
// fill shops by name
If fTable[i].Item.DataAccessible then
For j := fTable[i].Item.ShopLowIndex to fTable[i].Item.ShopHighIndex do
begin
Index := CDA_IndexOf(fKnownShops,fTable[i].Item[j].Name);
If CDA_CheckIndex(fKnownShops,Index) then
fTable[i].Shops[Index] := fTable[i].Item[j];
end;
end;
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.FillTable;
var
TableVisible: Boolean;
i: Integer;
MinHeight: Integer;
TempSize: TSize;
begin
If Length(fTable) > 0 then
TableVisible := Length(fTable[Low(fTable)].Shops) > 0
else
TableVisible := False;
If TableVisible then
begin
dgTable.RowCount := Length(fTable) + 1;
dgTable.ColCount := Length(fTable[0].Shops) + 1;
dgTable.DefaultRowHeight := IL_ITEMSHOPTABLE_ITEMCELL_HEIGHT;
// get minimal height of the first line
MinHeight := 0;
For i := CDA_Low(fKnownShops) to CDA_High(fKnownShops) do
If IL_GetRotatedTextSize(dgTable.Canvas,CDA_GetItem(fKnownShops,i),90,TempSize) then
If MinHeight < TempSize.cy + 20 then
MinHeight := TempSize.cy + 20;
If MinHeight > 50 then
dgTable.RowHeights[0] := MinHeight
else
dgTable.RowHeights[0] := 50;
end;
dgTable.Visible := TableVisible;
lblNoTable.Visible := not TableVisible;
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.AdjustTable;
begin
If cbCompactView.Checked then
dgTable.DefaultColWidth := 25
else
dgTable.DefaultColWidth := 65;
dgTable.ColWidths[0] := IL_ITEMSHOPTABLE_ITEMCELL_WIDTH;
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.UpdateInfo(SelCol,SelRow: Integer);
var
Shop: TILItemShop;
TempStr: String;
begin
If dgTable.Visible and (SelRow > 0) and (SelCol > 0) then
begin
If Assigned(fTable[Pred(SelRow)].Shops[Pred(SelCol)]) then
begin
Shop := fTable[Pred(SelRow)].Shops[Pred(SelCol)];
If Shop.Price > 0 then
begin
If Shop.Available <> 0 then
begin
If Shop.Price <= fTable[Pred(SelRow)].Item.UnitPriceLowest then
TempStr := ' (lowest price)'
else If Shop.Price >= fTable[Pred(SelRow)].Item.UnitPriceHighest then
TempStr := ' (highest price)'
else
TempStr := '';
lblSelectedInfo.Caption := IL_Format('%s %sin %s: %s%d pcs at %d %s%s%s',
[fTable[Pred(SelRow)].Item.TitleStr,IL_BoolToStr(Shop.Selected,'','selected '),
Shop.Name,IL_BoolToStr(Shop.Available < 0,'','more than '),Abs(Shop.Available),Shop.Price,
IL_CURRENCY_SYMBOL,TempStr,IL_BoolToStr(Shop.IsAvailableHere(True),', too few pieces','')]);
end
else lblSelectedInfo.Caption := IL_Format('%s %sin %s: %d %s, not available',
[fTable[Pred(SelRow)].Item.TitleStr,IL_BoolToStr(Shop.Selected,'','selected '),
Shop.Name,Shop.Price,IL_CURRENCY_SYMBOL]);
end
else lblSelectedInfo.Caption := IL_Format('%s is not available in %s',
[fTable[Pred(SelRow)].Item.TitleStr,CDA_GetItem(fKnownShops,Pred(SelCol))]);
end
else lblSelectedInfo.Caption := IL_Format('%s is not available in %s',
[fTable[Pred(SelRow)].Item.TitleStr,CDA_GetItem(fKnownShops,Pred(SelCol))]);
end
else lblSelectedInfo.Caption := '';
end;
//==============================================================================
procedure TfItemShopTableForm.Initialize(ILManager: TILManager);
begin
fILManager := ILManager;
CDA_Init(fKnownShops);
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.Finalize;
begin
// nothing to do here
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.ShowTable;
begin
EnumShops;
BuildTable;
FillTable;
AdjustTable;
UpdateInfo(dgTable.Col,dgTable.Row);
fTracking := False;
ShowModal;
end;
//==============================================================================
procedure TfItemShopTableForm.FormCreate(Sender: TObject);
begin
fDrawBuffer := TBitmap.Create;
fDrawBuffer.PixelFormat := pf24bit;
fDrawBuffer.Canvas.Font.Assign(dgTable.Font);
dgTable.DoubleBuffered := True;
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.FormDestroy(Sender: TObject);
begin
FreeAndNil(fDrawBuffer);
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.dgTableDblClick(Sender: TObject);
var
MousePos: TPoint;
CellCoords: TPoint;
i,TempInt: Integer;
ItemShop: TILItemShop;
Dummy: Boolean;
begin
// get which cell was clicked
MousePos := dgTable.ScreenToClient(Mouse.CursorPos);
CellCoords.X := -1;
CellCoords.Y := -1;
// get column
TempInt := dgTable.ColWidths[0]; // fixed col
For i := dgTable.LeftCol to Pred(dgTable.ColCount) do
If (MousePos.X >= TempInt) and (MousePos.X < TempInt + dgTable.ColWidths[i]) then
begin
CellCoords.X := i;
Break{For i};
end
else Inc(TempInt,dgTable.ColWidths[i]);
// get row
TempInt := dgTable.RowHeights[0]; // fixed row
For i := dgTable.TopRow to Pred(dgTable.RowCount) do
If (MousePos.Y >= TempInt) and (MousePos.Y < TempInt + dgTable.RowHeights[i]) then
begin
CellCoords.Y := i;
Break{For i};
end
else Inc(TempInt,dgTable.RowHeights[i]);
// do the (un)selection
If (CellCoords.X >= 0) and (CellCoords.Y >= 0) then
begin
ItemShop := fTable[Pred(CellCoords.Y)].Shops[Pred(CellCoords.X)];
If Assigned(ItemShop) then
ItemShop.Selected := not ItemShop.Selected;
Dummy := True;
dgTableSelectCell(nil,CellCoords.X,CellCoords.Y,Dummy);
dgTable.Invalidate;
end
else Beep;
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.dgTableSelectCell(Sender: TObject; ACol,
ARow: Integer; var CanSelect: Boolean);
begin
UpdateInfo(ACol,ARow);
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.dgTableDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
BoundsRect: TRect;
TempStr: String;
Shop: TILItemShop;
TempSize: TSize;
procedure SetBrushColors(Brush: TBrush; TrueColor,FalseColor: TColor; Switch: Boolean);
begin
If Switch then
Brush.Color := TrueColor
else
Brush.Color := FalseColor;
end;
begin
If (Sender is TDrawGrid) and Assigned(fDrawBuffer) then
begin
// adjust draw buffer size
If fDrawBuffer.Width < (Rect.Right - Rect.Left) then
fDrawBuffer.Width := Rect.Right - Rect.Left;
If fDrawBuffer.Height < (Rect.Bottom - Rect.Top) then
fDrawBuffer.Height := Rect.Bottom - Rect.Top;
BoundsRect := Classes.Rect(0,0,Rect.Right - Rect.Left,Rect.Bottom - Rect.Top);
with fDrawBuffer.Canvas do
begin
Font.Style := [];
Pen.Style := psClear;
Brush.Style := bsSolid;
If gdFixed in State then
begin // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If (ARow > 0) and (ACol = 0) then
begin
// items
Draw(BoundsRect.Left,BoundsRect.Top,fTable[Pred(ARow)].Item.RenderMini);
fTable[Pred(ARow)].Item.RenderMini.Dormant;
Pen.Color := clSilver; // for grid lines
end
else If (ARow = 0) and (ACol > 0) then
begin
// shops
Brush.Color := $00E4E4E4;
Rectangle(BoundsRect);
If IL_GetRotatedTextSize(fDrawBuffer.Canvas,CDA_GetItem(fKnownShops,Pred(ACol)),90,TempSize) then
IL_DrawRotatedText(fDrawBuffer.Canvas,CDA_GetItem(fKnownShops,Pred(ACol)),90,
((BoundsRect.Right - BoundsRect.Left) - TempSize.cx) div 2,
((BoundsRect.Bottom - BoundsRect.Top) - TempSize.cy) div 2 + TempSize.cy);
Pen.Color := clGray;
end
else
begin
// other fixed cells (upper left corner)
Brush.Color := $00FFE8EA; // shade of blue
Rectangle(BoundsRect);
Pen.Color := clGray;
end;
end
else // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
begin
// shop cells
Shop := fTable[Pred(ARow)].Shops[Pred(ACol)];
If Assigned(Shop) then
begin
// background
If Shop.IsAvailableHere(True) then
begin
// valid shop
If Shop.Price <= fTable[Pred(ARow)].Item.UnitPriceLowest then
SetBrushColors(Brush,$0069FF62,$00CEFFCC,gdSelected in State)
else If Shop.Price >= fTable[Pred(ARow)].Item.UnitPriceHighest then
SetBrushColors(Brush,$003CFAFF,$00C4FEFF,gdSelected in State)
else
SetBrushColors(Brush,$00E6E6E6,clWhite,gdSelected in State);
end
// invalid shop (item not available here
else SetBrushColors(Brush,$005EBAFF,$00B7E0FF,gdSelected in State);
Rectangle(BoundsRect);
// text
Brush.Style := bsClear;
If cbCompactView.Checked then
begin
// price rotated by 90deg and without currency symbol
Font.Style := font.Style + [fsBold];
If Shop.Price > 0 then
TempStr := IL_Format('%d',[Shop.Price])
else
TempStr := '-';
If IL_GetRotatedTextSize(fDrawBuffer.Canvas,TempStr,90,TempSize) then
IL_DrawRotatedText(fDrawBuffer.Canvas,TempStr,90,
BoundsRect.Right - TempSize.cx - 1,
BoundsRect.Top + TempSize.cy + 2);
end
else
begin
// price
Font.Style := Font.Style + [fsBold];
If Shop.Price > 0 then
TempStr := IL_Format('%d %s',[Shop.Price,IL_CURRENCY_SYMBOL])
else
TempStr := '-';
TextOut(BoundsRect.Right - TextWidth(TempStr) - 5,2,TempStr);
// available
Font.Style := font.Style - [fsBold];
If Shop.Available > 0 then
TempStr := IL_Format('%d pcs',[Shop.Available])
else If Shop.Available < 0 then
TempStr := IL_Format('%d+ pcs',[Abs(Shop.Available)])
else
TempStr := '';
TextOut(BoundsRect.Right - TextWidth(TempStr) - 5,18,TempStr);
end;
// selection mark
If Shop.Selected then
begin
Pen.Style := psClear;
Brush.Style := bsSolid;
Brush.Color := clBlue;
Rectangle(BoundsRect.Left,BoundsRect.Top,BoundsRect.Left + 10,BoundsRect.Bottom);
end;
end
else
begin
// shop not assigned
SetBrushColors(Brush,$00D6D6D6,$00F7F7F7,gdSelected in State);
Rectangle(BoundsRect);
end;
Pen.Color := clSilver;
end; // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Pen.Style := psSolid;
MoveTo(BoundsRect.Left,BoundsRect.Bottom - 1);
LineTo(BoundsRect.Right - 1,BoundsRect.Bottom - 1);
LineTo(BoundsRect.Right - 1,BoundsRect.Top - 1);
end;
// move drawbuffer to the canvas
TDrawGrid(Sender).Canvas.CopyRect(Rect,fDrawBuffer.Canvas,BoundsRect);
end;
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.dgTableMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
begin
If ssRight in Shift then
begin
If fTracking then
begin
// horizontal movement
If Abs(fTrackPoint.X - Mouse.CursorPos.X) >= dgTable.DefaultColWidth then
begin
If (fTrackPoint.X - Mouse.CursorPos.X) > 0 then
begin
// cursor moved left
If dgTable.LeftCol < (dgTable.ColCount - ((dgTable.ClientWidth - dgTable.ColWidths[0]) div dgTable.DefaultColWidth)) then
dgTable.LeftCol := dgTable.LeftCol + 1;
fTrackPoint.X := fTrackPoint.X - dgTable.DefaultColWidth;
end
else
begin
// cursor moved right
If dgTable.LeftCol > 1 then
dgTable.LeftCol := dgTable.LeftCol - 1;
fTrackPoint.X := fTrackPoint.X + dgTable.DefaultColWidth;
end;
end;
// vertical movement
If Abs(fTrackPoint.Y - Mouse.CursorPos.Y) >= dgTable.DefaultRowHeight then
begin
If (fTrackPoint.Y - Mouse.CursorPos.Y) > 0 then
begin
// cursor moved up
If dgTable.TopRow < (dgTable.RowCount - ((dgTable.ClientHeight - dgTable.RowHeights[0]) div dgTable.DefaultRowHeight)) then
dgTable.TopRow := dgTable.TopRow + 1;
fTrackPoint.Y := fTrackPoint.Y - dgTable.DefaultRowHeight;
end
else
begin
// cursor moved down
If dgTable.TopRow > 1 then
dgTable.TopRow := dgTable.TopRow - 1;
fTrackPoint.Y := fTrackPoint.Y + dgTable.DefaultRowHeight;
end;
end;
end
else
begin
fTracking := True;
fTrackPoint := Mouse.CursorPos;
end;
end
else fTracking := False;
end;
//------------------------------------------------------------------------------
procedure TfItemShopTableForm.cbCompactViewClick(Sender: TObject);
begin
AdjustTable;
end;
end.