unit UClientGUI;

{$mode delphi}

interface

uses
  Types, Classes, SysUtils, Graphics, UGameClient, UPlayer, UMap, UGame, UGeometry,
  Math;

type
  { TView }

  TView = class
  private
    FDestRect: TRect;
    FZoom: Double;
    procedure SetDestRect(AValue: TRect);
    procedure SetZoom(AValue: Double);
  public
    Game: TObject; //TGame;
    SourceRect: TRect;
    FocusedCell: TPlayerCell;
    SelectedCell: TPlayerCell;
    procedure Clear;
    function IsCellVisible(Cell: TCell): Boolean;
    constructor Create;
    destructor Destroy; override;
    procedure SelectCell(Pos: TPoint; Player: TPlayer; ShiftState: TShiftState);
    procedure CenterMap;
    procedure CenterPlayerCity(Player: TPlayer);
    procedure ZoomAll;
    function CanvasToCellPos(Pos: TPoint): TPoint;
    function CellToCanvasPos(Pos: TPoint): TPoint;
    function CellToCanvasPosF(Pos: TPointF): TPointF;
    function CanvasToCellRect(Pos: TRect): TRect;
    function CellToCanvasRect(Pos: TRect): TRect;
    function CellToCanvasRectF(Pos: TRectF): TRectF;
    procedure Assign(Source: TView);
    property DestRect: TRect read FDestRect write SetDestRect;
    property Zoom: Double read FZoom write SetZoom;
  end;

  { TClientGUI }

  TClientGUI = class(TClient)
  protected
    procedure SetGame(AValue: TGame); override;
  public
    View: TView;
    CellGridVisible: Boolean;
    UnitShapeVisible: Boolean;
    procedure DrawArrow(Canvas: TCanvas; Pos: TPoint; Angle: Double;
      Text: string; View: TView);
    procedure PaintCell(Canvas: TCanvas; Pos: TPoint; Text: string; View: TView;
      Cell: TPlayerCell);
    procedure PaintMapCell(Canvas: TCanvas; Pos: TPoint; Text: string; View: TView;
      Cell: TCell);
    procedure DrawArrows(Canvas: TCanvas; View: TView);
    procedure DrawCells(Canvas: TCanvas; View: TView);
    procedure DrawCellLinks(Canvas: TCanvas; View: TView);
    procedure DrawNeighborLinks(Canvas: TCanvas; View: TView);
    procedure DrawCities(Canvas: TCanvas; View: TView);
    procedure DrawSelection(Canvas: TCanvas; View: TView);
    procedure Paint(Canvas: TCanvas; View: TView);
    constructor Create; override;
    destructor Destroy; override;
  end;

implementation

uses
  UBuilding;

resourcestring
  SZeroZoomNotAlowed = 'Zero zoom not allowed';
  SWrongArrowAngle = 'Wrong arrow angle %s';

{ TClientGUI }

procedure TClientGUI.Paint(Canvas: TCanvas; View: TView);
begin
  DrawCellLinks(Canvas, View);
  DrawCells(Canvas, View);
  DrawCities(Canvas, View);
  DrawSelection(Canvas, View);
  DrawArrows(Canvas, View);
  if TGame(Game).DevelMode then DrawNeighborLinks(Canvas, View);
end;

constructor TClientGUI.Create;
begin
  inherited;
  View := TView.Create;
end;

destructor TClientGUI.Destroy;
begin
  FreeAndNil(View);
  inherited Destroy;
end;

procedure TClientGUI.SetGame(AValue: TGame);
begin
  inherited;
  View.Game := AValue;
end;

procedure TClientGUI.PaintCell(Canvas: TCanvas; Pos: TPoint; Text: string;
  View: TView; Cell: TPlayerCell);
var
  I: Integer;
  TextPos: TPoint;
  Points: array of Classes.TPoint;
  TextSize: TSize;
  R: Integer;
