Created
August 24, 2016 17:25
-
-
Save Phuket2/a2dfa4cda3cb9b13262dd998f39e086a to your computer and use it in GitHub Desktop.
tester.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
import editor | |
import math | |
import ui | |
import circular_slider 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 | |
def create_lb_list(N = 12, *args, **kwargs): | |
def apply_kwargs(obj, **kwargs): | |
for k, v in kwargs.items(): | |
if hasattr(obj, k): | |
setattr(obj, k, v) | |
lb_list = [] | |
for i in range(0, N): | |
lb = ui.Label(name = str(i)) | |
lb.text = str(i) | |
lb.font=('Arial Rounded MT Bold', 16) | |
lb.alignment = ui.ALIGN_CENTER | |
apply_kwargs(lb, **kwargs) | |
lb_list.append(lb) | |
return lb_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 = 0 | |
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 | |
self.set_needs_display() | |
def draw(self): | |
draw_arc(self.r, 0, self.degree, | |
ln_color = css_clr_to_rgba('lime', 5), ln_width = 30, offset = -90) | |
knob(self.r, self.degree) | |
def knob(r, degree = 0): | |
inset_v = 35 | |
with ui.GState(): | |
ui.set_color('deeppink') | |
obj_r = ui.Rect(*r).inset(inset_v, inset_v) | |
ui.set_shadow('orange', 5, 5, 8) | |
s = ui.Path.oval(*obj_r) | |
s.fill() | |
ui.set_shadow('black', 5, 5, 8) | |
dot_r = ui.Rect(*obj_r).inset(30, 30) | |
draw_arc(dot_r, degree, degree, | |
ln_color = css_clr_to_rgba('yellow', 5), ln_width = 30) | |
if __name__ == '__main__': | |
_use_theme = True | |
w=h = 500 | |
f = (0, 0, w, h) | |
style = 'sheet' | |
margin = 40 | |
N = 12 | |
ow = 20 | |
#obj_list = create_btn_list(N = N, bg_color = 'lime') | |
obj_list = create_lb_list(N = N, tint_color = 'orange') | |
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