% !TeX root = ./examples/bat.tex
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% This package is part of the TikZlings package
%% A package to bring cute little animals and other beings into tikz
%% Maintained by samcarter
%%
%% Project repository and bug tracker:
%% https://github.com/samcarter/tikzlings
%%
%% Released under the LaTeX Project Public License v1.3c or later
%% See https://www.latex-project.org/lppl.txt
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\ProvidesPackage{tikzlings-bats}[2025/03/23 version v2.3 Drawing bats in TikZ]

\RequirePackage{tikz}
\RequirePackage{tikzlings-addons}

\newcommand*{\bat}[1][]{%
 \begin{scope}%
   \tikzset{/bat/.cd,#1}%
   \bat@draw%
 \end{scope}%
 \thing[#1]%
 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \UseHook{tikzlings/bat/foreground}
 \UseHook{tikzlings/foreground}
 \csname bathookforeground\endcsname
 \csname tikzlinghookforeground\endcsname
 %
}

\newif\ifbat@threeD
\newif\ifbat@back
\newif\ifbat@contour
\newif\ifbat@teeth
\newif\ifbat@openmouth

\NewHook{tikzlings/bat/background}
\NewHook{tikzlings/bat/belly}
\NewHook{tikzlings/bat/body}
\NewHook{tikzlings/bat/foreground}

\tikzset{
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % Pass unknown keys on to tikz
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 /bat/.search also={/tikz,/pgf,/thing},
 /bat/.cd,
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % body
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 body/.store in     = \bat@body,
 body               = black!75!white,
 ears/.store in     = \bat@ears,
 ears               = \bat@body,
 ear/.forward to    = /bat/ears,
 eyes/.store in     = \bat@eyes,
 eyes               = white,
 eye/.forward to    = /bat/eyes,
 pupils/.store in   = \bat@pupils,
 pupils             = black,
 pupil/.forward to  = /bat/pupils,
 mouth/.store in    = \bat@mouth,
 mouth              = black,
 feet/.store in     = \bat@feet,
 feet               = orange,
 foot/.forward to   = /bat/feet,
 wings/.store in    = \bat@wings,
 wings              = 1,
 wing/.forward to   = /bat/wings,
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % options
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 teeth/.code        = \bat@teethtrue
                      \def\bat@teeth{#1},
 teeth/.default     = white,
 openmouth/.code    = \bat@openmouthtrue,
 open mouth/.forward to = /bat/openmouth,
 3D/.code           = \bat@threeDtrue,
 back/.code         = \bat@backtrue,
 contour/.code      = \bat@contourtrue
                      \def\bat@contour{#1}
                      \def\bat@antennas{#1}
                      \def\bat@mouth{#1},
 contour/.default   = black,
 outline/.forward to = /bat/contour,
}

\def\bat@draw{%
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % minimal bounding box size
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \path (-0.44,0.0) rectangle (0.44, 2.2);
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % store the current scale factor
 % from https://github.com/samcarter/tikzlings/issues/3#issuecomment-461373494
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \pgfgettransformentries{\tmpscaleA}{\tmpscaleB}{\tmpscaleC}{\tmpscaleD}{\tmp}{\tmp}%
 \pgfmathsetmacro{\scalingfactor}{sqrt(abs(\tmpscaleA*\tmpscaleD-\tmpscaleB*\tmpscaleC))*sqrt(abs((\pgf@xx/1cm)*(\pgf@yy/1cm)-(\pgf@xy/1cm)*(\pgf@yx/1cm)))}%
 \pgfmathsetmacro{\xscalefactor}{\tmpscaleA}
 \pgfmathsetmacro{\yscalefactor}{\tmpscaleD}
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % switch between 2D, 3D and contours, from
 % https://chat.stackexchange.com/transcript/message/45991801#45991801
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \ifbat@threeD
   \def\bat@part@draw[##1]{\shade[ball color=##1]}
 \else% 3D
   \ifbat@contour
     \def\bat@part@draw[##1]{\draw[##1,\bat@contour,fill=white,line width=\scalingfactor*0.4pt]}
   \else% contour
     \def\bat@part@draw[##1]{\fill[##1]}
   \fi% contour
 \fi% 3D
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % Body parts
 %
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 %
 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \UseHook{tikzlings/bat/background}
 \UseHook{tikzlings/background}
 \csname batshookbackground\endcsname
 \csname tikzlinghookbackground\endcsname
 %
 % Wings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \bat@part@draw[\bat@body] (-0.4147-\bat@wings*1.8711, 0.6377) .. controls (-0.4147-\bat@wings*2.0410, 1.1512) and (-0.4147-\bat@wings*1.1342, 1.5336) .. (-0.4147-\bat@wings*0.5687, 1.6054) .. controls (-0.4147-\bat@wings*0.3860, 1.4742) and (-0.4147-\bat@wings*0.0711, 1.3009) .. (-0.35, 1.2365) -- (-0.35, 0.5586) .. controls (-0.4147-\bat@wings*0.0890, 0.6892) and (-0.4147-\bat@wings*0.2882, 0.7333) .. (-0.4147-\bat@wings*0.4758, 0.6377) .. controls (-0.4147-\bat@wings*0.6753, 0.841) and (-0.4147-\bat@wings*0.9758, 0.8349) .. (-0.4147-\bat@wings*1.1476, 0.6377) .. controls (-0.4147-\bat@wings*1.2902, 0.8407) and (-0.4147-\bat@wings*1.72, 0.8553) .. (-0.4147-\bat@wings*1.8711, 0.6377) -- cycle;
 \bat@part@draw[\bat@body] (0.4147+\bat@wings*1.8711, 0.6377) .. controls (0.4147+\bat@wings*2.0410, 1.1512) and (0.4147+\bat@wings*1.1342, 1.5336) .. (0.4147+\bat@wings*0.5687, 1.6054) .. controls (0.4147+\bat@wings*0.3860, 1.4742) and (0.4147+\bat@wings*0.0711, 1.3009) .. (0.35, 1.2365) -- (0.35, 0.5586) .. controls (0.4147+\bat@wings*0.0890, 0.6892) and (0.4147+\bat@wings*0.2882, 0.7333) .. (0.4147+\bat@wings*0.4758, 0.6377) .. controls (0.4147+\bat@wings*0.6753, 0.841) and (0.4147+\bat@wings*0.9758, 0.8349) .. (0.4147+\bat@wings*1.1476, 0.6377) .. controls (0.4147+\bat@wings*1.2902, 0.8407) and (0.4147+\bat@wings*1.72, 0.8553) .. (0.4147+\bat@wings*1.8711, 0.6377) -- cycle;
 %
 % Ears %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \bat@part@draw[\bat@ears] (-0.3498, 1.7392) .. controls (-0.3498, 1.7392) and (-0.4724, 1.9226) .. (-0.3896, 2.177) .. controls (-0.2561, 2.2679) and (-0.0953, 1.9447) .. (0.0234, 1.9394) -- cycle;
 \bat@part@draw[\bat@ears] (0.3498, 1.7392) .. controls (0.3498, 1.7392) and (0.4724, 1.9226) .. (0.3896, 2.177) .. controls (0.2561, 2.2679) and (0.0953, 1.9447) .. (-0.0234, 1.9394) -- cycle;
 %
 % Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \ifbat@back
   \bat@part@draw[\bat@feet] (0.175,0.1) ellipse[x radius=0.03, y radius=0.1];
   \bat@part@draw[\bat@feet] (0.225,0.11) ellipse[x radius=0.03, y radius=0.1, rotate=20];
   \bat@part@draw[\bat@feet] (0.275,0.13) ellipse[x radius=0.03, y radius=0.1, rotate=40];
   \bat@part@draw[\bat@feet] (-0.175,0.1) ellipse[x radius=0.03, y radius=0.1];
   \bat@part@draw[\bat@feet] (-0.225,0.11) ellipse[x radius=0.03, y radius=0.1, rotate=-20];
   \bat@part@draw[\bat@feet] (-0.275,0.13) ellipse[x radius=0.03, y radius=0.1, rotate=-40];
 \fi% back
 %
 % Body %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \bat@part@draw[\bat@body] (0,0.8) ellipse [y radius=0.7, x radius=0.45];
 %
 % Head %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \bat@part@draw[\bat@body] (0,1.55) circle [radius=0.43];
 %
 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \UseHook{tikzlings/bat/belly}
 \UseHook{tikzlings/belly}
 \csname batshookbelly\endcsname
 \csname tikzlinghookbelly\endcsname
 %
 % Show details only from the front %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \unless\ifbat@back
   %
   % Feet %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \bat@part@draw[\bat@feet] (0.175,0.1) ellipse[x radius=0.03, y radius=0.1];
   \bat@part@draw[\bat@feet] (0.225,0.11) ellipse[x radius=0.03, y radius=0.1, rotate=20];
   \bat@part@draw[\bat@feet] (0.275,0.13) ellipse[x radius=0.03, y radius=0.1, rotate=40];
   \bat@part@draw[\bat@feet] (-0.175,0.1) ellipse[x radius=0.03, y radius=0.1];
   \bat@part@draw[\bat@feet] (-0.225,0.11) ellipse[x radius=0.03, y radius=0.1, rotate=-20];
   \bat@part@draw[\bat@feet] (-0.275,0.13) ellipse[x radius=0.03, y radius=0.1, rotate=-40];
   %
   % Eyes %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \bat@part@draw[\bat@eyes] (-0.18,1.66) ellipse[x radius=0.1, y radius=0.15, rotate=10];
   \bat@part@draw[\bat@pupils] (-0.15,1.645) ellipse[x radius=0.0357, y radius=0.0714, rotate=3];
   \bat@part@draw[\bat@eyes] (0.18,1.66) ellipse[x radius=0.1, y radius=0.15, rotate=-10];
   \bat@part@draw[\bat@pupils] (0.15,1.645) ellipse[x radius=0.0357, y radius=0.0714, rotate=-3];
   %
   % Teeth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \ifbat@teeth
     \bat@part@draw[\bat@teeth] (-0.1,1.36) -- (-0.12,1.3) -- (-0.14,1.38) -- cycle;
     \bat@part@draw[\bat@teeth] (0.1,1.36) -- (0.12,1.3) -- (0.14,1.38) -- cycle;
   \fi
   %
   % Mouth %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
   \ifbat@openmouth
     \filldraw[\bat@mouth,line width=\scalingfactor*0.4pt] (0.18, 1.42) arc [start angle=-40, end angle=-140, radius=0.23];
   \else
     \draw[\bat@mouth,line width=\scalingfactor*0.4pt] (0.18, 1.42) arc [start angle=-40, end angle=-140, radius=0.23];
   \fi
   %
   %
 \fi% back
 % adding hook for users to customise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 \UseHook{tikzlings/bat/body}
 \UseHook{tikzlings/body}
 \csname batshookbody\endcsname
 \csname tikzlinghookbody\endcsname
 %
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%% Documentation
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\clearpage
%\section[Bat]{Natalie, the bat}
%
%\emph{Named after a dear friend from my undergrad studies}
%
%\subsection{Package name}
%
%\begin{tcolorbox}[title={Package usage}]
%\begin{samcode}
%\usepackage{tikzlings-bats}
%\end{samcode}
%\end{tcolorbox}
%
%\subsection{Basic Usage}
%
%\begin{tcblisting}{title={Basic bat}}
%\bat
%\end{tcblisting}
%
%\subsection{Options}
%
%The basic bat can be modified by changing its colour:
%\begin{tcblisting}{title={Body colour}}
%\bat[body=blue]
%\end{tcblisting}
%
%In addition to the colour of the body, the colour of various body parts can be adjusted:
%\begin{tcblisting}{title={Eye colour}}
%\bat[eyes=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Pupil colour}}
%\bat[pupils=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Mouth colour}}
%\bat[mouth=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Ear colour}}
%\bat[ears=red]
%\end{tcblisting}
%\begin{tcblisting}{title={Foot colour}}
%\bat[feet=red]
%\end{tcblisting}
%
% The bat can open its mouth:
%\begin{tcblisting}{title={Open mouth}}
%\bat[openmouth]
%\end{tcblisting}
%
%With the \saminline|wings| option, one can adjust the spread of the wings. It accepts values between~0 and~1:
%\begin{tcblisting}{title={Wing span}}
%\bat[wings=0.5]
%\end{tcblisting}
%
%To turn the bat into a vampire bat, the \saminline|teeth| option can be used:
%\begin{tcblisting}{title={Vampire}}
%\bat[teeth=white]
%\end{tcblisting}
%
%To view the bat from behind:
%\begin{tcblisting}{title={Back view}}
%\bat[back]
%\end{tcblisting}
%
%The key \saminline|3D| will make the bat 3-dimensional:
%\begin{tcblisting}{title={3D view}}
%\bat[3D]
%\end{tcblisting}
%
%And finally the \saminline|contour| key will only draw the outlines:
%\begin{tcblisting}{title={Contours}}
%\bat[contour=black]
%\end{tcblisting}