Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Made add_background_rectangle are more general method for all mobjects

Grant Sanderson4403399050d6
diff --git a/active_projects/eola2/cramer.py b/active_projects/eola2/cramer.py
index 7dd9d9519..69eb2d0a0 100644
--- a/active_projects/eola2/cramer.py
+++ b/active_projects/eola2/cramer.py
@@ -2154,7 +2154,7 @@ class Thumbnail(TransformingAreasYCoord):
words = TextMobject("Cramer's", "rule")
words.scale_to_fit_width(7)
- # words.add_background_rectangle_to_parts()
+ # words.add_background_rectangle_to_submobjects()
words.add_background_rectangle()
words.to_edge(UP)
self.add(words)
diff --git a/mobject/matrix.py b/mobject/matrix.py
index 924b99173..727f8f4db 100644
--- a/mobject/matrix.py
+++ b/mobject/matrix.py
@@ -116,10 +116,6 @@ class Matrix(VMobject):
self.brackets = VGroup(l_bracket, r_bracket)
return self
- def add_background_rectangle(self):
- self.background_rectangle = BackgroundRectangle(self)
- self.add_to_back(self.background_rectangle)
-
def set_color_columns(self, *colors):
for i, color in enumerate(colors):
VGroup(*self.mob_matrix[:, i]).set_color(color)
diff --git a/mobject/mobject.py b/mobject/mobject.py
index 235ed515c..0e8fa6651 100644
--- a/mobject/mobject.py
+++ b/mobject/mobject.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
import copy
import itertools as it
import numpy as np
@@ -464,6 +466,27 @@ class Mobject(Container):
self.shift(start - self.points[0])
return self
+ # Background rectangle
+ def add_background_rectangle(self, color=BLACK, opacity=0.75, **kwargs):
+ from mobject.shape_matchers import BackgroundRectangle
+ self.background_rectangle = BackgroundRectangle(
+ self, color=color,
+ fill_opacity=opacity,
+ **kwargs
+ )
+ self.add_to_back(self.background_rectangle)
+ return self
+
+ def add_background_rectangle_to_submobjects(self, **kwargs):
+ for submobject in self.submobjects:
+ submobject.add_background_rectangle(**kwargs)
+ return self
+
+ def add_background_rectangle_to_family_members_with_points(self, **kwargs):
+ for mob in self.family_members_with_points():
+ mob.add_background_rectangle(**kwargs)
+ return self
+
# Match other mobvject properties
def match_color(self, mobject):
@@ -643,6 +666,7 @@ class Mobject(Container):
return result
# Pseudonyms for more general get_critical_point method
+
def get_edge_center(self, direction):
return self.get_critical_point(direction)
@@ -719,7 +743,8 @@ class Mobject(Container):
def submobject_family(self):
sub_families = map(Mobject.submobject_family, self.submobjects)
- all_mobjects = [self] + list(it.chain(*sub_families))
+ # all_mobjects = [self] + list(it.chain(*sub_families))
+ all_mobjects = list(it.chain(*sub_families)) + [self]
return remove_list_redundancies(all_mobjects)
def family_members_with_points(self):
diff --git a/mobject/numbers.py b/mobject/numbers.py
index 118289f70..5a6ff6820 100644
--- a/mobject/numbers.py
+++ b/mobject/numbers.py
@@ -3,9 +3,7 @@ from __future__ import absolute_import
from constants import *
from mobject.svg.tex_mobject import TexMobject
-from mobject.types.vectorized_mobject import VGroup
from mobject.types.vectorized_mobject import VMobject
-from mobject.shape_matchers import BackgroundRectangle
class DecimalNumber(VMobject):
@@ -51,7 +49,7 @@ class DecimalNumber(VMobject):
)
if self.unit is not None:
- self.unit_sign = TexMobject(self.unit, color = self.color)
+ self.unit_sign = TexMobject(self.unit, color=self.color)
self.add(self.unit_sign)
self.arrange_submobjects(
@@ -70,16 +68,6 @@ class DecimalNumber(VMobject):
if self.include_background_rectangle:
self.add_background_rectangle()
- def add_background_rectangle(self):
- # TODO, is this the best way to handle
- # background rectangles?
- self.background_rectangle = BackgroundRectangle(self)
- self.submobjects = [
- self.background_rectangle,
- VGroup(*self.submobjects)
- ]
- return self
-
class Integer(DecimalNumber):
CONFIG = {
diff --git a/mobject/svg/tex_mobject.py b/mobject/svg/tex_mobject.py
index 16eebe7f3..9d1b70f1e 100644
--- a/mobject/svg/tex_mobject.py
+++ b/mobject/svg/tex_mobject.py
@@ -116,16 +116,6 @@ class SingleStringTexMobject(SVGMobject):
self.sort_submobjects(lambda p: p[0])
return self
- def add_background_rectangle(self, color=BLACK, opacity=0.75, **kwargs):
- self.background_rectangle = BackgroundRectangle(
- self, color=color,
- fill_opacity=opacity,
- **kwargs
- )
- letters = VMobject(*self.submobjects)
- self.submobjects = [self.background_rectangle, letters]
- return self
-
class TexMobject(SingleStringTexMobject):
CONFIG = {
@@ -232,10 +222,13 @@ class TexMobject(SingleStringTexMobject):
part = self.get_part_by_tex(tex, **kwargs)
return self.index_of_part(part)
- def add_background_rectangle_to_parts(self):
- for part in self:
- part.add_background_rectangle()
- return self
+ def split(self):
+ # Many old scenes assume that when you pass in a single string
+ # to TexMobject, it indexes across the characters.
+ if len(self.submobjects) == 1:
+ return self.submobjects[0].split()
+ else:
+ return super(TexMobject, self).split()
class TextMobject(TexMobject):
diff --git a/old_projects/wallis.py b/old_projects/wallis.py
index be7efe7d2..b2ffb1edd 100644
--- a/old_projects/wallis.py
+++ b/old_projects/wallis.py
@@ -2135,7 +2135,7 @@ class PlugObserverIntoPolynomial(DistanceProductScene):
)
question.scale(text_scale_val)
question.next_to(dot, RIGHT)
- question.add_background_rectangle_to_parts()
+ question.add_background_rectangle_to_submobjects()
f_words = TextMobject("$f$", "of the way")
third_words = TextMobject("$\\frac{1}{3}$", "of the way")