Changeset 290 for trunk/UBuilding.pas


Ignore:
Timestamp:
Mar 25, 2019, 12:51:41 AM (6 years ago)
Author:
chronos
Message:
  • Modified: List and item forms for BuildingKind and Nation converted to generic forms.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UBuilding.pas

    r289 r290  
    66
    77uses
    8   Classes, SysUtils, fgl, UGeometry, DOM, UXMLUtils;
     8  Classes, SysUtils, UItemList;
    99
    1010type
     
    1212  { TBuildingKind }
    1313
    14   TBuildingKind = class
    15     Id: Integer;
    16     Name: string;
     14  TBuildingKind = class(TItem)
    1715    Cost: Integer;
    18     procedure Assign(Source: TBuildingKind);
    19     procedure LoadFromNode(Node: TDOMNode);
    20     procedure SaveToNode(Node: TDOMNode);
     16    class function GetFields: TItemFields; override;
     17    procedure GetValue(Index: Integer; out Value); override;
     18    procedure SetValue(Index: Integer; var Value); override;
     19    class function GetClassSysName: string; override;
     20    class function GetClassName: string; override;
    2121  end;
    2222
    2323  { TBuildingKinds }
    2424
    25   TBuildingKinds = class(TFPGObjectList<TBuildingKind>)
    26     function AddNew(Name: string): TBuildingKind;
    27     procedure LoadFromNode(Node: TDOMNode);
    28     procedure SaveToNode(Node: TDOMNode);
    29     procedure Assign(Source: TBuildingKinds);
     25  TBuildingKinds = class(TItemList)
     26    class function GetItemClass: TItemClass; override;
    3027  end;
    3128
     
    3532
    3633resourcestring
    37   SBuilding = 'Building';
     34  SBuildingKind = 'Building';
     35  SCost = 'Cost';
    3836
    3937
     
    4240{ TBuildingKind }
    4341
    44 procedure TBuildingKind.Assign(Source: TBuildingKind);
     42class function TBuildingKind.GetFields: TItemFields;
    4543begin
    46   Name := Source.Name;
    47   Cost := Source.Cost;
     44  Result := inherited;
     45  Result.AddField(2, 'Cost', SCost, dtInteger);
    4846end;
    4947
    50 procedure TBuildingKind.LoadFromNode(Node: TDOMNode);
     48procedure TBuildingKind.GetValue(Index: Integer; out Value);
    5149begin
    52   Id := ReadInteger(Node, 'Id', 0);
    53   Name := ReadString(Node, 'Name', '');
    54   Cost := ReadInteger(Node, 'Cost', 0);
     50  if Index = 1 then string(Value) := Name
     51  else if Index = 2 then Integer(Value) := Cost
     52  else raise Exception.Create('Unsupported value index ' + IntToStr(Index));
    5553end;
    5654
    57 procedure TBuildingKind.SaveToNode(Node: TDOMNode);
     55procedure TBuildingKind.SetValue(Index: Integer; var Value);
    5856begin
    59   WriteInteger(Node, 'Id', Id);
    60   WriteString(Node, 'Name', Name);
    61   WriteInteger(Node, 'Cost', Cost);
     57  if Index = 1 then Name := string(Value)
     58  else if Index = 2 then Cost := Integer(Value)
     59  else raise Exception.Create('Unsupported value index ' + IntToStr(Index));
     60end;
     61
     62class function TBuildingKind.GetClassSysName: string;
     63begin
     64  Result := 'BuildingKind';
     65end;
     66
     67class function TBuildingKind.GetClassName: string;
     68begin
     69  Result := SBuildingKind;
    6270end;
    6371
    6472{ TBuildingKinds }
    6573
    66 function TBuildingKinds.AddNew(Name: string): TBuildingKind;
     74class function TBuildingKinds.GetItemClass: TItemClass;
    6775begin
    68   Result := TBuildingKind.Create;
    69   Result.Name := Name;
    70   Add(Result);
    71 end;
    72 
    73 procedure TBuildingKinds.LoadFromNode(Node: TDOMNode);
    74 var
    75   Node2: TDOMNode;
    76   NewItem: TBuildingKind;
    77 begin
    78   Count := 0;
    79   Node2 := Node.FirstChild;
    80   while Assigned(Node2) and (Node2.NodeName = 'BuildingKind') do begin
    81     NewItem := TBuildingKind.Create;
    82     NewItem.LoadFromNode(Node2);
    83     Add(NewItem);
    84     Node2 := Node2.NextSibling;
    85   end;
    86 end;
    87 
    88 procedure TBuildingKinds.SaveToNode(Node: TDOMNode);
    89 var
    90   I: Integer;
    91   NewNode2: TDOMNode;
    92 begin
    93   for I := 0 to Count - 1 do
    94   with Items[I] do begin
    95     NewNode2 := Node.OwnerDocument.CreateElement('BuildingKind');
    96     Node.AppendChild(NewNode2);
    97     SaveToNode(NewNode2);
    98   end;
    99 end;
    100 
    101 procedure TBuildingKinds.Assign(Source: TBuildingKinds);
    102 var
    103   I: Integer;
    104 begin
    105   while Count > Source.Count do Delete(Count - 1);
    106   while Count < Source.Count do AddNew('');
    107   for I := 0 to Count - 1 do
    108     Items[I].Assign(Source.Items[I]);
     76  Result := TBuildingKind;
    10977end;
    11078
Note: See TracChangeset for help on using the changeset viewer.