begin
  if Cell.MapCell.Extra = etObjectiveTarget then begin
    Text := Text + '!';
  end;
  with Canvas do begin
    // Cannot set clear border as it will display shifted on gtk2
    //Pen.Style := psClear;
    Pen.Style := psSolid;
    if CellGridVisible then begin
      Pen.Color := clBlack;
      Pen.Width := Round(2 * View.Zoom);
    end else begin
      Pen.Color := Brush.Color;
      Pen.Width := 0;
    end;

    // Transform view
    SetLength(Points, Length(Cell.MapCell.Polygon.Points));
    for I := 0 to Length(Points) - 1 do
      Points[I] := PointToStdPoint(View.CellToCanvasPos(Cell.MapCell.Polygon.Points[I]));
    Brush.Style := bsSolid;
    TCanvasEx.PolygonEx(Canvas, Points, False);

    // Show cell text
    if (Cell.GetAvialPower <> 0) or (Cell.MapCell.Extra = etObjectiveTarget) then begin
      Pen.Style := psSolid;
      Font.Color := clWhite;
      Brush.Style := bsClear;
      Font.Size := Trunc(42 * View.Zoom);
      TextPos := View.CellToCanvasPos(Pos);
      TextSize := TextExtent(Text);
      TCanvasEx.TextOutEx(Canvas, Round(TextPos.X) - TextSize.cx div 2,
        Round(TextPos.Y) - TextSize.cy div 2, Text, False);
    end;
    if UnitShapeVisible and Assigned(Cell.MapCell.OneUnit) then begin
      TextPos := View.CellToCanvasPos(Pos);
      R := Trunc(70 * View.Zoom);
      Pen.Color := clWhite;
      Pen.Style := psSolid;
      Pen.Width := Round(10 * View.Zoom);
      Brush.Style := bsClear;
      TCanvasEx.EllipseEx(Canvas, TRect.Create(
        TPoint.Create(TextPos.X - R, TextPos.Y - R),
        TPoint.Create(TextPos.X + R, TextPos.Y + R)
      ));
    end;
  end;
end;

procedure TClientGUI.PaintMapCell(Canvas: TCanvas; Pos: TPoint; Text: string;
  View: TView; Cell: TCell);
var
  I: Integer;
  TextPos: TPoint;
  Points: array of Classes.TPoint;
  TextSize: TSize;
  R: Integer;
begin
  if Cell.Extra = etObjectiveTarget then begin
    Text := Text + '!';
  end;
  with Canvas do begin
    if Assigned(View.FocusedCell) and (View.FocusedCell.MapCell = Cell) then begin
      Pen.Color := clYellow;
      Pen.Style := psSolid;
      Pen.Width := 1;
    end else
    if Assigned(Cell.Building) and Assigned(Cell.Building.Kind) and
    (Cell.Building.Kind.SpecialType = stCity) then begin
      // Cannot set clear border as it will display shifted on gtk2
      //Pen.Style := psClear;
      Pen.Color := clBlack;
      Pen.Style := psSolid;
      Pen.Width := Trunc(5 * View.Zoom);
    end else begin
      // Cannot set clear border as it will display shifted on gtk2
      //Pen.Style := psClear;
      Pen.Color := Brush.Color;
      Pen.Style := psSolid;
      Pen.Width := 0;
    end;
    // Transform view
    SetLength(Points, Length(Cell.Polygon.Points));
    for I := 0 to Length(Points) - 1 do
      Points[I] := PointToStdPoint(View.CellToCanvasPos(Cell.Polygon.Points[I]));
    Brush.Style := bsSolid;
    //Polygon(Points, False, 0, Length(Points));
    TCanvasEx.PolygonEx(Canvas, Points, False);
    //MoveTo(Points[0].X, Points[0].Y);
    //LineTo(Points[1].X, Points[1].Y);

    // Show cell text
    if Assigned(Cell.OneUnit) and (Cell.OneUnit.Power <> 0) or
      (Cell.Extra = etObjectiveTarget) then begin
      Pen.Style := psSolid;
      Font.Color := clWhite;
      Brush.Style := bsClear;
      Font.Size := Trunc(42 * View.Zoom);
      TextPos := View.CellToCanvasPos(Pos);
      TextSize := TextExtent(Text);
      TCanvasEx.TextOutEx(Canvas, Round(TextPos.X) - TextSize.cx div 2,
        Round(TextPos.Y) - TextSize.cy div 2, Text, False);
    end;
    if UnitShapeVisible and Assigned(Cell.OneUnit) then begin
      TextPos := View.CellToCanvasPos(Pos);
      R := Trunc(70 * View.Zoom);
      Pen.Color := clWhite;
      Pen.Style := psSolid;
      Pen.Width := Round(10 * View.Zoom);
      Brush.Style := bsClear;
      TCanvasEx.EllipseEx(Canvas, TRect.Create(
        TPoint.Create(TextPos.X - R, TextPos.Y - R),
        TPoint.Create(TextPos.X + R, TextPos.Y + R)
      ));
    end;
  end;
