%% This is file `beamerthemeTUDa-mecheng.sty' version 4.02 (2025-02-25),
%% it is part of
%% TUDa-CI -- Corporate Design for TU Darmstadt
%% ----------------------------------------------------------------------------
%%
%%  Copyright (C) 2018--2025 by Marei Peischl <marei@peitex.de>
%%
%% ============================================================================
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% 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.3c or later is part of all distributions of LaTeX
%% version 2008/05/04 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is
%%   Marei Peischl <tuda-ci@peitex.de>
%%
%% The development respository can be found at
%% https://github.com/tudace/tuda_latex_templates
%% Please use the issue tracker for feedback!
%%
%% ============================================================================
%%

\ProvidesExplPackage{beamerthemeTUDa-mecheng}{2025-02-25}{4.02}{specific tudabeamer configuration for the department of mechanical engineering}


\tl_if_empty:NT \g_ptxcd_departmentlogo_tl {
	\keys_set:nn {ptxcd/beamer}{departmentlogofile=tuda_maschinenbau_logo_rgb}
}


\mode<presentation>


% Setup Arrow
\RequirePackage{pgf}

\cs_new:Nn \__ptxcd_mecheng_arrow:N {
	\begin{pgfpicture}
		\pgfsetcolor{TUDa-Arrow}
		\pgfsetrectcap
		\pgfsetlinewidth{2\c_ptxcd_smallrule_dim}
		\pgfpathmoveto{\pgfpointorigin}
		\pgfpathlineto{\pgfpointpolar{174}{.05#1}}
		\pgfpathlineto{\pgfpointpolar{186}{.05#1}}
		\pgfpathclose
		\pgfusepath{fill}
		\pgfpathmoveto{\pgfpoint{-.04#1}{0}}
		\pgfpathlineto{\pgfpoint{-#1}{0}}
		\pgfusepath{stroke}
	\end{pgfpicture}
}

\NewDocumentCommand{\MechEngArrow}{sm}{
	\IfBooleanT{#1}{\begingroup\colorlet{TUDa-Arrow}{.}}
	\dim_set:Nn \l_tmpa_dim {#2}\__ptxcd_mecheng_arrow:N \l_tmpa_dim
	\IfBooleanT{#1}{\endgroup}
}

%adjustments for colortheme
\ExplSyntaxOff
\setbeamercolor*{Primary1}{fg=TUDa-Primary1, bg=TUDa-Primary2}
\setbeamercolor*{Primary2}{fg=TUDa-Primary2, bg=TUDa-Primary1}

\setbeamercolor*{footer info}{fg=black!50}
\setbeamercolor*{department in head/foot}{parent=footer info}
\setbeamercolor*{institute in head/foot}{parent=footer info}
\setbeamercolor*{title in head/foot}{parent=footer info}
\setbeamercolor*{author in head/foot}{parent=footer info}
\setbeamercolor*{date in head/foot}{parent=footer info}
\setbeamercolor{footline}{use=Primary2,fg=Primary2.fg}
\setbeamercolor*{title}{parent=normal~text}

\setbeamercolor*{departmentlogo}{parent=normal text}
\setbeamercolor*{logo}{parent=normal text}

\ExplSyntaxOn

%innertheme adjustments
\defbeamertemplate*{title~ page}{TUDa-mecheng}[1][]
{
	\nointerlineskip
	\vspace{\dimexpr.5\c_ptxcd_logoheight_dim-\ht\strutbox+
	\box_dp:N \ptxcd_beamer_headrule_box + \box_ht:N \ptxcd_beamer_headrule_box
	}
	\hbox_set:Nn \l__ptxcd_titlehead_box {
		\parbox{\__ptxcd_beamer_linewidth:}{%
			\begin{beamercolorbox}[
				wd=\linewidth,
			]{title}
				\raggedright
				\usebeamerfont{title}
				\hbox_set:Nn \l__ptxcd_frametitle_box {
					\parbox[c]{\dimexpr\linewidth-\box_wd:N \g__ptxcd_logo_box-1em}{
						\raggedright{}
						\inserttitle
						\par
					}
				}
				\dim_compare:nT {\box_ht:N \l__ptxcd_frametitle_box > \baselineskip} {\raisebox{\dimexpr-\height+.5\c_ptxcd_logoheight_dim}}
				{\box_use:N \l__ptxcd_frametitle_box}
				\hspace{\fill}
				\raisebox{\dimexpr-.5\height+.5\ht\strutbox-.5\dp\strutbox}{\box_use:N \g__ptxcd_logo_box}\hspace*{\dimexpr-\beamer@rightmargin+\g__ptxcd_beamer_logosep_dim}
				\ifx\insertsubtitle\@empty
				\else
				\par\vspace{\c_ptxcd_largerule_dim}
				\parbox[t]{\dimexpr\linewidth- \box_wd:N \g__ptxcd_logo_box}{
					\usebeamerfont{subtitle}\insertsubtitle
				}
				\fi
			\bool_if:NT \g_ptxcd_authorontitle_bool {
				\par
					\parbox[t]{\dimexpr\linewidth-2ex}{
						\ifx\insertauthor\@empty
						\else
						\usebeamercolor[fg]{author}\usebeamerfont{author}\insertauthor\par
						\fi
						\ifx\insertauthor\@empty
						\else
						\usebeamercolor[fg]{institute}\usebeamerfont{institute}\insertinstitute
						\fi
					}
			}
			\end{beamercolorbox}
		}
	}
	\box_use:N \l__ptxcd_titlehead_box
	\par\skip_vertical:n {\textheight-\box_ht:N\l__ptxcd_titlehead_box-\box_dp:N \l__ptxcd_titlehead_box+.5\c_ptxcd_smallrule_dim}
	\begin{beamercolorbox}[
		rightskip=.07\linewidth,%place image left of arrow tip
		ht=\c_ptxcd_smallrule_dim,%dp=1mm,
		wd=\textwidth]{footline}
		\setbeamertemplate{departmentlogo}[TUDa-mecheng][width=\dimexpr2\linewidth/11\relax]
		\insertdepartmentlogo
		\hfill
		\bool_if:NF \g_ptxcd_authorontitle_bool {
			\begingroup
			\usebeamerfont{footline}
			\bool_set_false:N \l_ptxcd_tmpa_bool
			\clist_map_variable:nNn {department, institute, author,date} \l_tmpa_tl {
				\tl_if_empty:cF {beamer@short\l_tmpa_tl} {
					\bool_if:NTF \l_ptxcd_tmpa_bool
						{\space|\space}
						{\bool_set_true:N \l_ptxcd_tmpa_bool}
					\use:c {insert\l_tmpa_tl}
				}
			}
			\endgroup
		\quad
		}
		\insertlogo
		\par\nointerlineskip\skip_vertical:n {2\c_ptxcd_smallrule_dim}
		\MechEngArrow*{\linewidth}
	\end{beamercolorbox}
}


\defbeamertemplate{background}{TUDa-mecheng-title}{
	\dim_set:Nn	\l__ptxcd_titlegraphic_ht_dim {\paperheight}
	\dim_set:Nn	\l__ptxcd_titlegraphic_wd_dim {\paperwidth}
	\let\height\paperheight
	\let\width\paperwidth
	\inserttitlegraphic
}


\renewcommand*{\maketitle}{
	\begingroup
	\setbeamertemplate{background}[TUDa-mecheng-title]
	\ifbeamer@inframe\titlepage\else\frame[plain]{\titlepage}\fi
	\endgroup
}

\newcommand*{\insertdepartmentlogo}{\usebeamertemplate*{departmentlogo}}

\defbeamertemplate*{departmentlogo}{TUDa-mecheng}[1][height=1.7\baselineskip]{
	\str_set:Nx \l_tmpa_str {\str_range:Nnn \beamer@thc@fg {1} {-4}}
		\file_if_exist:nTF {\g_ptxcd_departmentlogo_tl-\l_tmpa_str.pdf} {
			\includegraphics[#1]{\g_ptxcd_departmentlogo_tl-\l_tmpa_str}
		} {
			\includegraphics[#1]{\g_ptxcd_departmentlogo_tl}
		}
}

\defbeamertemplate*{footline}{TUDa-mecheng}
{
	\begin{beamercolorbox}[
		wd=\linewidth,
		leftskip=\beamer@leftmargin,
    rightskip=\beamer@rightmargin,
	]{footline}
		\MechEngArrow*{\linewidth}
		\par\skip_vertical:n {.5\g__ptxcd_beamer_logosep_dim}
		\makebox[\linewidth][s]{
			\insertdepartmentlogo
			\hfill
			\bool_set_false:N \l_ptxcd_tmpa_bool
			\clist_map_variable:nNn {department, institute, author,title} \l_tmpa_tl {
				\tl_if_empty:cF {beamer@short\l_tmpa_tl} {
					\bool_if:NTF \l_ptxcd_tmpa_bool
					{\space|\space}
					{\bool_set_true:N \l_ptxcd_tmpa_bool}
					\usebeamercolor[fg]{\l_tmpa_tl\space in\space head/foot}\usebeamerfont{\l_tmpa_tl\space in\space head/foot}
					\use:c {insertshort\l_tmpa_tl}
				}
			}\quad
			\tl_if_empty:cF {beamer@shortdate} {
				\usebeamercolor{date~ in~ head/foot}\usebeamerfont{date~ in~ head/foot}
				\insertshortdate\quad
			}
			\usebeamerfont{framenumber}
			\makebox[.05\linewidth][l]{
				\quad\llap{\insertframenumber}
			}
		}
		\skip_vertical:n {\g__ptxcd_beamer_logosep_dim}
	\end{beamercolorbox}
}

\defbeamertemplate{frametitle}{TUDa-mecheng.logo}{%
	\nointerlineskip%
	\begin{beamercolorbox}[
		sep=\g__ptxcd_beamer_logosep_dim,
		wd=\__ptxcd_beamer_linewidth:,
		rightskip=-\g__ptxcd_beamer_logosep_dim,
		leftskip=-\g__ptxcd_beamer_logosep_dim,
	]{frametitle}
		\hbox_set:Nn \l__ptxcd_frametitle_box {
			\parbox[t]
			{\dimexpr\linewidth- \box_wd:N \g__ptxcd_logo_box-1em\relax\relax}{%
				\usebeamerfont{frametitle}\strut\insertframetitle
				\ifx\insertframesubtitle\@empty
				\else
				\par
				\usebeamercolor[fg]{framesubtitle}
				\usebeamerfont{framesubtitle}
				\insertframesubtitle
				\fi
		}}
		\leavevmode
		\bool_if:NTF \g_ptxcd_centerframetitle_bool
		{\raisebox{\dimexpr-.5\height+.5\depth}}
		{\use:n}
		{\box_use:N \l__ptxcd_frametitle_box}
		\hfill%
		\raisebox{\dimexpr-.5\height+.5\depth}
		{\box_use:N \g__ptxcd_logo_box}
	\end{beamercolorbox}%
	\nointerlineskip
	\begin{beamercolorbox}[
		sep=\g__ptxcd_beamer_logosep_dim,
		wd=\__ptxcd_beamer_linewidth:,
		rightskip=-\g__ptxcd_beamer_logosep_dim,
		leftskip=-\g__ptxcd_beamer_logosep_dim,
		ht=\c_ptxcd_smallrule_dim.
		dp=0
		]{smallrule}
	\end{beamercolorbox}
}

\cs_set:Nn \__ptxcd_reset_framebgcolor: {
	\setbeamercolor{normal~text}{fg=black,bg=}
	\usebeamercolor[fg]{normal~text}
	\setbeamercolor{footline}{use=Primary2,fg=Primary2.fg}
	\setbeamercolor{footer~info}{fg=black!50}
	\cs_gset_eq:cN {beamer@thcbg@normal~text} \__ptxcd_save_background_canvas:
	\cs_undefine:N \__ptxcd_save_background_canvas:
	\cs_if_exist:NT \__ptxcd_save_department_textaccentcolor:
		{\cs_set_eq:cN {\string\color@textaccentcolor} \__ptxcd_save_department_textaccentcolor:}
	\cs_undefine:N \__ptxcd_save_department_textaccentcolor:
	\__ptxcd_setup_frametitle_color:
}

\cs_set:Nn \__ptxcd_set_framebgcolor:n {
	\str_case:nnTF {#1} {
		{Primary1} {\tl_set:Nn \l_tmpa_tl {Primary2}}
		{Primary2} {\tl_set:Nn \l_tmpa_tl {Primary1}}
	}{
		\exp_args:Nnx \setbeamercolor{frametitle}{use=\l_tmpa_tl,fg=\l_tmpa_tl.fg,bg=}
		\exp_args:Nnx \setbeamercolor{normal~text}{use=\l_tmpa_tl,fg=\l_tmpa_tl.fg,bg=\l_tmpa_tl.bg}
		\exp_args:Nnx \setbeamercolor{footer~info}{use=\l_tmpa_tl,fg=\l_tmpa_tl.fg}
		\exp_args:Nnx \setbeamercolor{footline}{use=\l_tmpa_tl,fg=\l_tmpa_tl.fg}
		\cs_set_eq:Nc \__ptxcd_save_department_textaccentcolor: {\string\color@textaccentcolor}
		\colorlet{textaccentcolor}{TUDa-\l_tmpa_tl}
	}{
		\cs_if_exist:cTF {\string\color@#1}
		{\setbeamercolor{normal~text}{bg=#1}}
		{\setbeamercolor{normal~text}{bg=TUDa-#1}}
	}
	\usebeamercolor[fg]{normal~text}
}

\setbeamertemplate{headline}[TUDa.simple]

%font adjustments
\setbeamerfont{subtitle}{size=\normalsize,series=\mdseries}

\mode<all>

\endinput