From 97aca12945a48784b0e5fa3fce051b18015820bb Mon Sep 17 00:00:00 2001 From: iggy <iggy@muc.ccc.de> Date: Mon, 5 Jun 2023 04:13:50 +0200 Subject: [PATCH] ui: flow3r icons to celebrate the naming --- python_payload/demo_menu.py | 2 +- python_payload/menu.py | 8 ++++-- python_payload/ui.py | 55 ++++++++++++++++++------------------- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/python_payload/demo_menu.py b/python_payload/demo_menu.py index b1de86295a..133b0fff52 100644 --- a/python_payload/demo_menu.py +++ b/python_payload/demo_menu.py @@ -33,7 +33,7 @@ testmenu.add(item_foo) testmenu.add(item_sub) testmenu.add(item_add) -menu_main = menu.Menu("main",has_back=False) +menu_main = menu.Menu("flow3r",has_back=False) menu_main.add(menu.MenuItemSubmenu(testmenu)) menu_main.add(menu.MenuItemSubmenu(menu_demo)) menu_main.add(menu.MenuItemSubmenu(menu_settings.get_menu())) diff --git a/python_payload/menu.py b/python_payload/menu.py index 885bb9f5de..1125267c41 100644 --- a/python_payload/menu.py +++ b/python_payload/menu.py @@ -13,8 +13,9 @@ class Menu(): self.items=[] self.__index = 0 self.ui = ui.GroupRing(r=80) - self.ui.element_center = ui.Text(self.name) - self.icon = ui.Icon(label=name) + self.icon = ui.IconFlower(label=name,size=80) + self.ui.element_center = self.icon + self.angle = 0 self.angle_step= 0.2 if has_back: @@ -47,6 +48,7 @@ class Menu(): def rotate_to(self, angle): self.angle = angle%(math.pi*2) self.ui.angle_offset = self.angle + self.icon.phi_offset = self.angle def rotate_steps(self, steps=1): self.rotate_by(self.angle_step*steps) @@ -90,7 +92,7 @@ class MenuItem(): def __init__(self,name="item",action=None): self.name= name self.action= action - self.ui = ui.Icon(label=name) + self.ui = ui.IconFlower(label=name) def __repr__(self): return "item: {} (action: {})".format(self.name,"?") diff --git a/python_payload/ui.py b/python_payload/ui.py index b3d3bebb62..cd1c451c53 100644 --- a/python_payload/ui.py +++ b/python_payload/ui.py @@ -22,12 +22,10 @@ PUSH_RED = (251/255,72/255,196/255) the_ctx = hardware.get_ctx() # Utility functions -def xy_from_polar(r,deg): - #rad = deg/180*math.pi - +def xy_from_polar(r,phi): return ( - r * math.sin(deg), #x - r * math.cos(deg) #y + r * math.sin(phi), #x + r * math.cos(phi) #y ) #def ctx_circle(self, x,y, radius, arc_from = -math.pi, arc_to = math.pi): @@ -84,6 +82,7 @@ class Text(UIElement): def _draw(self, pos): self.ctx.text_align = self.ctx.CENTER self.ctx.text_baseline = self.ctx.MIDDLE + self.ctx.font_size = 30 self.ctx.rgb(1,1,1).move_to(pos[0],pos[1]).text(self.s) class Icon(UIElement): @@ -101,6 +100,7 @@ class Icon(UIElement): self.ctx.text_align = self.ctx.CENTER self.ctx.text_baseline = self.ctx.MIDDLE + self.ctx.font_size = 30 if self.has_highlight: hs = self.size+5; @@ -118,11 +118,31 @@ class Icon(UIElement): class IconFlower(Icon): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - self.petal_size=size/2 - self.petal_count=5 + self.petal_count= random.randint(4,8) + self.petal_color = (random.random(),random.random(),random.random()) + self.phi_offset = random.random() + self.size_offset = random.randint(0,20) def _draw(self,pos): (x,y)=pos + petal_size=2.3*self.size/self.petal_count+self.size_offset + self.ctx.font_size=self.size/3 + + hs = 3 + + for i in range(self.petal_count): + phi = math.pi*2 / self.petal_count * i + self.phi_offset + r = self.size/2 + (x_,y_) = xy_from_polar(r, phi) + if self.has_highlight: + self.ctx.move_to(x+x_,y+y_).rgb(1,1,1).arc(x+x_,y+y_, petal_size/2+hs,-math.pi,math.pi,True).fill() + self.ctx.move_to(x+x_,y+y_).rgb(*self.petal_color).arc(x+x_,y+y_, petal_size/2,-math.pi,math.pi,True).fill() + + #if self.has_highlight: + # self.ctx.rgb(1,1,1).arc(x,y, self.size/2+hs,-math.pi,math.pi,True).fill() + self.ctx.move_to(x,y).rgb(*self.bg).arc(x,y,self.size/2,-math.pi,math.pi,True).fill() + + self.ctx.rgb(*GO_GREEN).move_to(x,y).text(self.label) @@ -170,24 +190,3 @@ class GroupRing(UIElement): def _draw(self,pos): if self.element_center: self.element_center._draw(pos) - -def test(): - group = UIElement((10,0)) - group.add(UIElement((10,10))) - group.add(UIElement((20,20))) - #group.draw() - - ring = GroupRing(r=80) - ctx = hardware.get_ctx() - for i in range(12): - ring.add(Icon(str(i))) - hardware.display_update() - while True: - ctx.rectangle(0,0,240,240).rgb(1,1,1).fill() - ring.draw() - hardware.display_update() - ring.angle_offset+=2*math.pi/60 - time.sleep(0.01) - hardware.display_update() - -#test() -- GitLab