Changeset 238 for trunk/UClientGUI.pas


Ignore:
Timestamp:
Sep 21, 2018, 12:15:23 AM (6 years ago)
Author:
chronos
Message:
  • Modified: Enabled back drawing only of visible cells.
  • Added: Draw only visible arrows between cells.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UClientGUI.pas

    r237 r238  
    3232    function CanvasToCellPos(Pos: TPoint): TPoint;
    3333    function CellToCanvasPos(Pos: TPoint): TPoint;
     34    function CellToCanvasPosF(Pos: TPointF): TPointF;
    3435    function CanvasToCellRect(Pos: TRect): TRect;
    3536    function CellToCanvasRect(Pos: TRect): TRect;
     37    function CellToCanvasRectF(Pos: TRectF): TRectF;
    3638    procedure Assign(Source: TView);
    3739    property DestRect: TRect read FDestRect write SetDestRect;
     
    4648  public
    4749    View: TView;
     50    procedure DrawArrow(Canvas: TCanvas; Pos: TPoint; Angle: Double;
     51      Text: string; View: TView);
    4852    procedure PaintCell(Canvas: TCanvas; Pos: TPoint; Text: string; View: TView;
    4953      Cell: TPlayerCell);
     
    242246
    243247      if Sign(PosTo.X - PosFrom.X) = -1 then Angle := Angle + Pi;
    244       ArrowCenter := View.CellToCanvasPos(TPoint.Create(Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
    245         Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 2)));
    246       TGame(Game).Map.DrawArrow(Canvas, ArrowCenter,
    247         Angle, IntToStr(Move.CountOnce), View.Zoom);
    248     end;
    249   end;
    250 end;
     248      ArrowCenter := View.CellToCanvasPos(TPoint.Create(
     249        Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
     250        Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 2)
     251      ));
     252      DrawArrow(Canvas, ArrowCenter,
     253        Angle, IntToStr(Move.CountOnce), View);
     254    end;
     255  end;
     256end;
     257
     258procedure TClientGUI.DrawArrow(Canvas: TCanvas; Pos: TPoint;
     259  Angle: Double; Text: string; View: TView);
     260var
     261  Points: array of Classes.TPoint;
     262  Arrow: TPolygonF;
     263  I: Integer;
     264  ArrowSize: TPoint;
     265  RectPolygon: TRectF;
     266begin
     267  SetLength(Arrow.Points, 8);
     268  ArrowSize := TPoint.Create(Trunc(TGame(Game).Map.DefaultCellSize.X / 3 * View.Zoom),
     269    Trunc(TGame(Game).Map.DefaultCellSize.Y / 3 * View.Zoom));
     270  Arrow.Points[0] := TPointF.Create(+0.5 * ArrowSize.X, +0 * ArrowSize.Y);
     271  Arrow.Points[1] := TPointF.Create(+0 * ArrowSize.X, +0.5 * ArrowSize.Y);
     272  Arrow.Points[2] := TPointF.Create(+0 * ArrowSize.X, +0.25 * ArrowSize.Y);
     273  Arrow.Points[3] := TPointF.Create(-0.5 * ArrowSize.X, +0.25 * ArrowSize.Y);
     274  Arrow.Points[4] := TPointF.Create(-0.5 * ArrowSize.X, -0.25 * ArrowSize.Y);
     275  Arrow.Points[5] := TPointF.Create(+0 * ArrowSize.X, -0.25 * ArrowSize.Y);
     276  Arrow.Points[6] := TPointF.Create(+0 * ArrowSize.X, -0.5 * ArrowSize.Y);
     277  Arrow.Points[7] := TPointF.Create(+0.5 * ArrowSize.X, 0 * ArrowSize.Y);
     278  // Rotate
     279  for I := 0 to Length(Arrow.Points) - 1 do begin
     280    Arrow.Points[I] := TPointF.Create(
     281      Arrow.Points[I].X * Cos(Angle) - Arrow.Points[I].Y * Sin(Angle),
     282      Arrow.Points[I].X * Sin(Angle) + Arrow.Points[I].Y * Cos(Angle)
     283    );
     284    Arrow.Points[I] := Arrow.Points[I] + TPointF.Create(Pos.X, Pos.Y);
     285  end;
     286
     287  RectPolygon := Arrow.GetRect;
     288  if (RectPolygon.P1.X < View.DestRect.Size.X) and
     289    (RectPolygon.P2.X > 0) and
     290    (RectPolygon.P1.Y < View.DestRect.Size.Y) and
     291    (RectPolygon.P2.Y > 0) then begin
     292
     293    // Convert to standard points
     294    SetLength(Points, 8);
     295    for I := 0 to Length(Points) - 1 do
     296      Points[I] := Point(Trunc(Arrow[I].X), Trunc(Arrow[I].Y));
     297    with Canvas do begin
     298      Pen.Color := clBlack;
     299      Brush.Color := clWhite;
     300      Brush.Style := bsSolid;
     301      Polygon(Points);
     302      Brush.Style := bsClear;
     303      Font.Color := clBlack;
     304      Font.Size := Trunc(18 * View.Zoom);
     305      TextOut(Pos.X - TextWidth(Text) div 2, Pos.Y - TextHeight(Text) div 2, Text);
     306      Pen.Width := 1;
     307    end;
     308  end;
     309end;
     310
    251311
    252312procedure TClientGUI.DrawCells(Canvas: TCanvas; View: TView);
     
    396456end;
    397457
     458function TView.CellToCanvasPosF(Pos: TPointF): TPointF;
     459begin
     460  Result := TPointF.Create((Pos.X - SourceRect.P1.X) * Zoom + DestRect.P1.X,
     461    (Pos.Y - SourceRect.P1.Y) * Zoom + DestRect.P1.Y);
     462end;
     463
    398464function TView.CanvasToCellRect(Pos: TRect): TRect;
    399465begin
     
    406472  Result.P1 := CellToCanvasPos(Pos.P1);
    407473  Result.P2 := CellToCanvasPos(Pos.P2);
     474end;
     475
     476function TView.CellToCanvasRectF(Pos: TRectF): TRectF;
     477begin
     478  Result.P1 := CellToCanvasPosF(Pos.P1);
     479  Result.P2 := CellToCanvasPosF(Pos.P2);
    408480end;
    409481
     
    491563function TView.IsCellVisible(Cell: TCell): Boolean;
    492564var
    493   RectPolygon, RectView: TRect;
    494 begin
    495   RectPolygon := Cell.Polygon.GetRect;
    496   RectView := SourceRect;
     565  RectPolygon: TRect;
     566begin
     567  RectPolygon := CellToCanvasRect(Cell.Polygon.GetRect);
    497568  Result := (
    498     (RectPolygon.P1.X < RectView.P2.X) and
    499     (RectPolygon.P2.X > RectView.P1.X) and
    500     (RectPolygon.P1.Y < RectView.P2.Y) and
    501     (RectPolygon.P2.Y > RectView.P1.Y)
     569    (RectPolygon.P1.X < DestRect.Size.X) and
     570    (RectPolygon.P2.X > 0) and
     571    (RectPolygon.P1.Y < DestRect.Size.Y) and
     572    (RectPolygon.P2.Y > 0)
    502573  );
    503   Result := True;
    504574end;
    505575
Note: See TracChangeset for help on using the changeset viewer.