From eddf4a4633f7c97a348d68b568c007c90526ebef Mon Sep 17 00:00:00 2001 From: uwezi Date: Sun, 10 Sep 2023 13:51:27 +0200 Subject: [PATCH 1/8] feat: DecimalNumber() - added spacing between values and unit --- 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..64a7ad94b5 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 `.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 `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): From fc7770e5803159b7d9b96102f90288b6e1e35ff7 Mon Sep 17 00:00:00 2001 From: Tristan Schulz Date: Sat, 4 Nov 2023 15:02:21 +0100 Subject: [PATCH 2/8] Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl --- manim/mobject/text/numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py index 64a7ad94b5..3dcc65fd7a 100644 --- a/manim/mobject/text/numbers.py +++ b/manim/mobject/text/numbers.py @@ -25,7 +25,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL): Parameters ---------- number - The numeric value to be displayed. It can later be modified using `.set_value()`. + 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 From 59f3317eafa40b45db5aaac01f5cbd8832b1307a Mon Sep 17 00:00:00 2001 From: Tristan Schulz Date: Sat, 4 Nov 2023 15:02:28 +0100 Subject: [PATCH 3/8] Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl --- manim/mobject/text/numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py index 3dcc65fd7a..8cc7df8368 100644 --- a/manim/mobject/text/numbers.py +++ b/manim/mobject/text/numbers.py @@ -29,7 +29,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL): 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 `MathTex`. + 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 From f1e193f02a26ca63a721d81e71834d8f146dfada Mon Sep 17 00:00:00 2001 From: Tristan Schulz Date: Sat, 4 Nov 2023 15:02:34 +0100 Subject: [PATCH 4/8] Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl --- manim/mobject/text/numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py index 8cc7df8368..6b8ea80909 100644 --- a/manim/mobject/text/numbers.py +++ b/manim/mobject/text/numbers.py @@ -31,7 +31,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL): 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. + 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 From ca4960d98cf423c6e1b9d21ffb2558baa66aefcb Mon Sep 17 00:00:00 2001 From: Tristan Schulz Date: Sat, 4 Nov 2023 15:02:40 +0100 Subject: [PATCH 5/8] Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl --- manim/mobject/text/numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py index 6b8ea80909..30569b4abd 100644 --- a/manim/mobject/text/numbers.py +++ b/manim/mobject/text/numbers.py @@ -33,7 +33,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL): 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. + 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 From cde41a689726c7a00b96389fc253cfdf434e71a8 Mon Sep 17 00:00:00 2001 From: Tristan Schulz Date: Sat, 4 Nov 2023 15:02:47 +0100 Subject: [PATCH 6/8] Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl --- manim/mobject/text/numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py index 30569b4abd..231a8fe674 100644 --- a/manim/mobject/text/numbers.py +++ b/manim/mobject/text/numbers.py @@ -37,7 +37,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL): 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 (...). + 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 From 76cb56785f24e71592748bfffac86302503af02b Mon Sep 17 00:00:00 2001 From: Tristan Schulz Date: Sat, 4 Nov 2023 15:02:53 +0100 Subject: [PATCH 7/8] Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl --- manim/mobject/text/numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py index 231a8fe674..59c016e59c 100644 --- a/manim/mobject/text/numbers.py +++ b/manim/mobject/text/numbers.py @@ -42,7 +42,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL): 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. + 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 From f5339e95a39d984db22df9fa5b5937fb921ea0af Mon Sep 17 00:00:00 2001 From: Tristan Schulz Date: Sat, 4 Nov 2023 15:02:58 +0100 Subject: [PATCH 8/8] Update manim/mobject/text/numbers.py Co-authored-by: Benjamin Hackl --- manim/mobject/text/numbers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manim/mobject/text/numbers.py b/manim/mobject/text/numbers.py index 59c016e59c..10bf2e7829 100644 --- a/manim/mobject/text/numbers.py +++ b/manim/mobject/text/numbers.py @@ -48,7 +48,7 @@ class DecimalNumber(VMobject, metaclass=ConvertToOpenGL): edge_to_fix Assuring right- or left-alignment of the full object. font_size - Size of the font + Size of the font. Examples --------