| 1 | import time |
| 2 | import pytermgui as ptg |
| 3 | from dataclasses import dataclass |
| 4 | |
| 5 | |
| 6 | @dataclass |
| 7 | class MyAnimatedObject: |
| 8 | attr: int |
| 9 | |
| 10 | def __str__(self): |
| 11 | return f"({self.attr}, {id(self)})" |
| 12 | |
| 13 | |
| 14 | looped = ptg.animator.animate_float(duration=100, loop=True) |
| 15 | backward = ptg.animator.animate_float( |
| 16 | duration=100, |
| 17 | direction=-1, |
| 18 | on_finish=lambda anim: { |
| 19 | setattr(anim, "_remaining", anim.duration), |
| 20 | ptg.animator.schedule(anim), |
| 21 | }, |
| 22 | ) |
| 23 | forward = ptg.animator.animate_float( |
| 24 | duration=100, |
| 25 | direction=1, |
| 26 | on_finish=lambda anim: { |
| 27 | setattr(anim, "_remaining", anim.duration), |
| 28 | ptg.animator.schedule(anim), |
| 29 | }, |
| 30 | ) |
| 31 | |
| 32 | obj = MyAnimatedObject(0) |
| 33 | obj2 = MyAnimatedObject(1000) |
| 34 | |
| 35 | attr_forward = ptg.animator.animate_attr( |
| 36 | target=obj, attr="attr", end=1000, duration=1000 |
| 37 | ) |
| 38 | |
| 39 | attr_backward = ptg.animator.animate_attr( |
| 40 | target=obj2, attr="attr", end=0, duration=1000 |
| 41 | ) |
| 42 | |
| 43 | counter = 0 |
| 44 | while True: |
| 45 | if counter >= 1.0: |
| 46 | counter = 0 |
| 47 | |
| 48 | print(f"\x1b[KLOOPED: {looped.direction} {looped.state!r}") |
| 49 | print(f"\x1b[KBACKWARD: {backward.direction} {backward.state!r}") |
| 50 | print(f"\x1b[KBACKWARD: {forward.direction} {forward.state!r}") |
| 51 | print(f"\x1b[KATTR_FORE: {attr_forward.direction} {attr_forward.state} {obj}") |
| 52 | print(f"\x1b[KATTR_BACK: {attr_backward.direction} {attr_backward.state} {obj2}") |
| 53 | print(f"\x1b[6A") |
| 54 | |
| 55 | ptg.animator.step(0.001) |
| 56 | time.sleep(0.001) |