Changeset 284 for trunk/UNation.pas
- Timestamp:
- Mar 10, 2019, 6:19:51 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UNation.pas
r283 r284 6 6 7 7 uses 8 Classes, SysUtils, fgl, DOM, UXMLUtils, Graphics ;8 Classes, SysUtils, fgl, DOM, UXMLUtils, Graphics, UItemList; 9 9 10 10 type … … 12 12 { TNation } 13 13 14 TNation = class 15 Id: Integer; 16 Name: string; 14 TNation = class(TItem) 17 15 Color: TColor; 18 procedure Assign(Source: TNation); 19 procedure LoadFromNode(Node: TDOMNode); 20 procedure SaveToNode(Node: TDOMNode); 16 function GetFields: TItemFields; override; 17 procedure GetValue(Index: Integer; out Value); override; 18 procedure SetValue(Index: Integer; var Value); override; 19 procedure Assign(Source: TItem); override; 20 procedure LoadFromNode(Node: TDOMNode); override; 21 procedure SaveToNode(Node: TDOMNode); override; 21 22 end; 22 23 23 24 { TNations } 24 25 25 TNations = class(TFPGObjectList<TNation>) 26 NewId: Integer; 27 function FindById(Id: Integer): TNation; 28 function GetNewId: Integer; 29 function AddNew(Name: string): TNation; 26 TNations = class(TItemList) 27 function GetItemClass: TItemClass; override; 30 28 procedure LoadFromNode(Node: TDOMNode); 31 29 procedure SaveToNode(Node: TDOMNode); 32 30 constructor Create(FreeObjects: Boolean = True); 33 procedure Assign(Source: TNations);34 31 end; 32 33 resourcestring 34 SNation = 'Nation'; 35 35 36 36 37 implementation 37 38 39 uses 40 UCommon; 41 38 42 { TNation } 39 43 40 procedure TNation.Assign(Source: TNation);44 function TNation.GetFields: TItemFields; 41 45 begin 42 Id := Source.Id; 43 Name := Source.Name; 44 Color := Source.Color; 46 inherited; 47 Result.AddField('Name'); 48 Result.AddField('Color'); 49 end; 50 51 procedure TNation.GetValue(Index: Integer; out Value); 52 begin 53 if Index = 0 then string(Value) := Name 54 else if Index = 1 then TColor(Value) := Color 55 else raise Exception.Create('Unsupported value index ' + IntToStr(Index)); 56 end; 57 58 procedure TNation.SetValue(Index: Integer; var Value); 59 begin 60 if Index = 0 then Name := string(Value) 61 else if Index = 1 then Color := TColor(Value) 62 else raise Exception.Create('Unsupported value index ' + IntToStr(Index)); 63 end; 64 65 procedure TNation.Assign(Source: TItem); 66 begin 67 inherited; 68 Color := TNation(Source).Color; 45 69 end; 46 70 47 71 procedure TNation.LoadFromNode(Node: TDOMNode); 48 72 begin 49 Id := ReadInteger(Node, 'Id', 0); 50 Name := ReadString(Node, 'Name', ''); 73 inherited; 51 74 Color := ReadInteger(Node, 'Color', 0); 52 75 end; … … 54 77 procedure TNation.SaveToNode(Node: TDOMNode); 55 78 begin 56 WriteInteger(Node, 'Id', Id); 57 WriteString(Node, 'Name', Name); 79 inherited; 58 80 WriteInteger(Node, 'Color', Color); 59 81 end; … … 61 83 { TNations } 62 84 63 function TNations.FindById(Id: Integer): TNation; 64 var 65 I: Integer; 85 function TNations.GetItemClass: TItemClass; 66 86 begin 67 I := 0; 68 while (I < Count) and (Items[I].Id <> Id) do Inc(I); 69 if I < Count then Result := Items[I] 70 else Result := nil; 71 end; 72 73 function TNations.GetNewId: Integer; 74 begin 75 Result := NewId; 76 Inc(NewId); 77 end; 78 79 function TNations.AddNew(Name: string): TNation; 80 begin 81 Result := TNation.Create; 82 Result.Name := Name; 83 Result.Id := GetNewId; 84 Add(Result); 87 Result := TNation; 85 88 end; 86 89 … … 116 119 begin 117 120 inherited; 118 NewId := 1;119 end;120 121 procedure TNations.Assign(Source: TNations);122 var123 I: Integer;124 begin125 while Count > Source.Count do Delete(Count - 1);126 while Count < Source.Count do AddNew('');127 for I := 0 to Count - 1 do128 Items[I].Assign(Source.Items[I]);129 121 end; 130 122
Note:
See TracChangeset
for help on using the changeset viewer.