% -*- coding: utf-8; -*-
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
%                                                                                                  |
% (C)opyright A. Hilbig, mail@andrehilbig.de                                                       |
%             http://www.andrehilbig.de/Publications                                               |
%                                                                                                  | 
% diese Datei: ahilbig-comics.sty                                                                  |
%                                                                                                  |
% Das Dokument steht unter der Lizenz: Creative Commons by-nc-sa Version 4.0                       |
% http://creativecommons.org/licenses/by-nc-sa/4.0/deed.de                                         |
%                                                                                                  |
% Nach dieser Lizenz darf das Dokument beliebig kopiert und bearbeitet werden,                     |
% sofern das Folgeprodukt wiederum unter gleichen Lizenzbedingungen vertrieben                     |
% und auf die ursprünglichen Urheber verwiesen wird.                                               |
% Eine kommerzielle Nutzung ist ausdrücklich ausgeschlossen.                                       |
%                                                                                                  |
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
%                                                                                                  |
% HINWEISE/ERWEITERUNG                                                                             |
% Diese Pakete basieren auf den entsprechenden Paketen von Prof. Dr. Ludger Humbert                |
% (http://ddi.uni-wuppertal.de/ -- humbert@uni-wuppertal.de) und wurden zum Teil angepasst. Seine  |
% Pakete basieren wiederum auf Entwicklungen von Prof. Dr. Till Tantau                             |
% (http://www.tcs.uni-luebeck.de/de/mitarbeiter/tantau/). Beiden gilt mein Dank hierfür.           |
%                                                                                                  |
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -+
%                                                                                                  |

\ProvidesPackage{ahilbig-comics}

\@ifclassloaded{beamer}{}{\RequirePackage{beamerarticle}}

\RequirePackage{emerald}
\RequirePackage{tikz}

\usetikzlibrary{calc,matrix}

\pgfkeys{/comics/.cd,
  professor says/.code=\ddiprofessorsays{#1},
  professor thinks/.code=\ddiprofessorthinks{#1},
  student says/.code=\ddistudentsays{#1},
  student thinks/.code=\ddistudentthinks{#1},
  bingo/.code={\alt<article>{\ddibingo#1}{\def\ddibingonow{\ddibingo#1}}},
  done/.code=\ddicomicdone,
}

\def\ddicomicpicture[#1]{
  \begin{tikzpicture}[baseline={([yshift=1.2cm]people)},inner sep=0pt,outer sep=0pt]
    \coordinate (last) at (0,0);
    \coordinate (last professor) at (0,0);
    \coordinate (last student) at (0,0);
    \ddicomicspokendonefalse
    \def\ddibingonow{}
    \pgfkeys{/comics/.cd,#1,done}
  \end{tikzpicture}
}

\mode
<article>

\def\ddicomic[#1]{
  {%
    \setlength{\marginparsep}{.65cm}%
    \setlength{\marginparwidth}{4.1cm}%
    \marginpar{%
      \ddicomicpicture[#1]
    }%
  }\refstepcounter{framenumber}%
  \ignorespaces%
}

\mode
<presentation>

\def\ddicomic[#1]{
  \begin{frame}
    \begin{center}
      \ddicomicpicture[#1]
    \end{center}
  \end{frame}
}

\mode
<all>

\newif\ifddicomicspokendone
\newif\ifddicomicspokenprofessor
\newif\ifddicomicspokenstudent
\newif\ifddicomicthoughtprofessor
\newif\ifddicomicthoughtstudent

\def\ddicomicdone{
  \ifddicomicspokendone\else\ddicomicpeople\fi

  \ifddicomicthoughtprofessor
    \draw[overlay,black!50]
    {
      [shift=(people),xshift=.3cm,yshift=.5cm]
      \foreach \angle/\radius in {0/1,20/1.2,40/1.4,60/1.6,80/1.7}
      { (90+\angle:.5cm) circle[radius=\radius*1pt] }
      let \p1 = ($ (people) - (professor thinks) + (0,10pt) $)
      in \foreach \pos in {3,8,...,\y1} { (-.5cm,-\pos pt) circle
        [radius=1.7pt] \pgfextra{\global\let\lastpos\pos} }
      (-.5cm+3pt,-\lastpos pt-4pt) circle [radius=1.7pt]
    };
    \fi

  \ifddicomicthoughtstudent
    \draw[overlay,black!50]
    {
      [shift=(people),xshift=3.9cm,yshift=.6cm]
      \foreach \angle/\radius in {10/1.2,40/1.4,80/1.7}
      { (90-\angle:.3cm) circle[radius=\radius*1pt] }
      let \p1 = ($ (people) - (student thinks) + (0,10pt) $)
      in \foreach \pos in {3,8,...,\y1} { (.3cm,-\pos pt) circle
        [radius=1.7pt] \pgfextra{\global\let\lastpos\pos} }
      (.3cm-3pt,-\lastpos pt-4pt) circle [radius=1.7pt]
    };
    \fi
}


\def\ddibingo#1#2#3#4#5#6#7#8#9{%
  \alt<presentation>{\tikzset{where/.style={matrix anchor=south east,at={(-20pt,10pt)}}}}{\tikzset{where/.style={matrix anchor=north west,at=(last)}}}
  \matrix [where,anchor=center] (last bingo) 
  [text width=1.1cm,align=center,matrix of nodes, inner sep=2pt,
  nodes={fill=black!10, anchor=center, minimum height=1.2cm, minimum width=1.2cm}, column sep=1mm, row sep=1mm, 
  execute at begin
    cell=\ECFAugie\fontsize{6}{7}\selectfont\hskip0pt,ampersand replacement=\&] 
    {#1\par\&#2\par\&#3\par\\#4\par\&#5\par\&#6\par\\#7\par\&#8\par\&#9\par\\};
  \node [above] at ([yshift=1em]last bingo.north)
  {\ECFAugie\fontsize{8}{9}\selectfont Bingo gegen Langeweile};
  \coordinate (last) at ([yshift=-2em]last bingo.south west);
}

\newbox\testbox
\long\def\ddiprofessorsays#1{%
  \node [below right] (last professor) at (last)
    [text width=3.5cm,align=left] 
    {\ECFAugie \fontsize{8}{9}\selectfont\ignorespaces#1\par};
  \coordinate (last) at ([yshift=-1em]last professor.south west);
  \ddicomicspokenprofessortrue
}

\newif\ifddicomicwide

\def\dditestwide#1{
  \pgfinterruptpicture%
    \setbox\testbox=\hbox{\ECFAugie \fontsize{8}{9}\selectfont #1}%
    \ifdim\wd\testbox>3.5cm%
    \global\ddicomicwidetrue%
    \else%
    \global\ddicomicwidefalse%
    \fi%
  \endpgfinterruptpicture%
}

\long\def\ddistudentsays#1{%
  \dditestwide{#1}
  \node [below right] (last student) at (last)
    [text width=3.5cm,xshift=5mm,align/.expanded=\ifddicomicwide
    left\else flush right\fi] 
    {\ECFAugie \fontsize{8}{9}\selectfont\ignorespaces#1\par};
  \coordinate (last) at ([xshift=-5mm,yshift=-1em]last student.south west);
  \ddicomicspokenstudenttrue
}


\long\def\ddiprofessorthinks#1{%
  \ifddicomicspokendone\else\ddicomicpeople\fi
  \node [below right] (x) at (last)
    [text width=3.5cm,align=left,text=black!50] 
    {\ECFAugie \fontsize{8}{9}\selectfont\ignorespaces#1\par};
  \coordinate (last) at ([yshift=-1em]x.south west);
  \ifddicomicthoughtprofessor
  \else  
    \ddicomicthoughtprofessortrue
    \coordinate (professor thinks) at (x.north west);
  \fi
}

\long\def\ddistudentthinks#1{%
  \dditestwide{#1}
  \ifddicomicspokendone\else\ddicomicpeople\fi
  \node [below right] (x) at (last)
    [xshift=5mm,text width=3.5cm,align/.expanded=\ifddicomicwide
    left\else flush right\fi,text=black!50] 
    {\ECFAugie \fontsize{8}{9}\selectfont\ignorespaces#1\par};
  \coordinate (last) at ([xshift=-5mm,yshift=-1em]x.south west);
  \ifddicomicthoughtstudent
  \else  
    \ddicomicthoughtstudenttrue
    \coordinate (student thinks) at (x.north east);
  \fi
}

\def\ddicomicpeople{%
  \ddicomicspokendonetrue
  
  \begin{scope}[shift=(last),yshift=-1.6cm]

    % Professor
    \begin{scope}[shift={(0.5,1)}]
      \node (head) [minimum size=6pt, draw, inner sep=0pt, circle] {};
    
      \draw (head.south) -- ++(0,-7pt) coordinate (groin);
      
      \draw (groin) -- ++(-70:8pt) (groin) -- ++(-110:8pt);
      
      \draw ([yshift=-2pt]head.south) coordinate (shoulders) -- ++(10:5pt) (shoulders) -- ++(-30:5pt);
    \end{scope}
  
  \foreach \j in {3,2,1}{
  \begin{scope}[xshift=1cm,shift=(20:\j*0.6)]
    
    \foreach \i in {1,...,5}
    {
      \begin{scope}[shift={(0.2,1.3)},shift=(-45:\i*.25)]
        \node (head) [minimum size=5pt, draw, inner sep=0pt, fill=white,circle] {};
        \pgfmathsetmacro{\dir}{0.5}
        \draw (head.south) -- ++(70+\dir*20:-8pt) coordinate [near start] (shoulders\i);
      \end{scope}
    } 

    \draw [line join=round] (0,1) -- (1,0) -- (1,-0.3) -- (0,0.7) -- (0,1);
    \filldraw [fill=white,line join=round] (0,1) -- (0.2,1) -- (1.2,0) -- (1,0) -- cycle;
      
    \foreach \i in {1,...,5}
    {
      \draw (shoulders\i) -- ++(-180+0.5*20:5pt) (shoulders\i) -- ++(-150+0.5*20:5pt);
    }
  \end{scope}
  }

  \ifddicomicspokenprofessor
    \draw[overlay] (.6,1.1) to [out=100,in=-86] (-0.2,1.7)
    to [out=94,in=-94] ([xshift=-2mm]last professor.south west)
    to [out=86,in=-135] ([yshift=2mm,xshift=-1mm]last professor.south west);
  \fi
  
  \ifddicomicspokenstudent
    \draw[overlay] (3.75,1.15) to [out=100,in=-94] (4.2,1.9)
    to [out=86,in=-86] ([xshift=2mm]last student.south east)
    to [out=94,in=-45] ([yshift=2mm,xshift=1mm]last student.south east);
  \fi

  \coordinate (last) at (0,-.3);
  \coordinate (people) at (0,0);

  \only<presentation>{\ddibingonow}
  \end{scope}
  
}


\def\defddicomic#1[#2]{
  \expandafter\def\csname ddi@comic@#1\endcsname{#2}
}

\def\useddicomic#1{%
  \expandafter\let\expandafter\dditemp\csname ddi@comic@#1\endcsname
  \expandafter\ddicomic\expandafter[\dditemp]}

\endinput

