%% physketch.circuits.sty
%% Circuits part of physketch: circuitikz with the house settings, a
%% current source with chevrons and meters with an index.
%%
%% Copyright (C) 2024-2026 Azamat Gimaev
%% 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.
\NeedsTeXFormat{LaTeX2e}[2020/10/01]
\ProvidesPackage{physketch.circuits}
  [2026/10/20 v0.1.0 physketch: circuits]

\RequirePackage{physketch.general}
\RequirePackage{circuitikz}
\ctikzset{american, resistor=european, inductor=american}

% circuitikz defines the bipole `spring'; keep it as `circuit spring'
% and give `spring' back to physketch.mechanics
\def\physketch@copykey#1#2{%
  \pgfkeysifdefined{#1}{\pgfkeysgetvalue{#1}\physketch@tmp
    \pgfkeyslet{#2}\physketch@tmp}{}}
\physketch@copykey{/tikz/spring/.@cmd}{/tikz/circuit spring/.@cmd}
\physketch@copykey{/tikz/spring/.@def}{/tikz/circuit spring/.@def}
\@ifpackageloaded{physketch.mechanics}{\physketch@definespring}{}

% ---------------------------------------------------------------------
% Second variant of the current source I, with two chevrons: to[I']
% ---------------------------------------------------------------------
\pgfcircdeclarebipolescaled{sources}
{}
{\ctikzvalof{bipoles/isource/height}}
{isourcex}
{\ctikzvalof{bipoles/isource/height}}
{\ctikzvalof{bipoles/isource/width}}
{
  \pgf@circ@setlinewidth{bipoles}{\pgfstartlinewidth}
  \pgfpathellipse{\pgfpointorigin}{\pgfpoint{0}{\pgf@circ@res@up}}%
    {\pgfpoint{\pgf@circ@res@left}{0}}
  \pgf@circ@draworfill
  \pgfsetlinewidth{1.5\pgflinewidth}
  \pgfpathmoveto{\pgfpoint{0.1\pgf@circ@res@right}{0.5\pgf@circ@res@up}}
  \pgfpathlineto{\pgfpoint{0.6\pgf@circ@res@right}{0pt}}
  \pgfpathlineto{\pgfpoint{0.1\pgf@circ@res@right}{-0.5\pgf@circ@res@up}}
  \pgfpathmoveto{\pgfpoint{-0.4\pgf@circ@res@right}{0.5\pgf@circ@res@up}}
  \pgfpathlineto{\pgfpoint{0.1\pgf@circ@res@right}{0pt}}
  \pgfpathlineto{\pgfpoint{-0.4\pgf@circ@res@right}{-0.5\pgf@circ@res@up}}
  \pgfusepath{draw}
}
\pgfcirc@activate@bipole@opt{i}{I'}{isourcex}{current source'}{%
  \circuitikzbasekey/bipole/is current=true}
\pgfcirc@style@to@style{current source'}{I'}

% ---------------------------------------------------------------------
% Meters with an optional index: to[ammeter], to[voltmeter=2], ...
% The letter together with its index is centred in the circle.
% ---------------------------------------------------------------------
% #1 = letter, #2 = kern before the index (V leaves room under its slope),
% #3 = index
\newcommand\physketch@meter[3]{%
  {\fontsize{12pt}{14pt}\selectfont
   #1\ifx\relax#3\relax\else
     \kern#2\raisebox{-0.21em}{\fontsize{8pt}{9pt}\selectfont\mdseries#3}%
   \fi}}
\tikzset{
  ammeter/.style   = {rmeter, t={\physketch@meter{\textbf{A}}{-0.03em}{#1}}},
  ammeter/.default = {},
  voltmeter/.style   = {rmeter, t={\physketch@meter{\textbf{V}}{-0.16em}{#1}}},
  voltmeter/.default = {},
  ohmmeter/.style   = {rmeter, t={\physketch@meter{{\boldmath$\Omega$}}{-0.03em}{#1}}},
  ohmmeter/.default = {},
}

\endinput
