Skip to content

Commit 608f7f7

Browse files
committed
add pausing, improve display, add missing funcs
1 parent 694ae00 commit 608f7f7

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

arcade/examples/easing_functions.py

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from arcade.clock import GLOBAL_CLOCK
1111
from arcade.types.rect import LBWH, Rect
1212

13-
from pyglet.graphics import Batch
13+
from pyglet.graphics import Batch # type: ignore -- Batch isn't in __all__?
1414

1515
WINDOW_WIDTH = 1200
1616
WINDOW_HEIGHT = 800
@@ -25,7 +25,7 @@
2525
# Label all the functions
2626
# There's nine "types", not counting linear, and three of each.
2727

28-
EASING_TYPES = 9
28+
EASING_TYPES = 10
2929
EASING_SUB_TYPES = 3
3030

3131
easing_functions = {
@@ -51,6 +51,9 @@
5151
"Circ": Easing.CIRC,
5252
"Circ In": Easing.CIRC_IN,
5353
"Circ Out": Easing.CIRC_OUT,
54+
"Back": Easing.BACK,
55+
"Back In": Easing.BACK_IN,
56+
"Back Out": Easing.BACK_OUT,
5457
"Elastic": Easing.ELASTIC,
5558
"Elastic In": Easing.ELASTIC_IN,
5659
"Elastic Out": Easing.ELASTIC_OUT,
@@ -60,7 +63,7 @@
6063
}
6164

6265
# We need these for sorting them later; they're in reverse order since we draw bottom-up.
63-
function_names = ["Bounce", "Elastic", "Circ", "Expo", "Quint", "Quart", "Cubic", "Quad", "Sine", "Linear"]
66+
function_names = ["Bounce", "Elastic", "Back", "Circ", "Expo", "Quint", "Quart", "Cubic", "Quad", "Sine", "Linear"]
6467

6568
def px_to_pt(px: int) -> int:
6669
return round(px // (4 / 3))
@@ -75,15 +78,16 @@ def __init__(self):
7578

7679
self.background_color = arcade.color.ARCADE_GREEN
7780
self.time = 0.0
81+
self.paused = False
7882

7983
# "Layouting"
8084
rect = self.window.rect
8185
self.areas: list[Rect] = []
82-
# We want a 9 x 3 grid, but pretending there's an 11 x 3 grid
86+
# We want a 10 x 3 grid, but pretending there's an 12 x 3 grid
8387
# and we're ignoring the first two rows.
8488
buffer = 10 # px
85-
cols = 3
86-
rows = 11
89+
cols = EASING_SUB_TYPES
90+
rows = EASING_TYPES + 2
8791
rect_width = rect.width / cols
8892
rect_height = rect.height / rows
8993

@@ -105,7 +109,11 @@ def __init__(self):
105109

106110
self.random_colors = [arcade.types.Color.random(a = 255) for _ in range(len(self.areas))]
107111

108-
self.title_text = arcade.Text("Easing Functions", buffer, self.window.rect.top - buffer, font_size = px_to_pt(int(rect_height - buffer)), font_name = "Josefin Sans", anchor_y = "top")
112+
self.title_text = arcade.Text("Easing Functions", buffer, self.window.rect.top - buffer, font_size = px_to_pt(int(rect_height - buffer)),
113+
font_name = "Josefin Sans", anchor_y = "top")
114+
self.subtitle_text = arcade.Text("Press [SPACE] to pause/unpause.", self.window.rect.right - buffer, self.window.rect.top - buffer,
115+
font_size = px_to_pt(int(rect_height - buffer) // 2), font_name = "Josefin Sans", color = arcade.color.ARCADE_YELLOW,
116+
anchor_y = "top", anchor_x = "right")
109117

110118
self.labels = []
111119
self.progress_labels = []
@@ -115,9 +123,11 @@ def __init__(self):
115123
for n, a in enumerate(self.areas):
116124
name = self.idx_to_func_name(n)
117125
func = easing_functions[name]
118-
label = arcade.Text(func.__name__, a.left, a.bottom, font_size = px_to_pt(int(a.height / 2 - buffer)), font_name = "Fira Code", batch = self.text_batch)
126+
label = arcade.Text(func.__name__, a.left, a.bottom, font_size = px_to_pt(int(a.height / 2 - buffer)),
127+
font_name = "Fira Code", batch = self.text_batch)
119128
self.labels.append(label)
120-
progress_label = arcade.Text("0.00", a.right, a.bottom, font_size = px_to_pt(int(a.height / 2 - buffer)), font_name = "Josefin Sans", bold = "light", anchor_x = "right", batch = self.text_batch)
129+
progress_label = arcade.Text("0.00", a.right, a.bottom, font_size = px_to_pt(int(a.height / 2 - buffer)),
130+
font_name = "Josefin Sans", bold = "light", anchor_x = "right", batch = self.text_batch)
121131
self.progress_labels.append(progress_label)
122132
sprite = SpriteCircle(int(a.height / 4), arcade.color.ARCADE_YELLOW)
123133
sprite.left = a.left
@@ -138,10 +148,14 @@ def idx_to_func_name(self, i: int) -> str:
138148
return f"{t} Out"
139149

140150
def on_update(self, delta_time):
141-
if int(GLOBAL_CLOCK.time) % 2:
151+
if int(GLOBAL_CLOCK.time) % 4 == 0:
142152
self.time = 1 - (GLOBAL_CLOCK.time % 1)
143-
else:
153+
elif int(GLOBAL_CLOCK.time) % 4 == 1:
154+
self.time = 0
155+
elif int(GLOBAL_CLOCK.time) % 4 == 2:
144156
self.time = GLOBAL_CLOCK.time % 1
157+
else:
158+
self.time = 1
145159

146160
for n, a in enumerate(self.areas):
147161
name = self.idx_to_func_name(n)
@@ -152,7 +166,7 @@ def on_update(self, delta_time):
152166
x = ease(a.left, right, 0, 1, self.time, func)
153167
p = ease(0.0, 1.0, 0, 1, self.time, func)
154168
sprite.left = x
155-
self.progress_labels[n].text = f"{p:.02}"
169+
self.progress_labels[n].text = f"{round(p, 2):.02}"
156170

157171

158172
def on_draw(self):
@@ -161,6 +175,7 @@ def on_draw(self):
161175
"""
162176
self.clear()
163177
self.title_text.draw()
178+
self.subtitle_text.draw()
164179
for n, r in enumerate(self.areas):
165180
arcade.draw_rect_filled(r, arcade.color.BLACK.replace(a = 64))
166181
self.text_batch.draw()
@@ -170,6 +185,12 @@ def on_key_press(self, symbol: int, modifiers: int):
170185
""" Handle key press events """
171186
if symbol == arcade.key.ESCAPE:
172187
self.window.close()
188+
if symbol == arcade.key.SPACE:
189+
self.paused = not self.paused
190+
if self.paused:
191+
GLOBAL_CLOCK.set_tick_speed(0)
192+
else:
193+
GLOBAL_CLOCK.set_tick_speed(1)
173194

174195

175196
def main():

0 commit comments

Comments
 (0)