end;

procedure TClientGUI.DrawArrows(Canvas: TCanvas; View: TView);
var
  PosFrom, PosTo: TPoint;
  Angle: Double;
  ArrowCenter: TPoint;
  Move: TUnitMove;
  P: TPoint;
begin
  with Canvas, View do begin
    Pen.Color := clCream;
    if Assigned(ControlPlayer) then
    for Move in ControlPlayer.Moves do begin
      PosFrom := TGame(Game).Map.CellToPos(Move.CellFrom.MapCell);
      PosTo := TGame(Game).Map.CellToPos(Move.CellTo.MapCell);
      if TGame(Game).Map.Cyclic then begin
        P := TPoint.Create(PosTo.X + TGame(Game).Map.PixelRect.Size.X,
          PosTo.Y);
        if TLine.Create(PosFrom, PosTo).Distance > TLine.Create(PosFrom, P).Distance then
          PosTo := TPoint.Create(P.X, PosTo.Y);
        P := TPoint.Create(PosTo.X, PosTo.Y + TGame(Game).Map.PixelRect.Size.Y);
        if TLine.Create(PosFrom, PosTo).Distance > TLine.Create(PosFrom, P).Distance then
          PosTo := TPoint.Create(PosTo.X, P.Y);
        P := TPoint.Create(PosTo.X - TGame(Game).Map.PixelRect.Size.X,
          PosTo.Y);
        if TLine.Create(PosFrom, PosTo).Distance > TLine.Create(PosFrom, P).Distance then
          PosTo := TPoint.Create(P.X, PosTo.Y);
        P := TPoint.Create(PosTo.X, PosTo.Y - TGame(Game).Map.PixelRect.Size.Y);
        if TLine.Create(PosFrom, PosTo).Distance > TLine.Create(PosFrom, P).Distance then
          PosTo := TPoint.Create(PosTo.X, P.Y);
      end;
      // In Fog of war mode show only
      if TGame(Game).FogOfWar and not ControlPlayer.PlayerMap.Cells.SearchCell(Move.CellFrom.MapCell).InVisibleRange and
        not ControlPlayer.PlayerMap.Cells.SearchCell(Move.CellTo.MapCell).InVisibleRange then
        Continue;
      if Move.CountRepeat > 0 then Pen.Width := Round(2 * View.Zoom)
        else Pen.Width := Round(1 * View.Zoom);
      Angle := ArcTan2(PosTo.Y - PosFrom.Y, PosTo.X - PosFrom.X);
      if (Angle > +Pi) or (Angle < -Pi) then
        raise Exception.Create(Format(SWrongArrowAngle, [FloatToStr(Angle)]));

      ArrowCenter := View.CellToCanvasPos(TPoint.Create(
        Trunc(PosFrom.X + (PosTo.X - PosFrom.X) / 2),
        Trunc(PosFrom.Y + (PosTo.Y - PosFrom.Y) / 2)
      ));
      DrawArrow(Canvas, ArrowCenter,
        Angle, IntToStr(Move.CountOnce), View);
    end;
  end;
end;

procedure TClientGUI.DrawArrow(Canvas: TCanvas; Pos: TPoint;
  Angle: Double; Text: string; View: TView);
var
  Points: array of Classes.TPoint;
  Arrow: TPolygonF;
  I: Integer;
  ArrowSize: TPoint;
  RectPolygon: TRectF;
