Changeset 237 for trunk


Ignore:
Timestamp:
Sep 20, 2018, 4:39:37 PM (6 years ago)
Author:
chronos
Message:
  • Modified: Drawing parts split to separate methods.
  • Fixed: Draw correctly unit move arrows across map boundaries in cyclic mode.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormClient.pas

    r236 r237  
    217217          end;
    218218        end;
     219        for Y := 0 to CountP.Y do begin
     220          for X := 0 to CountP.X do begin
     221            TempView.Assign(View);
     222            TempView.DestRect := TRect.Create(
     223              TPoint.Create(
     224                -StartP.X + R.Size.X * X,
     225                -StartP.Y + R.Size.Y * Y
     226              ),
     227              TPoint.Create(
     228                -StartP.X + R.Size.X * X + View.DestRect.Size.X,
     229                -StartP.Y + R.Size.Y * Y + View.DestRect.Size.Y
     230              )
     231            );
     232            Client.DrawArrows(PaintBox1.Canvas, TempView);
     233          end;
     234        end;
    219235        TempView.Free;
    220236      end else
    221237      Client.Paint(PaintBox1.Canvas, View);
     238      Client.DrawArrows(PaintBox1.Canvas, View);
    222239    end;
    223240  end;
  • trunk/UClientGUI.pas

    r236 r237  
    5050    procedure PaintMapCell(Canvas: TCanvas; Pos: TPoint; Text: string; View: TView;
    5151      Cell: TCell);
     52    procedure DrawArrows(Canvas: TCanvas; View: TView);
     53    procedure DrawCells(Canvas: TCanvas; View: TView);
     54    procedure DrawCellLinks(Canvas: TCanvas; View: TView);
     55    procedure DrawNeighborLinks(Canvas: TCanvas; View: TView);
    5256    procedure Paint(Canvas: TCanvas; View: TView);
    5357    constructor Create; override;
     
    6468
    6569procedure TClientGUI.Paint(Canvas: TCanvas; View: TView);
     70begin
     71  DrawCellLinks(Canvas, View);
     72  DrawCells(Canvas, View);
     73  if TGame(Game).DevelMode then DrawNeighborLinks(Canvas, View);
     74  //DrawArrows(Canvas, View);
     75end;
     76
     77constructor TClientGUI.Create;
     78begin
     79  inherited;
     80  View := TView.Create;
     81end;
     82
     83destructor TClientGUI.Destroy;
     84begin
     85  FreeAndNil(View);
     86  inherited Destroy;
     87end;
     88
     89procedure TClientGUI.SetGame(AValue: TGame);
     90begin
     91  inherited;
     92  View.Game := AValue;
     93end;
     94
     95procedure TClientGUI.PaintCell(Canvas: TCanvas; Pos: TPoint; Text: string;
     96  View: TView; Cell: TPlayerCell);
    6697var
    6798  I: Integer;
    68   Cell: TPlayerCell;
    69   MapCell: TCell;
     99  TextPos: TPoint;
     100  Points: array of Classes.TPoint;
     101  TextSize: TSize;
     102begin
     103  if Cell.MapCell.Extra = etObjectiveTarget then begin
     104    Text := Text + '!';
     105  end;
     106  with Canvas do begin
     107    if Assigned(View.FocusedCell) and (View.FocusedCell = Cell) then begin
     108      Pen.Color := clYellow;
     109      Pen.Style := psSolid;
     110      Pen.Width := 1;
     111    end else
     112    if Cell.MapCell.Terrain = ttCity then begin
     113      // Cannot set clear border as it will display shifted on gtk2
     114      //Pen.Style := psClear;
     115      Pen.Color := clBlack;
     116      Pen.Style := psSolid;
     117      Pen.Width := 3;
     118    end else begin
     119      // Cannot set clear border as it will display shifted on gtk2
     120      //Pen.Style := psClear;
     121      Pen.Color := Brush.Color;
     122      Pen.Style := psSolid;
     123      Pen.Width := 0;
     124    end;
     125    // Transform view
     126    SetLength(Points, Length(Cell.MapCell.Polygon.Points));
     127    for I := 0 to Length(Points) - 1 do
     128      Points[I] := PointToStdPoint(View.CellToCanvasPos(Cell.MapCell.Polygon.Points[I]));
     129    Brush.Style := bsSolid;
     130    //Polygon(Points, False, 0, Length(Points));
     131    TCanvasEx.PolygonEx(Canvas, Points, False);
     132    //MoveTo(Points[0].X, Points[0].Y);
     133    //LineTo(Points[1].X, Points[1].Y);
     134
     135    // Show cell text
     136    if (Cell.GetAvialPower <> 0) or (Cell.MapCell.Extra = etObjectiveTarget) then begin
     137      Pen.Style := psSolid;
     138      Font.Color := clWhite;
     139      Brush.Style := bsClear;
     140      Font.Size := Trunc(42 * View.Zoom);
     141      TextPos := View.CellToCanvasPos(Pos);
     142      TextSize := TextExtent(Text);
     143      TCanvasEx.TextOutEx(Canvas, Round(TextPos.X) - TextSize.cx div 2,
     144        Round(TextPos.Y) - TextSize.cy div 2, Text, False);
     145    end;
     146  end;
     147end;
     148
     149procedure TClientGUI.PaintMapCell(Canvas: TCanvas; Pos: TPoint; Text: string;
     150  View: TView; Cell: TCell);
     151var
     152  I: Integer;
     153  TextPos: TPoint;
     154  Points: array of Classes.TPoint;
     155  TextSize: TSize;
     156begin
     157  if Cell.Extra = etObjectiveTarget then begin
     158    Text := Text + '!';
     159  end;
     160  with Canvas do begin
     161    if Assigned(View.FocusedCell) and (View.FocusedCell.MapCell = Cell) then begin
     162      Pen.Color := clYellow;
     163      Pen.Style := psSolid;
     164      Pen.Width := 1;
     165    end else
     166    if Cell.Terrain = ttCity then begin
     167      // Cannot set clear border as it will display shifted on gtk2
     168      //Pen.Style := psClear;
     169      Pen.Color := clBlack;
     170      Pen.Style := psSolid;
     171      Pen.Width := 3;
     172    end else begin
     173      // Cannot set clear border as it will display shifted on gtk2
     174      //Pen.Style := psClear;
     175      Pen.Color := Brush.Color;
     176      Pen.Style := psSolid;
     177      Pen.Width := 0;
     178    end;
     179    // Transform view
     180    SetLength(Points, Length(Cell.Polygon.Points));
     181    for I := 0 to Length(Points) - 1 do
     182      Points[I] := PointToStdPoint(View.CellToCanvasPos(Cell.Polygon.Points[I]));
     183    Brush.Style := bsSolid;
     184    //Polygon(Points, False, 0, Length(Points));
     185    TCanvasEx.PolygonEx(Canvas, Points, False);
     186    //MoveTo(Points[0].X, Points[0].Y);
     187    //LineTo(Points[1].X, Points[1].Y);
     188
     189    // Show cell text
     190    if (Cell.Power <> 0) or (Cell.Extra = etObjectiveTarget) then begin
     191      Pen.Style := psSolid;
     192      Font.Color := clWhite;
     193      Brush.Style := bsClear;
     194      Font.Size := Trunc(42 * View.Zoom);
     195      TextPos := View.CellToCanvasPos(Pos);
     196      TextSize := TextExtent(Text);
     197      TCanvasEx.TextOutEx(Canvas, Round(TextPos.X) - TextSize.cx div 2,
     198        Round(TextPos.Y) - TextSize.cy div 2, Text, False);
     199    end;
     200  end;
     201end;
     202
     203procedure TClientGUI.DrawArrows(Canvas: TCanvas; View: TView);
     204var
    70205  PosFrom, PosTo: TPoint;
    71206  Angle: Double;
    72207  ArrowCenter: TPoint;
    73208  Move: TUnitMove;
     209  P: TPoint;
     210begin
     211  with Canvas, View do begin
     212    Pen.Color := clCream;
     213    if Assigned(ControlPlayer) then
     214    for Move in ControlPlayer.Moves do begin
     215      PosFrom := TGame(Game).Map.CellToPos(Move.CellFrom.MapCell);
     216      PosTo := TGame(Game).Map.CellToPos(Move.CellTo.MapCell);
     217      if TGame(Game).Map.Cyclic then begin
     218        P := TPoint.Create(PosTo.X + TGame(Game).Map.PixelRect.Size.X,
     219          PosTo.Y);
     220        if TLine.Create(PosFrom, PosTo).Distance > TLine.Create(PosFrom, P).Distance then
     221          PosTo := TPoint.Create(P.X, PosTo.Y);
     222        P := TPoint.Create(PosTo.X, PosTo.Y + TGame(Game).Map.PixelRect.Size.Y);
     223        if TLine.Create(PosFrom, PosTo).Distance > TLine.Create(PosFrom, P).Distance then
     224          PosTo := TPoint.Create(PosTo.X, P.Y);
     225        P := TPoint.Create(PosTo.X - TGame(Game).Map.PixelRect.Size.X,
     226          PosTo.Y);
     227        if TLine.Create(PosFrom, PosTo).Distance > TLine.Create(PosFrom, P).Distance then
     228          PosTo := TPoint.Create(P.X, PosTo.Y);
     229        P := TPoint.Create(PosTo.X, PosTo.Y - TGame(Game).Map.PixelRect.Size.Y);
     230        if TLine.Create(PosFrom, PosTo).Distance > TLine.Create(PosFrom, P).Distance then
     231          PosTo := TPoint.Create(PosTo.X, P.Y);
     232      end;
     233      // In Fog of war mode show only
     234      if TGame(Game).FogOfWar and not ControlPlayer.PlayerMap.Cells.SearchCell(Move.CellFrom.MapCell).InVisibleRange and
     235        not ControlPlayer.PlayerMap.Cells.SearchCell(Move.CellTo.MapCell).InVisibleRange then
     236        Continue;
     237      if Move.CountRepeat > 0 then Pen.Width := 2
     238        else Pen.Width := 1;
     239      Angle := ArcTan((PosTo.Y - PosFrom.Y) / (PosTo.X - PosFrom.X));
     240      if (Angle > +Pi) or (Angle < -Pi) then
     241        raise Exception.Create(Format(SWrongArrowAngle, [FloatToStr(Angle)]));
     242
     243      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;
     250end;
     251
     252procedure TClientGUI.DrawCells(Canvas: TCanvas; View: TView);
     253var
     254  Cell: TPlayerCell;
     255  MapCell: TCell;
    74256  CellText: string;
    75   CellLink: TCellLink;
    76   NeighCell: TCell;
    77 begin
    78   with Canvas, View do
    79   try
    80     Lock;
    81     // Draw cell links
    82     Pen.Color := clBlack;
    83     Pen.Style := psSolid;
    84     Pen.Width := 3;
    85     for CellLink in TGame(Game).Map.CellLinks do
    86     with CellLink do begin
    87       if Length(Points) >= 2 then begin
    88         MoveTo(PointToStdPoint(View.CellToCanvasPos(Points[0])));
    89         for I := 1 to Length(Points) - 1 do
    90           LineTo(PointToStdPoint(View.CellToCanvasPos(Points[I])));
    91       end;
    92     end;
    93 
    94     // Draw cells
     257begin
     258  with Canvas, View do begin
    95259    if Assigned(ControlPlayer) then begin
    96260      for Cell in ControlPlayer.PlayerMap.Cells do begin
     
    132296      end;
    133297    end;
    134 
    135     // Draw links to neighbors
    136     if TGame(Game).DevelMode then
     298  end;
     299end;
     300
     301procedure TClientGUI.DrawCellLinks(Canvas: TCanvas; View: TView);
     302var
     303  I: Integer;
     304  CellLink: TCellLink;
     305begin
     306  with Canvas, View do begin
     307    Pen.Color := clBlack;
     308    Pen.Style := psSolid;
     309    Pen.Width := 3;
     310    for CellLink in TGame(Game).Map.CellLinks do
     311    with CellLink do begin
     312      if Length(Points) >= 2 then begin
     313        MoveTo(PointToStdPoint(View.CellToCanvasPos(Points[0])));
     314        for I := 1 to Length(Points) - 1 do
     315          LineTo(PointToStdPoint(View.CellToCanvasPos(Points[I])));
     316      end;
     317    end;
     318  end;
     319end;
     320
     321procedure TClientGUI.DrawNeighborLinks(Canvas: TCanvas; View: TView);
     322var
     323  Cell: TPlayerCell;
     324  C: TCell;
     325begin
     326  with Canvas, View do begin
    137327    for Cell in ControlPlayer.PlayerMap.Cells do begin
    138       for NeighCell in Cell.MapCell.Neighbors do begin
     328      for C in Cell.MapCell.Neighbors do begin
    139329        Pen.Color := clYellow;
    140330        MoveTo(PointToStdPoint(View.CellToCanvasPos(Cell.MapCell.PosPx)));
    141         LineTo(PointToStdPoint(View.CellToCanvasPos(NeighCell.PosPx)));
     331        LineTo(PointToStdPoint(View.CellToCanvasPos(C.PosPx)));
    142332      end;
    143333
     
    146336      TextOut(View.CellToCanvasPos(Cell.MapCell.PosPx).X,
    147337        View.CellToCanvasPos(Cell.MapCell.PosPx).Y, IntToStr(Cell.MapCell.Id));
    148     end;
    149 
    150     // Draw arrows
    151     Pen.Color := clCream;
    152     if Assigned(ControlPlayer) then
    153     for Move in ControlPlayer.Moves do begin
    154       PosFrom := TGame(Game).Map.CellToPos(Move.CellFrom.MapCell);
    155       PosTo := TGame(Game).Map.CellToPos(Move.CellTo.MapCell);
    156       // In Fog of war mode show only
    157       if TGame(Game).FogOfWar and not ControlPlayer.PlayerMap.Cells.SearchCell(Move.CellFrom.MapCell).InVisibleRange and
    158         not ControlPlayer.PlayerMap.Cells.SearchCell(Move.CellTo.MapCell).InVisibleRange then
    159         Continue;
    160       if Move.CountRepeat > 0 then Pen.Width := 2
    161         else Pen.Width := 1;
    162       Angle := ArcTan((PosTo.Y - PosFrom.Y) / (PosTo.X - PosFrom.X));
    163       if (Angle > +Pi) or (Angle < -Pi) then
    164         raise Exception.Create(Format(SWrongArrowAngle, [FloatToStr(Angle)]));
    165 
    166       if Sign(PosTo.X - PosFrom.X) = -1 then Angle := Angle + Pi;
    167       ArrowCenter := View.CellToCanvasPos(TPoint.Create(Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
    168         Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 2)));
    169       TGame(Game).Map.DrawArrow(Canvas, ArrowCenter,
    170         Angle, IntToStr(Move.CountOnce), View.Zoom);
    171     end;
    172   finally
    173     Unlock;
    174   end;
    175 end;
    176 
    177 constructor TClientGUI.Create;
    178 begin
    179   inherited;
    180   View := TView.Create;
    181 end;
    182 
    183 destructor TClientGUI.Destroy;
    184 begin
    185   FreeAndNil(View);
    186   inherited Destroy;
    187 end;
    188 
    189 procedure TClientGUI.SetGame(AValue: TGame);
    190 begin
    191   inherited;
    192   View.Game := AValue;
    193 end;
    194 
    195 procedure TClientGUI.PaintCell(Canvas: TCanvas; Pos: TPoint; Text: string;
    196   View: TView; Cell: TPlayerCell);
    197 var
    198   I: Integer;
    199   TextPos: TPoint;
    200   Points: array of Classes.TPoint;
    201   TextSize: TSize;
    202 begin
    203   if Cell.MapCell.Extra = etObjectiveTarget then begin
    204     Text := Text + '!';
    205   end;
    206   with Canvas do begin
    207     if Assigned(View.FocusedCell) and (View.FocusedCell = Cell) then begin
    208       Pen.Color := clYellow;
    209       Pen.Style := psSolid;
    210       Pen.Width := 1;
    211     end else
    212     if Cell.MapCell.Terrain = ttCity then begin
    213       // Cannot set clear border as it will display shifted on gtk2
    214       //Pen.Style := psClear;
    215       Pen.Color := clBlack;
    216       Pen.Style := psSolid;
    217       Pen.Width := 3;
    218     end else begin
    219       // Cannot set clear border as it will display shifted on gtk2
    220       //Pen.Style := psClear;
    221       Pen.Color := Brush.Color;
    222       Pen.Style := psSolid;
    223       Pen.Width := 0;
    224     end;
    225     // Transform view
    226     SetLength(Points, Length(Cell.MapCell.Polygon.Points));
    227     for I := 0 to Length(Points) - 1 do
    228       Points[I] := PointToStdPoint(View.CellToCanvasPos(Cell.MapCell.Polygon.Points[I]));
    229     Brush.Style := bsSolid;
    230     //Polygon(Points, False, 0, Length(Points));
    231     TCanvasEx.PolygonEx(Canvas, Points, False);
    232     //MoveTo(Points[0].X, Points[0].Y);
    233     //LineTo(Points[1].X, Points[1].Y);
    234 
    235     // Show cell text
    236     if (Cell.GetAvialPower <> 0) or (Cell.MapCell.Extra = etObjectiveTarget) then begin
    237       Pen.Style := psSolid;
    238       Font.Color := clWhite;
    239       Brush.Style := bsClear;
    240       Font.Size := Trunc(42 * View.Zoom);
    241       TextPos := View.CellToCanvasPos(Pos);
    242       TextSize := TextExtent(Text);
    243       TCanvasEx.TextOutEx(Canvas, Round(TextPos.X) - TextSize.cx div 2,
    244         Round(TextPos.Y) - TextSize.cy div 2, Text, False);
    245     end;
    246   end;
    247 end;
    248 
    249 procedure TClientGUI.PaintMapCell(Canvas: TCanvas; Pos: TPoint; Text: string;
    250   View: TView; Cell: TCell);
    251 var
    252   I: Integer;
    253   TextPos: TPoint;
    254   Points: array of Classes.TPoint;
    255   TextSize: TSize;
    256 begin
    257   if Cell.Extra = etObjectiveTarget then begin
    258     Text := Text + '!';
    259   end;
    260   with Canvas do begin
    261     if Assigned(View.FocusedCell) and (View.FocusedCell.MapCell = Cell) then begin
    262       Pen.Color := clYellow;
    263       Pen.Style := psSolid;
    264       Pen.Width := 1;
    265     end else
    266     if Cell.Terrain = ttCity then begin
    267       // Cannot set clear border as it will display shifted on gtk2
    268       //Pen.Style := psClear;
    269       Pen.Color := clBlack;
    270       Pen.Style := psSolid;
    271       Pen.Width := 3;
    272     end else begin
    273       // Cannot set clear border as it will display shifted on gtk2
    274       //Pen.Style := psClear;
    275       Pen.Color := Brush.Color;
    276       Pen.Style := psSolid;
    277       Pen.Width := 0;
    278     end;
    279     // Transform view
    280     SetLength(Points, Length(Cell.Polygon.Points));
    281     for I := 0 to Length(Points) - 1 do
    282       Points[I] := PointToStdPoint(View.CellToCanvasPos(Cell.Polygon.Points[I]));
    283     Brush.Style := bsSolid;
    284     //Polygon(Points, False, 0, Length(Points));
    285     TCanvasEx.PolygonEx(Canvas, Points, False);
    286     //MoveTo(Points[0].X, Points[0].Y);
    287     //LineTo(Points[1].X, Points[1].Y);
    288 
    289     // Show cell text
    290     if (Cell.Power <> 0) or (Cell.Extra = etObjectiveTarget) then begin
    291       Pen.Style := psSolid;
    292       Font.Color := clWhite;
    293       Brush.Style := bsClear;
    294       Font.Size := Trunc(42 * View.Zoom);
    295       TextPos := View.CellToCanvasPos(Pos);
    296       TextSize := TextExtent(Text);
    297       TCanvasEx.TextOutEx(Canvas, Round(TextPos.X) - TextSize.cx div 2,
    298         Round(TextPos.Y) - TextSize.cy div 2, Text, False);
    299338    end;
    300339  end;
Note: See TracChangeset for help on using the changeset viewer.