close Warning: Can't synchronize with repository "(default)" (No changeset 184 in the repository). Look in the Trac log for more information.

Changeset 170 for trunk/UGame.pas


Ignore:
Timestamp:
Nov 23, 2017, 5:02:49 PM (6 years ago)
Author:
chronos
Message:
  • Modified: UGeometry unit rewritten to use generics and define own TPoint and TRect types.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r168 r170  
    161161  TCanvasEx = class(TCanvas)
    162162    class procedure TextOutEx(Canvas: TCanvas; X,Y: Integer; const Text: string; MovePen: Boolean = True);
    163     class procedure PolygonEx(Canvas: TCanvas; const Points: array of TPoint; Winding: Boolean);
     163    class procedure PolygonEx(Canvas: TCanvas; const Points: array of Classes.TPoint; Winding: Boolean);
    164164  end;
    165165
     
    957957    with CellLink do begin
    958958      if Length(Points) >= 2 then begin
    959         MoveTo(View.CellToCanvasPos(Points[0]));
     959        MoveTo(PointToStdPoint(View.CellToCanvasPos(Points[0])));
    960960        for I := 1 to Length(Points) - 1 do
    961           LineTo(View.CellToCanvasPos(Points[I]));
     961          LineTo(PointToStdPoint(View.CellToCanvasPos(Points[I])));
    962962      end;
    963963    end;
     
    999999      for NeighCell in Cell.MapCell.Neighbors do begin
    10001000        Pen.Color := clYellow;
    1001         MoveTo(View.CellToCanvasPos(Cell.MapCell.PosPx));
    1002         LineTo(View.CellToCanvasPos(NeighCell.PosPx));
     1001        MoveTo(PointToStdPoint(View.CellToCanvasPos(Cell.MapCell.PosPx)));
     1002        LineTo(PointToStdPoint(View.CellToCanvasPos(NeighCell.PosPx)));
    10031003      end;
    10041004
     
    10221022      Angle := ArcTan((PosTo.Y - PosFrom.Y) / (PosTo.X - PosFrom.X));
    10231023      if Sign(PosTo.X - PosFrom.X) = -1 then Angle := Angle + Pi;
    1024       ArrowCenter := View.CellToCanvasPos(Point(Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
     1024      ArrowCenter := View.CellToCanvasPos(TPoint.Create(Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
    10251025        Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 2)));
    10261026      Player.Game.Map.DrawArrow(Canvas, View, ArrowCenter,
     
    10511051end;
    10521052
    1053 class procedure TCanvasEx.PolygonEx(Canvas: TCanvas; const Points: array of TPoint; Winding: Boolean);
     1053class procedure TCanvasEx.PolygonEx(Canvas: TCanvas; const Points: array of Classes.TPoint; Winding: Boolean);
    10541054begin
    10551055  //Changing;
     
    13491349  for Cell in Cells do begin
    13501350    for NeighborCell in Cell.Neighbors do
    1351       NeighborCell.Angle := ArcTan2Point(Point(
    1352         NeighborCell.PosPx.X - Cell.PosPx.X,
    1353         NeighborCell.PosPx.Y - Cell.PosPx.Y));
     1351      NeighborCell.Angle := TLine.Create(Cell.PosPx, NeighborCell.PosPx).GetAngle;
    13541352
    13551353    Cell.Neighbors.Sort(CompareCellAngle);
     
    13751373      Rect := PixelRect;
    13761374      with Image.Picture.Bitmap do begin
    1377         Pos := Point(Trunc(Coord.X / (Rect.Right - Rect.Left) * Width),
    1378           Trunc(Coord.Y / (Rect.Bottom - Rect.Top) * Height));
     1375        Pos := TPoint.Create(Trunc(Coord.X / Rect.Size.X * Width),
     1376          Trunc(Coord.Y / Rect.Size.Y * Height));
    13791377        Color := Canvas.Pixels[Pos.X, Pos.Y];
    13801378      end;
     
    13831381    msRounded: begin
    13841382      Rect := PixelRect;
    1385       Center := Point(Rect.Left + (Rect.Right - Rect.Left) div 2,
    1386         Rect.Top + (Rect.Bottom - Rect.Top) div 2);
    1387       Result := Sqr(Coord.X - Center.X) / Sqr((Rect.Right - Rect.Left) div 2) +
    1388         Sqr(Coord.Y - Center.Y) / Sqr((Rect.Bottom - Rect.Top) div 2) > 1;
     1383      Center := Rect.Center;
     1384      Result := Sqr(Coord.X - Center.X) / Sqr(Rect.Size.X div 2) +
     1385        Sqr(Coord.Y - Center.Y) / Sqr(Rect.Size.Y div 2) > 1;
    13891386    end
    13901387    else Result := False;
     
    13951392  Angle: Double; Text: string);
    13961393var
    1397   Points: array of TPoint;
     1394  Points: array of Classes.TPoint;
    13981395  FPoints: array of TFloatPoint;
    13991396  I: Integer;
     
    14041401  SetLength(Points, 8);
    14051402  SetLength(FPoints, 8);
    1406   ArrowSize := Point(Trunc(DefaultCellSize.X / 3 * View.Zoom),
     1403  ArrowSize := TPoint.Create(Trunc(DefaultCellSize.X / 3 * View.Zoom),
    14071404    Trunc(DefaultCellSize.Y / 3 * View.Zoom));
    14081405  FPoints[0] := FloatPoint(+0.5 * ArrowSize.X, +0 * ArrowSize.Y);
     
    14801477  Node2: TDOMNode;
    14811478begin
    1482   Size := Point(ReadInteger(Node, 'SizeX', 0), ReadInteger(Node, 'SizeY', 0));
     1479  Size := TPoint.Create(ReadInteger(Node, 'SizeX', 0), ReadInteger(Node, 'SizeY', 0));
    14831480  DefaultCellSize.X := ReadInteger(Node, 'DefaultCellSizeX', 1);
    14841481  DefaultCellSize.Y := ReadInteger(Node, 'DefaultCellSizeY', 1);
     
    15651562  I: Integer;
    15661563  TextPos: TPoint;
    1567   Points: array of TPoint;
     1564  Points: array of Classes.TPoint;
    15681565  TextSize: TSize;
    15691566begin
     
    15901587    SetLength(Points, Length(Cell.Polygon.Points));
    15911588    for I := 0 to Length(Points) - 1 do
    1592       Points[I] := View.CellToCanvasPos(Cell.Polygon.Points[I]);
     1589      Points[I] := PointToStdPoint(View.CellToCanvasPos(Cell.Polygon.Points[I]));
    15931590    Brush.Style := bsSolid;
    15941591    //Polygon(Points, False, 0, Length(Points));
     
    16401637    NewCell := TCell.Create;
    16411638    NewCell.Map := Self;
    1642     NewCell.PosPx := Point(X * DefaultCellSize.X, Y * DefaultCellSize.Y);
     1639    NewCell.PosPx := TPoint.Create(X * DefaultCellSize.X, Y * DefaultCellSize.Y);
    16431640    NewCell.Id := GetNewCellId;
    16441641    SetLength(NewCell.Polygon.Points, 1);
     
    17311728begin
    17321729  MaxPower := 99;
    1733   DefaultCellSize := Point(220, 220);
     1730  DefaultCellSize := TPoint.Create(220, 220);
    17341731  Cells := TCells.Create;
    17351732  Cells.Map := Self;
    1736   Size := Point(0, 0);
     1733  Size := TPoint.Create(0, 0);
    17371734  Image := TImage.Create(nil);
    17381735  CellLinks := TCellLinks.Create;
     
    17431740destructor TMap.Destroy;
    17441741begin
    1745   Size := Point(0, 0);
     1742  Size := TPoint.Create(0, 0);
    17461743  FreeAndNil(Areas);
    17471744  FreeAndNil(CellLinks);
     
    17561753  CellRect: TRect;
    17571754begin
    1758   Result := Rect(0, 0, 0, 0);
     1755  Result := TRect.Create(TPoint.Create(0, 0), TPoint.Create(0, 0));
    17591756  // This is generic algorithm to determine pixel size of entire map
    17601757  for I := 0 to Cells.Count - 1 do begin
     
    17621759    if I = 0 then Result := CellRect
    17631760      else begin
    1764         if CellRect.Right > Result.Right then Result.Right := CellRect.Right;
    1765         if CellRect.Bottom > Result.Bottom then Result.Bottom := CellRect.Bottom;
    1766         if CellRect.Left < Result.Left then Result.Left := CellRect.Left;
    1767         if CellRect.Top < Result.Top then  Result.Top := CellRect.Top;
     1761        Result.P1 := TPoint.Min(Result.P1, CellRect.P1);
     1762        Result.P2 := TPoint.Max(Result.P2, CellRect.P2);
    17681763      end;
    17691764  end;
     
    18401835    raise Exception.Create(SZeroZoomNotAlowed);
    18411836  FZoom := AValue;
    1842   SourceRect := Bounds(Trunc(SourceRect.Left + (SourceRect.Right - SourceRect.Left) div 2 - (DestRect.Right - DestRect.Left) / Zoom / 2),
    1843     Trunc(SourceRect.Top +  (SourceRect.Bottom - SourceRect.Top) div 2 - (DestRect.Bottom - DestRect.Top) / Zoom / 2),
    1844     Trunc((DestRect.Right - DestRect.Left) / Zoom),
    1845     Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
     1837  SourceRect := TRect.CreateBounds(TPoint.Create(Trunc(SourceRect.P1.X + SourceRect.Size.X div 2 - DestRect.Size.X / Zoom / 2),
     1838    Trunc(SourceRect.P1.Y +  SourceRect.Size.Y div 2 - DestRect.Size.Y / Zoom / 2)),
     1839    TPoint.Create(Trunc(DestRect.Size.X / Zoom),
     1840    Trunc(DestRect.Size.Y / Zoom)));
    18461841end;
    18471842
     
    18561851  Diff: TPoint;
    18571852begin
    1858   if RectEquals(FDestRect, AValue) then Exit;
    1859   Diff := Point(Trunc((DestRect.Right - DestRect.Left) / Zoom - (AValue.Right - AValue.Left) / Zoom) div 2,
    1860     Trunc((DestRect.Bottom - DestRect.Top) / Zoom - (AValue.Bottom - AValue.Top) / Zoom) div 2);
     1853  if FDestRect = AValue then Exit;
     1854  Diff := TPoint.Create(Trunc(DestRect.Size.X / Zoom - AValue.Size.X / Zoom) div 2,
     1855    Trunc(DestRect.Size.Y / Zoom - AValue.Size.Y / Zoom) div 2);
    18611856  FDestRect := AValue;
    1862   SourceRect := Bounds(SourceRect.Left + Diff.X, SourceRect.Top + Diff.Y,
    1863     Trunc((DestRect.Right - DestRect.Left) / Zoom),
    1864     Trunc((DestRect.Bottom - DestRect.Top) / Zoom));
     1857  SourceRect := TRect.CreateBounds(TPoint.Create(SourceRect.P1.X + Diff.X, SourceRect.P1.Y + Diff.Y),
     1858    TPoint.Create(Trunc(DestRect.Size.X / Zoom),
     1859    Trunc(DestRect.Size.Y / Zoom)));
    18651860end;
    18661861
     
    19841979    Node2 := Node3.FirstChild;
    19851980    while Assigned(Node2) and (Node2.NodeName = 'Point') do begin
    1986       Polygon.AddPoint(Point(ReadInteger(Node2, 'X', 0), ReadInteger(Node2, 'Y', 0)));
     1981      Polygon.AddPoint(TPoint.Create(ReadInteger(Node2, 'X', 0), ReadInteger(Node2, 'Y', 0)));
    19871982      Node2 := Node2.NextSibling;
    19881983    end;
     
    20342029function TCell.IsVisible(View: TView): Boolean;
    20352030var
    2036   RectA, RectB: TRect;
    2037 begin
    2038   RectA := Polygon.GetRect;
    2039   RectB := View.SourceRect;
    2040   Result := ((RectA.Left < RectB.Right) and (RectA.Right > RectB.Left) and
    2041     (RectA.Top < RectB.Bottom) and (RectA.Bottom > RectB.Top));
     2031  RectPolygon, RectView: TRect;
     2032begin
     2033  RectPolygon := Polygon.GetRect;
     2034  RectView := View.SourceRect;
     2035  Result := (
     2036    (RectPolygon.P1.X < RectView.P2.X) and
     2037    (RectPolygon.P2.X > RectView.P1.X) and
     2038    (RectPolygon.P1.Y < RectView.P2.Y) and
     2039    (RectPolygon.P2.Y > RectView.P1.Y)
     2040  );
    20422041end;
    20432042
     
    21082107function TView.CanvasToCellPos(Pos: TPoint): TPoint;
    21092108begin
    2110   Result := Point(Trunc(Pos.X / Zoom + SourceRect.Left),
    2111     Trunc(Pos.Y / Zoom + SourceRect.Top));
     2109  Result := TPoint.Create(Trunc(Pos.X / Zoom + SourceRect.P1.X),
     2110    Trunc(Pos.Y / Zoom + SourceRect.P1.Y));
    21122111end;
    21132112
    21142113function TView.CellToCanvasPos(Pos: TPoint): TPoint;
    21152114begin
    2116   Result := Point(Trunc((Pos.X - SourceRect.Left) * Zoom),
    2117     Trunc((Pos.Y - SourceRect.Top) * Zoom));
     2115  Result := TPoint.Create(Trunc((Pos.X - SourceRect.P1.X) * Zoom),
     2116    Trunc((Pos.Y - SourceRect.P1.Y) * Zoom));
    21182117end;
    21192118
    21202119function TView.CanvasToCellRect(Pos: TRect): TRect;
    21212120begin
    2122   Result.TopLeft := CanvasToCellPos(Pos.TopLeft);
    2123   Result.BottomRight := CanvasToCellPos(Pos.BottomRight);
     2121  Result.P1 := CanvasToCellPos(Pos.P1);
     2122  Result.P2 := CanvasToCellPos(Pos.P2);
    21242123end;
    21252124
    21262125function TView.CellToCanvasRect(Pos: TRect): TRect;
    21272126begin
    2128   Result.TopLeft := CellToCanvasPos(Pos.TopLeft);
    2129   Result.BottomRight := CellToCanvasPos(Pos.BottomRight);
     2127  Result.P1 := CellToCanvasPos(Pos.P1);
     2128  Result.P2 := CellToCanvasPos(Pos.P2);
    21302129end;
    21312130
     
    25782577begin
    25792578  MapRect := Game.Map.PixelRect;
    2580   SourceRect := Bounds(MapRect.Left + (MapRect.Right - MapRect.Left) div 2 - (SourceRect.Right - SourceRect.Left) div 2,
    2581     MapRect.Top + (MapRect.Bottom - MapRect.Top) div 2 - (SourceRect.Bottom - SourceRect.Top) div 2,
    2582     SourceRect.Right - SourceRect.Left,
    2583     SourceRect.Bottom - SourceRect.Top);
     2579  SourceRect := TRect.CreateBounds(TPoint.Create(MapRect.P1.X + MapRect.Size.X div 2 - SourceRect.Size.X div 2,
     2580    MapRect.P1.Y + MapRect.Size.Y div 2 - SourceRect.Size.Y div 2),
     2581    TPoint.Create(SourceRect.Size.X,
     2582    SourceRect.Size.Y));
    25842583end;
    25852584
    25862585procedure TView.CenterPlayerCity(Player: TPlayer);
    25872586begin
    2588   SourceRect := Bounds(Player.StartCell.PosPx.X - (SourceRect.Right - SourceRect.Left) div 2,
    2589     Player.StartCell.PosPx.Y - (SourceRect.Bottom - SourceRect.Top) div 2,
    2590     SourceRect.Right - SourceRect.Left,
    2591     SourceRect.Bottom - SourceRect.Top);
     2587  SourceRect := TRect.CreateBounds(TPoint.Create(Player.StartCell.PosPx.X - SourceRect.Size.X div 2,
     2588    Player.StartCell.PosPx.Y - SourceRect.Size.Y div 2),
     2589    TPoint.Create(SourceRect.Size.X,
     2590    SourceRect.Size.Y));
    25922591end;
    25932592
     
    32473246  with Config do begin
    32483247    MapType := TMapType(GetValue(DOMString(Path + '/GridType'), Integer(mtHexagon)));
    3249     Map.Size := Point(GetValue(DOMString(Path + '/MapSizeX'), 10),
     3248    Map.Size := TPoint.Create(GetValue(DOMString(Path + '/MapSizeX'), 10),
    32503249      GetValue(DOMString(Path + '/MapSizeY'), 10));
    32513250    MapImageFileName := string(GetValue(DOMString(Path + '/MapImage'), DOMString(MapImageFileName)));
     
    35083507
    35093508  Map.Game := Self;
    3510   Map.Size := Point(3, 3);
     3509  Map.Size := TPoint.Create(3, 3);
    35113510end;
    35123511
     
    36013600    with CellLink do begin
    36023601      if Length(Points) >= 2 then begin
    3603         MoveTo(View.CellToCanvasPos(Points[0]));
     3602        MoveTo(PointToStdPoint(View.CellToCanvasPos(Points[0])));
    36043603        for I := 1 to Length(Points) - 1 do
    3605           LineTo(View.CellToCanvasPos(Points[I]));
     3604          LineTo(PointToStdPoint(View.CellToCanvasPos(Points[I])));
    36063605      end;
    36073606    end;
     
    36303629      Angle := ArcTan((PosTo.Y - PosFrom.Y) / (PosTo.X - PosFrom.X));
    36313630      if Sign(PosTo.X - PosFrom.X) = -1 then Angle := Angle + Pi;
    3632       ArrowCenter := View.CellToCanvasPos(Point(Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
     3631      ArrowCenter := View.CellToCanvasPos(TPoint.Create(Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
    36333632        Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 2)));
    36343633      DrawArrow(Canvas, View, ArrowCenter,
Note: See TracChangeset for help on using the changeset viewer.