summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/wargame/source/hex/labels.dtx
blob: 7767dcc66105fbe708ba282f1fc4091f8ae45383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
% \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.
%
%    \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{1}{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