Debian Codegraph @

texmf-dist/source/latex/wargame/hex/shape.dtx

raw
1 % \iffalse
2 % --------------------------------------------------------------------
3 %<*hex>
4 % \fi
5 %
6 % \subsubsection{Hexes}
7 % \label{sec:impl:hex:hex}
8 %
9 % In this part, we make macros etc. for the hexes.
10 %
11 % A hex shape. We make a node of this shape if we are to give a name
12 % to the hex added. We add a bunch of anchors to it so we may easily
13 % refer to it. This is also where we actual fill stuff into the hex,
14 % such as terrain and so on.
15 %
16 % \begin{macrocode}
17 \tikzset{%
18 /hex/.cd,
19 bev/.store in=\hex@bevel, bev/.initial=,
20 bevel fraction/.store in=\hex@bevel@frac,bevel fraction/.initial=10,
21 bevel/.is choice,
22 bevel/none/.style = {/hex/bev=},
23 bevel/north west/.style = {/hex/bev=1},
24 bevel/north east/.style = {/hex/bev=2},
25 bevel/south west/.style = {/hex/bev=3},
26 bevel/south east/.style = {/hex/bev=4},
27 bevel/NW/.style = {/hex/bev=1},
28 bevel/NE/.style = {/hex/bev=2},
29 bevel/SW/.style = {/hex/bev=3},
30 bevel/SE/.style = {/hex/bev=4},
31 bevel/.default = {north west},
32 }
33 \def\hex@bevel@frac{10}
34 \tikzset{
35 hex/bevel highlight/.style={fill=white,opacity=.25},
36 hex/bevel shadow/.style={fill=black,opacity=.25},
37 }
38 % \end{macrocode}
39 %
40 %
41 %
42 % \begin{macrocode}
43 \newdimen\wg@tmpe
44 \newdimen\wg@tmpf
45 \newdimen\wg@tmpg
46 \def\hex@bevel@path#1{%
47 \scope[#1]
48 \wg@tmpe=\wg@tmpa\multiply\wg@tmpe by \hex@bevel@frac
49 \wg@tmpf=\wg@tmpb\multiply\wg@tmpf by \hex@bevel@frac
50 \wg@tmpg=\wg@tmpc\multiply\wg@tmpg by \hex@bevel@frac
51 \divide\wg@tmpe100
52 \divide\wg@tmpf100
53 \divide\wg@tmpg100
54 % Start
55 \pgfpathmoveto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
56 % Left
57 \pgfpathlineto{\pgfqpoint{-\wg@tmpa}{\wg@tmpb}}%
58 % Left-down
59 \pgfpathlineto{\pgfqpoint{\wg@tmpc}{\wg@tmpd}}%
60 % Right down
61 \wg@tmpa=-\wg@tmpa%
62 \wg@tmpb=-\wg@tmpb%
63 \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
64 % Up, in
65 \advance\wg@tmpa\wg@tmpe%
66 \advance\wg@tmpb\wg@tmpf%
67 \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
68 % Left-down, in
69 \advance\wg@tmpc-\wg@tmpg
70 \pgfpathlineto{\pgfqpoint{\wg@tmpc}{\wg@tmpd}}%
71 % Left, down in
72 \advance\wg@tmpb-\wg@tmpf\wg@tmpb-\wg@tmpb%
73 \advance\wg@tmpb-\wg@tmpf
74 \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
75 % Start, down in
76 \advance\wg@tmpa-\wg@tmpe\wg@tmpa-\wg@tmpa%
77 \advance\wg@tmpa-\wg@tmpe
78 \pgfpathlineto{\pgfqpoint{\wg@tmpa}{\wg@tmpb}}%
79 % %
80 \pgfclosepath%
81 \pgfusepath{fill}
82 \endscope}%
83 % \end{macrocode}
84 %
85 %
86 %
87 % \begin{macrocode}
88 \hex@dbg{5}{Base vertex: \hex@xx,\hex@yy}
89 \hex@dbg{5}{Base edges: \hex@e@xx,\hex@e@yy}
90 \pgfdeclareshape{hex/hex}{%
91 \saveddimen\radius{\pgf@x=\hex@radius}
92 \savedanchor{\east}{ \pgfqpoint{ \hex@radius}{0cm}}
93 \savedanchor{\west}{ \pgfqpoint{-\hex@radius}{0cm}}
94 \savedanchor{\northeast}{ \pgfqpoint{ \hex@dx}{ \hex@dy}}
95 \savedanchor{\northwest}{ \pgfqpoint{-\hex@dx}{ \hex@dy}}
96 \savedanchor{\southwest}{ \pgfqpoint{-\hex@dx}{-\hex@dy}}
97 \savedanchor{\southeast}{ \pgfqpoint{ \hex@dx}{-\hex@dy}}
98 \savedanchor{\northedge}{ \pgfqpoint{ 0cm}{ \hex@dy}}
99 \savedanchor{\southedge}{ \pgfqpoint{ 0cm}{-\hex@dy}}
100 \savedanchor{\northeastedge}{\pgfqpoint{ \hex@e@dx}{ \hex@e@dy}}
101 \savedanchor{\northwestedge}{\pgfqpoint{-\hex@e@dx}{ \hex@e@dy}}
102 \savedanchor{\southwestedge}{\pgfqpoint{-\hex@e@dx}{-\hex@e@dy}}
103 \savedanchor{\southeastedge}{\pgfqpoint{ \hex@e@dx}{-\hex@e@dy}}
104 \savedmacro\init{%
105 \def\hexpath{%
106 \pgfpathmoveto{\east}%
107 \pgfpathlineto{\northeast}%
108 \pgfpathlineto{\northwest}%
109 \pgfpathlineto{\west}%
110 \pgfpathlineto{\southwest}%
111 \pgfpathlineto{\southeast}%
112 \pgfpathclose}
113 }
114 % \end{macrocode}
115 %
116 % These are the actual user callable anchors. We make anchors for
117 % each vertex and mid points on each edge.
118 %
119 % \begin{macrocode}
120 %%
121 \anchor{center}{ \pgfpointorigin}
122 \anchor{east}{ \east}
123 \anchor{west}{ \west}
124 \anchor{north east}{ \northeast}
125 \anchor{north west}{ \northwest}
126 \anchor{south west}{ \southwest}
127 \anchor{south east}{ \southeast}
128 \anchor{north edge}{ \northedge}
129 \anchor{south edge}{ \southedge}
130 \anchor{north east edge}{\northeastedge}
131 \anchor{north west edge}{\northwestedge}
132 \anchor{south west edge}{\southwestedge}
133 \anchor{south east edge}{\southeastedge}
134 % \end{macrocode}
135 %
136 % Next we make some short hand aliases for each of these anchors.
137 %
138 % \begin{macrocode}
139 \anchor{E}{ \east}
140 \anchor{W}{ \west}
141 \anchor{NE}{ \northeast}
142 \anchor{NW}{ \northwest}
143 \anchor{SW}{ \southwest}
144 \anchor{SE}{ \southeast}
145 \anchor{N edge}{ \northedge}
146 \anchor{S edge}{ \southedge}
147 \anchor{NE edge}{\northeastedge}
148 \anchor{NW edge}{\northwestedge}
149 \anchor{SW edge}{\southwestedge}
150 \anchor{SE edge}{\southeastedge}
151 % \end{macrocode}
152 %
153 % The next part is commented out because its not obvious we'll use
154 % these.
155 %
156 % \begin{macrocode}
157 %%
158 \savedanchor{\chitnorth}{ \pgfqpoint{ 0cm}{ 0.6cm}}
159 \savedanchor{\chitsouth}{ \pgfqpoint{ 0cm}{ -0.6cm}}
160 \savedanchor{\chiteast}{ \pgfqpoint{ 0.6cm}{ 0cm}}
161 \savedanchor{\chitwest}{ \pgfqpoint{-0.6cm}{ 0cm}}
162 \savedanchor{\chitnortheast}{\pgfqpoint{ 0.6cm}{ 0.6cm}}
163 \savedanchor{\chitnorthwest}{\pgfqpoint{-0.6cm}{ 0.6cm}}
164 \savedanchor{\chitsouthwest}{\pgfqpoint{-0.6cm}{-0.6cm}}
165 \savedanchor{\chitsoutheast}{\pgfqpoint{ 0.6cm}{-0.6cm}}
166 %
167 \anchor{chit north}{\chitnorth}
168 \anchor{chit south}{\chitsouth}
169 \anchor{chit east}{\chiteast}
170 \anchor{chit west}{\chitwest}
171 \anchor{chit north east}{\chitnortheast}
172 \anchor{chit north west}{\chitnorthwest}
173 \anchor{chit south west}{\chitsouthwest}
174 \anchor{chit south east}{\chitsoutheast}
175 %
176 \anchor{chit N}{\chitnorth}
177 \anchor{chit S}{\chitsouth}
178 \anchor{chit E}{\chiteast}
179 \anchor{chit W}{\chitwest}
180 \anchor{chit NE}{\chitnortheast}
181 \anchor{chit NW}{\chitnorthwest}
182 \anchor{chit SW}{\chitsouthwest}
183 \anchor{chit SE}{\chitsoutheast}
184 %
185 % \end{macrocode}
186 %
187 % The background path. This path may be drawn when the node is
188 % drawn. However, we will do most of the work in the
189 % \cs{behindbackgroundpath} which gets drawn \emph{after} this path.
190 %
191 % \begin{macrocode}
192 \backgroundpath{\init\hexpath}
193 % \end{macrocode}
194 %
195 % The \emph{behind} background path, where we do most of the work.
196 %
197 % \begin{macrocode}
198 \behindforegroundpath{%
199 \hex@dbg{2}{Hex behind foreground path:
200 ^^JTerrain: `\meaning\hex@terrain'
201 ^^JRidges: `\meaning\hex@ridges'
202 ^^JTown: `\meaning\hex@town'
203 ^^JExtra clipped: `\meaning\hex@extra@clip'
204 ^^JLabel: `\meaning\hex@label'
205 ^^JExtra: `\meaning\hex@extra'
206 ^^JLast node name:`\meaning\tikzlastnode'
207 ^^JHex row: `\meaning\hex@row'
208 ^^JHex col: `\meaning\hex@col'
209 }%
210 \init%
211 % \end{macrocode}
212 %
213 % We start a scope and clip to the hex path first.
214 %
215 % \begin{macrocode}
216 \scope%
217 \hexpath%
218 \pgfusepath{clip}%
219 % \end{macrocode}
220 %
221 % Anything inside this scope is clipped to the hex path. The next
222 % step is to see if we have a specified terrain for the hex.
223 %
224 % \begin{macrocode}
225 \@ifundefined{hex@terrain}{\let\hex@terrain\empty}{}%
226 \ifx\hex@terrain\empty\else\hex@do@terrain\fi%
227 % \end{macrocode}
228 %
229 % This concludes the processing of the terrain of the hex. Next, we
230 % must see if the user specified ridges.
231 %
232 % \begin{macrocode}
233 \@ifundefined{hex@ridges}{\let\hex@ridges\empty}{}%
234 \ifx\hex@ridges\empty\else\hex@do@ridges\fi%
235 % \end{macrocode}
236 %
237 % This concludes the processing of the ridges of the hex. Next, we
238 % should process any extra (clipped) stuff specified. The user may
239 % pass options to each \spec{pic}ture by preceding it with
240 % \oarg{options}.
241 %
242 % \begin{macrocode}
243 \@ifundefined{hex@extra@clip}{\let\hex@extra@clip\empty}{}
244 \ifx\hex@extra@clip\empty\else%
245 \hex@dbg{5}{Extra clipped: `\meaning\hex@extra'}
246 \pgfpointorigin\wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
247 \wg@pic@all{\hex@extra@clip}{}{\the\wg@tmpa,\the\wg@tmpb}{}%
248 \fi%
249 % \end{macrocode}
250 %
251 % This concludes the extra stuff put in the hex. Next, we should
252 % place the label is specified. Note, we may know the hex row and
253 % column at this point, stored in \cs{hex@row} and \cs{hex@column},
254 % respectively. We may want to name the generated node from these if
255 % the user specified that option (perhaps use \cs{pgfnoderename} or
256 % similar).
257 %
258 % \begin{macrocode}
259 \@ifundefined{hex@label}{\let\hex@label\empty}{}
260 \ifx\hex@label\empty\else\hex@do@label\fi%
261 % \end{macrocode}
262 %
263 % \begin{macrocode}
264 \@ifundefined{hex@bevel}{\let\hex@bevel\empty}{}
265 \ifx\hex@bevel\empty\else%
266 \northeast
267 \wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
268 \west
269 \wg@tmpc=\pgf@x\wg@tmpd=\pgf@y%
270 \ifcase\hex@bevel\relax
271 \or%1
272 \or\wg@tmpa=-\wg@tmpa\wg@tmpc=-\wg@tmpc%2
273 \or\wg@tmpb=-\wg@tmpb\wg@tmpd=-\wg@tmpd%3
274 \or% 4
275 \wg@tmpa=-\wg@tmpa\wg@tmpc=-\wg@tmpc%
276 \wg@tmpb=-\wg@tmpb\wg@tmpd=-\wg@tmpd%
277 \fi
278 \hex@bevel@path{chit/bevel highlight}
279 \northeast
280 \wg@tmpa=-\pgf@x\wg@tmpb=-\pgf@y%
281 \west
282 \wg@tmpc=-\pgf@x\wg@tmpd=-\pgf@y%
283 \ifcase\hex@bevel\relax
284 \or%1
285 \or\wg@tmpa=-\wg@tmpa\wg@tmpc=-\wg@tmpc%2
286 \or\wg@tmpb=-\wg@tmpb\wg@tmpd=-\wg@tmpd%3
287 \or% 4
288 \wg@tmpa=-\wg@tmpa\wg@tmpc=-\wg@tmpc%
289 \wg@tmpb=-\wg@tmpb\wg@tmpd=-\wg@tmpd%
290 \fi
291 \hex@bevel@path{chit/bevel shadow}
292 \fi
293 % \end{macrocode}
294 % \begin{macrocode}
295 \endscope%
296 % \end{macrocode}
297 %
298 % This concludes the label processing, and stuff that should be
299 % clipped to the hex shape. If the user specified a town, we can now
300 % make that.
301 %
302 % \begin{macrocode}
303 \@ifundefined{hex@town}{\let\hex@town\empty}{}
304 \@ifundefined{hex@c@pic}{\let\hex@c@pic\empty}{}
305 \ifx\hex@town\empty\else\hex@do@town\fi%
306 % \end{macrocode}
307 %
308 % We can now add extra (non-clipped) stuff. We assume that extra
309 % stuff is \spec{pic}tures. The user may pass options to each
310 % \spec{pic}ture by preceding it with \oarg{options}.
311 %
312 % \begin{macrocode}
313 \@ifundefined{hex@extra}{\let\hex@extra\empty}{}
314 \ifx\hex@extra\empty\else%
315 \hex@dbg{5}{Extra: `\meaning\hex@extra'}
316 \pgfpointorigin\wg@tmpa=\pgf@x\wg@tmpb=\pgf@y%
317 \wg@pic@all{\hex@extra}{}{\the\wg@tmpa,\the\wg@tmpb}{}%
318 \fi%
319 }
320 }
321 % \end{macrocode}
322 %
323 % \begin{HexKey*}{
324 % /hex/terrain,
325 % /hex/town,
326 % /hex/label,
327 % /hex/ridges,
328 % /hex/extra,
329 % /hex/extra clipped}
330 %
331 % Next, we set up the name space for hex keys. This is the top level
332 % name space for hexes. Sub keys \spec{terrain}, \spec{ridges},
333 % \spec{town}, \spec{extra}, \spec{label}, and \spec{extra clipped},
334 % store their arguments in macros and we expand these later on. This
335 % allows us to scope some of the keys given to those specific parts.
336 %
337 % Define keys for hexagon options. These are
338 %
339 % \begin{tabular}{|l|p{.7\linewidth}|}
340 % \hline
341 % \rowcolor{headbg}
342 % {\color{headfg}\textbf{Name}}
343 % & {\color{headfg}\textbf{Description}}\\
344 % \hline
345 % \spec{terrain} & Terrain\\
346 % \spec{label} & Label on hex\\
347 % \spec{town} & Town in hex. Optionally with a name\\
348 % \spec{ridges} & Ridge markings on hex\\
349 % \spec{extra} & More \\
350 % \spec{extra clipped} & More clipped to hex\\
351 % \hline
352 % \end{tabular}
353 %
354 % \begin{macrocode}
355 \tikzset{%
356 /hex/.search also={/tikz},%
357 /hex/.cd,%
358 terrain/.store in=\hex@terrain,%
359 ridges/.store in=\hex@ridges,%
360 town/.store in=\hex@town,%
361 extra/.store in=\hex@extra,%
362 label/.store in=\hex@label,%
363 extra clipped/.store in=\hex@extra@clip%
364 }
365 % \end{macrocode}
366 % \end{HexKey*}
367 %
368 % \begin{TikzKey}{hex}
369 % The next key is the real work horse of the show. Specifying the
370 % \spec{hex} key to a node effectively creates a hex for us. Now,
371 % there are some things we cannot do outright in the node shape
372 % code. For example, we cannot set the name of the node created
373 % from the shape code. Therefore, the use of \cs{hex} is often the
374 % right choice.
375 %
376 % \begin{macrocode}
377 \tikzset{%
378 hex/hex/.style={
379 transform shape,
380 anchor=center,
381 draw=pgfstrokecolor,
382 fill=none,
383 thick,
384 solid},
385 hex/.code={%
386 \hex@dbg{1}{=== Hex with options: `#1'}%
387 \pgfkeys{/tikz/transform shape,/tikz/shape=hex/hex}
388 \pgfkeys{/hex/.cd,/tikz/hex/hex,/tikz/every hex/.try,#1}}}
389 % \end{macrocode}
390 % \end{TikzKey}
391 %
392 % The first thing is to set the default graphics options. The key
393 % \spec{every hex} can be set to hex options to be used for all hexes.
394 % For example, if one want to label all hexes with an auto-generated
395 % label, one can do
396 %
397 % \begin{Syntax}
398 % \cs{tikzset}\{every hex/.style=\{label=\{auto=numbered\}\}\}
399 % \end{Syntax}
400 %
401 % This, coupled with the \spec{hex/label is name} option allows us to
402 % set up the board with really minimal effort. We can then use the
403 % board coordinates when placing units, and other things.
404 %
405 %
406 % Now we have set up these tools we can go on and define the user
407 % facing macro.
408 %
409 % \begin{Macro}{\hex,\hex@,\hex@@}
410 %
411 % This will add a hex to the output graphics. Note, the macro
412 % need not be followed by a semi-colon (\spec{;}).
413 %
414 % First argument is optional options.
415 %
416 % \begin{macrocode}
417 \def\hex{%
418 \@ifnextchar[{\hex@}{\hex@[]}%]
419 }
420 % \end{macrocode}
421 %
422 % Second optional argument is the coordinates. These should be given
423 % in the hex coordinate system.
424 %
425 % \begin{macrocode}
426 \def\hex@[#1]{%
427 \@ifnextchar({\hex@@{#1}}{%
428 \hex@@{#1}(c=0,r=0)}%)
429 }
430 % \end{macrocode}
431 %
432 % Third argument is the name to be used.
433 %
434 % \begin{macrocode}
435 \def\hex@@#1(#2){%
436 \@ifnextchar({\hex@@@{#1}{#2}}{\hex@@@{#1}{#2}()}%)
437 }
438 % \end{macrocode}
439 %
440 % Now for the real work-horse. First thing is to reset keys and parse
441 % them out from the arguments.
442 %
443 % \begin{macrocode}
444 % Third argument is name
445 \def\hex@@@#1#2(#3){%
446 \node[hex={#1}] (tmp) at (hex cs:#2) {};%
447 \hex@dbg{8}{=== Label text: `\meaning\hex@l@text'}
448 \ifx|#3|\relax%
449 \@ifundefined{hex@l@text}{%
450 \hex@dbg{8}{=== Label text of hex (#2) not defined}%
451 \let\hex@l@text\empty%
452 }{}
453 \ifhex@label@is@name%
454 \hex@dbg{5}{=== Use label text of hex (#2) as name}%
455 \ifx\hex@l@text\@empty%
456 \hex@dbg{8}{=== Argh! Label text is empty! `\meaning\hex@l@text'}
457 \else%
458 \hex@dbg{3}{=== Renaming hex to label text `\hex@l@text'}
459 \pgfnoderename{\hex@l@text}{tmp}%
460 \fi%
461 \fi%
462 \else%
463 \hex@dbg{3}{=== Renaming hex to user defined name `#3'}%
464 \pgfnoderename{#3}{tmp}%
465 \fi%
466 \@ifnextchar;{\@gobble}{}%
467 }
468 % \end{macrocode}
469 % \end{Macro}
470 % \iffalse
471 %</hex>
472 % --------------------------------------------------------------------
473 % \fi