begin
  SetLength(Arrow.Points, 8);
  ArrowSize := TPoint.Create(Trunc(TGame(Game).Map.DefaultCellSize.X / 3 * View.Zoom),
    Trunc(TGame(Game).Map.DefaultCellSize.Y / 3 * View.Zoom));
  Arrow.Points[0] := TPointF.Create(+0.5 * ArrowSize.X, +0 * ArrowSize.Y);
  Arrow.Points[1] := TPointF.Create(+0 * ArrowSize.X, +0.5 * ArrowSize.Y);
  Arrow.Points[2] := TPointF.Create(+0 * ArrowSize.X, +0.25 * ArrowSize.Y);
  Arrow.Points[3] := TPointF.Create(-0.5 * ArrowSize.X, +0.25 * ArrowSize.Y);
  Arrow.Points[4] := TPointF.Create(-0.5 * ArrowSize.X, -0.25 * ArrowSize.Y);
  Arrow.Points[5] := TPointF.Create(+0 * ArrowSize.X, -0.25 * ArrowSize.Y);
  Arrow.Points[6] := TPointF.Create(+0 * ArrowSize.X, -0.5 * ArrowSize.Y);
  Arrow.Points[7] := TPointF.Create(+0.5 * ArrowSize.X, 0 * ArrowSize.Y);
  // Rotate
  for I := 0 to Length(Arrow.Points) - 1 do begin
    Arrow.Points[I] := TPointF.Create(
      Arrow.Points[I].X * Cos(Angle) - Arrow.Points[I].Y * Sin(Angle),
      Arrow.Points[I].X * Sin(Angle) + Arrow.Points[I].Y * Cos(Angle)
    );
    Arrow.Points[I] := Arrow.Points[I] + TPointF.Create(Pos.X, Pos.Y);
  end;

  RectPolygon := Arrow.GetRect;
  if (RectPolygon.P1.X < View.DestRect.Size.X) and
    (RectPolygon.P2.X > 0) and
    (RectPolygon.P1.Y < View.DestRect.Size.Y) and
    (RectPolygon.P2.Y > 0) then begin

    // Convert to standard points
    SetLength(Points, 8);
    for I := 0 to Length(Points) - 1 do
      Points[I] := Point(Trunc(Arrow[I].X), Trunc(Arrow[I].Y));
    with Canvas do begin
      Pen.Color := clBlack;
      Brush.Color := clWhite;
      Brush.Style := bsSolid;
      Polygon(Points);
      Brush.Style := bsClear;
      Font.Color := clBlack;
      Font.Size := Trunc(18 * View.Zoom);
      TextOut(Pos.X - TextWidth(Text) div 2, Pos.Y - TextHeight(Text) div 2, Text);
      Pen.Width := 1;
    end;
  end;
end;


procedure TClientGUI.DrawCells(Canvas: TCanvas; View: TView);
var
  Cell: TPlayerCell;
  MapCell: TCell;
  CellText: string;
begin
  with Canvas, View do begin
    if Assigned(ControlPlayer) then begin
      for Cell in ControlPlayer.PlayerMap.Cells do begin
        if View.IsCellVisible(Cell.MapCell) and (Cell.MapCell.Terrain <> ttVoid) then begin
          if Cell.MapCell.Player = ControlPlayer then
            CellText := IntToStr(Cell.GetAvialPower)
            else begin
              if Assigned(Cell.MapCell.OneUnit) then
                CellText := IntToStr(Cell.MapCell.OneUnit.Power)
                else CellText := '';
            end;
          if Assigned(SelectedCell) and (SelectedCell = Cell) then
              Brush.Color := clGreen
            else if Assigned(SelectedCell) and ControlPlayer.IsAllowedMoveTarget(SelectedCell, Cell) then
              Brush.Color := clPurple
            else if TGame(Game).FogOfWar then begin
              if Cell.InVisibleRange then begin
                Brush.Color := Cell.MapCell.GetColor;
              end else begin
                if Cell.Explored then begin
                  Brush.Color := $404040;
                  CellText := '';
                end else begin
                  Brush.Color := clBlack;
                  CellText := '';
                end;
              end;
            end else Brush.Color := Cell.MapCell.GetColor;
          PaintCell(Canvas, Cell.MapCell.PosPx, CellText, View, Cell);
        end else
        if TGame(Game).FogOfWar and (Cell.MapCell.Terrain = ttVoid) then begin
          Brush.Color := clBlack;
          PaintCell(Canvas, Cell.MapCell.PosPx, '', View, Cell);
        end;
      end;
    end else begin
      // Draw cells
      for MapCell in TGame(Game).Map.Cells do begin
        if (MapCell.Terrain <> ttVoid) and View.IsCellVisible(MapCell) then begin
          Brush.Color := MapCell.GetColor;
          if Assigned(MapCell.OneUnit) then
            CellText := IntToStr(MapCell.OneUnit.Power)
            else CellText := '';
          PaintMapCell(Canvas, MapCell.PosPx, CellText, View, MapCell);
        end;
      end;
    end;
  end;
end;

procedure TClientGUI.DrawCellLinks(Canvas: TCanvas; View: TView);
var
  I: Integer;
  CellLink: TCellLink;
