Last active
March 8, 2024 21:48
-
-
Save baby-bird/548daad7a26c60755b4d965603a4db23 to your computer and use it in GitHub Desktop.
Time_Timer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A simple Time Timer app made of ShapeNodes. | |
<Change Line 120 for seconds or minutes counting> | |
<Uncomment Line 126 and fill in your shortcut name to | |
automatically run other shortcut you defined | |
after timer stopped> | |
Odd touch attemps will start the timer | |
Even touch attemps will stop the timer | |
Stops automatically when the set times passed | |
''' | |
from scene import * | |
from math import pi, sin, cos, radians, degrees, atan2 | |
from datetime import datetime | |
from time import time | |
import sound, shortcuts | |
def draw_arc(scene, start, finish, offset = -90): | |
radius = min(scene.center) * 0.9 * 0.84 | |
start = radians(start + offset) | |
finish = radians(finish + offset ) | |
x = radius * cos(start) | |
y = radius * sin(start) | |
s = ui.Path() | |
s.move_to(0,0) | |
s.line_to(0,y) | |
s.add_arc(0, 0, radius, start, finish) | |
s.line_to(0,0) | |
return s | |
def calc_ang(ang): | |
ang = ang + 360 | |
alt = radians(ang + 180) | |
if 0 < ang <=90: | |
x = 0 | |
y = 0 | |
elif ang == 0: | |
x = 0.5 | |
y = 0.5 | |
elif 90 < ang <= 180: | |
x = 0 | |
y = cos(alt)/(1+cos(alt)) | |
elif 180 < ang < 270: | |
x = sin(alt)/(1+sin(alt)) | |
y = 0.5 | |
else: | |
x = 0.5 | |
y = 0.5 | |
anglst = [x, y] | |
return anglst | |
class Clock (Scene): | |
def setup(self): | |
self.first_touched = 0 | |
self.background_color ='white' | |
self.center = Point(self.size.w/2, self.size.h/2) | |
r = min(self.size)/2*0.9 | |
rect = ui.Path.oval(0, 0, min(self.size), min(self.size)) | |
self.face = ShapeNode(rect) | |
self.add_child(self.face) | |
'''Display Time Text and Ticker Symbol''' | |
for i in range(12): | |
label = LabelNode(str((i+1)*5), font=('HelveticaNeue-UltraLight', 0.2*r)) | |
label.color = 'black' | |
a = 2 * pi * (i+1)/12.0 | |
label.position = sin(a)*(r), cos(a)*(r) | |
self.add_child(label) | |
rectSign = ShapeNode(ui.Path.rounded_rect(0,0,r*0.1, 5, 4), 'black',position=(sin(a)*(r*0.84), cos(a)*(r*0.84))) | |
rectSign.rotation = -(a + pi/2) | |
self.add_child(rectSign) | |
for i in range(60): | |
a = 2 * pi * (i+1)/60 | |
rectSign_small = ShapeNode(ui.Path.rounded_rect(0,0,r*0.05, 0.3, 4), 'black',position=(sin(a)*(r*0.84), cos(a)*(r*0.84))) | |
rectSign_small.rotation = -(a + pi/2) | |
self.add_child(rectSign_small) | |
'''Display Time Elapsed Circle Segment''' | |
start = 0 | |
finish = 0 - 360 | |
circle_seg = ShapeNode(draw_arc(self,start,finish)) | |
circle_seg.fill_color = 'red' | |
pos = calc_ang(finish) | |
circle_seg.anchor_point = (pos[0], pos[1]) | |
self.face.add_child(circle_seg) | |
self.hands = [] | |
shape = ShapeNode(ui.Path.rounded_rect(0, 0, 4, r*0.9, 4), 'white') | |
shape.stroke_color = 'black' | |
shape.anchor_point = (0.5, 0) | |
self.hands.append(shape) | |
self.add_child(shape) | |
self.hands.append(circle_seg) | |
self.init_circle = circle_seg | |
self.add_child(ShapeNode(ui.Path.oval(0,0, 30, 30), 'black')) | |
self.did_change_size() | |
def touch_began(self, touch): | |
# Calculate vector between centre point and touched point | |
dx = touch.location.x - self.size.w/2 | |
dy = touch.location.y - self.size.h/2 | |
ang = atan2(dx,dy) | |
if ang < 0: | |
ang = ang + 2*pi | |
self.ang = ang | |
self.original_ang = ang | |
self.first_touched += 1 | |
self.start_time = time() | |
def did_change_size(self): | |
self.position = self.size/2 | |
def update(self): | |
tick = 2 * pi / 60.0 | |
if self.first_touched % 2 == 1: | |
elapsed = time() - self.start_time | |
minutes = elapsed / 60 | |
seconds = elapsed % 60.0 | |
angle = self.ang - tick * seconds # You can either select minutes or seconds here | |
# Stop the timer | |
if degrees(angle) < 0.1: | |
self.first_touched += 1 | |
sound.play_effect('game:Ding_3',0.1,0.9) | |
self.view.close() | |
#shortcuts.open_shortcuts_app(name="YOUR_SHORTCUT_NAME") | |
self.hands[0].rotation = -angle | |
self.hands[1].remove_from_parent() | |
'''Update Time Elapsed Circle Segment''' | |
start = 0 | |
finish = degrees(angle) - 360 | |
circle_seg = ShapeNode(draw_arc(self,start,finish)) | |
circle_seg.fill_color = 'red' | |
pos = calc_ang(finish) | |
circle_seg.anchor_point = (pos[0], pos[1]) | |
self.face.add_child(circle_seg) | |
self.hands[1] = circle_seg | |
else: | |
self.hands[0].rotation = 0 | |
self.hands[1].remove_from_parent() | |
self.face.add_child(self.init_circle) | |
self.hands[1] = self.init_circle | |
run(Clock()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment