Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1361a2b
Add the Variable class with an example
nilaybhatia Aug 30, 2020
d1367ff
Remove getter for tracker
nilaybhatia Aug 30, 2020
58df231
Add the num_decimal_places argument
nilaybhatia Aug 31, 2020
d3c02c4
Add support for choosing the class for variable i.e DecimalNumber or …
nilaybhatia Aug 31, 2020
f789bbc
Switch to inheriting from VMobject instead of VDict
nilaybhatia Aug 31, 2020
6ff3f03
Add blank line
nilaybhatia Aug 31, 2020
41e4aec
Change TexMobject to TextMobject
nilaybhatia Sep 1, 2020
2661c7b
Add docstrings
nilaybhatia Sep 2, 2020
1650c28
Merge branch 'master' of https://github.com/ManimCommunity/manim into…
nilaybhatia Sep 2, 2020
6aa21ad
Tempfix for underscores
nilaybhatia Sep 2, 2020
2e4c830
Better description for the Variable class
nilaybhatia Sep 2, 2020
64456fb
Remove ` or to the CONFIG` from docstring.
nilaybhatia Sep 2, 2020
f80a232
Merge branch 'master' of https://github.com/ManimCommunity/manim into…
nilaybhatia Sep 2, 2020
944d343
Merge branch 'variable-class' of https://github.com/nilaybhatia/manim…
nilaybhatia Sep 2, 2020
3e051e8
Automatically detect math/text mode
nilaybhatia Sep 2, 2020
9f0d4bf
Allow passing a Tex, MathTex, Text as label
nilaybhatia Sep 2, 2020
094de9e
Add support for generic label
nilaybhatia Sep 2, 2020
99d14ae
Merge branch 'master' of https://github.com/ManimCommunity/manim into…
nilaybhatia Sep 2, 2020
5b698d7
Remove comment about old_projects folder
nilaybhatia Sep 3, 2020
991ff1c
Remove extra MathTex
nilaybhatia Sep 3, 2020
0d51f3d
Shorten a block of if-else
nilaybhatia Sep 3, 2020
2d81080
Update changelog with Variable class
nilaybhatia Sep 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Mobjects, Scenes, and Animations
#. Most :code:`get_` and :code:`set_` methods have been removed in favor of instance attributes and properties
#. The :code:`Container` class has been made into an AbstractBaseClass, i.e. in cannot be instantiated. Instead, use one of its children classes
#. The ``TextMobject`` and ``TexMobject`` objects have been deprecated, due to their confusing names, in favour of ``Tex`` and ``MathTex``. You can still, however, continue to use ``TextMobject`` and ``TexMobject``, albeit with Deprecation Warnings constantly reminding you to switch.
#. Add a :code:`Variable` class for displaying text that continuously updates to reflect the value of a python variable.



Expand Down
42 changes: 41 additions & 1 deletion example_scenes/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,44 @@ def construct(self):
self.wait()


# See old_projects folder for many, many more
class VariableExample(Scene):
def construct(self):
var = 0.5
on_screen_var = Variable(var, Text("var"), num_decimal_places=3)

# You can also change the colours for the label and value
on_screen_var.label.set_color(RED)
on_screen_var.value.set_color(GREEN)

self.play(Write(on_screen_var))
# The above line will just display the variable with
# its initial value on the screen. If you also wish to
# update it, you can do so by accessing the `tracker` attribute
self.wait()
var_tracker = on_screen_var.tracker
var = 10.5
self.play(var_tracker.set_value, var)
self.wait()

int_var = 0
on_screen_int_var = Variable(int_var, Text("int_var"), var_type=Integer).next_to(
on_screen_var, DOWN
)
on_screen_int_var.label.set_color(RED)
on_screen_int_var.value.set_color(GREEN)

self.play(Write(on_screen_int_var))
self.wait()
var_tracker = on_screen_int_var.tracker
var = 10.5
self.play(var_tracker.set_value, var)
self.wait()

# If you wish to have a somewhat more complicated label for your
# variable with subscripts, superscripts, etc. the default class
# for the label is MathTex
subscript_label_var = 10
on_screen_subscript_var = Variable(subscript_label_var, "{a}_{i}").next_to(on_screen_int_var, DOWN)
self.play(Write(on_screen_subscript_var))
self.wait()

76 changes: 73 additions & 3 deletions manim/mobject/numbers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
__all__ = ["DecimalNumber", "Integer"]
__all__ = ["DecimalNumber", "Integer", "Variable"]


from ..constants import *
from ..mobject.svg.tex_mobject import SingleStringMathTex
from ..mobject.types.vectorized_mobject import VMobject
from ..mobject.svg.tex_mobject import MathTex, SingleStringMathTex, Tex
from ..mobject.svg.text_mobject import Text
from ..mobject.types.vectorized_mobject import VDict, VMobject
from ..mobject.value_tracker import ValueTracker


class DecimalNumber(VMobject):
Expand Down Expand Up @@ -143,3 +145,71 @@ class Integer(DecimalNumber):

def get_value(self):
return int(np.round(super().get_value()))


class Variable(VMobject):
"""A class for displaying text that continuously updates to reflect the value of a python variable.

Automatically adds the text for the label and the value when instantiated and added to the screen.

Parameters
----------
var : Union[:class:`int`, :class:`float`]
The python variable you need to keep track of and display.
label : Union[:class:`str`, :class:`~.Tex`, :class:`~.MathTex`, :class:`Text`]
The label for your variable, for example `x = ...`. To use math mode, for e.g.
subscripts, superscripts, etc. simply pass in a raw string.
var_type : Union[:class:`DecimalNumber`, :class:`Integer`], optional
The class used for displaying the number. Defaults to :class:`DecimalNumber`.
num_decimal_places : :class:`int`, optional
The number of decimal places to display in your variable. Defaults to 2.
If `var_type` is an :class:`Integer`, this parameter is ignored.
kwargs : Any
Other arguments to be passed to `~.Mobject`.

Attributes
----------
label : Union[:class:`str`, :class:`~.Tex`, :class:`~.MathTex`, :class:`Text`]
The label for your variable, for example `x = ...`.
tracker : :class:`~.ValueTracker`
Useful in updating the value of your variable on-screen.
value : Union[:class:`DecimalNumber`, :class:`Integer`]
The tex for the value of your variable.

Examples
--------
Normal usage::
# DecimalNumber type
var = 0.5
on_screen_var = Variable(var, "var", num_decimal_places=3)
# Integer type
int_var = 0
on_screen_int_var = Variable(int_var, "int_var", var_type=Integer)
# Using math mode for the label
on_screen_int_var = Variable(int_var, "${a}_{i}$", var_type=Integer)

"""

def __init__(
self, var, label, var_type=DecimalNumber, num_decimal_places=2, **kwargs
):

self.label = MathTex(label) if isinstance(label,str) else label
equals = MathTex("=").next_to(self.label, RIGHT)
self.label.add(equals)

self.tracker = ValueTracker(var)

if var_type == DecimalNumber:
self.value = DecimalNumber(
self.tracker.get_value(), num_decimal_places=num_decimal_places
)
elif var_type == Integer:
self.value = Integer(self.tracker.get_value())

self.value.add_updater(lambda v: v.set_value(self.tracker.get_value())).next_to(
self.label, RIGHT
)

VMobject.__init__(self, **kwargs)
self.add(self.label, self.value)