File: animations.py

package info (click to toggle)
pytermgui 7.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,888 kB
  • sloc: python: 12,931; makefile: 40; sh: 37
file content (56 lines) | stat: -rw-r--r-- 1,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import time
import pytermgui as ptg
from dataclasses import dataclass


@dataclass
class MyAnimatedObject:
    attr: int

    def __str__(self):
        return f"({self.attr}, {id(self)})"


looped = ptg.animator.animate_float(duration=100, loop=True)
backward = ptg.animator.animate_float(
    duration=100,
    direction=-1,
    on_finish=lambda anim: {
        setattr(anim, "_remaining", anim.duration),
        ptg.animator.schedule(anim),
    },
)
forward = ptg.animator.animate_float(
    duration=100,
    direction=1,
    on_finish=lambda anim: {
        setattr(anim, "_remaining", anim.duration),
        ptg.animator.schedule(anim),
    },
)

obj = MyAnimatedObject(0)
obj2 = MyAnimatedObject(1000)

attr_forward = ptg.animator.animate_attr(
    target=obj, attr="attr", end=1000, duration=1000
)

attr_backward = ptg.animator.animate_attr(
    target=obj2, attr="attr", end=0, duration=1000
)

counter = 0
while True:
    if counter >= 1.0:
        counter = 0

    print(f"\x1b[KLOOPED:     {looped.direction} {looped.state!r}")
    print(f"\x1b[KBACKWARD:   {backward.direction} {backward.state!r}")
    print(f"\x1b[KBACKWARD:   {forward.direction} {forward.state!r}")
    print(f"\x1b[KATTR_FORE:  {attr_forward.direction} {attr_forward.state} {obj}")
    print(f"\x1b[KATTR_BACK:  {attr_backward.direction} {attr_backward.state} {obj2}")
    print(f"\x1b[6A")

    ptg.animator.step(0.001)
    time.sleep(0.001)