Skip to content

Commit 117feed

Browse files
committed
examples/main.py: another tidyup pass.
1 parent e827739 commit 117feed

File tree

1 file changed

+27
-44
lines changed

1 file changed

+27
-44
lines changed

examples/main.py

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ class Application:
9999

100100
maximum_scale = 1.6
101101
minimum_scale = 0.6
102+
count = 0
102103

103104
def __init__(self, w, h, file):
104-
global icon_count
105-
self.index = icon_count
106-
icon_count += 1
105+
self.index = Application.count
106+
Application.count += 1
107107

108108
self.selected = False
109109
self.icon = Polygon()
@@ -164,7 +164,7 @@ def touched(self, touch):
164164
return touch.x > x and touch.x < x + w and touch.y > y and touch.y < y + h
165165

166166
def update(self, move_angle):
167-
angle_per_icon = 2 * math.pi / icon_count
167+
angle_per_icon = 2 * math.pi / Application.count
168168
self.angle = angle_per_icon * self.index + move_angle
169169

170170
self.angle %= 2 * math.pi
@@ -250,27 +250,12 @@ def launch(self):
250250
machine.reset()
251251

252252

253-
icon_count = 0
253+
icons = [
254+
Application(60, 60, file) for file in os.listdir()
255+
if file.endswith(".py") and file not in ("main.py", "secrets.py")]
254256

255-
256-
files = [
257-
file
258-
for file in os.listdir()
259-
if file.endswith(".py") and file not in ("main.py", "secrets.py")
260-
]
261-
w = 60
262-
h = 60
263-
icons = [Application(w, h, x) for x in files]
264-
265-
num_icons = len(icons)
266-
267-
sx, sy = 0, 0
268-
ex, ey = 0, 0
269-
st, et = None, None
270-
touch_ms = 0
271-
touch_dist = 0
272-
touch_speed = 0
273-
touch_dir = 0
257+
touch_start_x = 0
258+
touch_start_time = None
274259
tap = False
275260

276261
move_angle = 0
@@ -299,48 +284,46 @@ def launch(self):
299284

300285
touch.poll()
301286

302-
if touch.state and st is None:
303-
st = time.ticks_ms()
304-
sx, sy = touch.x, touch.y
287+
if touch.state and touch_start_time is None:
288+
touch_start_time = time.ticks_ms()
289+
touch_start_x = touch.x
305290
tap = True
306291

307292
elif touch.state:
308-
ex, ey = touch.x, touch.y
309-
310-
et = time.ticks_ms()
311-
touch_ms = et - st # et phone home
312-
# get the x distance between the touch start and current touch
313-
touch_dist = abs(sx - ex)
314-
# Get the touch direction bounded to -1, 0, +1, for easy multiply later
315-
touch_dir = max(min(1, sx - ex), -1)
293+
# Get the duration of the touch in milliseconds
294+
touch_ms = time.ticks_ms() - touch_start_time
295+
296+
# Get the x distance between the touch start and current touch
297+
touch_dist = touch_start_x - touch.x
298+
316299
# Calculate the touch speed, speed = distance / time
317300
touch_speed = touch_dist / touch_ms
318-
touch_speed *= 2
319301

320-
# Any movement at all should cancel our tap action
321-
if touch_dist > 4:
302+
# Any movement should cancel our tap action
303+
if abs(touch_dist) > 4:
322304
tap = False
323305

324306
# If a touch is under this minimal distance it counts as a "stop spinning, darn it"
325-
if touch_dist > 10:
307+
if abs(touch_dist) > 10:
326308
# Apply the touch speed with the direction multiplier from above
327-
move -= math.radians(touch_speed * touch_dir)
328-
if touch_speed > 1:
309+
move -= math.radians(touch_speed)
310+
if abs(touch_speed) > 0.5:
329311
friction = 0.98 # Normal friction ( the closer this is to 1 the longer it will take to slow down )
330312
else:
331313
friction = 0.8
314+
332315
else:
333316
# Pick the one you like best
334317
# move = 0 # Stop abruptly
335318
friction = 0.7 # Apply a braking friction
336319
else:
337-
st = None
320+
touch_start_time = None
338321

339322
move_angle += move # Apply the movement distance, this is in degrees and needs finagled to follow your finger
340-
move_angle %= 2 * math.pi # Wrap to 0-359
323+
move_angle %= 2 * math.pi # Wrap at 360 degrees (in radians)
341324
move *= friction # Apply friction, this will slowly decrease "move" when there's no touch, to slow the spin down
342325

343-
# Pre-calculate the scales and angles
326+
# Pre-calculate the scales and angles for sorting.
344327
for icon in icons:
345328
icon.update(move_angle)
346329

0 commit comments

Comments
 (0)