From 20028cef46b326d24f974ee05fc2162cc917f81a Mon Sep 17 00:00:00 2001 From: Uwe Zimmermann Date: Sat, 4 Nov 2023 15:07:24 +0100 Subject: [PATCH] feat: DecimalNumber() - added spacing between values and unit (#3366) * feat: DecimalNumber() - added spacing between values and unit * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl * Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl --------- Co-authored-by: Tristan Schulz Co-authored-by: Benjamin Hackl --- manim/mobject/text/numbers.py | 51 ++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py index f5eff7fada..10bf2e7829 100644 --- a/manim/mobject/text/numbers.py +++ b/manim/mobject/text/numbers.py @@ -22,6 +22,34 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL): """An mobject representing a decimal number. + Parameters + ---------- + number + The numeric value to be displayed. It can later be modified using :meth:`.set_value`. + num_decimal_places + The number of decimal places after the decimal separator. Values are automatically rounded. + mob_class + The class for rendering digits and units, by default :class:`.MathTex`. + include_sign + Set to ``True`` to include a sign for positive numbers and zero. + group_with_commas + When ``True`` thousands groups are separated by commas for readability. + digit_buff_per_font_unit + Additional spacing between digits. Scales with font size. + show_ellipsis + When a number has been truncated by rounding, indicate with an ellipsis (``...``). + unit + A unit string which can be placed to the right of the numerical values. + unit_buff_per_font_unit + An additional spacing between the numerical values and the unit. A value + of ``unit_buff_per_font_unit=0.003`` gives a decent spacing. Scales with font size. + include_background_rectangle + Adds a background rectangle to increase contrast on busy scenes. + edge_to_fix + Assuring right- or left-alignment of the full object. + font_size + Size of the font. + Examples -------- @@ -34,6 +62,8 @@ def construct(self): show_ellipsis=True, num_decimal_places=3, include_sign=True, + unit=r"\text{M-Units}", + unit_buff_per_font_unit=0.003 ) square = Square().to_edge(UP) @@ -59,6 +89,7 @@ def __init__( digit_buff_per_font_unit: float = 0.001, show_ellipsis: bool = False, unit: str | None = None, # Aligned to bottom unless it starts with "^" + unit_buff_per_font_unit: float = 0, include_background_rectangle: bool = False, edge_to_fix: Sequence[float] = LEFT, font_size: float = DEFAULT_FONT_SIZE, @@ -75,6 +106,7 @@ def __init__( self.digit_buff_per_font_unit = digit_buff_per_font_unit self.show_ellipsis = show_ellipsis self.unit = unit + self.unit_buff_per_font_unit = unit_buff_per_font_unit self.include_background_rectangle = include_background_rectangle self.edge_to_fix = edge_to_fix self._font_size = font_size @@ -89,6 +121,7 @@ def __init__( "digit_buff_per_font_unit": digit_buff_per_font_unit, "show_ellipsis": show_ellipsis, "unit": unit, + "unit_buff_per_font_unit": unit_buff_per_font_unit, "include_background_rectangle": include_background_rectangle, "edge_to_fix": edge_to_fix, "font_size": font_size, @@ -130,15 +163,25 @@ def _set_submobjects_from_number(self, number): self._string_to_mob("\\dots", SingleStringMathTex, color=self.color), ) - if self.unit is not None: - self.unit_sign = self._string_to_mob(self.unit, SingleStringMathTex) - self.add(self.unit_sign) - self.arrange( buff=self.digit_buff_per_font_unit * self._font_size, aligned_edge=DOWN, ) + if self.unit is not None: + self.unit_sign = self._string_to_mob(self.unit, SingleStringMathTex) + self.add( + self.unit_sign.next_to( + self, + direction=RIGHT, + buff=(self.unit_buff_per_font_unit + self.digit_buff_per_font_unit) + * self._font_size, + aligned_edge=DOWN, + ) + ) + + self.move_to(ORIGIN) + # Handle alignment of parts that should be aligned # to the bottom for i, c in enumerate(num_string):