begin
  with Canvas, View do begin
    Pen.Color := clBlack;
    Pen.Style := psSolid;
    Pen.Width := 3;
    for CellLink in TGame(Game).Map.CellLinks do
    with CellLink do begin
      if Length(Points) >= 2 then begin
        MoveTo(PointToStdPoint(View.CellToCanvasPos(Points[0])));
        for I := 1 to Length(Points) - 1 do
          LineTo(PointToStdPoint(View.CellToCanvasPos(Points[I])));
      end;
    end;
  end;
end;

procedure TClientGUI.DrawNeighborLinks(Canvas: TCanvas; View: TView);
var
  Cell: TPlayerCell;
  C: TCell;
begin
  with Canvas, View do begin
    for Cell in ControlPlayer.PlayerMap.Cells do begin
      for C in Cell.MapCell.Neighbors do begin
        Pen.Color := clYellow;
        MoveTo(PointToStdPoint(View.CellToCanvasPos(Cell.MapCell.PosPx)));
        LineTo(PointToStdPoint(View.CellToCanvasPos(C.PosPx)));
      end;

      Font.Color := clRed;
      Brush.Style := bsClear;
      TextOut(View.CellToCanvasPos(Cell.MapCell.PosPx).X,
        View.CellToCanvasPos(Cell.MapCell.PosPx).Y, IntToStr(Cell.MapCell.Id));
    end;
  end;
end;

procedure TClientGUI.DrawCities(Canvas: TCanvas; View: TView);
var
  Cell: TPlayerCell;
  MapCell: TCell;
  Points: array of Classes.TPoint;
  I: Integer;
begin
  with Canvas, View do begin
    if Assigned(ControlPlayer) then begin
      for Cell in ControlPlayer.PlayerMap.Cells do begin
        if (Cell.MapCell.Terrain <> ttVoid) and View.IsCellVisible(Cell.MapCell) then begin
          if Assigned(Cell.MapCell.Building) and Assigned(Cell.MapCell.Building.Kind) and
          (Cell.MapCell.Building.Kind.SpecialType = stCity) then begin
            // Cannot set clear border as it will display shifted on gtk2
            //Pen.Style := psClear;
            Pen.Color := clBlack;
            Pen.Style := psSolid;
            Pen.Width := Round(6 * View.Zoom);

            // Transform view
            SetLength(Points, Length(Cell.MapCell.Polygon.Points) + 1);
            for I := 0 to Length(Cell.MapCell.Polygon.Points) - 1 do
              Points[I] := PointToStdPoint(View.CellToCanvasPos(Cell.MapCell.Polygon.Points[I]));
            Points[Length(Points) - 1] := Points[0];
            TCanvasEx.PolyLineEx(Canvas, Points);
          end;
        end;
      end;
    end else begin
      for MapCell in TGame(Game).Map.Cells do begin
        if (MapCell.Terrain <> ttVoid) and View.IsCellVisible(MapCell) then begin
          if View.IsCellVisible(MapCell) and (MapCell.Terrain <> ttVoid) then begin
            if Assigned(MapCell.Building) and Assigned(MapCell.Building.Kind) and
              (MapCell.Building.Kind.SpecialType = stCity) then begin
              // Cannot set clear border as it will display shifted on gtk2
              //Pen.Style := psClear;
              Pen.Color := clBlack;
              Pen.Style := psSolid;
              Pen.Width := Round(6 * View.Zoom);

              // Transform view
              SetLength(Points, Length(MapCell.Polygon.Points) + 1);
              for I := 0 to Length(MapCell.Polygon.Points) - 1 do
                Points[I] := PointToStdPoint(View.CellToCanvasPos(MapCell.Polygon.Points[I]));
              Points[Length(Points) - 1] := Points[0];
              TCanvasEx.PolyLineEx(Canvas, Points);
            end;
          end;
        end;
      end;
    end;
  end;
end;

procedure TClientGUI.DrawSelection(Canvas: TCanvas; View: TView);
var
  Points: array of Classes.TPoint;
  I: Integer;
begin
  with Canvas, View do begin
    if Assigned(ControlPlayer) then begin
      if Assigned(View.FocusedCell) then begin
        Pen.Color := clYellow;
        Pen.Style := psSolid;
        Pen.Width := Round(2 * View.Zoom);

        // Transform view
        SetLength(Points, Length(View.FocusedCell.MapCell.Polygon.Points) + 1);
        for I := 0 to Length(View.FocusedCell.MapCell.Polygon.Points) - 1 do
          Points[I] := PointToStdPoint(View.CellToCanvasPos(View.FocusedCell.MapCell.Polygon.Points[I]));
        Points[Length(Points) - 1] := Points[0];
        TCanvasEx.PolyLineEx(Canvas, Points);
      end;
    end;
  end;
