% \iffalse
% --------------------------------------------------------------------
%<*hex>
% \fi
% 
% \subsubsection{Board clipping and frame}
%
% \begin{Macro}{\boardframe}
% 
%   Define the bounding box around the board
%   
%   \begin{Syntax}
%     \cs{boardframe}\oarg{margin}\parg{lower=left}\parg{upper-right}\marg{margin}
%   \end{Syntax}
%   
%   where \meta{lower-left} and \meta{upper-right} specifies the lower
%   left and upper right hexes (inclusive) of the board.  
%   
%
%    \begin{macrocode}
\tikzset{
  hex/board frame/.style={draw}
}
\def\boardframe{%
  \@ifnextchar[{\bo@rdframe}{\bo@rdframe[0]}%}
}
\def\bo@rdframe[#1](#2)(#3){%
  \hex@coords@conv{#2}
  \edef\llx{\hex@x}
  \edef\lly{\hex@y}
  \edef\llc{\hex@col}
  \edef\llr{\hex@row}
  \edef\ellc{\hex@eff@col}
  \edef\ellr{\hex@eff@row}
  %
  \hex@coords@conv{#3}
  \edef\urx{\hex@x}
  \edef\ury{\hex@y}
  \edef\urc{\hex@col}
  \edef\urr{\hex@row}
  \edef\eurc{\hex@eff@col}
  \edef\eurr{\hex@eff@row}
  %
  \def\margin{#1}
  %
  \hex@dbg{2}{%
    Board Hex range: (\llc,\llr)x(\urc,\urr)
    ^^JEffective range: (\ellc,\ellr)x(\eurc,\eurr)
    ^^JBB:              (\llx,\lly)x(\urx,\ury)}%
  \ifnum\hexdbglvl>1
    %\draw[red,very thick](hex cs:c=\llc,r=\llr) rectangle(hex cs:c=\urc,r=\urr);
    \draw[red,ultra thick,dashed](\llx,\lly) rectangle(\urx,\ury);
    \draw[->,very thick,blue] (0,0) -- (0,1) (0,0) -- (1,0);
  \fi
  % Calculate how many half hex hides to add to the "bottom"
  %
  \def\oddeven{isodd}
  \ifnum\hex@coords@row@fac<0\def\oddeven{iseven}\fi%
  \pgfmathparse{
    ifthenelse(\hex@got@bot@short(\ellc),
     ifthenelse(\hex@bot@short@col(\llc)*not(\oddeven(\ellc)),2,
      ifthenelse(\hex@bot@short@col(\llc),0,1)),
     ifthenelse(\oddeven(\ellc),1,2))}
  \edef\olly{\pgfmathresult}%
  \hex@dbg{2}{Delta lly: \olly half heights}
  % Calculate how many half hex heights to add to the "top"
  \def\oddeven{iseven}
  \ifnum\hex@coords@row@fac<0\def\oddeven{isodd}\fi%
  %
  \pgfmathparse{
    ifthenelse(\hex@got@top@short(\urc),
     ifthenelse(\hex@top@short@col(\urc)*\oddeven(\eurc),0,
      ifthenelse(\hex@top@short@col(\urc),2,1)),
     ifthenelse(\oddeven(\eurc),1,2))}
  \edef\oury{\pgfmathresult}%
  \hex@dbg{2}{Delta ury: \oury half heights}
  % Calculate new LLY and URY
  \pgfmathparse{\lly-\hex@coords@row@fac*(\olly*\hex@yy+\margin)}
  \edef\lly{\pgfmathresult}
  \pgfmathparse{\ury+\hex@coords@row@fac*(\oury*\hex@yy+\margin)}
  \edef\ury{\pgfmathresult}
  % Calculate new LLX and URX 
  \pgfmathparse{\llx-1-\margin}\edef\llx{\pgfmathresult}
  \pgfmathparse{\urx+1+\margin}\edef\urx{\pgfmathresult}
  % Calculate width and height 
  \pgfmathparse{\urx-\llx}\edef\w{\pgfmathresult}
  \pgfmathparse{\ury-\lly}\edef\h{\pgfmathresult}
  \hex@dbg{0}{Board Frame: (\llx,\lly)x(\urx,\ury) (\w x\h) (\llc,\llr)x(\urc,\urr)}
  \draw[hex/board frame/.try](\llx,\lly) rectangle(\urx,\ury);
  \xdef\boardXmin{\llx}%
  \xdef\boardYmin{\lly}%
  \xdef\boardXmax{\urx}%
  \xdef\boardYmax{\ury}%
}
  
%    \end{macrocode}
% \end{Macro}
%    
% \begin{Macro}{\boardclip}
% 
%   Clip the board to not show incomplete hexes 
% 
% \begin{Syntax}
%   \cs{boardclip}\marg{nx}\marg{ny}\marg{preaction}  
% \end{Syntax}
%    \begin{macrocode}
\def\boardpath(#1)(#2){%
  \hex@coords@reset%
  \tikzset{/hex/coords/.cd, #1}
  \edef\llx{\hex@col}
  \edef\lly{\hex@row}
  %%
  \hex@coords@reset%
  \tikzset{/hex/coords/.cd, #2}
  \edef\urx{\hex@col}
  \edef\ury{\hex@row}
  \let\board@odd\@undefined%
  \hex@dbg{1}{Board BB in hex: (\llx,\lly)x(\urx,\ury)}
  %%
  \def\fv{south}
  \def\sv{north}
  \ifnum\hex@coords@row@fac<0
    \def\fv{north}
    \def\sv{south}
  \fi

  \edef\hex@board@path{(hex cs:c=\llx,r=\lly,v=\fv\space west)}
  %% First the left side
  \foreach \r in {\lly,...,\ury} {%
    \edef\t{
      --(hex cs:c=\llx,r=\r,v=west)
      --(hex cs:c=\llx,r=\r,v=\sv\space west)}
    \wg@addto@macro{\hex@board@path}{\t}}
  %% Then for top of board
  \foreach \c in {\llx,...,\urx} {%
    % To be done
    %\pgfmathparse{int(ifthenelse(\hex@bot@short@col(\c),1,0))}
    %\edef\tmp{\pgfmathresult}
    %\ifnum\tmp>0
    %\edef\t{
    %  --(hex cs:c=\c,r=\ury,v=\sv\space west)
    %  --(hex cs:c=\c,r=\ury,v=\sv\space east)}
    %\else
    \edef\t{
      --(hex cs:c=\c,r=\ury,v=\fv\space east)
      --(hex cs:c=\c,r=\ury,v=\fv\space west)}
    %\fi
    \wg@addto@macro{\hex@board@path}{\t}}
  %% Then for right of board
  \foreach \r in {\ury,...,\lly} {%
    \edef\t{
      --(hex cs:c=\urx,r=\r,v=east)
      --(hex cs:c=\urx,r=\r,v=\fv\space east)}
    \wg@addto@macro{\hex@board@path}{\t}}
      
  %% Then for bottom of board
  \edef\t{--(hex cs:r=\lly,c=\urx,v=\fv\space west)}
  \wg@addto@macro{\hex@board@path}{\t}
  \foreach \c in {\urx,...,\llx} {%
    \pgfmathparse{int(ifthenelse(\hex@bot@short@col(\c),1,0))}
    \edef\tmp{\pgfmathresult}
    \ifnum\tmp>0
    \edef\t{
      --(hex cs:c=\c,r=\lly,v=\sv\space east)
      --(hex cs:c=\c,r=\lly,v=\sv\space west)}
    \else
    \edef\t{
      --(hex cs:c=\c,r=\lly,v=\fv\space east)
      --(hex cs:c=\c,r=\lly,v=\fv\space west)}
    \fi
    \wg@addto@macro{\hex@board@path}{\t}}
    
  \def\t{--cycle}
  \wg@addto@macro{\hex@board@path}{\t}
  \global\let\hexboardpath\hex@board@path
}
%\def\boardclip#1#2#3{%
%  \pgfmathparse{int(#1-1)}\xdef\board@range{\pgfmathresult,...,0}%
%  %% \show\board@range
%  \draw \ifx|#3|\else[preaction={#3}]\fi%
%  [clip]
%  % [decorate,decoration={show path construction,
%  % moveto code={\fill[red](\tikzinputsegmentfirst) circle(2pt)
%  % node [fill=none,below]{moveto};},
%  % lineto code={\draw[thick,blue,->](\tikzinputsegmentfirst)--
%  % (\tikzinputsegmentlast) node [above] {lineto};},
%  % curveto code={\draw[thick,green,->](\tikzinputsegmentfirst)..
%  % controls(\tikzinputsegmentsupporta) and
%  % (\tikzinputsegmentsupportb)
%  % ..(\tikzinputsegmentlast) node[above]{curveto};},
%  % closepath code={\draw[thick,orange,->](\tikzinputsegmentfirst)--
%  % (\tikzinputsegmentlast) node [above] {closepath};}
%  % }]
%  (hex cs:r=0,c=0,v=south west)
%  %% First the left side
%  \foreach \r in {0,1,...,#2} {%
%    --(hex cs:c=0,r=\r,v=west)--(hex cs:c=0,r=\r,v=north west)}
%  %% Then for top of board
%  \foreach \c in {0,1,...,#1} {%
%    --(hex cs:r=#2,c=\c,v=north west)--(hex cs:c=\c,r=#2,v=north east)}
%  %% Then for right of board
%  \foreach \r in {#2,...,0} {%
%    --(hex cs:c=#1,r=\r,v=east)--(hex cs:c=#1,r=\r,v=south east)}
%  %% Then for bottom of board
%  --(hex cs:r=0,c=#1,v=south west) \foreach \c in \board@range {%
%    --(hex cs:r=0,c=\c,v=south east) --(hex cs:c=\c,r=0,v=south west) }
%  --cycle; }
%% New definition - much simpler 
\def\boardclip(#1)(#2)#3{%
  \boardpath(#1)(#2)
  \draw \ifx|#3|\else[preaction={#3}]\fi%
  [clip] \hexboardpath;
}
  
%    \end{macrocode}
% \end{Macro}
%    
% 
% \begin{Macro}{\debuggrid}
%  
%   Show a debug grid.  This requires \cs{boardframe}.
%
%    \begin{macrocode}
\def\debuggrid{%
  \foreach \i in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1} {%
    \pgfmathparse{\i*\boardW+\boardXmin}%
    \edef\debug@x{\pgfmathresult}%
    \draw [very thin,gray](\debug@x,\boardYmin) --
    (\debug@x,\boardYmax) node [below,rotate=90] at
    (\debug@x,\boardYmin) {$\i$}; }%
  \foreach \i in {0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1} {%
    \pgfmathparse{\i*\boardH+\boardYmin}%
    \edef\debug@x{\pgfmathresult}%
    \draw [very thin,gray] (\boardXmin,\debug@x) --
    (\boardXmax,\debug@x) node [left,rotate=90] at
    (\boardXmin,\debug@x) {$\i$}; } }
%    \end{macrocode}
% \end{Macro}
%
% Some dummy styles.  These will be defined by the export class to
% facilitate getting information from the board.
% 
%    \begin{macrocode}
\tikzset{%
  zoned/.style={},
  zone scope/.style={},
  zone path/.style={}
}
%    \end{macrocode}
% \iffalse
%</hex>
% --------------------------------------------------------------------
% \fi
