Changeset 32


Ignore:
Timestamp:
Apr 19, 2015, 12:25:37 AM (10 years ago)
Author:
chronos
Message:
  • Modified: More TTrack structures cleanup and preparation for separation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r31 r32  
    2222  TTrackPoints = class;
    2323  TTrack = class;
     24  TTrackLinks = class;
    2425
    2526  { TMapStation }
     
    7172    PositionDesigned: TPoint;
    7273    Pending: Boolean;
     74    Track: TTrack;
    7375    NeighPoints: TTrackPoints;
    74     Track: TTrack;
     76    NeighLinks: TTrackLinks;
     77    procedure Connect(TrackPoint: TTrackPoint);
     78    procedure Disconnect(TrackPoint: TTrackPoint);
    7579    function GetDown: TTrackPoint;
    7680    function GetUp: TTrackPoint;
     
    9498  TTrackLink = class
    9599    Points: TTrackPoints;
    96     PointDown: TTrackPoint;
    97     PointUp: TTrackPoint;
    98     Line: TMetroLine;
    99100    Shift: TPoint;
    100101    Track: TTrack;
     
    103104  end;
    104105
     106  { TTrackLinks }
     107
    105108  TTrackLinks = class(TObjectList)
     109    Track: TTrack;
     110    function SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
     111    function AddNew: TTrackLink;
    106112  end;
    107113
     
    110116    Links: TTrackLinks;
    111117    Line: TMetroLine;
     118    procedure RouteTrack(TP1, TP2: TTrackPoint);
     119    procedure RemoveTrackBetween(TP1, TP2: TTrackPoint);
    112120    constructor Create;
    113121    destructor Destroy; override;
     
    150158    procedure ConnectStation(Station: TMapStation; LineStationDown, LineStationUp: TLineStation);
    151159    procedure DisconnectStation(ALineStation: TLineStation);
    152     procedure RouteTrack(TP1, TP2: TTrackPoint);
    153     procedure RemoveTrackBetween(TP1, TP2: TTrackPoint);
    154160    function GetTrackLength: Integer;
    155161    constructor Create;
     
    335341  //TimePerSecond = (60 * OneMinute);
    336342  TimePerSecond = (60 * OneMinute);
    337   NewStationPeriod = 1;
     343  NewStationPeriod = 0.01;
    338344  NewShapePeriod = 10;
    339345  NewTrainPeriod = 7; // Each week
     
    350356  SZeroZoomNotAlowed = 'Zero zoom not allowed';
    351357
     358{ TTrackLinks }
     359
     360function TTrackLinks.SearchPoints(Point1, Point2: TTrackPoint): TTrackLink;
     361var
     362  I: Integer;
     363begin
     364  I := 0;
     365  while (I < 0) and
     366  ((TTrackLink(Items[I]).Points.First <> Point1) or (TTrackLink(Items[I]).Points.Last <> Point2))
     367  and ((TTrackLink(Items[I]).Points.First <> Point2) or (TTrackLink(Items[I]).Points.Last <> Point1)) do
     368  Inc(I);
     369  if I < 0 then Result := TTrackLink(Items[I])
     370    else Result := nil;
     371end;
     372
     373function TTrackLinks.AddNew: TTrackLink;
     374begin
     375  Result := TTrackLink.Create;
     376  Result.Track := Track;
     377end;
     378
    352379{ TTrackPoints }
    353380
     
    365392  Points.Track := Self;
    366393  Links := TTrackLinks.Create;
     394  Links.Track := Self;
    367395end;
    368396
     
    475503begin
    476504  I := 0;
    477   while (I < Count) and (TTrackLink(Items[I]).PointUp <> TrackPoint) and (TTrackLink(Items[I]) <> Skip) do Inc(I);
     505  while (I < Count) and (TTrackLink(Items[I]).Points[1] <> TrackPoint) and (TTrackLink(Items[I]) <> Skip) do Inc(I);
    478506  if I < Count then Result := TTrackLink(Items[I])
    479507    else Result := nil;
     
    485513begin
    486514  I := 0;
    487   while (I < Count) and (TTrackLink(Items[I]).PointDown <> TrackPoint) and (TTrackLink(Items[I]) <> Skip) do Inc(I);
     515  while (I < Count) and (TTrackLink(Items[I]).Points[0] <> TrackPoint) and (TTrackLink(Items[I]) <> Skip) do Inc(I);
    488516  if I < Count then Result := TTrackLink(Items[I])
    489517    else Result := nil;
     
    517545
    518546{ TTrackPoint }
     547
     548procedure TTrackPoint.Connect(TrackPoint: TTrackPoint);
     549var
     550  NewLink: TTrackLink;
     551begin
     552  if NeighPoints.IndexOf(TrackPoint) = -1 then begin
     553    NeighPoints.Add(TrackPoint);
     554    TrackPoint.NeighPoints.Add(Self);
     555
     556    // Add new link to both self and connected track point
     557    NewLink := Track.Links.AddNew;
     558    NewLink.Points.Add(TrackPoint);
     559    NewLink.Points.Add(Self);
     560    NeighLinks.Add(NewLink);
     561    TrackPoint.NeighLinks.Add(NewLink);
     562    Track.Links.Add(NewLink);
     563  end else raise Exception.Create('Trying to connect already connected track point');
     564end;
     565
     566procedure TTrackPoint.Disconnect(TrackPoint: TTrackPoint);
     567var
     568  Index: Integer;
     569  Link: TTrackLink;
     570begin
     571  Index := NeighPoints.IndexOf(TrackPoint);
     572  if NeighPoints.IndexOf(TrackPoint) <> -1 then begin
     573    NeighPoints.Delete(Index);
     574    TrackPoint.NeighPoints.Remove(Self);
     575
     576    // Remove link from both track points
     577    Link := NeighLinks.SearchPoints(Self, TrackPoint);
     578    NeighLinks.Remove(Link);
     579    TrackPoint.NeighLinks.Remove(Link);
     580    Track.Links.Remove(Link);
     581  end else raise Exception.Create('Trying to disconnect not connected track point');
     582end;
    519583
    520584function TTrackPoint.GetDown: TTrackPoint;
     
    573637  NeighPoints := TTrackPoints.Create;
    574638  NeighPoints.OwnsObjects := False;
     639  NeighLinks := TTrackLinks.Create;
     640  NeighLinks.OwnsObjects := False;
    575641end;
    576642
    577643destructor TTrackPoint.Destroy;
    578644begin
     645  NeighLinks.Free;
    579646  NeighPoints.Free;
    580647  inherited Destroy;
     
    667734  L := Step;
    668735  repeat
    669     Result.Position := Point(Trunc(Engine.Map.Size.X / 2 + Cos(Angle) * L),
     736    Result.Position := Point(Trunc(Engine.Map.Size.X / 2 + Cos(Angle) * L * 1.5),
    670737      Trunc(Engine.Map.Size.Y / 2 + Sin(Angle) * L));
    671738    MinD := High(Integer);
     
    772839
    773840  if Assigned(LineStationDown) then
    774     RouteTrack(NewLineStation.TrackPoint.GetDown, NewLineStation.TrackPoint);
     841    Track.RouteTrack(NewLineStation.TrackPoint.GetDown, NewLineStation.TrackPoint);
    775842  if Assigned(LineStationUp) then
    776     RouteTrack(NewLineStation.TrackPoint, NewLineStation.TrackPoint.GetUp);
     843    Track.RouteTrack(NewLineStation.TrackPoint, NewLineStation.TrackPoint.GetUp);
    777844
    778845  // Place one train if at least two stations present
     
    836903
    837904  if ((Index - 1) >= 0) and (Index < Track.Points.Count) then
    838     RouteTrack(TTrackPoint(Track.Points[Index - 1]), TTrackPoint(Track.Points[Index]));
     905    Track.RouteTrack(TTrackPoint(Track.Points[Index - 1]), TTrackPoint(Track.Points[Index]));
    839906
    840907  ALineStation.MapStation.Lines.Remove(Self);
     
    860927end;
    861928
    862 procedure TMetroLine.RouteTrack(TP1, TP2: TTrackPoint);
     929procedure TTrack.RouteTrack(TP1, TP2: TTrackPoint);
    863930var
    864931  NewTrackPoint: TTrackPoint;
     
    868935begin
    869936  RemoveTrackBetween(TP1, TP2);
    870   Index1 := Track.Points.IndexOf(TP1);
    871   Index2 := Track.Points.IndexOf(TP2);
    872   P1 := TTrackPoint(Track.Points[Index1]).Position;
    873   P2 := TTrackPoint(Track.Points[Index2]).Position;
    874   NewTrackPoint := Track.Points.AddNew;
     937  Index1 := Points.IndexOf(TP1);
     938  Index2 := Points.IndexOf(TP2);
     939  P1 := TTrackPoint(Points[Index1]).Position;
     940  P2 := TTrackPoint(Points[Index2]).Position;
     941  NewTrackPoint := Points.AddNew;
    875942  Delta := Point(P2.X - P1.X, P2.Y - P1.Y);
    876943  if Abs(Delta.X) > Abs(Delta.Y) then begin
     
    881948    NewTrackPoint.PositionDesigned := NewTrackPoint.Position;
    882949  end;
    883   Track.Points.Insert(Index1 + 1, NewTrackPoint);
    884 end;
    885 
    886 procedure TMetroLine.RemoveTrackBetween(TP1, TP2: TTrackPoint);
     950  Points.Insert(Index1 + 1, NewTrackPoint);
     951end;
     952
     953procedure TTrack.RemoveTrackBetween(TP1, TP2: TTrackPoint);
    887954var
    888955  Index1, Index2: Integer;
     
    890957  I: Integer;
    891958begin
    892   Index1 := Track.Points.IndexOf(TP1);
    893   Index2 := Track.Points.IndexOf(TP2);
     959  Index1 := Points.IndexOf(TP1);
     960  Index2 := Points.IndexOf(TP2);
    894961  if (Index1 = -1) then
    895962    raise Exception.Create('TrackPoint1 not found');
     
    902969  end;
    903970  for I := 1 to Index2 - Index1 - 1 do
    904     Track.Points.Delete(Index1 + 1);
     971    Points.Delete(Index1 + 1);
    905972end;
    906973
     
    10561123    if Index > 0 then begin
    10571124      NewTrack := TTrackLink.Create;
    1058       NewTrack.PointDown := TTrackPoint(Line.Track.Points[Index - 1]);
    1059       NewTrack.PointUp := TTrackPoint(Line.Track.Points[Index]);
     1125      NewTrack.Points.Count := 2;
     1126      NewTrack.Points[0] := TTrackPoint(Line.Track.Points[Index - 1]);
     1127      NewTrack.Points[1] := TTrackPoint(Line.Track.Points[Index]);
    10601128      Tracks.Add(NewTrack);
    10611129    end;
    10621130    if Index < (Line.Track.Points.Count - 1) then begin
    10631131      NewTrack := TTrackLink.Create;
    1064       NewTrack.PointDown := TTrackPoint(Line.Track.Points[Index + 1]);
    1065       NewTrack.PointUp := TTrackPoint(Line.Track.Points[Index]);
     1132      NewTrack.Points.Count := 2;
     1133      NewTrack.Points[0] := TTrackPoint(Line.Track.Points[Index + 1]);
     1134      NewTrack.Points[1] := TTrackPoint(Line.Track.Points[Index]);
    10661135      Tracks.Add(NewTrack);
    10671136    end;
     
    10711140  TPAngleGroup := TTrackPointsAngleGroup.Create;
    10721141  for I := 0 to Tracks.Count - 1 do begin
    1073     Angle := ArcTan2(TTrackLink(Tracks[I]).PointDown.PositionDesigned.Y - Position.Y,
    1074       TTrackLink(Tracks[I]).PointDown.PositionDesigned.X - Position.X);
     1142    Angle := ArcTan2(TTrackPoint(TTrackLink(Tracks[I]).Points[0]).PositionDesigned.Y - Position.Y,
     1143      TTrackPoint(TTrackLink(Tracks[I]).Points[0]).PositionDesigned.X - Position.X);
    10751144    GroupItem := TPAngleGroup.SearchAngle(Angle);
    10761145    if not Assigned(GroupItem) then begin
     
    10921161        Shift.X := Trunc(MetroLineThickness * Cos(HAngle) * (J - (Tracks.Count - 1) / 2));
    10931162        Shift.Y := Trunc(MetroLineThickness * Sin(HAngle) * (J - (Tracks.Count - 1) / 2));
    1094         PointDown.PositionShift := Shift;
    1095         PointUp.PositionShift := Shift;
     1163        TTrackPoint(Points[0]).PositionShift := Shift;
     1164        TTrackPoint(Points[1]).PositionShift := Shift;
    10961165      end;
    10971166  end;
     
    12361305begin
    12371306  Result := TTrackLink.Create;
    1238   Result.PointDown := nil;
    1239   Result.PointUp := nil;
     1307  Result.Points.Count := 2;
     1308  Result.Points[0] := nil;
     1309  Result.Points[1] := nil;
    12401310  I := 0;
    12411311  MinD := High(Integer);
     
    12461316      if (D < MinD) and (D < TrackClickDistance) then begin
    12471317        MinD := D;
    1248         Result.PointDown := TTrackPoint(Track.Points[T - 1]);
    1249         Result.PointUp := TTrackPoint(Track.Points[T]);
     1318        Result.Points[0] := TTrackPoint(Track.Points[T - 1]);
     1319        Result.Points[1] := TTrackPoint(Track.Points[T]);
    12501320      end;
    12511321    end;
     
    18491919      end;
    18501920      FocusedTrack := GetTrackOnPos(View.PointDestToSrc(Position));
    1851       if Assigned(FocusedTrack.PointDown) then begin
    1852         SelectedTrain.Line := FocusedTrack.PointDown.Track.Line;
     1921      if Assigned(FocusedTrack.Points[0]) then begin
     1922        SelectedTrain.Line := TTrackPoint(FocusedTrack.Points[0]).Track.Line;
    18531923        SelectedTrain.Line.Trains.Add(SelectedTrain);
    1854         SelectedTrain.BaseTrackPoint := FocusedTrack.PointDown;
     1924        SelectedTrain.BaseTrackPoint := TTrackPoint(FocusedTrack.Points[0]);
    18551925      end else
    1856       if Assigned(FocusedTrack.PointDown) then begin
    1857         SelectedTrain.Line := FocusedTrack.PointUp.Track.Line;
     1926      if Assigned(FocusedTrack.Points[1]) then begin
     1927        SelectedTrain.Line := TTrackPoint(FocusedTrack.Points[1]).Track.Line;
    18581928        SelectedTrain.Line.Trains.Add(SelectedTrain);
    1859         SelectedTrain.BaseTrackPoint := FocusedTrack.PointUp;
     1929        SelectedTrain.BaseTrackPoint := TTrackPoint(FocusedTrack.Points[1]);
    18601930      end;
    18611931      FocusedTrack.Free;
     
    19161986    // Line selection
    19171987    Track := GetTrackOnPos(View.PointDestToSrc(Position));
    1918     if Assigned(Track) and Assigned(Track.PointDown) and Assigned(Track.PointUp) then begin
    1919       SelectedLine := Track.PointDown.Track.Line;
    1920 
    1921       TrackStationDown := Track.PointDown;
     1988    if Assigned(Track) and Assigned(Track.Points[0]) and Assigned(Track.Points[1]) then begin
     1989      SelectedLine := TTrackPoint(Track.Points[0]).Track.Line;
     1990
     1991      TrackStationDown := TTrackPoint(Track.Points[0]);
    19221992      NewIndex := TrackStationDown.Track.Points.IndexOf(TrackStationDown);
    19231993      while Assigned(TrackStationDown) and (not Assigned(TrackStationDown.LineStation)) do begin
     
    19261996          else TrackStationDown := nil;
    19271997      end;
    1928       TrackStationUp := Track.PointUp;
     1998      TrackStationUp := TTrackPoint(Track.Points[1]);
    19291999      NewIndex := TrackStationUp.Track.Points.IndexOf(TrackStationDown);
    19302000      while Assigned(TrackStationUp) and (not Assigned(TrackStationUp.LineStation)) do begin
Note: See TracChangeset for help on using the changeset viewer.