% \iffalse
% --------------------------------------------------------------------
%<*hex>
% \fi
% 
% \subsubsection{Labels}
% \label{sec:impl:hex:label}
%
% Like terrains, we will set up some macros for dealing with labels.
%
% To process coordinates and turn them into labels, we set up two
% counters.
%
%    \begin{macrocode}
\newcounter{hex@l@c}
\newcounter{hex@l@r}    
%    \end{macrocode}
%
% In case we want to invert the row axis, we set-up a key to set the
% maximum row number.
%
%    \begin{macrocode}
\def\hex@max@row{-1} 
\tikzset{ 
  max hex row/.store in=\hex@max@row,
}
%    \end{macrocode}
%    
% Again, we will make a separate namespace/family for the handling
% labels.  We also define a counter which we will use to typeset
% alphabetic column numbers.
%
% First a graphics style. 
%
%    \begin{macrocode}
\tikzset{%
  hex/label/.style={%
    draw=none,%
    shape=rectangle,%
    anchor=north,%
    color=gray,%
    font=\sffamily\bfseries\scriptsize,%
    inner sep=0},
}
%    \end{macrocode}
%
% Next, the choices of how to make a label.  These are put in the
% \spec{/hex/label} family to make it easy to parse out only these
% keys.  This uses some macros defined below.  Note, this uses the
% macros \cs{hex@col} and \cs{hex@row} defined by the hex coordinate
% system.  \emph{Important:} If you do not want to set a label but
% they are otherwise automatically added, then set \texttt{label=none}
% rather than \texttt{label=} (empty), which will not work.
%
%    \begin{macrocode}
\tikzset{%
  /hex/label/.search also={/tikz},
  /hex/label/.cd,
  none/.code={\global\let\hex@l@text\@empty},
  auto/.is choice,
  auto/none/.code={\global\let\hex@l@text\@empty},
  auto/numbers/.code={%
    \hex@l@abs%
    \xdef\hex@l@text{%
      \hex@l@n@pad{\the\c@hex@l@c}%
      \hex@l@n@pad{\the\c@hex@l@r}}},
  auto/alpha column/.code={%
    \xdef\hex@l@text{%
      \ifnum0>\hex@col\AlphAlph{-\hex@col}\else\AlphAlph{\hex@col}\fi%
      \hex@row}},
  auto/alpha 2 column/.code={%
    \hex@l@abs%
    \advance\c@hex@l@c27\relax%
    \xdef\hex@l@text{%
      \AlphAlph{\value{hex@l@c}}%
      \hex@l@n@pad{\hex@row}}},
  auto/inv y x plus 1/.code={%
    \hex@dbg{3}{Inverse row, add one to column with arg `#1'}
    \let\hex@l@text\@empty%
    \ifnum\hex@max@row>0%
      \pgfmathtruncatemacro{\hex@l@row}{\hex@max@row-\hex@row}
      \pgfmathtruncatemacro{\hex@l@col}{1+\hex@col}
      \xdef\hex@l@text{%
        \hex@l@n@pad{\hex@l@col}%
        \hex@l@n@pad{\hex@l@row}}
    \else\message{Max row number not set}\fi},
  auto/x and y plus 1/.code={%
    \hex@dbg{3}{Inverse row, add one to column with arg `#1'}
    \pgfmathtruncatemacro{\hex@l@row}{1+\hex@row}
    \pgfmathtruncatemacro{\hex@l@col}{1+\hex@col}
    \xdef\hex@l@text{%
      \hex@l@n@pad{\hex@l@col}%
      \hex@l@n@pad{\hex@l@row}}},
  auto/.default=numbers,
  %text/.store in=\hex@l@text,
  text/.code={\gdef\hex@l@text{#1}},
  place/.store in=\hex@l@pos,
  location/.forward to=/hex/label/place,
  rotate/.store in=\hex@l@rot
}
%    \end{macrocode}
% \begin{Macro}{\hex@l@abs}
%
%   This takes the absolute value of row and column numbers.
%   
%    \begin{macrocode}
\def\hex@l@abs{
  \setcounter{hex@l@c}{\hex@col}
  \setcounter{hex@l@r}{\hex@row}
  \expandafter\ifnum\value{hex@l@c}<0\multiply\c@hex@l@c by-1\fi%
  \expandafter\ifnum\value{hex@l@r}<0\multiply\c@hex@l@r by-1\fi%
  % \hex@dbg{0}{\hex@col->\the\c@hex@l@c\space\hex@row->\the\c@hex@l@r}
}
%    \end{macrocode}
% \end{Macro}
% 
% \begin{Macro}{\hex@l@n@pad}
%   This will pad a number with a 0 if the number is smaller than 10.
%
%    \begin{macrocode}
\long\def\hex@l@n@pad#1{%
  \ifnum#1<10 0\fi%
  #1}
%    \end{macrocode}
% \end{Macro}
% 
%
% \begin{Macro}{\hex@do@label}
%
%   This macro puts in the label.  First, we reset label keys, then we
%   read in the keys from the argument.  If this results in the macro
%   \cs{hex@l@text} to be non-empty, then we set the label via a
%   \TikZ{} \spec{node}.
%
%    \begin{macrocode}
\def\hex@do@label{%
  \hex@dbg{3}{Hex label: `\meaning\hex@label'}%
  \edef\hex@l@tmp{[%
    /hex/label/.cd,%
    rotate=0,%
    place={(90:.8)},%
    /tikz/hex/label/.try,%
    /tikz/every hex label/.try,%
    \hex@label]}%
  \expandafter\scope\hex@l@tmp%
    \hex@dbg{1}{Label:
      ^^J  Text: `\meaning\hex@l@text'
      ^^J  Location: `\meaning\hex@l@pos'
      ^^J  Rotation: `\meaning\hex@l@rot'
    }%
    \@ifundefined{hex@l@text}{\let\hex@l@text\empty}{}%
    \ifx\hex@l@text\empty\else%
    \node[rotate=\hex@l@rot] at \hex@l@pos {\hex@l@text};%
    \fi%
  \endscope%
}
%    \end{macrocode}
% \end{Macro}
% \iffalse
%</hex>
% --------------------------------------------------------------------
% \fi