Changeset 18


Ignore:
Timestamp:
Mar 27, 2015, 11:36:33 PM (10 years ago)
Author:
chronos
Message:
  • Added: If track line is grabbed then design time floating track attached to mouse is displayed.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UEngine.pas

    r17 r18  
    6565  TTrackPoints = class(TObjectList)
    6666    Line: TMetroLine;
     67  end;
     68
     69  TTrack = record
     70    PointDown: TTrackPoint;
     71    PointUp: TTrackPoint;
    6772  end;
    6873
     
    150155  private
    151156    LastMousePos: TPoint;
    152     LastSelectedStation: TMapStation;
     157    LastFocusedStation: TMapStation;
    153158    MouseHold: Boolean;
    154159    LastNewStationTime: TDateTime;
     
    162167    function GetExistStationShapes: TStationShapeSet;
    163168    function GetStationOnPos(Pos: TPoint): TMapStation;
    164     function GetLineOnPos(Pos: TPoint): TMetroLine;
     169    function GetTrackOnPos(Pos: TPoint): TTrack;
    165170    procedure DrawLine(Canvas: TCanvas; Pos: TPoint);
    166171    procedure DrawShape(Canvas: TCanvas; Position: TPoint; Shape: TStationShape;
     
    182187    View: TView;
    183188    SelectedLine: TMetroLine;
    184     SelectedStation: TMapStation;
     189    TrackStationDown: TTrackPoint;
     190    TrackStationUp: TTrackPoint;
    185191    ServedPassengerCount: Integer;
    186192    State: TGameState;
     
    620626end;
    621627
    622 function TEngine.GetLineOnPos(Pos: TPoint): TMetroLine;
     628function TEngine.GetTrackOnPos(Pos: TPoint): TTrack;
    623629var
    624630  I: Integer;
     
    626632  D: Integer;
    627633  MinD: Integer;
    628 const
    629   Distance = 30;
    630 begin
    631   Result := nil;
     634begin
     635  Result.PointDown := nil;
     636  Result.PointUp := nil;
    632637  I := 0;
    633638  MinD := High(Integer);
     
    638643      if (D < MinD) and (D < TrackClickDistance) then begin
    639644        MinD := D;
    640         Result := TMetroLine(Lines[I]);
     645        Result.PointDown := TTrackPoint(TrackPoints[T - 1]);
     646        Result.PointUp := TTrackPoint(TrackPoints[T]);
    641647      end;
    642648    end;
     
    10501056procedure TEngine.MouseMove(Position: TPoint);
    10511057var
    1052   Station: TMapStation;
     1058  FocusedStation: TMapStation;
     1059  Line: TMetroLine;
    10531060begin
    10541061  LastMousePos := Position;
    10551062  if MouseHold then begin
    1056     if Assigned(SelectedLine) then begin
    1057       Station := GetStationOnPos(Position);
    1058       if not Assigned(LastSelectedStation) and Assigned(Station) then begin
    1059         if (SelectedLine.LineStations.SearchMapStation(Station) = nil) then begin
    1060           SelectedLine.ConnectStation(Station);
    1061           SelectedStation := Station;
     1063    if Assigned(TrackStationDown) then begin
     1064      FocusedStation := GetStationOnPos(Position);
     1065      if not Assigned(LastFocusedStation) and Assigned(FocusedStation) then begin
     1066        if (TrackStationDown.Line.LineStations.SearchMapStation(FocusedStation) = nil) then begin
     1067          TrackStationDown.Line.ConnectStation(FocusedStation);
     1068          TrackStationDown := TTrackPoint(TrackStationDown.Line.TrackPoints.Last);
    10621069        end else
    1063         if (SelectedLine.LineStations.Count > 0) and (TLineStation(SelectedLine.LineStations.Last).MapStation = Station) then begin
    1064           SelectedLine.DisconnectStation(Station);
    1065           if SelectedLine.LineStations.Count > 0 then
    1066             SelectedStation := TLineStation(SelectedLine.LineStations.Last).MapStation
    1067             else SelectedStation := nil;
    1068         end else if (SelectedLine.LineStations.Count > 0) and (TLineStation(SelectedLine.LineStations.First).MapStation = Station) then begin
    1069           SelectedLine.ConnectStation(Station);
    1070           SelectedStation := Station;
     1070        if (SelectedLine.TrackPoints.Count > 0) and (TLineStation(TrackStationDown.Line.LineStations.Last).MapStation = FocusedStation) then begin
     1071          Line := TrackStationDown.Line;
     1072          TrackStationDown.Line.DisconnectStation(FocusedStation);
     1073          TrackStationDown := TTrackPoint(Line.TrackPoints.Last);
     1074        end else if (TrackStationDown.Line.LineStations.Count > 0) and (TLineStation(TrackStationDown.Line.LineStations.Last).MapStation = FocusedStation) then begin
     1075          TrackStationDown.Line.ConnectStation(FocusedStation);
     1076          TrackStationDown := TTrackPoint(TrackStationDown.Line.TrackPoints.Last);
    10711077        end;
    10721078      end;
    1073       LastSelectedStation := Station;
     1079      LastFocusedStation := FocusedStation;
    10741080    end;
    10751081  end;
     
    10831089begin
    10841090  MouseHold := False;
     1091  TrackStationDown := nil;
     1092  TrackStationUp := nil;
     1093
    10851094  if Button = mbLeft then begin
    1086     SelectedStation := nil;
    10871095
    10881096    // Line color selection
     
    11051113  I: Integer;
    11061114  NewLine: TMetroLine;
     1115  Track: TTrack;
     1116  NewIndex: Integer;
    11071117begin
    11081118  if Button = mbLeft then begin
    11091119    MouseHold := True;
    1110     LastSelectedStation := nil;
    1111 
    1112     // Station selection
    1113     Station := GetStationOnPos(Position);
    1114     if Assigned(Station) then begin
    1115       SelectedStation := Station;
    1116       if not Assigned(SelectedLine) then
    1117       for I := 0 to Lines.Count - 1 do
    1118       if TMetroLine(Lines[I]).LineStations.Count = 0 then begin
    1119         SelectedLine := TMetroLine(Lines[I]);
    1120         Break;
     1120    LastFocusedStation := nil;
     1121
     1122    // Line selection
     1123    Track := GetTrackOnPos(Position);
     1124    if Assigned(Track.PointDown) and Assigned(Track.PointUp) then begin
     1125      SelectedLine := Track.PointDown.Line;
     1126
     1127      TrackStationDown := Track.PointDown;
     1128      NewIndex := TrackStationDown.Line.TrackPoints.IndexOf(TrackStationDown);
     1129      while Assigned(TrackStationDown) and (not Assigned(TrackStationDown.LineStation)) do begin
     1130        NewIndex := NewIndex - 1;
     1131        if NewIndex >= 0 then TrackStationDown := TTrackPoint(TrackStationDown.Line.TrackPoints[NewIndex])
     1132          else TrackStationDown := nil;
    11211133      end;
    1122     end;
    1123 
    1124     // Line selection
    1125     Line := GetLineOnPos(Position);
    1126     if Assigned(Line) then begin
    1127       SelectedLine := Line;
     1134      TrackStationUp := Track.PointUp;
     1135      NewIndex := TrackStationUp.Line.TrackPoints.IndexOf(TrackStationDown);
     1136      while Assigned(TrackStationUp) and (not Assigned(TrackStationUp.LineStation)) do begin
     1137        NewIndex := NewIndex + 1;
     1138        if NewIndex < TrackStationUp.Line.TrackPoints.Count then
     1139          TrackStationUp := TTrackPoint(TrackStationUp.Line.TrackPoints[NewIndex])
     1140          else TrackStationUp := nil;
     1141      end;
     1142
    11281143      Exit;
    11291144    end;
     
    11351150      if Assigned(NewLine) then begin
    11361151        NewLine.ConnectStation(Station);
    1137         SelectedStation := Station;
    1138         LastSelectedStation := Station;
     1152        TrackStationDown := TTrackPoint(NewLine.TrackPoints.Last);
     1153        TrackStationUp := nil;
     1154        LastFocusedStation := Station;
    11391155      end;
    11401156    end;
     
    12701286      Canvas.Pen.EndCap := pecRound;
    12711287    end;
    1272 
    1273     // Draw design line
    1274     if (SelectedLine = TMetroLine(Lines[I])) and Assigned(SelectedStation) then begin
    1275       Canvas.MoveTo(SelectedStation.Position);
    1276       DrawLine(Canvas, LastMousePos);
    1277     end;
     1288  end;
     1289
     1290  // Draw design time lines
     1291  if Assigned(TrackStationDown) and Assigned(TrackStationDown.LineStation) then begin
     1292    Canvas.Pen.Color := TrackStationDown.Line.Color;
     1293    Canvas.MoveTo(TrackStationDown.LineStation.TrackPoint.Point);
     1294    DrawLine(Canvas, LastMousePos);
     1295  end;
     1296  if Assigned(TrackStationUp) and Assigned(TrackStationUp.LineStation) then begin
     1297    Canvas.Pen.Color := TrackStationUp.Line.Color;
     1298    Canvas.MoveTo(TrackStationUp.LineStation.TrackPoint.Point);
     1299    DrawLine(Canvas, LastMousePos);
    12781300  end;
    12791301
Note: See TracChangeset for help on using the changeset viewer.