% Define node group background
% The first 4 arguments are nodes that define the group borders in (left,
% top, right, bottom) order, the last argument is a text label
\usetikzlibrary{shapes,arrows,positioning,patterns}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}  % Layer order
\newcommand{\group}[5]{
    \begin{pgfonlayer}{background}
        % Left top corner
        \path (#1.west |- #2.north)+(-0.7, 0.7)
            node (lefttop) {};
        % Right bottom corner
        \path (#3.east |- #4.south)+(0.7, -0.4)
            node (rightbottom) {};
        % Draw rectangle
        \path[draw, rounded corners, dashed] (lefttop) rectangle (rightbottom);
        % Text label
        \path (rightbottom.west |- lefttop.south)+(-1.2, -0.3) node {\emph{#5}};
    \end{pgfonlayer}
}
\tikzstyle{block} = [
    rectangle,
    draw=black,
    fill=white,
    thick,
    minimum height=2em,
    text centered,
    rounded corners,
    text width=6em
]
\tikzstyle{lineto} = [
    ->,
    thick,
    shorten <= 2pt,
    shorten >= 2pt
]
\tikzstyle{linefrom} = [
    <-,
    thick,
    shorten <= 2pt,
    shorten >= 2pt
]
\newcommand{\architecture}[1]{
    \begin{tikzpicture}[node distance=6em, scale=0.7, transform shape]
        \node[block] (driver) {Driver};
        #1
    \end{tikzpicture}
}

\def\basicdiagram{
    \begin{figure}[h!]
        \center
        \begin{tikzpicture}[node distance=6em, scale=0.7, transform shape]
            \node[block] (driver) {Device driver};
            \node[block, below of=driver, dashed] (architecture) {Architecture}
                edge[linefrom] node[right] {device-specific messages} (driver);
            \node[block, below of=architecture] {Application}
                edge[linefrom, dotted] node[right] {gesture} (architecture);
        \end{tikzpicture}
    \end{figure}
}

\def\eventdriverdiagram{
    \begin{figure}[h!]
        \center
        \architecture{
            \node[block, below of=driver] (eventdriver) {Event driver}
                edge[linefrom] node[right, near end] {device-specific messages} (driver);
            \node[block, below of=eventdriver, dashed] (analysis) {Event analysis}
                edge[linefrom] node[right] {(low-level) event} (eventdriver);
            \node[block, below of=analysis] {Application}
                edge[linefrom, dotted] node[right, near start] {(high-level) gesture} (analysis);

            \node[right of=eventdriver, xshift=2em] (dummy) {};
            \group{eventdriver}{eventdriver}{dummy}{analysis}{Architecture}
        }
    \end{figure}
}

\def\lefthand{\includegraphics[width=50pt]{data/hand.png}}
\def\righthand{\reflectbox{\includegraphics[width=50pt, angle=-45]{data/hand.png}}}

\def\areaproblem{
    \begin{figure}[H]
        \center
        \begin{tikzpicture}[transform shape, scale=0.7]
            \draw node[draw, black, minimum width=190, minimum height=140] at (0,0) {};
            \draw node[fill=gray!50, draw=black!70, minimum height=40, minimum width=40] at (-1,-1) {\lefthand};
            \draw node[] at (1.2,1) {\righthand};
            \draw node[draw=black!80, diamond, minimum height=70, minimum width=70] at (1.2,1) {};
        \end{tikzpicture}
    \end{figure}
}

\def\areadiagram{
    \begin{figure}[H]
        \center
        \architecture{
            \node[block, below of=driver] (eventdriver) {Event driver}
                edge[linefrom] node[right, near end] {device-specific messages} (driver);
            \node[block, below of=eventdriver] (area) {Event areas}
                edge[linefrom] node[right] {event} (eventdriver);
            \node[block, right of=area, xshift=7em, dashed] (tracker) {Gesture detection}
                edge[linefrom, bend right=10] node[above] {event} (area)
                edge[lineto, bend left=10, dotted] node[below] {gesture} (area);
            \node[block, below of=area] {Application}
                edge[linefrom, dotted] node[right, near start] {gesture} (area);

            \group{eventdriver}{eventdriver}{tracker}{area}{Architecture}
        }
    \end{figure}
}

\def\propagationproblem{
    \begin{figure}[H]
        \center
        \begin{tikzpicture}[node distance=5.5em, transform shape, scale=0.6]
            \draw node[draw=black, minimum width=190, minimum height=140] at (0,0) {};
            \draw node[draw=black!60, minimum height=100, minimum width=100, pattern=north east lines] at (-0.1,-0.1) {};
            \draw node[fill=gray!60, draw=black!70, minimum height=40, minimum width=40] at (0.3,0.4) {};
            \fill (0.4, 0.6) circle (0.15);
        \end{tikzpicture}
    \end{figure}
}

\def\nopropagationdiagram{
    \begin{figure}[H]
        \center
        \begin{tikzpicture}[node distance=5.5em, transform shape, scale=0.7]
            \draw node[block, yshift=-10em, xshift=-3em] (driver) {Event driver};
            \draw node[block, below of=driver] (gray) {Outer event area}
                edge[linefrom] node[left] {1} (driver);
            \draw node[block, below of=gray] (white) {Inner event area}
                edge[linefrom] node[left] {2} (gray);
            \draw node[block, right of=white, xshift=4em] {\emph{drag} detection}
                edge[linefrom, bend right=15] node[above] {3} (white)
                edge[lineto, dotted, bend left=15] node[below] {4} (white);
            \draw node[block, right of=gray, xshift=4em] {\emph{drag} detection};
            \draw node[block, below of=white] {Application}
                edge[linefrom, dotted] node[left] {5} (white);
        \end{tikzpicture}
    \end{figure}
}

\def\nodelegationdiagram{
    \begin{figure}[H]
        \center
        \subfigure{
            \begin{tikzpicture}[node distance=5.5em, transform shape, scale=0.6]
                \draw node[draw=black, minimum width=190, minimum height=140] at (0,0) {};
                \draw node[draw=black!60, minimum height=100, minimum width=100, pattern=north east lines] at (-0.1,-0.1) {};
                \draw node[fill=gray!60, draw=black!70, minimum height=40, minimum width=40] at (0.3,0.4) {};
                \fill (-0.9, -0.7) circle (0.15);
            \end{tikzpicture}
        }
        \subfigure{
            \begin{tikzpicture}[node distance=5.5em, transform shape, scale=0.7]
                \draw node[block, yshift=-10em, xshift=-3em] (driver) {Event driver};
                \draw node[block, below of=driver] (gray) {Outer event area}
                    edge[linefrom] node[left] {1} (driver);
                \draw node[block, below of=gray] (white) {Inner event area};
                \draw node[block, right of=white, xshift=4em] {\emph{drag} detection};
                \draw node[block, right of=gray, xshift=4em] {\emph{drag}
                detection} edge[linefrom, bend right=15] node[above, black] {2} (gray)
                    edge[lineto, dotted, bend left=15] node[below, black] {3} (gray);
                \draw node[block, below of=white] {Application}
                    edge[linefrom, dotted, bend left=65] node[left, black] {4} (gray);
            \end{tikzpicture}
        }
    \end{figure}
}

\def\fulldiagram{
    \begin{figure}[h!]
        \center
        \architecture{
            \node[block, below of=driver] (eventdriver) {Event driver}
                edge[linefrom] node[right, near end] {device-specific messages} (driver);
            \node[block, below of=eventdriver] (area) {Event areas}
                edge[linefrom] node[right] {event} (eventdriver);
            \node[block, right of=area, xshift=7em] (tracker) {Gesture trackers}
                edge[linefrom, bend right=10] node[above] {event} (area)
                edge[lineto, bend left=10, dotted] node[below] {gesture} (area);
            \node[block, below of=area] {Application}
                edge[linefrom, dotted] node[right, near start] {gesture} (area);

            \group{eventdriver}{eventdriver}{tracker}{area}{Architecture}
        }
    \end{figure}
}

\def\daemondiagram{
    \begin{figure}[H]
        \centering
        \begin{tikzpicture}[node distance=4em, transform shape, scale=0.7]
            \node[block] (daemon) {Daemon};

            \node[block, above of=daemon] (driver) {Device driver}
                edge[lineto] (daemon);
            \node[block, xshift=-4em, left of=driver] {Device driver}
                edge[lineto] (daemon);
            \node[block, xshift=4em, right of=driver] {Device driver}
                edge[lineto] (daemon);

            \node[block, below of=daemon] (app) {Application}
                edge[linefrom, dotted] (daemon);
            \node[block, xshift=-4em, left of=app] {Application}
                edge[linefrom, dotted] (daemon);
            \node[block, xshift=4em, right of=app] {Application}
                edge[linefrom, dotted] (daemon);

            \draw[dashed] (app.north)+(-4, 0.35) -- node[right=4, yshift=1] {Network protocol} ++(4, 0.35);
        \end{tikzpicture}
    \end{figure}
}

\def\demodiagram{
    \begin{figure}[H]
        \center
        \begin{tikzpicture}[node distance=4em, transform shape, scale=0.6]
            \node (dummy) {};
            \node[block, below of=dummy] (screen) {Screen area}
                edge[linefrom] (dummy);

            \node[block, below of=screen, xshift=7em] (overlay) {Overlay area}
                edge[lineto, <->] (screen);
            \node[below of=overlay, xshift=2em] {Hand tracker}
                edge[lineto, bend left=15, dotted] (overlay)
                edge[linefrom, bend right=15] (overlay);

            \node[block, below of=screen, xshift=-7em] (window) {Appliation window area}
                edge[lineto, <->] (screen);
            \node[above of=window, xshift=-4em] {Transformation+tap trackers}
                edge[lineto, bend left=15, dotted] (window)
                edge[linefrom, bend right=15] (window);

            \node[block, below of=window, xshift=-7em] (rect) {Rectangle area}
                edge[lineto, <->] (window);
            \node[below of=rect, yshift=-0.5em] {Transformation+tap trackers}
                edge[lineto, bend left=15, dotted] (rect)
                edge[linefrom, bend right=15] (rect);

            \node[block, below of=window, xshift=7em] (triangle) {Triangle area}
                edge[lineto, <->] (window);
            \node[below of=triangle, yshift=-0.5em] {Transformation+tap trackers}
                edge[lineto, bend left=15, dotted] (triangle)
                edge[linefrom, bend right=15] (triangle);
        \end{tikzpicture}
    \end{figure}
}