end;

{ TView }

procedure TView.SetZoom(AValue: Double);
begin
  if FZoom = AValue then Exit;
  if AValue = 0 then
    raise Exception.Create(SZeroZoomNotAlowed);
  FZoom := AValue;
  SourceRect := TRect.CreateBounds(TPoint.Create(Trunc(SourceRect.P1.X + SourceRect.Size.X div 2 - DestRect.Size.X / Zoom / 2),
    Trunc(SourceRect.P1.Y +  SourceRect.Size.Y div 2 - DestRect.Size.Y / Zoom / 2)),
    TPoint.Create(Trunc(DestRect.Size.X / Zoom),
    Trunc(DestRect.Size.Y / Zoom)));
end;

procedure TView.Clear;
begin
  FocusedCell := nil;
  SelectedCell := nil;
end;

procedure TView.SetDestRect(AValue: TRect);
var
  Diff: TPoint;
begin
  if FDestRect = AValue then Exit;
  Diff := TPoint.Create(Trunc(DestRect.Size.X / Zoom - AValue.Size.X / Zoom) div 2,
    Trunc(DestRect.Size.Y / Zoom - AValue.Size.Y / Zoom) div 2);
  FDestRect := AValue;
  SourceRect := TRect.CreateBounds(TPoint.Create(SourceRect.P1.X + Diff.X, SourceRect.P1.Y + Diff.Y),
    TPoint.Create(Trunc(DestRect.Size.X / Zoom),
    Trunc(DestRect.Size.Y / Zoom)));
end;

constructor TView.Create;
begin
  Zoom := 1.5;
  Clear;
end;

destructor TView.Destroy;
begin
  inherited Destroy;
end;

function TView.CanvasToCellPos(Pos: TPoint): TPoint;
begin
  Result := TPoint.Create(Trunc((Pos.X - DestRect.P1.X) / Zoom + SourceRect.P1.X),
    Trunc((Pos.Y - DestRect.P1.Y) / Zoom + SourceRect.P1.Y));
end;

function TView.CellToCanvasPos(Pos: TPoint): TPoint;
begin
  Result := TPoint.Create(Trunc((Pos.X - SourceRect.P1.X) * Zoom) + DestRect.P1.X,
    Trunc((Pos.Y - SourceRect.P1.Y) * Zoom) + DestRect.P1.Y);
end;

function TView.CellToCanvasPosF(Pos: TPointF): TPointF;
begin
  Result := TPointF.Create((Pos.X - SourceRect.P1.X) * Zoom + DestRect.P1.X,
    (Pos.Y - SourceRect.P1.Y) * Zoom + DestRect.P1.Y);
end;

function TView.CanvasToCellRect(Pos: TRect): TRect;
begin
  Result.P1 := CanvasToCellPos(Pos.P1);
  Result.P2 := CanvasToCellPos(Pos.P2);
end;

function TView.CellToCanvasRect(Pos: TRect): TRect;
begin
  Result.P1 := CellToCanvasPos(Pos.P1);
  Result.P2 := CellToCanvasPos(Pos.P2);
end;

function TView.CellToCanvasRectF(Pos: TRectF): TRectF;
begin
  Result.P1 := CellToCanvasPosF(Pos.P1);
  Result.P2 := CellToCanvasPosF(Pos.P2);
end;

procedure TView.Assign(Source: TView);
begin
  SourceRect := Source.SourceRect;
  FDestRect := Source.DestRect;
  FZoom := Source.Zoom;
  SelectedCell := Source.SelectedCell;
  FocusedCell := Source.FocusedCell;
end;

procedure TView.SelectCell(Pos: TPoint; Player: TPlayer; ShiftState: TShiftState);
var
  NewSelectedCell: TPlayerCell;
  UnitMove: TUnitMove;
  I: Integer;
  CellPos: TPoint;
  R: TRect;
