%% timing-diagrams.sty
%% Copyright 2013 Matthieu Moy <Matthieu.Moy@imag.fr>
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is M. Matthieu Moy.
%
% This work consists of the files timing-diagrams.sty and the example file
% diagrams-examples.tex.
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{timing-diagrams}[2013/08/21 Draw timing diagrams in TikZ]

\RequirePackage{tikz}
\RequirePackage{ifthen}

\usetikzlibrary{shadows}
\usetikzlibrary{shapes.callouts}
\usetikzlibrary{decorations.pathreplacing}
% Work around a bug in some versions of PGF
% http://tex.stackexchange.com/questions/31921/callout-and-beamer
\usetikzlibrary{decorations.text}

% Style (override to customize ...)
\tikzstyle{tboxshadow}=[
    drop shadow={shadow xshift=1.5pt,shadow yshift=-1.5pt}
]
\tikzstyle{tbox}=[
    top color=green!7,
    bottom color=green!80!black!25,
    fill=green!80!black!25,
    shading=axis,
    shading angle=30,
    draw=black,
    drop shadow={shadow xshift=1.5pt,shadow yshift=-1.5pt}
]
\tikzstyle{tcallout}=[
top color=yellow!7,bottom color=yellow!40!orange!80!black!25,shading=axis,
% Cause display bug :-(
% shading angle=45,
ellipse callout, draw, drop shadow={shadow xshift=1pt,shadow yshift=-1pt}]

\tikzstyle{tarrow}=[->,line width=.05cm,draw=red!90!blue!60!black]
\newcommand{\boxheight}{.4}

\newcommand{\tsetcurrent}[2]{
  \path #2 coordinate (current#1);
  \path #2 ++(0,\boxheight)  coordinate (current#1U);
  \path #2 ++(0,-\boxheight) coordinate (current#1L);
}

\newcommand{\tsetcurrentabs}[2]{
  % correct absissa, wrong ordinate
  \coordinate (next#1) at (#2, 0);
  \tsetcurrent{#1}{(start#1 -| next#1)};
}

\newcommand{\tremember}[2]{
  \coordinate (remember#2_#1) at (current#1);
}
\newcommand{\trecall}[2]{
  \tsetcurrent{#1}{(remember#2_#1)};
}

\newcommand{\tline}[2]{
  \coordinate (start#1) at (0,#2);
  \tsetcurrent{#1}{(start#1)};
}

\newcommand{\ttimeline}[2]{
  \path (current#1) ++(#2, 0) coordinate (tend#1);
  \draw[->,thick] (current#1)++(-.1,0) -- (tend#1);
}

\newcommand{\tcaption}[2]{
  \draw (start#1) ++(-.1,0) node[anchor=east] {#2};
}

\newcommand{\tadvance}[3]{
  \path (current#1) +(#2,0) coordinate (next#1);
  #3
  \tsetcurrent{#1}{(next#1)};
}

\newcommand{\tbox}[3]{
  \tadvance{#1}{#2}{
    \path (current#1) +(0,\boxheight) coordinate (topleft#1);
    \path (next#1)   +(0,-\boxheight) coordinate (bottomright#1);
    \draw[tbox] (topleft#1) rectangle (bottomright#1);
    \draw (barycentric cs:current#1=.5,next#1=.5) node[anchor=center] {#3};
  }
}

\newcommand{\tsmallbox}[3]{
  \tskip{#1}{.05};
  \tbox{#1}{#2-.1}{#3};
  \tskip{#1}{.05};
}

\newcommand{\tskiptext}[3]{
  \tadvance{#1}{#2}{
    \path (current#1) +(0,.2) coordinate (topleft#1);
    \path (next#1)   +(0,.2) coordinate (topright#1);
    \draw (topleft#1) edge[<->] node[auto] {#3} (topright#1);
  }
}

\newcommand{\tskiptextL}[3]{
  \tadvance{#1}{#2}{
    \path (current#1) +(0,-.2) coordinate (bottomleft#1);
    \path (next#1)   +(0,-.2) coordinate (bottomright#1);
    \draw (bottomleft#1) edge[<->] node[midway,below] {#3} (bottomright#1);
  }
}

\newcommand{\tskiptextCONF}[4]{
  \tadvance{#1}{#2}{
    \path (current#1) +(0,-.2) coordinate (bottomleft#1);
    \path (next#1)   +(0,-.2) coordinate (bottomright#1);
    \draw (bottomleft#1) edge[<->] node[#3] {#4} (bottomright#1);
  }
}

\newcommand{\tskiptextinbox}[3]{
  \tadvance{#1}{#2}{
    \path (current#1) +(0,-.2) coordinate (topleft#1);
    \path (next#1)    +(0,-.2) coordinate (topright#1);
    \draw (topleft#1) edge[<->] node[auto] {#3} (topright#1);
  }
}

\newcommand{\tskiparrowU}[3]{
  \tadvance{#1}{#2}{
    \path (current#1) +(0,\boxheight) coordinate (topleft#1);
    \path (next#1)   +(0,\boxheight) coordinate (topright#1);
    \draw (topleft#1) edge[bend left,->] node[above] {#3} (topright#1);
  }
}

\newcommand{\tskiparrowL}[3]{
  \tadvance{#1}{#2}{
    \path (current#1) +(0,-\boxheight) coordinate (topleft#1);
    \path (next#1)   +(0,-\boxheight) coordinate (topright#1);
    \draw (topleft#1) edge[bend right,->] node[below] {#3} (topright#1);
  }
}

\newcommand{\tskip}[2]{
  \tadvance{#1}{#2}{}
}

\newcommand{\tevent}[1]{
  \draw[thick,->] (current#1) -- ++(0,.5);
}

\newcommand{\teventA}[2]{
  \draw[thick,->] (current#1) -- ++(#2+90:.5);
}

\newcommand{\ttick}[1]{
  \draw[tbox,thick] (current#1) ++(0,\boxheight) rectangle ++(.01,-2*\boxheight);
}

\newcommand{\tstrongtick}[1]{
  \draw[tboxshadow,fill=black] (current#1) +(-.04,\boxheight) rectangle +(.04,-\boxheight);
}

\newcommand{\tlighttick}[1]{
  \draw (current#1) ++(0,\boxheight) rectangle ++(.01,-2*\boxheight);
}

\newcommand{\tlonglighttick}[1]{
  \draw (current#1) ++(0,1.2*\boxheight) rectangle ++(.01,-2.4*\boxheight);
}

\newcommand{\tcatchup}[2]{
  \coordinate (next#1) at (current#2|-current#1);
  \tsetcurrent{#1}{(next#1)};
}

\newcommand{\ttextU}[2]{
  \draw (current#1) ++(0,\boxheight) node[anchor=south] {#2};
}

\newcommand{\ttextL}[2]{
  \draw (current#1) ++(0,-\boxheight) node[anchor=north] {#2};
}

\newcommand{\ttextM}[2]{
  \draw (current#1) node {#2};
}

\newcommand{\tcalloutU}[3][(0,1)]{
  \draw (current#2) ++#1
  node[anchor=south,callout absolute pointer={(current#2)++(0,\boxheight)}, tcallout] {#3};
}

\newcommand{\ttextarrowU}[3][++(0, .5)]{
  \path (current#2) ++(0,\boxheight) ++(0,.015) coordinate (tmpcurrentU);
  \draw[black] (current#2) ++(0,\boxheight) #1
  node[anchor=south,inner sep=.1](tmptext) {#3};
  \draw[->] (tmptext) -- (tmpcurrentU);
}

\newcommand{\tstartbrace}[1]{
  \coordinate (startbrace#1) at (current#1);
}

\newcommand{\tendbrace}[2]{
  \coordinate (endbrace#1) at (current#1);
  \draw[decoration={brace,amplitude=1.5mm},decorate] (endbrace#1) -- node[below]{#2} (startbrace#1);
}

\newcommand{\tcalloutL}[3][(0,-1)]{
  \draw (current#2) ++#1
  node[anchor=north,callout absolute pointer={(current#2)++(0,-\boxheight)}, tcallout] {#3};
}

\newcommand{\tarrowLU}[3]{
  \tarrowCoord{(current#1U)}{(current#2L)}{#3}
}
\newcommand{\tarrowUL}[3]{
  \tarrowCoord{(current#1L)}{(current#2U)}{#3}
}

\newcommand{\tarrowCoord}[3]{
  \coordinate (tmp1) at #1;
  \coordinate (tmp2) at #2;
  \coordinate (tmpmid1) at (barycentric cs:tmp1=.2,tmp2=.8);
  \coordinate (tmpmid2) at (barycentric cs:tmp1=.8,tmp2=.2);
  \coordinate (tmpmid) at (barycentric cs:tmp1=.5,tmp2=.5);  
  \draw[tarrow] (tmp1) .. controls (tmp1 |- tmpmid1) and (tmpmid2 -| tmp2) .. (tmp2);
  \ifthenelse{\equal{#3}{}}{}{
  \draw (tmpmid) node[shape=circle,fill=white,draw=black] {#3};
  }
}
