Made add_background_rectangle are more general method for all mobjects

This commit is contained in:
Grant Sanderson 2018-05-07 13:32:47 -07:00
parent c2a67346f9
commit 4403399050
6 changed files with 36 additions and 34 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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):

View file

@ -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 = {

View file

@ -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):

View file

@ -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")