source: trunk/Forms/FormScore.pas

Last change on this file was 110, checked in by chronos, 6 weeks ago
  • Added: Sorting by individual columns in Score window.
File size: 3.4 KB
Line 
1unit FormScore;
2
3interface
4
5uses
6 Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ComCtrls, StdCtrls,
7 FormEx, ListViewSort, Score, DateUtils, Generics.Collections;
8
9type
10
11 { TFormScore }
12
13 TFormScore = class(TFormEx)
14 ButtonClose: TButton;
15 ButtonClear: TButton;
16 ListView1: TListView;
17 ListViewSort1: TListViewSort;
18 procedure ButtonClearClick(Sender: TObject);
19 procedure FormShow(Sender: TObject);
20 procedure ListView1Data(Sender: TObject; Item: TListItem);
21 function ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
22 procedure ListViewSort1Filter(ListViewSort: TListViewSort);
23 private
24 FScores: TScores;
25 procedure SetScores(AValue: TScores);
26 procedure FilterList(List: TObjectList<TObject>);
27 public
28 procedure ReloadList;
29 property Scores: TScores read FScores write SetScores;
30 end;
31
32
33implementation
34
35{$R *.lfm}
36
37resourcestring
38 SClearCaption = 'Clear score';
39 SClearQuery = 'Do you really want to clear score history?';
40
41{ TFormScore }
42
43procedure TFormScore.ListView1Data(Sender: TObject; Item: TListItem);
44begin
45 if Item.Index < ListViewSort1.List.Count then
46 with TScore(ListViewSort1.List[Item.Index]) do begin
47 Item.Caption := DateTimeToStr(StartTime);
48 Item.Data := ListViewSort1.List[Item.Index];
49 Item.SubItems.Add(BoardSize);
50 Item.SubItems.Add(IntToStr(Score));
51 Item.SubItems.Add(IntToStr(1 shl BiggestTile));
52 Item.SubItems.Add(IntToStr(Moves));
53 Item.SubItems.Add(IntToStr(UsedUndos));
54 Item.SubItems.Add(IntToStr(DisabledTiles));
55 Item.SubItems.Add(IntToStr(UnmergeableTiles));
56 Item.SubItems.Add(TimeToStr(Duration));
57 end;
58end;
59
60function TFormScore.ListViewSort1CompareItem(Item1, Item2: TObject): Integer;
61var
62 Score1, Score2: TScore;
63begin
64 Result := 0;
65 if Assigned(Item1) and Assigned(Item2) and (ListViewSort1.Order <> soNone) then begin
66 Score1 := TScore(Item1);
67 Score2 := TScore(Item2);
68 with ListViewSort1 do
69 case Column of
70 0: Result := CompareDateTime(Score1.StartTime, Score2.StartTime);
71 1: Result := CompareString(Score1.BoardSize, Score2.BoardSize);
72 2: Result := CompareInteger(Score1.Score, Score2.Score);
73 3: Result := CompareInteger(Score1.BiggestTile, Score2.BiggestTile);
74 4: Result := CompareInteger(Score1.Moves, Score2.Moves);
75 5: Result := CompareInteger(Score1.UsedUndos, Score2.UsedUndos);
76 6: Result := CompareInteger(Score1.DisabledTiles, Score2.DisabledTiles);
77 7: Result := CompareInteger(Score1.UnmergeableTiles, Score2.UnmergeableTiles);
78 8: Result := CompareDateTime(Score1.Duration, Score2.Duration);
79 end;
80 if ListViewSort1.Order = soDown then Result := -Result;
81 end else Result := 0;
82end;
83
84procedure TFormScore.ListViewSort1Filter(ListViewSort: TListViewSort);
85begin
86 if Assigned(Scores) then Scores.AssignToList(ListViewSort1.List)
87 else begin
88 ListViewSort1.List.Clear;
89 end;
90 FilterList(ListViewSort1.List);
91end;
92
93procedure TFormScore.FormShow(Sender: TObject);
94begin
95 ReloadList;
96end;
97
98procedure TFormScore.ButtonClearClick(Sender: TObject);
99begin
100 if MessageDlg(SClearCaption, SClearQuery, mtConfirmation, [mbOk, mbCancel], 0) = mrOK then begin
101 FScores.Clear;
102 ReloadList;
103 end;
104end;
105
106procedure TFormScore.SetScores(AValue: TScores);
107begin
108 if FScores = AValue then Exit;
109 FScores := AValue;
110 ReloadList;
111end;
112
113procedure TFormScore.FilterList(List: TObjectList<TObject>);
114begin
115end;
116
117procedure TFormScore.ReloadList;
118begin
119 ListViewSort1.Refresh;
120end;
121
122end.
123
Note: See TracBrowser for help on using the repository browser.