Changeset 284 for trunk/UNation.pas


Ignore:
Timestamp:
Mar 10, 2019, 6:19:51 PM (6 years ago)
Author:
chronos
Message:
  • Modified: TNation and TNations classes now uses generic TItem and TItemList classes.
  • Added: Translation files for LCL.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UNation.pas

    r283 r284  
    66
    77uses
    8   Classes, SysUtils, fgl, DOM, UXMLUtils, Graphics;
     8  Classes, SysUtils, fgl, DOM, UXMLUtils, Graphics, UItemList;
    99
    1010type
     
    1212  { TNation }
    1313
    14   TNation = class
    15     Id: Integer;
    16     Name: string;
     14  TNation = class(TItem)
    1715    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;
    2122  end;
    2223
    2324  { TNations }
    2425
    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;
    3028    procedure LoadFromNode(Node: TDOMNode);
    3129    procedure SaveToNode(Node: TDOMNode);
    3230    constructor Create(FreeObjects: Boolean = True);
    33     procedure Assign(Source: TNations);
    3431  end;
     32
     33resourcestring
     34  SNation = 'Nation';
     35
    3536
    3637implementation
    3738
     39uses
     40  UCommon;
     41
    3842{ TNation }
    3943
    40 procedure TNation.Assign(Source: TNation);
     44function TNation.GetFields: TItemFields;
    4145begin
    42   Id := Source.Id;
    43   Name := Source.Name;
    44   Color := Source.Color;
     46  inherited;
     47  Result.AddField('Name');
     48  Result.AddField('Color');
     49end;
     50
     51procedure TNation.GetValue(Index: Integer; out Value);
     52begin
     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));
     56end;
     57
     58procedure TNation.SetValue(Index: Integer; var Value);
     59begin
     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));
     63end;
     64
     65procedure TNation.Assign(Source: TItem);
     66begin
     67  inherited;
     68  Color := TNation(Source).Color;
    4569end;
    4670
    4771procedure TNation.LoadFromNode(Node: TDOMNode);
    4872begin
    49   Id := ReadInteger(Node, 'Id', 0);
    50   Name := ReadString(Node, 'Name', '');
     73  inherited;
    5174  Color := ReadInteger(Node, 'Color', 0);
    5275end;
     
    5477procedure TNation.SaveToNode(Node: TDOMNode);
    5578begin
    56   WriteInteger(Node, 'Id', Id);
    57   WriteString(Node, 'Name', Name);
     79  inherited;
    5880  WriteInteger(Node, 'Color', Color);
    5981end;
     
    6183{ TNations }
    6284
    63 function TNations.FindById(Id: Integer): TNation;
    64 var
    65   I: Integer;
     85function TNations.GetItemClass: TItemClass;
    6686begin
    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;
    8588end;
    8689
     
    116119begin
    117120  inherited;
    118   NewId := 1;
    119 end;
    120 
    121 procedure TNations.Assign(Source: TNations);
    122 var
    123   I: Integer;
    124 begin
    125   while Count > Source.Count do Delete(Count - 1);
    126   while Count < Source.Count do AddNew('');
    127   for I := 0 to Count - 1 do
    128     Items[I].Assign(Source.Items[I]);
    129121end;
    130122
Note: See TracChangeset for help on using the changeset viewer.