Skip to content

Instantly share code, notes, and snippets.

/p1.py Secret

Created Aug 24, 2016
Embed
What would you like to do?
p1.py
import editor
import math
import ui
import circularslider as CS
def css_clr_to_rgba(css_name, a):
c = ui.parse_color(css_name)
return (c[0], c[1], c[2], a)
def rects_on_circle_path(rect_path, obj_width,
margin=2, N=12):
def calculate_a_rect(i):
a = 2 * math.pi * i/ N - math.pi/2
# careful: cos,sin! not sin,cos
pos = (math.cos(a)*(radius*1), math.sin(a)*(radius*1))
r1 = ui.Rect(*pos, obj_width, obj_width)
r1.x += r.width / 2 - obj_width / 2 + r.x
r1.y += r.height / 2 - obj_width / 2 + r.y
return r1
r = ui.Rect(*rect_path).inset(obj_width / 2 + margin, obj_width / 2 + margin)
radius = r.width / 2
return r, [calculate_a_rect(i) for i in range(N)]
def draw_arc(rect, start, finish,
ln_color = 'white', ln_width = 15, offset = -90):
# offset = -90 for degress, offset = 0 for radians
r = ui.Rect(*rect)
cy = r.center()[0]
cx = r.center()[1]
s = ui.Path()
radius = r.width / 2
start = math.radians(start + offset)
finish = math.radians(finish + offset )
x = cy+radius * math.cos(start)
y = cx+radius * math.sin(start)
s.move_to(x, y)
s.add_arc(cy, cx, radius, start, finish)
ui.set_color(ln_color)
s.line_width = ln_width
s.line_cap_style = ui.LINE_CAP_ROUND
s.stroke()
def create_btn_list(N = 12, *args, **kwargs):
def btn_action(sender):
print('btn_name:{}'.format(sender.name))
def apply_kwargs(obj, **kwargs):
for k, v in kwargs.items():
if hasattr(obj, k):
setattr(obj, k, v)
btn_list = []
for i in range(0, N):
btn = ui.Button(name = str(i), title = str(i))
btn.action = btn_action
apply_kwargs(btn, **kwargs)
btn_list.append(btn)
return btn_list
class Test(ui.View):
def __init__(self, obj_list, obj_w=32, margin = 2, action =None,
*args, **kwargs):
super().__init__(*args, **kwargs)
self.cs = None
self.r = None
self.obj_list = obj_list
self.obj_w = obj_w
self.margin = margin
self.action = action
self.degree = 90
self.make_view()
def make_view(self):
for obj in self.obj_list:
self.add_subview(obj)
def layout(self):
r , rects = rects_on_circle_path(self.bounds, self.obj_w, margin=self.margin, N=len(self.obj_list))
self.r = r
if not self['CS']:
self.cs = CS.CircularSlider(frame = r, name = 'CS')
self.cs.action = self.cs_action
self.cs.continuous = True
self.cs.alpha = .05
self.add_subview(self.cs)
for i, r in enumerate(rects):
self.obj_list[i].frame = r
def cs_action(self, sender):
self.degree = sender.value*360 - 90#(sender.value / .6) * 360
self.set_needs_display()
def draw(self):
draw_arc(self.r, 0, self.degree,
ln_color = css_clr_to_rgba('deeppink', 5), ln_width = 15, offset = -90)
if __name__ == '__main__':
_use_theme = True
w=h = 500
f = (0, 0, w, h)
style = 'sheet'
margin = 40
N = 12
ow = 10
obj_list = create_btn_list(N = N, bg_color = 'lime')
mc = Test(obj_list, margin = margin, frame=f, bg_color='white', obj_w = ow)
if not _use_theme:
mc.present(style = style, animated=False)
else:
editor.present_themed(mc, theme_name='Oceanic', style=style, animated=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment