| 1 | unit History;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, RegistryEx, Generics.Collections, Board;
|
|---|
| 7 |
|
|---|
| 8 | type
|
|---|
| 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 |
|
|---|
| 49 | implementation
|
|---|
| 50 |
|
|---|
| 51 | { THistoryMoves }
|
|---|
| 52 |
|
|---|
| 53 | procedure THistoryMoves.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
|
|---|
| 54 | var
|
|---|
| 55 | I: Integer;
|
|---|
| 56 | begin
|
|---|
| 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;
|
|---|
| 64 | end;
|
|---|
| 65 |
|
|---|
| 66 | procedure THistoryMoves.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
|
|---|
| 67 | var
|
|---|
| 68 | I: Integer;
|
|---|
| 69 | begin
|
|---|
| 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;
|
|---|
| 78 | end;
|
|---|
| 79 |
|
|---|
| 80 | procedure THistoryMoves.Assign(Source: THistoryMoves);
|
|---|
| 81 | var
|
|---|
| 82 | I: Integer;
|
|---|
| 83 | NewMove: THistoryMove;
|
|---|
| 84 | begin
|
|---|
| 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;
|
|---|
| 91 | end;
|
|---|
| 92 |
|
|---|
| 93 | { THistoryMove }
|
|---|
| 94 |
|
|---|
| 95 | procedure THistoryMove.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
|
|---|
| 96 | begin
|
|---|
| 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;
|
|---|
| 104 | end;
|
|---|
| 105 |
|
|---|
| 106 | procedure THistoryMove.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
|
|---|
| 107 | begin
|
|---|
| 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;
|
|---|
| 115 | end;
|
|---|
| 116 |
|
|---|
| 117 | procedure THistoryMove.Assign(Source: THistoryMove);
|
|---|
| 118 | begin
|
|---|
| 119 | Direction := Source.Direction;
|
|---|
| 120 | NewItemPos := Source.NewItemPos;
|
|---|
| 121 | NewItemValue := Source.NewItemValue;
|
|---|
| 122 | end;
|
|---|
| 123 |
|
|---|
| 124 | procedure THistory.Clear;
|
|---|
| 125 | begin
|
|---|
| 126 | Moves.Clear;
|
|---|
| 127 | SetLength(InitialTiles, 0);
|
|---|
| 128 | end;
|
|---|
| 129 |
|
|---|
| 130 | constructor THistory.Create;
|
|---|
| 131 | begin
|
|---|
| 132 | Moves := THistoryMoves.Create;
|
|---|
| 133 | end;
|
|---|
| 134 |
|
|---|
| 135 | destructor THistory.Destroy;
|
|---|
| 136 | begin
|
|---|
| 137 | FreeAndNil(Moves);
|
|---|
| 138 | inherited;
|
|---|
| 139 | end;
|
|---|
| 140 |
|
|---|
| 141 | procedure THistory.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
|
|---|
| 142 | var
|
|---|
| 143 | I: Integer;
|
|---|
| 144 | begin
|
|---|
| 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;
|
|---|
| 163 | end;
|
|---|
| 164 |
|
|---|
| 165 | procedure THistory.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
|
|---|
| 166 | var
|
|---|
| 167 | I: Integer;
|
|---|
| 168 | begin
|
|---|
| 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;
|
|---|
| 188 | end;
|
|---|
| 189 |
|
|---|
| 190 | procedure THistory.Assign(Source: THistory);
|
|---|
| 191 | var
|
|---|
| 192 | I: Integer;
|
|---|
| 193 | begin
|
|---|
| 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);
|
|---|
| 198 | end;
|
|---|
| 199 |
|
|---|
| 200 | end.
|
|---|
| 201 |
|
|---|