Changeset 3 for trunk


Ignore:
Timestamp:
Mar 26, 2015, 11:06:50 AM (10 years ago)
Author:
chronos
Message:
  • Added: Select metro line by clicking on its track.
Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/BigMetro.lpi

    r2 r3  
    3333      </Item1>
    3434    </RequiredPackages>
    35     <Units Count="3">
     35    <Units Count="4">
    3636      <Unit0>
    3737        <Filename Value="BigMetro.lpr"/>
    3838        <IsPartOfProject Value="True"/>
    39         <UnitName Value="BigMetro"/>
    4039      </Unit0>
    4140      <Unit1>
     
    4342        <IsPartOfProject Value="True"/>
    4443        <ComponentName Value="FormMain"/>
     44        <HasResources Value="True"/>
    4545        <ResourceBaseClass Value="Form"/>
    4646        <UnitName Value="UFormMain"/>
     
    5151        <UnitName Value="UEngine"/>
    5252      </Unit2>
     53      <Unit3>
     54        <Filename Value="UGeometric.pas"/>
     55        <IsPartOfProject Value="True"/>
     56        <UnitName Value="UGeometric"/>
     57      </Unit3>
    5358    </Units>
    5459  </ProjectOptions>
  • trunk/BigMetro.lpr

    r2 r3  
    88  {$ENDIF}{$ENDIF}
    99  Interfaces, // this includes the LCL widgetset
    10   Forms, UFormMain, UEngine
     10  Forms, UFormMain, UEngine, UGeometric
    1111  { you can add units after this };
    1212
  • trunk/UEngine.pas

    r2 r3  
    7070    TargetStation: TMetroStation;
    7171    function GetPos: TPoint;
    72     function GetNextStation: TMetroStation;
    7372    constructor Create;
    7473    destructor Destroy; override;
     
    115114    function GetExistStationShapes: TStationShapeSet;
    116115    function GetStationOnPos(Pos: TPoint): TMetroStation;
     116    function GetLineOnPos(Pos: TPoint): TMetroLine;
    117117    procedure DrawLine(Canvas: TCanvas; Pos: TPoint);
    118118    procedure DrawShape(Canvas: TCanvas; Position: TPoint; Shape: TStationShape;
     
    133133    procedure Tick;
    134134    procedure MouseMove(Position: TPoint);
    135     procedure MouseClick(Button: TMouseButton; Position: TPoint);
     135    procedure MouseUp(Button: TMouseButton; Position: TPoint);
     136    procedure MouseDown(Button: TMouseButton; Position: TPoint);
    136137    procedure Reset;
    137138    constructor Create;
     
    155156implementation
    156157
    157 
    158 function Distance(P1, P2: TPoint): Integer;
    159 begin
    160   Result := Trunc(Sqrt(Sqr(P2.x - P1.X) + Sqr(P2.Y - P1.Y)));
    161 end;
     158uses
     159  UGeometric;
    162160
    163161{ TMetroPassengers }
     
    303301end;
    304302
    305 function TMetroTrain.GetNextStation: TMetroStation;
    306 begin
    307 
    308 end;
    309 
    310303constructor TMetroTrain.Create;
    311304begin
     
    361354  if I < Stations.Count then Result := TMetroStation(Stations[I])
    362355    else Result := nil;
     356end;
     357
     358function TEngine.GetLineOnPos(Pos: TPoint): TMetroLine;
     359var
     360  I: Integer;
     361  T: Integer;
     362  D: Integer;
     363const
     364  Distance = 30;
     365begin
     366  Result := nil;
     367  I := 0;
     368  while (I < Lines.Count) do
     369  with TMetroLine(Lines[I]) do begin
     370    for T := 1 to High(TrackPoints) do begin
     371      D := PointToLineDistance(Pos, TrackPoints[T - 1], TrackPoints[T]);
     372      if D < Distance then begin
     373        Result := TMetroLine(Lines[I]);
     374        Break;
     375      end;
     376    end;
     377    if Assigned(Result) then Break;
     378    Inc(I);
     379  end;
    363380end;
    364381
     
    515532end;
    516533
    517 procedure TEngine.MouseClick(Button: TMouseButton; Position: TPoint);
     534procedure TEngine.MouseUp(Button: TMouseButton; Position: TPoint);
    518535var
    519536  Station: TMetroStation;
     537  Line: TMetroLine;
    520538  I: Integer;
    521539  Train: TMetroTrain;
    522540begin
    523541  if Button = mbLeft then begin
     542    // Line color selection
    524543    for I := 0 to Lines.Count - 1 do
    525544      if Distance(Point(View.Size.X div 2 - Length(LineColors) div 2 * LineColorsDist + I * LineColorsDist,
     
    529548        end;
    530549
     550    // Station selection
    531551    Station := GetStationOnPos(Position);
    532552    if Assigned(Station) then
    533       if Assigned(SelectedLine) and (SelectedLine.Stations.IndexOf(Station) = -1) then begin
     553      if Assigned(SelectedLine) and ((SelectedLine.Stations.IndexOf(Station) = -1) or
     554      (((SelectedLine.Stations.First = Station) and (SelectedLine.Stations.Last <> Station)))) then begin
    534555        SelectedLine.Stations.Add(Station);
    535556        Station.Lines.Add(SelectedLine);
     
    548569          end;
    549570        end;
     571        Exit
    550572      end;
     573
     574    // Line selection
     575    Line := GetLineOnPos(Position);
     576    if Assigned(Line) then begin
     577      SelectedLine := Line;
     578      Exit;
     579    end;
    551580  end;
    552581  if Button = mbRight then begin
    553582    SelectedLine := nil;
    554583  end;
     584end;
     585
     586procedure TEngine.MouseDown(Button: TMouseButton; Position: TPoint);
     587begin
     588
    555589end;
    556590
  • trunk/UFormMain.pas

    r2 r3  
    7070  Shift: TShiftState; X, Y: Integer);
    7171begin
    72 
     72  Engine.MouseDown(Button, Point(X, Y));
    7373end;
    7474
     
    8282  Shift: TShiftState; X, Y: Integer);
    8383begin
    84   Engine.MouseClick(Button, Point(X, Y));
     84  Engine.MouseUp(Button, Point(X, Y));
    8585end;
    8686
Note: See TracChangeset for help on using the changeset viewer.