% Florian Sihler, 2022
% Licensed under GNU General Public License version 3
% https://opensource.org/licenses/gpl-3.0.html
\newif\if@pingu@x@lightsaber@glow@left
\newif\if@pingu@x@lightsaber@glow@right
\newif\if@pingu@x@lightsaber@double@left
\newif\if@pingu@x@lightsaber@double@right
\def\pingu@x@saberglow@w@half{.9mm}
\def\pingulightsaberfactor{.028}
\pingu@create@wing@extra{lightsaber}{%
  lightsaber \@lr/.code                = {\pingu@set@extra{lightsaber\@lr}{true}\pingu@color{lightsaber\@lr}{##1}},
  lightsaber \@lr/.default             = pingu@blue,
  lightsaber \@lr\space handle/.code   = \pingu@color{lightsaber\@lr @handle}{##1},%
  lightsaber \@lr\space deco/.code     = \pingu@color{lightsaber\@lr @deco}{##1},%
  lightsaber \@lr\space ribbs/.code    = \pingu@color{lightsaber\@lr @ribbs}{##1},%
  lightsaber \@lr\space button/.code   = \pingu@color{lightsaber\@lr @button}{##1},%
  lightsaber \@lr\space button b/.code   = \pingu@color{lightsaber\@lr @button@b}{##1},%
  lightsaber \@lr\space color b/.code   = \pingu@color{lightsaber\@lr @color@b}{##1},%
  lightsaber \@lr\space length/.code   = {\@namedef{pingu@x@lightsaber@length@\@lr}{##1}},%
  lightsaber \@lr\space length b/.code   = {\@namedef{pingu@x@lightsaber@length@b@\@lr}{##1}},%
  lightsaber \@lr\space yshift/.code   = {\@namedef{pingu@x@lightsaber@yshift@\@lr}{##1}},%
  lightsaber \@lr\space glow/.is if    = @pingu@x@lightsaber@glow@\@lr,%
  lightsaber \@lr\space double/.is if  = @pingu@x@lightsaber@double@\@lr,%
  lightsaber \@lr\space glow core/.code   = \pingu@color{lightsaber\@lr @glow@core}{##1},%
  lightsaber \@lr\space solid/.style = {/pingu/lightsaber \@lr\space glow=false},
  lightsaber \@lr\space disabled/.style = {/pingu/lightsaber \@lr\space solid,/pingu/lightsaber \@lr=\@pingu@none},
  lightsaber \@lr\space outer glow factor/.code  = {\@namedef{pingu@x@lightsaber@ogf@\@lr}{##1}}%
}{lightsaber \@lr\space handle=pingu@silver, lightsaber \@lr\space deco=pingu@silver!12!pingu@black, lightsaber \@lr\space button=pingu@red!85!pingu@black,lightsaber \@lr\space button b=pingu@red!85!pingu@black, lightsaber \@lr\space ribbs=pingu@silver!50!pingu@black,lightsaber \@lr\space length=2cm,lightsaber \@lr\space glow=\ifpengu@setup@glows@ true\else false\fi,lightsaber \@lr\space glow core=white,lightsaber \@lr\space outer glow factor=\pingulightsaberfactor,lightsaber \@lr\space yshift=0pt,lightsaber \@lr\space length b=\csname pingu@x@lightsaber@length@\@lr\endcsname,lightsaber \@lr\space color b=\noexpand\@col}

% #region lightsaber base
\def\pingu@draw@wi@lightsaber@base#1{%
% deco-elements
    % base
    \pingu@block{\csname pingu@color@lightsaber#1@deco\endcsname}(\pingu@name-wing-#1)++(-.4mm,2.65mm+\csname pingu@x@lightsaber@yshift@#1\endcsname) -- ++(0,-5mm) -- ++(.8mm,0) |- cycle;
    \if@pingu@wing@item@side@left@
    % ribbs; single path saves memory :D
    \pingu@block{\csname pingu@color@lightsaber#1@ribbs\endcsname}(\pingu@name-wing-#1)++(-.40mm,2.225mm+\csname pingu@x@lightsaber@yshift@#1\endcsname)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -| ++(.8mm,-.165mm) -- ++(-.8mm,0)
    arc(90:270:.075mm) -- ++(.8mm,0) |- cycle;
    % button
    \pingu@block{\csname pingu@color@lightsaber#1@button@b\endcsname}(\pingu@name-wing-#1)++(.49mm,-1.75mm+\csname pingu@x@lightsaber@yshift@#1\endcsname) ellipse [x radius=.1mm, y radius=.3mm];
    \pingu@block{\csname pingu@color@lightsaber#1@button\endcsname}(\pingu@name-wing-#1)++(.49mm,1.9mm+\csname pingu@x@lightsaber@yshift@#1\endcsname) ellipse [x radius=.1mm, y radius=.3mm];
    % cover
    \pingu@block{\csname pingu@color@lightsaber#1@handle\endcsname}(\pingu@name-wing-#1)++(-.5mm,2.65mm+\csname pingu@x@lightsaber@yshift@#1\endcsname) arc(180:90:.15mm) -- ++(.7mm,0) arc(90:0:.16mm) -- ++(0,-5mm) arc(360:270:.15mm) -- ++(-.7mm,0) arc (270:180:.15mm) -- ++(0,.25mm) arc(270:360:.33mm and .2mm) -- ++(0,4.1mm) arc(0:90:.33mm and .2mm) -- ++(0,.25mm) -- cycle;
    \else
    % ribbs; single path saves memory :D (the second)
    \pingu@block{\csname pingu@color@lightsaber#1@ribbs\endcsname}(\pingu@name-wing-#1)++(.40mm,2.225mm+\csname pingu@x@lightsaber@yshift@#1\endcsname)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -| ++(-.8mm,-.165mm) -- ++(.8mm,0)
        arc(90:-90:.075mm) -- ++(-.8mm,0) |- cycle;
    % buttons
    \pingu@block{\csname pingu@color@lightsaber#1@button@b\endcsname}(\pingu@name-wing-#1)++(-.49mm,-1.75mm+\csname pingu@x@lightsaber@yshift@#1\endcsname) ellipse [x radius=.1mm, y radius=.3mm];
    \pingu@block{\csname pingu@color@lightsaber#1@button\endcsname}(\pingu@name-wing-#1)++(-.49mm,1.9mm+\csname pingu@x@lightsaber@yshift@#1\endcsname) ellipse [x radius=.1mm, y radius=.3mm];
    % cover
    \pingu@block{\csname pingu@color@lightsaber#1@handle\endcsname}(\pingu@name-wing-#1)++(.5mm,2.65mm+\csname pingu@x@lightsaber@yshift@#1\endcsname) arc(0:90:.15mm) -- ++(-.7mm,0) arc(90:180:.16mm) -- ++(0,-5mm) arc(180:270:.15mm) -- ++(.7mm,0) arc (270:360:.15mm) -- ++(0,.25mm) arc(270:180:.33mm and .2mm) -- ++(0,4.1mm) arc(180:90:.33mm and .2mm) -- ++(0,.25mm) -- cycle;
    \fi
}
% #endregion
% #1 wing, #2 yshift, #3 angle, #4 length, #5 color
\def\pingu@draw@wi@lightsaber@pre#1#2#3#4#5{
    \scope[rotate around={#3:(\pingu@name-wing-#1)}]
    \csname if@pingu@x@lightsaber@glow@#1\endcsname
    \@for\i:={0,2,4,6,7,8,9,10,11,12,13,14,15,16,17}\do{
        \pingu@block[fill opacity=\ifnum\i>14 .225\else.1\fi*\i/20]{#5} (\pingu@name-wing-#1)++(-\pingu@x@saberglow@w@half+\i*\i mm/440,#2) -- ++(.05mm,#4) arc(180:0:\pingu@x@saberglow@w@half-.05mm-\i*\i mm/440 and \ifnum\i>14 .85mm\else\pingu@one\fi-\i mm/30) -- ++(.05mm,-#4) arc (0:-180:\pingu@x@saberglow@w@half-\i*\i mm/440 and .35mm);
    }
    \else % draw solid
        \pingu@block{#5} (\pingu@name-wing-#1)++(-.425mm,#2-.1mm) -- ++(.05mm,#4) arc(180:0:.375mm and .375mm) -- ++(.05mm,-#4) arc (0:-180:.425mm and .05mm);
    \fi
    \endscope
}

% #1 wing, #2 yshift, #3 angle, #4 length, #5 color
\def\pingu@draw@wi@lightsaber@post#1#2#3#4#5{
    \scope[rotate around={#3:(\pingu@name-wing-#1)}]
    \csname if@pingu@x@lightsaber@glow@#1\endcsname
    \ifdim\csname pingu@x@lightsaber@ogf@#1\endcsname pt>\z@
    \pingu@layer{foreground}[]
    \pgfinterruptboundingbox
    \path[clip,/pingu/@invclip] (\pingu@name-wing-#1)++(-.275mm,#2-.1mm) -- ++(.1mm,#4) arc(180:0:.175mm and .25mm) -- ++(.1mm,-#4) arc (0:-180:.275mm and .065mm);
    \@for\i:={1,1.1,1.2,1.3,1.5}\do{
        \pingu@block[/pingu/@glow,fill opacity=\csname pingu@x@lightsaber@ogf@#1\endcsname/\i]{#5} (\pingu@name-wing-#1) ++(.05mm,#2+#4/2) circle[radius=\i*#4];
    }
    \endpgfinterruptboundingbox
    \endpingu@layer
    \fi
        % fadings hat a rotation angle bug
        \@for\i:={1,2}\do{
            \pingu@block[fill opacity=.125*\i/10]{#5} (\pingu@name-wing-#1)++(-\pingu@x@saberglow@w@half+\i*\i mm/440,#2) -- ++(.05mm,#4) arc(180:0:\pingu@x@saberglow@w@half-.05mm-\i*\i mm/440 and \pingu@one-\i mm/30) -- ++(.05mm,-#4) arc (0:-180:\pingu@x@saberglow@w@half-\i*\i mm/440 and .35mm);
        }
        \pingu@block{\csname pingu@color@lightsaber#1@glow@core\endcsname} (\pingu@name-wing-#1)++(-.275mm,#2-.1mm) -- ++(.1mm,#4) arc(180:0:.175mm and .25mm) -- ++(.1mm,-#4) arc (0:-180:.275mm and .065mm);
    \fi
    \endscope
}

% #1 wing
\def\pingu@draw@wi@lightsaber#1{%
    \pingu@setup@wing{lightsaber}{#1}%
    \scope[rotate around={\@angle:([yshift=+\csname pingu@x@lightsaber@yshift@#1\endcsname]\pingu@name-wing-#1)},xscale=1.2]
    \pingu@draw@wi@lightsaber@pre{#1}{2.9mm+\csname pingu@x@lightsaber@yshift@#1\endcsname}{0}{\csname pingu@x@lightsaber@length@#1\endcsname}{\@col}
    \csname if@pingu@x@lightsaber@double@#1\endcsname
    \pingu@draw@wi@lightsaber@pre{#1}{2.65mm-\csname pingu@x@lightsaber@yshift@#1\endcsname}{180}{\csname pingu@x@lightsaber@length@b@#1\endcsname}{\csname pingu@color@lightsaber#1@color@b\endcsname}
    \fi
    \pingu@draw@wi@lightsaber@base{#1}
    % afterglow (whuile watching everglow :D)
    \pingu@draw@wi@lightsaber@post{#1}{2.9mm+\csname pingu@x@lightsaber@yshift@#1\endcsname}{0}{\csname pingu@x@lightsaber@length@#1\endcsname}{\@col}
    \csname if@pingu@x@lightsaber@double@#1\endcsname
    \pingu@draw@wi@lightsaber@post{#1}{2.65mm-\csname pingu@x@lightsaber@yshift@#1\endcsname}{180}{\csname pingu@x@lightsaber@length@b@#1\endcsname}{\csname pingu@color@lightsaber#1@color@b\endcsname}
    \fi
    \endscope
}


\newif\if@pingu@x@lightstaff@glow@left
\newif\if@pingu@x@lightstaff@glow@right
\pingu@create@wing@extra{lightstaff}{%
    light-staff \@lr/.code         = {\pingu@set@extra{lightstaff\@lr}{true}\pingu@color{lightstaff\@lr}{##1}},
    light-staff \@lr/.default      = pingu@green,
    light-staff \@lr\space length/.code =\expandafter\def\csname pingu@lightstaff@\@lr @length\endcsname{##1},
    light-staff \@lr\space glow length/.code =\expandafter\def\csname pingu@lightstaff@\@lr @glow@length\endcsname{##1},
    light-staff \@lr\space head/.code = \pingu@color{lightstaff\@lr}{##1},%
    light-staff \@lr\space staff/.code = \pingu@color{lightstaff@\@lr @staff}{##1},%
    light-staff \@lr\space core/.code = \pingu@color{lightstaff@\@lr @head@core}{##1},%
    light-staff \@lr\space core width/.code = \expandafter\def\csname pingu@lightstaff@\@lr @core@width\endcsname{##1},%
    light-staff \@lr\space glow/.is if    = @pingu@x@lightstaff@glow@\@lr,%
    light-staff \@lr\space outer glow factor/.code  = {\@namedef{pingu@x@lightstaff@ogf@\@lr}{##1}},%
}{light-staff \@lr\space length=28mm,light-staff \@lr\space staff=pingu@bronze,light-staff \@lr\space head=pingu@green,light-staff \@lr\space core=pingu@white,light-staff \@lr\space glow=\ifpengu@setup@glows@ true\else false\fi,light-staff \@lr\space outer glow factor=.082,light-staff \@lr\space glow length=13mm,light-staff \@lr\space core width=.44mm}
\def\pingu@draw@wi@lightstaff#1{%
    \pingu@setup@wing{lightstaff}{#1}%
\scope[rotate around={\@angle:(\pingu@name-wing-#1)}]
    \pingu@block{\csname pingu@color@lightstaff@#1@staff\endcsname}(\pingu@name-wing-#1)++(\@sidesign*-.2mm,-11mm) -- ++(0,\csname pingu@lightstaff@#1@length\endcsname) arc(180:0:\@sidesign*.4mm and .3mm) coordinate[pos=.5] (\pingu@name-lightstaff-#1-top) -- ++(0,-\csname pingu@lightstaff@#1@length\endcsname) arc(360:180:\@sidesign*.4mm and .3mm) -- cycle;
    \pingu@draw@wi@lightstaff@pre{#1}{[yshift=-1.1mm]\pingu@name-lightstaff-#1-top}{\@col}
    \pingu@block{\csname pingu@color@lightstaff@#1@head@core\endcsname} ([yshift=-1.1mm]\pingu@name-lightstaff-#1-top) -- ++(-\csname pingu@lightstaff@#1@core@width\endcsname,1mm) -- ++(0,2mm) -- ++(\csname pingu@lightstaff@#1@core@width\endcsname,1mm) -- ++(\csname pingu@lightstaff@#1@core@width\endcsname,-1mm) -- ++(0,-2mm) -- cycle;
    \pingu@draw@wi@lightstaff@post{#1}{[yshift=-1.1mm]\pingu@name-lightstaff-#1-top}{\@col}
\endscope
}

% #1 wing, #2 start, #3 color
\def\pingu@draw@wi@lightstaff@pre#1#2#3{
\csname if@pingu@x@lightstaff@glow@#1\endcsname
\@for\i:={0,1,2,4,6,7,8,9,10,14}\do{
    \pingu@block[fill opacity=\ifnum\i>14 .225\else.1\fi*\i/20]{#3} (#2)++(0,-\i mm/15) -- ++(-\csname pingu@lightstaff@#1@core@width\endcsname-\i mm/30,1mm+\i mm/30) -- ++(0,2mm+\i mm/15) -- ++(\csname pingu@lightstaff@#1@core@width\endcsname+\i mm/30,1mm+\i mm/30) -- ++(\csname pingu@lightstaff@#1@core@width\endcsname+\i mm/30,-1mm-\i mm/30) -- ++(0,-2mm-\i mm/15) -- cycle;
    %  -- ++(.05mm,#4) arc(180:0:\pingu@x@saberglow@w@half-.05mm-\i*\i mm/440 and \ifnum\i>14 .85mm\else\pingu@one\fi-\i mm/30) -- ++(.05mm,-#4) arc (0:-180:\pingu@x@saberglow@w@half-\i*\i mm/440 and .35mm);
}
\else % draw solid
    \pingu@block{#3} (#2) -- ++(-.55mm,1mm) -- ++(0,2mm) -- ++(.55mm,1mm) -- ++(.55mm,-1mm) -- ++(0,-2mm) -- cycle;
\fi
}

\def\pingu@draw@wi@lightstaff@post#1#2#3{
\csname if@pingu@x@lightstaff@glow@#1\endcsname
\ifdim\csname pingu@x@lightstaff@ogf@#1\endcsname pt>\z@
\pingu@layer{foreground}[]
\pgfinterruptboundingbox
\@for\i:={1,1.05,1.1,1.2,1.3,1.315,1.4,1.5}\do{
    \pingu@block[/pingu/@glow,fill opacity=\csname pingu@x@lightstaff@ogf@#1\endcsname/\i]{#3} (#2)++(0,2mm) circle[radius=\i*\csname pingu@lightstaff@#1@glow@length\endcsname];
}
\endpgfinterruptboundingbox
\pingu@block[opacity=.65]{\csname pingu@color@lightstaff@#1@head@core\endcsname} (#2)++(0,.5mm) -- ++(-.20mm,.75mm) -- ++(0,1.5mm) -- ++(.20mm,.75mm) -- ++(.20mm,-.75mm) -- ++(0,-1.5mm) -- cycle;
\endpingu@layer
\fi
% fadings hat a rotation angle bug
\@for\i:={1,2}\do{
    \pingu@block[fill opacity=.125*\i/10]{#3} (#2)++(0,-\i mm/15+2mm) -- ++(-\csname pingu@lightstaff@#1@core@width\endcsname-\i mm/30,1mm+\i mm/30) -- ++(0,2mm+\i mm/15) -- ++(\csname pingu@lightstaff@#1@core@width\endcsname+\i mm/30,1mm+\i mm/30) -- ++(\csname pingu@lightstaff@#1@core@width\endcsname+\i mm/30,-1mm-\i mm/30) -- ++(0,-2mm-\i mm/15) -- cycle;
}
\pingu@block{\csname pingu@color@lightstaff@#1@head@core\endcsname} (#2) -- ++(-\csname pingu@lightstaff@#1@core@width\endcsname,1mm) -- ++(0,2mm) -- ++(\csname pingu@lightstaff@#1@core@width\endcsname,1mm) -- ++(\csname pingu@lightstaff@#1@core@width\endcsname,-1mm) -- ++(0,-2mm) -- cycle;
\fi
}