begin
  if TGame(Game).Map.Cyclic then begin
    R := CellToCanvasRect(TGame(Game).Map.PixelRect);
    CellPos := TPoint.Create(
      ModNeg(Pos.X - R.P1.X, R.Size.X) + R.P1.X,
      ModNeg(Pos.Y - R.P1.Y, R.Size.Y) + R.P1.Y
    );
    NewSelectedCell := Player.PlayerMap.PosToCell(
      CanvasToCellPos(CellPos));
  end else begin
    NewSelectedCell := Player.PlayerMap.PosToCell(
      CanvasToCellPos(Pos));
  end;
  if Assigned(NewSelectedCell) then begin
    if Assigned(SelectedCell) and (NewSelectedCell <> SelectedCell) and
    TGame(Game).CurrentPlayer.IsAllowedMoveTarget(SelectedCell, NewSelectedCell) then begin
      if ssShift in ShiftState then begin
        // Make maximum unit move without confirmation dialog
        for I := SelectedCell.MovesFrom.Count - 1 downto 0 do begin
          Player.Moves.Remove(SelectedCell.MovesFrom[I]);
        end;
        TGame(Game).CurrentPlayer.SetMove(SelectedCell, NewSelectedCell, SelectedCell.MapCell.OneUnit.Power, False);
        SelectedCell := nil;
      end else
      if ssCtrl in ShiftState then begin
        // If CTRL key pressed then storno all moved from selected cell and
        // move all power to new selected cell
        for I := SelectedCell.MovesFrom.Count - 1 downto 0 do
          Player.Moves.Remove(SelectedCell.MovesFrom[I]);
        UnitMove := TGame(Game).CurrentPlayer.SetMove(SelectedCell, NewSelectedCell, SelectedCell.MapCell.OneUnit.Power, False);
        if Assigned(UnitMove) then
          UnitMove.CountRepeat := TGame(Player.Game).Map.MaxPower;
        if NewSelectedCell.MapCell.Player = Player then SelectedCell := NewSelectedCell
          else SelectedCell := nil;
      end else begin
        TGame(Game).CurrentPlayer.SetMove(SelectedCell, NewSelectedCell, SelectedCell.MapCell.OneUnit.Power);
        SelectedCell := nil;
      end;
    end else
    if not Assigned(SelectedCell) and (NewSelectedCell <> SelectedCell) and (NewSelectedCell.MapCell.Player = Player) then begin
      SelectedCell := NewSelectedCell
    end else
    if (NewSelectedCell = SelectedCell) and (NewSelectedCell.MapCell.Player = Player) then begin
      SelectedCell := nil;
    end;
  end;
end;

procedure TView.CenterMap;
var
  MapRect: TRect;
begin
  MapRect := TGame(Game).Map.PixelRect;
  SourceRect := TRect.CreateBounds(TPoint.Create(MapRect.P1.X + MapRect.Size.X div 2 - SourceRect.Size.X div 2,
    MapRect.P1.Y + MapRect.Size.Y div 2 - SourceRect.Size.Y div 2),
    TPoint.Create(SourceRect.Size.X,
    SourceRect.Size.Y));
end;

procedure TView.CenterPlayerCity(Player: TPlayer);
begin
  SourceRect := TRect.CreateBounds(TPoint.Create(Player.StartCell.PosPx.X - SourceRect.Size.X div 2,
    Player.StartCell.PosPx.Y - SourceRect.Size.Y div 2),
    TPoint.Create(SourceRect.Size.X,
    SourceRect.Size.Y));
end;

procedure TView.ZoomAll;
var
  Factor: TPointF;
  MapRect: TRect;
  NewZoom: Single;
begin
  MapRect := TGame(Game).Map.CalculatePixelRect;
  Factor := TPointF.Create(DestRect.Size.X / MapRect.Size.X,
    DestRect.Size.Y / MapRect.Size.Y);
  if Factor.X < Factor.Y then NewZoom := Factor.X
    else NewZoom := Factor.Y;
  if NewZoom = 0 then NewZoom := 1;
  Zoom := NewZoom;
  CenterMap;
end;

function TView.IsCellVisible(Cell: TCell): Boolean;
var
  RectPolygon: TRect;
begin
  RectPolygon := CellToCanvasRect(Cell.Polygon.GetRect);
  Result := (
    (RectPolygon.P1.X < DestRect.Size.X) and
    (RectPolygon.P2.X > 0) and
    (RectPolygon.P1.Y < DestRect.Size.Y) and
    (RectPolygon.P2.Y > 0)
  );
end;

end.

