44
55__all__ = ["ValueTracker" , "ComplexValueTracker" ]
66
7-
8- from typing import Any
7+ from typing import TYPE_CHECKING , Any
98
109import numpy as np
1110
1211from manim .mobject .mobject import Mobject
1312from manim .mobject .opengl .opengl_compatibility import ConvertToOpenGL
1413from manim .utils .paths import straight_path
1514
15+ if TYPE_CHECKING :
16+ from typing_extensions import Self
17+
18+ from manim .typing import PathFuncType
19+
1620
1721class ValueTracker (Mobject , metaclass = ConvertToOpenGL ):
1822 """A mobject that can be used for tracking (real-valued) parameters.
@@ -71,76 +75,110 @@ def construct(self):
7175
7276 """
7377
74- def __init__ (self , value : float = 0 , ** kwargs : Any ):
78+ def __init__ (self , value : float = 0 , ** kwargs : Any ) -> None :
7579 super ().__init__ (** kwargs )
7680 self .set (points = np .zeros ((1 , 3 )))
7781 self .set_value (value )
7882
7983 def get_value (self ) -> float :
8084 """Get the current value of this ValueTracker."""
81- return self .points [0 , 0 ]
85+ value : float = self .points [0 , 0 ]
86+ return value
8287
83- def set_value (self , value : float ):
84- """Sets a new scalar value to the ValueTracker"""
88+ def set_value (self , value : float ) -> Self :
89+ """Sets a new scalar value to the ValueTracker. """
8590 self .points [0 , 0 ] = value
8691 return self
8792
88- def increment_value (self , d_value : float ):
89- """Increments (adds) a scalar value to the ValueTracker"""
93+ def increment_value (self , d_value : float ) -> Self :
94+ """Increments (adds) a scalar value to the ValueTracker. """
9095 self .set_value (self .get_value () + d_value )
9196 return self
9297
93- def __bool__ (self ):
94- """Return whether the value of this value tracker evaluates as true."""
98+ def __bool__ (self ) -> bool :
99+ """Return whether the value of this ValueTracker evaluates as true."""
95100 return bool (self .get_value ())
96101
97- def __iadd__ (self , d_value : float ):
98- """adds ``+=`` syntax to increment the value of the ValueTracker"""
102+ def __add__ (self , d_value : float | Mobject ) -> ValueTracker :
103+ """Return a new :class:`ValueTracker` whose value is the current tracker's value plus
104+ ``d_value``.
105+ """
106+ if isinstance (d_value , Mobject ):
107+ raise ValueError (
108+ "Cannot increment ValueTracker by a Mobject. Please provide a scalar value."
109+ )
110+ return ValueTracker (self .get_value () + d_value )
111+
112+ def __iadd__ (self , d_value : float | Mobject ) -> Self :
113+ """adds ``+=`` syntax to increment the value of the ValueTracker."""
114+ if isinstance (d_value , Mobject ):
115+ raise ValueError (
116+ "Cannot increment ValueTracker by a Mobject. Please provide a scalar value."
117+ )
99118 self .increment_value (d_value )
100119 return self
101120
102- def __ifloordiv__ (self , d_value : float ):
103- """Set the value of this value tracker to the floor division of the current value by ``d_value``."""
121+ def __ifloordiv__ (self , d_value : float ) -> Self :
122+ """Set the value of this ValueTracker to the floor division of the current value by ``d_value``."""
104123 self .set_value (self .get_value () // d_value )
105124 return self
106125
107- def __imod__ (self , d_value : float ):
108- """Set the value of this value tracker to the current value modulo ``d_value``."""
126+ def __imod__ (self , d_value : float ) -> Self :
127+ """Set the value of this ValueTracker to the current value modulo ``d_value``."""
109128 self .set_value (self .get_value () % d_value )
110129 return self
111130
112- def __imul__ (self , d_value : float ):
113- """Set the value of this value tracker to the product of the current value and ``d_value``."""
131+ def __imul__ (self , d_value : float ) -> Self :
132+ """Set the value of this ValueTracker to the product of the current value and ``d_value``."""
114133 self .set_value (self .get_value () * d_value )
115134 return self
116135
117- def __ipow__ (self , d_value : float ):
118- """Set the value of this value tracker to the current value raised to the power of ``d_value``."""
136+ def __ipow__ (self , d_value : float ) -> Self :
137+ """Set the value of this ValueTracker to the current value raised to the power of ``d_value``."""
119138 self .set_value (self .get_value () ** d_value )
120139 return self
121140
122- def __isub__ (self , d_value : float ):
123- """adds ``-=`` syntax to decrement the value of the ValueTracker"""
141+ def __sub__ (self , d_value : float | Mobject ) -> ValueTracker :
142+ """Return a new :class:`ValueTracker` whose value is the current tracker's value minus
143+ ``d_value``.
144+ """
145+ if isinstance (d_value , Mobject ):
146+ raise ValueError (
147+ "Cannot decrement ValueTracker by a Mobject. Please provide a scalar value."
148+ )
149+ return ValueTracker (self .get_value () - d_value )
150+
151+ def __isub__ (self , d_value : float | Mobject ) -> Self :
152+ """Adds ``-=`` syntax to decrement the value of the ValueTracker."""
153+ if isinstance (d_value , Mobject ):
154+ raise ValueError (
155+ "Cannot decrement ValueTracker by a Mobject. Please provide a scalar value."
156+ )
124157 self .increment_value (- d_value )
125158 return self
126159
127- def __itruediv__ (self , d_value : float ):
128- """Sets the value of this value tracker to the current value divided by ``d_value``."""
160+ def __itruediv__ (self , d_value : float ) -> Self :
161+ """Sets the value of this ValueTracker to the current value divided by ``d_value``."""
129162 self .set_value (self .get_value () / d_value )
130163 return self
131164
132- def interpolate (self , mobject1 , mobject2 , alpha , path_func = straight_path ()):
133- """
134- Turns self into an interpolation between mobject1
135- and mobject2.
136- """
165+ def interpolate (
166+ self ,
167+ mobject1 : Mobject ,
168+ mobject2 : Mobject ,
169+ alpha : float ,
170+ path_func : PathFuncType = straight_path (),
171+ ) -> Self :
172+ """Turns ``self`` into an interpolation between ``mobject1`` and ``mobject2``."""
137173 self .set (points = path_func (mobject1 .points , mobject2 .points , alpha ))
138174 return self
139175
140176
141177class ComplexValueTracker (ValueTracker ):
142178 """Tracks a complex-valued parameter.
143179
180+ The value is internally stored as a points array [a, b, 0]. This can be accessed directly
181+ to represent the value geometrically, see the usage example.
144182 When the value is set through :attr:`animate`, the value will take a straight path from the
145183 source point to the destination point.
146184
@@ -163,16 +201,12 @@ def construct(self):
163201 self.play(tracker.animate.set_value(tracker.get_value() / (-2 + 3j)))
164202 """
165203
166- def get_value (self ):
167- """Get the current value of this value tracker as a complex number.
168-
169- The value is internally stored as a points array [a, b, 0]. This can be accessed directly
170- to represent the value geometrically, see the usage example.
171- """
204+ def get_value (self ) -> complex : # type: ignore [override]
205+ """Get the current value of this ComplexValueTracker as a complex number."""
172206 return complex (* self .points [0 , :2 ])
173207
174- def set_value (self , z ) :
175- """Sets a new complex value to the ComplexValueTracker"""
176- z = complex (z )
208+ def set_value (self , value : complex | float ) -> Self :
209+ """Sets a new complex value to the ComplexValueTracker. """
210+ z = complex (value )
177211 self .points [0 , :2 ] = (z .real , z .imag )
178212 return self
0 commit comments