So, I'm having a go at defining a tikz/pgf shape. Basically these nodes will not contain any text, and it is just a method of not copying a lot of code to draw a few of these boxes.
I have a few questions
In a shape is there an official method of accessing the already defined anchors? I made a macro for it (
\pgfutil@useanchor
), but thought there might be a better method.The shape it self, is there a better method for drawing these two extra areas? (I'm probably not getting the margins right, just trying to support a thick outer line width)
The keys, especially
dbox strib width
, why doesn't a change away from the initial value, change the extra anchors on the node? See the lower drawing It does change the\beforebackgroundpath
, clearly doing something wrong here.
Any ideas?
\documentclass[a4paper]{memoir}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{shapes}
\makeatletter
% access to anchor coordinates, got to be a better way
\def\pgfutil@useanchor#1#2{\csname pgf@anchor@#1@#2\endcsname}
\pgfkeys{/pgf/.cd,
dbox strib width/.initial=5mm,
% dbox strib width/.code={%
% \def\pgf@lib@temp{#1}%
% \pgfkeyslet{/pgf/dbox strib width}{\pgf@lib@temp}%
% },
dbox strib color/.initial=blue,
}
\pgfdeclareshape{dbox}
{
% this is just a rectangle with extra colored areas
\inheritsavedanchors[from=rectangle]
\inheritanchorborder[from=rectangle]
\inheritanchor[from=rectangle]{north}
\inheritanchor[from=rectangle]{north west}
\inheritanchor[from=rectangle]{north east}
\inheritanchor[from=rectangle]{center}
\inheritanchor[from=rectangle]{west}
\inheritanchor[from=rectangle]{east}
\inheritanchor[from=rectangle]{mid}
\inheritanchor[from=rectangle]{mid west}
\inheritanchor[from=rectangle]{mid east}
\inheritanchor[from=rectangle]{base}
\inheritanchor[from=rectangle]{base west}
\inheritanchor[from=rectangle]{base east}
\inheritanchor[from=rectangle]{south}
\inheritanchor[from=rectangle]{south west}
\inheritanchor[from=rectangle]{south east}
\anchor{center left}{
\pgf@process{\pgfutil@useanchor{dbox}{west}}
\advance\pgf@x by \pgfkeysvalueof{/pgf/dbox strib width}
\advance\pgf@x by \pgflinewidth
}
\anchor{center left above}{
\pgf@process{\pgfutil@useanchor{dbox}{north west}}
\advance\pgf@x by \pgfkeysvalueof{/pgf/dbox strib width}
\advance\pgf@x by \pgflinewidth
}
\anchor{center left below}{
\pgf@process{\pgfutil@useanchor{dbox}{south west}}
\advance\pgf@x by \pgfkeysvalueof{/pgf/dbox strib width}
\advance\pgf@x by \pgflinewidth
}
\anchor{center right}{
\pgf@process{\pgfutil@useanchor{dbox}{east}}
\advance\pgf@x by -\pgfkeysvalueof{/pgf/dbox strib width}
\advance\pgf@x by -\pgflinewidth
}
\anchor{center right above}{
\pgf@process{\pgfutil@useanchor{dbox}{north east}}
\advance\pgf@x by -\pgfkeysvalueof{/pgf/dbox strib width}
\advance\pgf@x by -\pgflinewidth
}
\anchor{center right below}{
\pgf@process{\pgfutil@useanchor{dbox}{south east}}
\advance\pgf@x by -\pgfkeysvalueof{/pgf/dbox strib width}
\advance\pgf@x by -\pgflinewidth
}
\beforebackgroundpath{
\pgfsetfillcolor{\pgfkeysvalueof{/pgf/dbox strib color}}
\pgfpathrectanglecorners{
\pgfpointadd{\southwest}{
\pgfpoint{\pgflinewidth}{\pgflinewidth}
}
}{
\pgfpointadd{\pgf@process{\pgfutil@useanchor{dbox}{north west}}}%
{\pgfpoint{\pgflinewidth+\pgfkeysvalueof{/pgf/dbox strib width}}{-\pgflinewidth}}
}
\pgfusepath{fill}
\pgfpathrectanglecorners{
\pgfpointadd{\northeast}{
\pgfpoint{-\pgflinewidth}{-\pgflinewidth}
}
}{
\pgfpointadd{\pgf@process{\pgfutil@useanchor{dbox}{south east}}}%
{\pgfpoint{-\pgflinewidth-\pgfkeysvalueof{/pgf/dbox strib width}}{\pgflinewidth}}
}
\pgfusepath{fill}
}
%
% Background path
%
\inheritbackgroundpath[from=rectangle]
}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{scope}[
ms/.style = {minimum height=17mm,minimum
width=6cm,draw,fill=cyan,shape=dbox,
dbox strib color=red!50!white,
},
]
\node[ms,
%dbox strib width=1cm,
] (BBb) at (0,2) {};
\node[ms,
dbox strib width=1cm,
] (BBa) at (0,0) {};
\end{scope}
\fill[green] (BBa.center left) circle (1mm);
\fill[green] (BBa.center left above) circle (1mm);
\fill[green] (BBa.center left below) circle (1mm);
\fill[green] (BBa.center right) circle (1mm);
\fill[green] (BBa.center right above) circle (1mm);
\fill[green] (BBa.center right below) circle (1mm);
\end{tikzpicture}
\end{document}
Here is what it looks like right now
\savedanchor
s. See the manual for the nuance.\anchor
s are only computed during runtime.\savedanchor
and then set the anchor to point to that macro? How annoying.\anchor{xxx}{
by a simple\savedanchor\xxx{
, if I do I get an error in\northeast
(which is interited from rectangle). Weird. Presumably because of the use of\pgf@process