2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.constants import *
|
2021-01-18 08:20:14 -10:00
|
|
|
from manimlib.mobject.svg.tex_mobject import SingleStringTex
|
2021-01-13 00:24:40 -10:00
|
|
|
from manimlib.mobject.svg.tex_mobject import tex_string_to_mob_map
|
|
|
|
from manimlib.mobject.svg.tex_mobject import SCALE_FACTOR_PER_FONT_POINT
|
2018-12-24 12:37:51 -08:00
|
|
|
from manimlib.mobject.types.vectorized_mobject import VMobject
|
2018-03-31 18:49:28 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 18:49:28 -07:00
|
|
|
class DecimalNumber(VMobject):
|
|
|
|
CONFIG = {
|
2020-06-05 17:57:44 -07:00
|
|
|
"stroke_width": 0,
|
|
|
|
"fill_opacity": 1.0,
|
2018-05-09 18:21:25 +02:00
|
|
|
"num_decimal_places": 2,
|
2018-06-26 14:39:47 -07:00
|
|
|
"include_sign": False,
|
|
|
|
"group_with_commas": True,
|
2021-01-03 12:03:39 -08:00
|
|
|
"digit_buff_per_font_unit": 0.001,
|
2018-04-06 13:58:59 -07:00
|
|
|
"show_ellipsis": False,
|
|
|
|
"unit": None, # Aligned to bottom unless it starts with "^"
|
|
|
|
"include_background_rectangle": False,
|
2018-08-27 16:32:32 -07:00
|
|
|
"edge_to_fix": LEFT,
|
2020-12-17 15:59:02 -08:00
|
|
|
"font_size": 48,
|
2018-03-31 18:49:28 -07:00
|
|
|
}
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2019-01-29 14:22:23 -08:00
|
|
|
def __init__(self, number=0, **kwargs):
|
2019-02-10 10:56:24 -08:00
|
|
|
super().__init__(**kwargs)
|
2021-01-13 00:12:08 -10:00
|
|
|
self.set_submobjects_from_number(number)
|
|
|
|
self.init_colors()
|
|
|
|
|
|
|
|
def set_submobjects_from_number(self, number):
|
2018-03-31 18:49:28 -07:00
|
|
|
self.number = number
|
2021-01-13 00:12:08 -10:00
|
|
|
self.set_submobjects([])
|
2018-03-31 18:49:28 -07:00
|
|
|
|
2021-01-13 11:11:25 -10:00
|
|
|
num_string = self.get_num_string(number)
|
2020-12-17 15:59:02 -08:00
|
|
|
self.add(*map(self.string_to_mob, num_string))
|
2018-03-31 18:49:28 -07:00
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
# Add non-numerical bits
|
2018-03-31 18:49:28 -07:00
|
|
|
if self.show_ellipsis:
|
2020-12-17 15:59:02 -08:00
|
|
|
self.add(self.string_to_mob("\\dots"))
|
2018-03-31 18:49:28 -07:00
|
|
|
|
|
|
|
if num_string.startswith("-"):
|
|
|
|
minus = self.submobjects[0]
|
|
|
|
minus.next_to(
|
|
|
|
self.submobjects[1], LEFT,
|
2021-01-12 13:08:24 -10:00
|
|
|
buff=self.digit_buff_per_font_unit * self.get_font_size(),
|
2018-03-31 18:49:28 -07:00
|
|
|
)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
if self.unit is not None:
|
2020-12-17 15:59:02 -08:00
|
|
|
self.unit_sign = self.string_to_mob(self.unit)
|
2018-03-31 18:49:28 -07:00
|
|
|
self.add(self.unit_sign)
|
|
|
|
|
2019-02-04 14:54:25 -08:00
|
|
|
self.arrange(
|
2021-01-12 13:08:24 -10:00
|
|
|
buff=self.digit_buff_per_font_unit * self.get_font_size(),
|
2018-04-06 13:58:59 -07:00
|
|
|
aligned_edge=DOWN
|
2018-03-31 18:49:28 -07:00
|
|
|
)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
# Handle alignment of parts that should be aligned
|
|
|
|
# to the bottom
|
2018-03-31 18:49:28 -07:00
|
|
|
for i, c in enumerate(num_string):
|
2018-04-06 13:58:59 -07:00
|
|
|
if c == "-" and len(num_string) > i + 1:
|
2019-03-08 14:39:47 -06:00
|
|
|
self[i].align_to(self[i + 1], UP)
|
2020-02-18 22:32:02 -08:00
|
|
|
self[i].shift(self[i + 1].get_height() * DOWN / 2)
|
2018-06-26 14:39:47 -07:00
|
|
|
elif c == ",":
|
|
|
|
self[i].shift(self[i].get_height() * DOWN / 2)
|
2018-03-31 18:49:28 -07:00
|
|
|
if self.unit and self.unit.startswith("^"):
|
|
|
|
self.unit_sign.align_to(self, UP)
|
2020-06-05 17:57:44 -07:00
|
|
|
|
2018-03-31 18:49:28 -07:00
|
|
|
if self.include_background_rectangle:
|
|
|
|
self.add_background_rectangle()
|
|
|
|
|
2021-01-13 11:11:25 -10:00
|
|
|
def get_num_string(self, number):
|
|
|
|
if isinstance(number, complex):
|
|
|
|
formatter = self.get_complex_formatter()
|
|
|
|
else:
|
|
|
|
formatter = self.get_formatter()
|
|
|
|
num_string = formatter.format(number)
|
|
|
|
|
|
|
|
rounded_num = np.round(number, self.num_decimal_places)
|
|
|
|
if num_string.startswith("-") and rounded_num == 0:
|
|
|
|
if self.include_sign:
|
|
|
|
num_string = "+" + num_string[1:]
|
|
|
|
else:
|
|
|
|
num_string = num_string[1:]
|
|
|
|
return num_string
|
|
|
|
|
2021-01-12 13:08:24 -10:00
|
|
|
def init_data(self):
|
|
|
|
super().init_data()
|
|
|
|
self.data["font_size"] = np.array([self.font_size], dtype=float)
|
|
|
|
|
|
|
|
def get_font_size(self):
|
|
|
|
return self.data["font_size"][0]
|
|
|
|
|
2020-12-17 15:59:02 -08:00
|
|
|
def string_to_mob(self, tex_string):
|
2021-01-18 08:20:14 -10:00
|
|
|
# Could just call SingleStringTex, and there is
|
2021-01-13 00:24:40 -10:00
|
|
|
# some code repetition here by looking to the same cache,
|
2021-01-13 11:11:25 -10:00
|
|
|
# but it keeps things from initializing a new object
|
2021-01-13 00:24:40 -10:00
|
|
|
# more than is necessary
|
|
|
|
if tex_string in tex_string_to_mob_map:
|
|
|
|
result = tex_string_to_mob_map[tex_string].copy()
|
|
|
|
result.scale(self.get_font_size() * SCALE_FACTOR_PER_FONT_POINT)
|
|
|
|
return result
|
|
|
|
else:
|
2021-01-18 08:20:14 -10:00
|
|
|
return SingleStringTex(tex_string, font_size=self.get_font_size())
|
2020-12-17 15:59:02 -08:00
|
|
|
|
2018-06-26 14:39:47 -07:00
|
|
|
def get_formatter(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Configuration is based first off instance attributes,
|
|
|
|
but overwritten by any kew word argument. Relevant
|
|
|
|
key words:
|
|
|
|
- include_sign
|
|
|
|
- group_with_commas
|
|
|
|
- num_decimal_places
|
|
|
|
- field_name (e.g. 0 or 0.real)
|
|
|
|
"""
|
2019-02-10 10:56:24 -08:00
|
|
|
config = dict([
|
|
|
|
(attr, getattr(self, attr))
|
|
|
|
for attr in [
|
|
|
|
"include_sign",
|
|
|
|
"group_with_commas",
|
|
|
|
"num_decimal_places",
|
|
|
|
]
|
|
|
|
])
|
2018-06-26 14:39:47 -07:00
|
|
|
config.update(kwargs)
|
2018-08-12 19:22:13 -07:00
|
|
|
return "".join([
|
2018-06-26 14:39:47 -07:00
|
|
|
"{",
|
|
|
|
config.get("field_name", ""),
|
|
|
|
":",
|
|
|
|
"+" if config["include_sign"] else "",
|
|
|
|
"," if config["group_with_commas"] else "",
|
|
|
|
".", str(config["num_decimal_places"]), "f",
|
|
|
|
"}",
|
|
|
|
])
|
|
|
|
|
|
|
|
def get_complex_formatter(self, **kwargs):
|
2018-08-12 19:22:13 -07:00
|
|
|
return "".join([
|
|
|
|
self.get_formatter(field_name="0.real"),
|
|
|
|
self.get_formatter(field_name="0.imag", include_sign=True),
|
|
|
|
"i"
|
|
|
|
])
|
2018-06-26 14:39:47 -07:00
|
|
|
|
2021-01-13 00:12:08 -10:00
|
|
|
def set_value(self, number):
|
|
|
|
move_to_point = self.get_edge_center(self.edge_to_fix)
|
|
|
|
style = self.get_style()
|
|
|
|
self.set_submobjects_from_number(number)
|
|
|
|
self.move_to(move_to_point, self.edge_to_fix)
|
|
|
|
self.set_style(**style)
|
2018-08-30 14:24:40 -07:00
|
|
|
return self
|
2018-08-12 19:05:31 -07:00
|
|
|
|
2021-01-03 11:44:53 -08:00
|
|
|
def scale(self, scale_factor, **kwargs):
|
|
|
|
super().scale(scale_factor, **kwargs)
|
2021-01-12 13:08:24 -10:00
|
|
|
self.data["font_size"] *= scale_factor
|
|
|
|
return self
|
2021-01-03 11:44:53 -08:00
|
|
|
|
2018-08-12 19:05:31 -07:00
|
|
|
def get_value(self):
|
|
|
|
return self.number
|
|
|
|
|
2019-04-06 14:01:04 -07:00
|
|
|
def increment_value(self, delta_t=1):
|
|
|
|
self.set_value(self.get_value() + delta_t)
|
|
|
|
|
2018-04-06 13:58:59 -07:00
|
|
|
|
2018-03-31 18:49:28 -07:00
|
|
|
class Integer(DecimalNumber):
|
|
|
|
CONFIG = {
|
2018-05-09 18:21:25 +02:00
|
|
|
"num_decimal_places": 0,
|
2018-03-31 18:49:28 -07:00
|
|
|
}
|
2019-01-04 14:14:15 -08:00
|
|
|
|
2019-01-17 14:12:14 -08:00
|
|
|
def get_value(self):
|
|
|
|
return int(np.round(super().get_value()))
|