Changeset 298 for trunk/UItemList.pas


Ignore:
Timestamp:
Jul 10, 2019, 5:35:15 PM (5 years ago)
Author:
chronos
Message:
  • Added: New item list type reference. Used in players for reference to selected nation.
  • Modified: New game players selection migrated to dynamic item/list forms.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UItemList.pas

    r296 r298  
    99
    1010type
     11  TItemList = class;
    1112  TUndefinedEnum = (eeNone);
    1213
    1314  TDataType = (dtNone, dtString, dtBoolean, dtInteger, dtFloat, dtColor,
    14     dtTime, dtDate, dtDateTime, dtEnumeration);
     15    dtTime, dtDate, dtDateTime, dtEnumeration, dtReference);
    1516
    1617  { TItemField }
     
    5455    function GetValueBoolean(Index: Integer): Boolean;
    5556    function GetValueEnumeration(Index: Integer): TUndefinedEnum;
     57    function GetValueReference(Index: Integer): TItem;
    5658    function GetValueAsText(Index: Integer): string;
    5759    procedure SetValue(Index: Integer; var Value); virtual;
     
    6163    procedure SetValueBoolean(Index: Integer; Value: Boolean);
    6264    procedure SetValueEnumeration(Index: Integer; Value: TUndefinedEnum);
     65    procedure SetValueReference(Index: Integer; Value: TItem);
    6366    procedure Assign(Source: TItem); virtual;
    6467    procedure LoadFromNode(Node: TDOMNode); virtual;
     
    6669    class function GetClassSysName: string; virtual;
    6770    class function GetClassName: string; virtual;
     71    function GetReferenceList(Index: Integer): TItemList; virtual;
     72    constructor Create; virtual;
    6873  end;
    6974
     
    8186    function GetNewId: Integer;
    8287    procedure Assign(Source: TItemList); virtual;
    83     function AddItem(Name: string): TItem; virtual;
     88    function AddItem(Name: string = ''): TItem; virtual;
     89    function CreateItem(Name: string = ''): TItem; virtual;
    8490    procedure LoadFromNode(Node: TDOMNode); virtual;
    8591    procedure SaveToNode(Node: TDOMNode); virtual;
     
    8995const
    9096  DataTypeStr: array[TDataType] of string = ('None', 'String', 'Boolean',
    91     'Integer', 'Float', 'Color', 'Time', 'Date', 'DateTime', 'Enumeration');
     97    'Integer', 'Float', 'Color', 'Time', 'Date', 'DateTime', 'Enumeration',
     98    'Reference');
    9299
    93100resourcestring
     
    136143  while Count < Source.Count do AddItem('');
    137144  for I := 0 to Count - 1 do
    138     Items[I].Assign(Source.Items[I]);
     145    TItem(Items[I]).Assign(Source.Items[I]);
    139146end;
    140147
    141148function TItemList.AddItem(Name: string): TItem;
     149begin
     150  Result := CreateItem(Name);
     151  Result.Id := GetNewId;
     152  Add(Result);
     153end;
     154
     155function TItemList.CreateItem(Name: string): TItem;
    142156begin
    143157  Result := GetItemClass.Create;
    144158  Result.Name := Name;
    145   Result.Id := GetNewId;
    146   Add(Result);
    147159end;
    148160
     
    155167  Node2 := Node.FirstChild;
    156168  while Assigned(Node2) and (Node2.NodeName = UnicodeString(GetItemClass.GetClassSysName)) do begin
    157     NewItem := GetItemClass.Create;
     169    NewItem := CreateItem;
    158170    NewItem.LoadFromNode(Node2);
    159171    Add(NewItem);
     
    177189constructor TItemList.Create(FreeObjects: Boolean);
    178190begin
    179   inherited Create(FreeObjects);
     191  inherited;
    180192  NewId := 1;
    181193end;
     
    273285    SetValueEnumeration(Field.Index, Source.GetValueEnumeration(Field.Index));
    274286  end else
     287  if Field.DataType = dtReference then begin
     288    SetValueReference(Field.Index, Source.GetValueReference(Field.Index));
     289  end else
    275290    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
    276291end;
    277292
    278293procedure TItem.LoadValueFromNode(Node: TDOMNode; Field: TItemField);
     294var
     295  ReadId: Integer;
     296  ReferenceList: TItemList;
    279297begin
    280298  if Field.DataType = dtString then begin
     
    293311    SetValueEnumeration(Field.Index, TUndefinedEnum(ReadInteger(Node, Field.SysName, 0)));
    294312  end else
     313  if Field.DataType = dtReference then begin
     314    ReadId := ReadInteger(Node, Field.SysName, 0);
     315    ReferenceList := GetReferenceList(Field.Index);
     316    if (ReadId > 0) and Assigned(ReferenceList) then
     317      SetValueReference(Field.Index, TItem(ReferenceList.List[ReadId]));
     318  end else
    295319    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
    296320end;
    297321
    298322procedure TItem.SaveValueToNode(Node: TDOMNode; Field: TItemField);
     323var
     324  Item: TItem;
    299325begin
    300326  if Field.DataType = dtString then begin
     
    312338  if Field.DataType = dtEnumeration then begin
    313339    WriteInteger(Node, Field.SysName, Integer(GetValueEnumeration(Field.Index)));
     340  end else
     341  if Field.DataType = dtReference then begin
     342    Item := TItem(GetValueReference(Field.Index));
     343    if Assigned(Item) then WriteInteger(Node, Field.SysName, Item.Id)
     344      else WriteInteger(Node, Field.SysName, 0);
    314345  end else
    315346    raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
     
    329360  Fields := GetFields;
    330361  try
    331     Result.Assign(Fields[Index]);
     362    Result.Assign(Fields.SearchByIndex(Index));
    332363  finally
    333364    Fields.Free;
     
    365396end;
    366397
     398function TItem.GetValueReference(Index: Integer): TItem;
     399begin
     400  GetValue(Index, Result);
     401end;
     402
    367403function TItem.GetValueAsText(Index: Integer): string;
    368404var
    369   Fields: TItemFields;
    370405  Field: TItemField;
    371 begin
    372   Fields := GetFields;
    373   Field := GetField(Fields.IndexOf(Fields.SearchByIndex(Index)));
    374   if Field.DataType = dtInteger then Result := IntToStr(GetValueInteger(Index))
    375   else if Field.DataType = dtString then Result := GetValueString(Index)
    376   else if Field.DataType = dtColor then Result := ''
    377   else if Field.DataType = dtEnumeration then Result := Field.EnumStates[Integer(GetValueEnumeration(Index))]
    378   else if Field.DataType = dtBoolean then begin
    379     if GetValueBoolean(Index) then Result := SYes else Result := SNo;
    380   end else
    381     raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
    382   Field.Free;
    383   Fields.Free;
     406  Item: TItem;
     407begin
     408  Field := GetField(Index);
     409  try
     410    if Field.DataType = dtInteger then Result := IntToStr(GetValueInteger(Index))
     411    else if Field.DataType = dtString then Result := GetValueString(Index)
     412    else if Field.DataType = dtColor then Result := ''
     413    else if Field.DataType = dtEnumeration then Result := Field.EnumStates[Integer(GetValueEnumeration(Index))]
     414    else if Field.DataType = dtReference then begin
     415      Item := TItem(GetValueReference(Index));
     416      if Assigned(Item) then Result := Item.Name
     417        else Result := '';
     418    end else if Field.DataType = dtBoolean then begin
     419      if GetValueBoolean(Index) then Result := SYes else Result := SNo;
     420    end else
     421      raise Exception.Create(Format(SUnsupportedDataType, [DataTypeStr[Field.DataType]]));
     422  finally
     423    Field.Free;
     424  end;
    384425end;
    385426
     
    411452procedure TItem.SetValueEnumeration(Index: Integer;
    412453  Value: TUndefinedEnum);
     454begin
     455  SetValue(Index, Value);
     456end;
     457
     458procedure TItem.SetValueReference(Index: Integer; Value: TItem);
    413459begin
    414460  SetValue(Index, Value);
     
    474520end;
    475521
     522function TItem.GetReferenceList(Index: Integer): TItemList;
     523begin
     524  Result := nil;
     525end;
     526
     527constructor TItem.Create;
     528begin
     529end;
     530
    476531end.
    477532
Note: See TracChangeset for help on using the changeset viewer.