11"""Implements an entity which transitions a value from one to another."""
2- import math
2+ from typing import Callable , Dict , Iterator , Optional , Union
33from string import ascii_lowercase
4- from typing import Callable , Dict , Optional
4+ import math
55
6- from srctools import conv_float , Output
6+ from srctools import Entity , conv_float , Output
77from srctools .fgd import EntityDef
88from srctools .logger import get_logger
9+ from srctools .vmf import conv_kv
910
1011from hammeraddons .bsp_transform import trans , Context
1112
13+
1214LOGGER = get_logger (__name__ )
1315
1416halfpi = math .pi / 2.0
1719EPSILON = 1e-6
1820
1921BRIGHT_LETTERS = {
20- let : i / 25
22+ let : i / 25
2123 for i , let in enumerate (ascii_lowercase )
2224}
2325
@@ -43,6 +45,7 @@ def lerp(x: float, in_min: float, in_max: float, out_min: float, out_max: float)
4345@trans ('comp_numeric_transition' )
4446def numeric_transition (ctx : Context ) -> None :
4547 """When triggered, animates a keyvalue/input over time with various options."""
48+ ent : Entity
4649 for ent in ctx .vmf .by_class ['comp_numeric_transition' ]:
4750 ent ['classname' ] = 'logic_relay'
4851
@@ -59,7 +62,8 @@ def numeric_transition(ctx: Context) -> None:
5962
6063 # Special case - if the transform type is "light", allow parsing these
6164 # as A-Z values.
62- value_start = value_end = None # type: Optional[float]
65+ value_start : Optional [float ] = None
66+ value_end : Optional [float ] = None
6367 if transform_type == 'light' :
6468 value_start = BRIGHT_LETTERS .get (ent ['startval' ].lower (), None )
6569 value_end = BRIGHT_LETTERS .get (ent ['endval' ].lower (), None )
@@ -128,6 +132,7 @@ def numeric_transition(ctx: Context) -> None:
128132 ease_end_func = ease_func_linear
129133
130134 def compute_point (x : float ) -> float :
135+ """Apply the easing equations to compute the value."""
131136 pos = x * ease_start_func (x ) + (1.0 - x ) * ease_end_func (x )
132137 if pos < 0.0 :
133138 pos = 0.0
@@ -141,6 +146,7 @@ def compute_point(x: float) -> float:
141146 point_count = 1
142147 points = [i / point_count for i in range (int (point_count ))]
143148
149+ result : Iterator [Union [str , float ]]
144150 if transform_type == 'speed' :
145151 # Compute the speed from x to x+1
146152 result = (
@@ -185,7 +191,7 @@ def compute_point(x: float) -> float:
185191 param = '{} {}' .format (input_name , point )
186192 else : # input
187193 io_input = input_name
188- param = format (point )
194+ param = conv_kv (point )
189195
190196 if param != last_inp :
191197 ent .add_out (Output (
@@ -232,18 +238,18 @@ def ease_func_sine_end(x: float) -> float:
232238 return 1.0 - math .cos (x * halfpi )
233239
234240
235- EASE_START_FUNC = {
241+ EASE_START_FUNC : Dict [ str , Callable [[ float ], float ]] = {
236242 'linear' : ease_func_linear ,
237243 'quad' : ease_func_power_start (2 ),
238244 'cubic' : ease_func_power_start (3 ),
239245 'quartic' : ease_func_power_start (4 ),
240246 'sine' : ease_func_sine_start ,
241- } # type: Dict[str, Callable[[float], float]]
247+ }
242248
243- EASE_END_FUNC = {
249+ EASE_END_FUNC : Dict [ str , Callable [[ float ], float ]] = {
244250 'linear' : ease_func_linear ,
245251 'quad' : ease_func_power_end (2 ),
246252 'cubic' : ease_func_power_end (3 ),
247253 'quartic' : ease_func_power_end (4 ),
248254 'sine' : ease_func_sine_end ,
249- } # type: Dict[str, Callable[[float], float]]
255+ }
0 commit comments