241 lines
5.3 KiB
Python
Raw Normal View History

import weakref
2023-02-09 23:17:40 +08:00
2023-02-25 15:30:50 +08:00
ALIGN = None
GPU = None
2023-02-25 15:33:19 +08:00
def set_GPU(gpu):
2023-02-25 15:30:50 +08:00
global GPU
global ALIGN
2023-02-25 15:33:19 +08:00
GPU = gpu
2023-02-25 15:30:50 +08:00
ALIGN = GPU.ALIGN
class Widget:
backend = None
width = 0
height = 0
pos = None
parent = None
align = None
text = None
isroot = False
2023-02-23 00:36:08 +08:00
needbuild = True
_label = None
_child = []
def __init__(self,
text=None,
width=100,
height=100,
pos=None,
2023-02-24 00:57:46 +08:00
align=ALIGN.TOP_MID,
onclick=None):
self.width = width
self.height = height
self.pos = pos
self.align = align
self.text = text
2023-02-24 00:57:46 +08:00
self.onclick = onclick
def build(self):
pass
2023-02-24 00:57:46 +08:00
def _set_perent(self, parent):
2023-02-23 21:43:40 +08:00
# use weakref to avoid circular reference
self.parent = weakref.ref(parent)
def update(self):
2023-02-23 00:36:08 +08:00
if self.needbuild:
built = self.build()
if not built is None:
self = self.add(built)
self.needbuild = False
if self.parent is None:
print('self.parent is None')
return
if self.parent.backend is None:
print('self.parent.backend is None')
return
if self.backend is None:
2023-02-24 00:57:46 +08:00
self.backend = self._create_backend(self.parent)
if not self.isroot:
2023-02-25 15:30:50 +08:00
self._update_backend()
for c in self._child:
c.update()
2023-02-24 00:57:46 +08:00
def _create_backend(self, parent: "Widget"):
2023-02-25 15:30:50 +08:00
return GPU.widget(parent.backend)
def _update_backend(self):
self._update_align(self.align)
self._update_attr(self.width, self.height, self.pos)
self._update_text(self.text)
self._update_event()
def _update_align(self, align):
self.backend.align(align, 0, 0)
2023-02-24 00:57:46 +08:00
def _update_attr(self,
width,
height,
pos):
self.backend.set_width(width)
self.backend.set_height(height)
if not pos is None:
self.backend.set_pos(pos[0], pos[1])
2023-02-24 00:57:46 +08:00
def _update_text(self, text):
if not None is text:
2023-02-25 15:30:50 +08:00
self._label = GPU.label(self.backend)
self._label.set_text(self.text)
2023-02-25 15:30:50 +08:00
self._label.align(GPU.ALIGN.CENTER, 0, 0)
2023-02-24 00:57:46 +08:00
def _update_event(self):
if not None is self.onclick:
2023-02-25 15:30:50 +08:00
self.backend.add_event_cb(self.onclick, GPU.EVENT.CLICKED, 0)
2023-02-24 00:57:46 +08:00
def add(self, *child):
for c in child:
2023-02-25 15:30:50 +08:00
if type(c) == list:
for cc in c:
cc._set_perent(self)
self._child.append(cc)
else:
c._set_perent(self)
self._child.append(c)
return self
2023-02-23 00:36:08 +08:00
def clean(self):
if self.backend is None:
return
if self.needbuild:
return
2023-02-25 10:35:19 +08:00
for c in self._child:
c.clean()
2023-02-25 11:16:53 +08:00
if not self.isroot:
self.backend.del_()
2023-02-23 00:36:08 +08:00
self.needbuild = True
2023-02-23 21:43:40 +08:00
self._child = []
2023-02-23 00:36:08 +08:00
2023-02-25 15:30:50 +08:00
class Container(Widget):
def _create_backend(self, parent: Widget):
return GPU.container(parent.backend)
class Button(Widget):
2023-02-24 00:57:46 +08:00
def _create_backend(self, parent: Widget):
2023-02-25 15:30:50 +08:00
return GPU.btn(parent.backend)
class Text(Widget):
2023-02-24 00:57:46 +08:00
def _create_backend(self, parent: Widget):
2023-02-25 15:30:50 +08:00
return GPU.label(parent.backend)
2023-02-24 00:57:46 +08:00
def _update_text(self, text):
self.backend.set_text(text)
2023-02-25 15:30:50 +08:00
class Page(Widget):
def __init__(self):
super().__init__()
self._set_perent(self)
self.isroot = True
self.backend = GPU.screen()
class PageManager:
2023-02-23 00:36:08 +08:00
pageThis: Page = None
pageList = []
2023-02-24 00:57:46 +08:00
pageNeedEnter = None
pageNeedBack = False
def enter(self, page: Page):
2023-02-24 00:57:46 +08:00
if not self.pageNeedEnter is None:
return
self.pageNeedEnter = page
def back(self):
2023-02-24 00:57:46 +08:00
if self.pageNeedBack:
return
2023-02-24 00:57:46 +08:00
self.pageNeedBack = True
2023-02-25 10:35:19 +08:00
2023-02-24 00:57:46 +08:00
def handle_timer(self):
if not self.pageNeedEnter is None:
page = self.pageNeedEnter
self.clean()
self.pageThis = page
self.pageList.append(page)
self.update()
self.pageNeedEnter = None
2023-02-25 10:35:19 +08:00
2023-02-24 00:57:46 +08:00
if self.pageNeedBack:
if len(self.pageList) <= 1:
return
self.clean()
_ = self.pageList.pop()
self.pageThis = self.pageList[-1]
self.update()
self.pageNeedBack = False
def update(self):
if self.pageThis is None:
return
self.pageThis.update()
def clean(self):
2023-02-23 00:36:08 +08:00
if self.pageThis is None:
return
self.pageThis.clean()
class _App:
pageManager = PageManager()
2023-02-24 00:57:46 +08:00
timer = None
2023-02-25 10:35:19 +08:00
2023-02-24 00:57:46 +08:00
def cb_timer(self, src):
self.pageManager.handle_timer()
def __init__(self):
2023-02-25 15:30:50 +08:00
self.timer = GPU.timer()
2023-02-24 00:57:46 +08:00
self.timer.set_period(50)
self.timer.set_cb(self.cb_timer)
def update(self):
self.pageManager.update()
2023-02-25 10:35:19 +08:00
2023-02-24 00:57:46 +08:00
def show(self, page: Page):
self.pageManager.enter(page)
2023-02-22 21:59:28 +08:00
2023-02-09 23:17:40 +08:00
try:
2023-02-25 15:30:50 +08:00
import PikaUI_GPU_lvgl
set_GPU(PikaUI_GPU_lvgl)
2023-02-25 15:32:01 +08:00
print('PikaUI: Using lvgl GPU')
except:
2023-02-09 23:17:40 +08:00
pass
try:
2023-02-25 15:30:50 +08:00
import PikaUI_GPU_text
set_GPU(PikaUI_GPU_text)
2023-02-25 15:32:01 +08:00
print('PikaUI: Using text GPU')
except:
2023-02-09 23:17:40 +08:00
pass
2023-02-23 21:43:40 +08:00
2023-02-24 00:57:46 +08:00
app = _App()
def App():
return app
2023-02-25 11:16:53 +08:00
2023-02-25 15:30:50 +08:00
2023-02-25 11:16:53 +08:00
def cleanup():
app.timer._del()