source: trunk/History.pas

Last change on this file was 106, checked in by chronos, 16 months ago
  • Added: Optionally generated unmergeable movable tiles.
File size: 5.7 KB
Line 
1unit History;
2
3interface
4
5uses
6 Classes, SysUtils, RegistryEx, Generics.Collections, Board;
7
8type
9 { THistoryMove }
10
11 THistoryMove = class
12 Direction: TMoveDirection;
13 NewItemPos: TPoint;
14 NewItemValue: Integer;
15 procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
16 procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
17 procedure Assign(Source: THistoryMove);
18 end;
19
20 { THistoryMoves }
21
22 THistoryMoves = class(TObjectList<THistoryMove>)
23 procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
24 procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
25 procedure Assign(Source: THistoryMoves);
26 end;
27
28 TTilePosValue = record
29 Pos: TPoint;
30 Value: Integer;
31 Unmergable: Boolean;
32 end;
33
34 { THistory }
35
36 THistory = class
37 Moves: THistoryMoves;
38 InitialTiles: array of TTilePosValue;
39 DisabledTiles: array of TPoint;
40 procedure Clear;
41 constructor Create;
42 destructor Destroy; override;
43 procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
44 procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
45 procedure Assign(Source: THistory);
46 end;
47
48
49implementation
50
51{ THistoryMoves }
52
53procedure THistoryMoves.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
54var
55 I: Integer;
56begin
57 with Reg do begin
58 CurrentContext := RegContext;
59 WriteInteger('Count', Count);
60 for I := 0 to Count - 1 do begin
61 Items[I].SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\' + IntToStr(I)));
62 end;
63 end;
64end;
65
66procedure THistoryMoves.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
67var
68 I: Integer;
69begin
70 with Reg do begin
71 CurrentContext := RegContext;
72 Count := ReadIntegerWithDefault('Count', 0);
73 for I := 0 to Count - 1 do begin
74 Items[I] := THistoryMove.Create;
75 THistoryMove(Items[I]).LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\' + IntToStr(I)));
76 end;
77 end;
78end;
79
80procedure THistoryMoves.Assign(Source: THistoryMoves);
81var
82 I: Integer;
83 NewMove: THistoryMove;
84begin
85 Clear;
86 for I := 0 to Source.Count - 1 do begin
87 NewMove := THistoryMove.Create;
88 NewMove.Assign(Source[I]);
89 Add(NewMove);
90 end;
91end;
92
93{ THistoryMove }
94
95procedure THistoryMove.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
96begin
97 with Reg do begin
98 CurrentContext := RegContext;
99 WriteInteger('Direction', Integer(Direction));
100 WriteInteger('NewItemPosX', NewItemPos.X);
101 WriteInteger('NewItemPosY', NewItemPos.Y);
102 WriteInteger('NewItemValue', NewItemValue);
103 end;
104end;
105
106procedure THistoryMove.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
107begin
108 with Reg do begin
109 CurrentContext := RegContext;
110 Direction := TMoveDirection(ReadIntegerWithDefault('Direction', Integer(drNone)));
111 NewItemPos := Point(ReadIntegerWithDefault('NewItemPosX', 0),
112 ReadIntegerWithDefault('NewItemPosY', 0));
113 NewItemValue := ReadIntegerWithDefault('NewItemValue', 0);
114 end;
115end;
116
117procedure THistoryMove.Assign(Source: THistoryMove);
118begin
119 Direction := Source.Direction;
120 NewItemPos := Source.NewItemPos;
121 NewItemValue := Source.NewItemValue;
122end;
123
124procedure THistory.Clear;
125begin
126 Moves.Clear;
127 SetLength(InitialTiles, 0);
128end;
129
130constructor THistory.Create;
131begin
132 Moves := THistoryMoves.Create;
133end;
134
135destructor THistory.Destroy;
136begin
137 FreeAndNil(Moves);
138 inherited;
139end;
140
141procedure THistory.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
142var
143 I: Integer;
144begin
145 with Reg do begin
146 CurrentContext := TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\InitialTilesPos');
147 WriteInteger('Count', Length(InitialTiles));
148 for I := 0 to Length(InitialTiles) - 1 do begin
149 WriteInteger('X' + IntToStr(I), InitialTiles[I].Pos.X);
150 WriteInteger('Y' + IntToStr(I), InitialTiles[I].Pos.Y);
151 WriteInteger('Value' + IntToStr(I), InitialTiles[I].Value);
152 WriteBool('Unmergable' + IntToStr(I), InitialTiles[I].Unmergable);
153 end;
154
155 CurrentContext := TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\DisabledTilesPos');
156 for I := 0 to Length(DisabledTiles) - 1 do begin
157 WriteInteger('X' + IntToStr(I), DisabledTiles[I].X);
158 WriteInteger('Y' + IntToStr(I), DisabledTiles[I].Y);
159 end;
160
161 Moves.SaveToRegistry(Reg, RegContext);
162 end;
163end;
164
165procedure THistory.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
166var
167 I: Integer;
168begin
169 with Reg do begin
170 CurrentContext := TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\InitialTilesPos');
171 SetLength(InitialTiles, ReadIntegerWithDefault('Count', 0));
172 for I := 0 to Length(InitialTiles) - 1 do begin
173 InitialTiles[I].Pos := Point(ReadIntegerWithDefault('X' + IntToStr(I), 0),
174 ReadIntegerWithDefault('Y' + IntToStr(I), 0));
175 InitialTiles[I].Value := ReadIntegerWithDefault('Value' + IntToStr(I), 0);
176 InitialTiles[I].Unmergable := ReadBoolWithDefault('Unmergable' + IntToStr(I), False);
177 end;
178
179 CurrentContext := TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\DisabledTilesPos');
180 SetLength(DisabledTiles, ReadIntegerWithDefault('Count', 0));
181 for I := 0 to Length(DisabledTiles) - 1 do begin
182 DisabledTiles[I] := Point(ReadIntegerWithDefault('X' + IntToStr(I), 0),
183 ReadIntegerWithDefault('Y' + IntToStr(I), 0));
184 end;
185
186 Moves.LoadFromRegistry(Reg, RegContext);
187 end;
188end;
189
190procedure THistory.Assign(Source: THistory);
191var
192 I: Integer;
193begin
194 SetLength(InitialTiles, Length(Source.InitialTiles));
195 for I := 0 to Length(Source.InitialTiles) - 1 do
196 InitialTiles[I] := Source.InitialTiles[I];
197 Moves.Assign(Source.Moves);
198end;
199
200end.
201
Note: See TracBrowser for help on using the repository browser.