说明
探索关于UI类的实现,主要是ui文件下container.py
UI 容器container
import math
import os
import image
import gc
try:
from core import agent, system
except ImportError:
from lib.core import agent, system
class container:
class demo:
def load():
pass
def free():
pass
def event():
pass
current, temp, history = demo, None, None
def forever():
if container.temp != None:
container.current, container.temp = container.temp, None
container.current.load()
if container.current != None:
container.current.event()
def reload(app):
if container.current != None:
tmp, container.current = container.current, None
tmp.free()
container.history, container.temp = tmp, app
def latest():
container.reload(container.history)
if __name__ == "__main__":
from ui.ui_canvas import ui
#from ui.ui_taskbar import taskbar
def switch_Launcher(self):
container.reload(Launcher)
self.remove(switch_Launcher)
def switch_Loading(self):
container.reload(Loading)
self.remove(switch_Loading)
class Loading:
h, w = 0, 0
def load():
Loading.h, Loading.w = 20, 20
#system.remove(Loading.event)
system.event(2000, switch_Launcher)
def free():
pass
def event():
pass
@ui.warp_template(ui.blank_draw)
@ui.warp_template(ui.grey_draw)
@ui.warp_template(ui.bg_in_draw)
def draw():
ui.display()
def event():
if Loading.h < 240:
Loading.h += 20
if Loading.w < 240:
Loading.w += 10
ui.height, ui.weight = Loading.h, Loading.w
Loading.draw()
class Launcher:
h, w = 0, 0
def load():
Launcher.h, Launcher.w = 20, 20
system.event(2000, switch_Loading)
def free():
pass
@ui.warp_template(ui.blank_draw)
@ui.warp_template(ui.grey_draw)
@ui.warp_template(ui.help_in_draw)
@ui.warp_template(ui.anime_in_draw)
def draw():
ui.display()
def event():
if Launcher.h < 240:
Launcher.h += 20
if Launcher.w < 240:
Launcher.w += 10
ui.height, ui.weight = Launcher.h, Launcher.w
Launcher.draw()
# Application
import time
last = time.ticks_ms()
container.reload(Loading)
while True:
container.forever()
system.parallel_cycle()
gc.collect()
print('ram total : ' + str(gc.mem_free() / 1024) + ' kb')
container包含三个参数,current / temp / history 当前,临时,历史。在current并创建一个demo对象,对象包含load free event
container包含方法reload,先从这个方法开始看,业务逻辑就是把类似launcher这样带有UI的对象放入container.temp中,并配合下面的forever的方法进行工作。
def reload(app):
if container.current != None:
tmp, container.current = container.current, None
tmp.free()
container.history, container.temp = tmp, app
forever方法
container包含方法有forever,forever逻辑是判断temp有否数据,把temp数据放到current ,然后temp设置为none。紧接这判断
def forever():
if container.temp != None:
container.current, container.temp = container.temp, None
container.current.load()
if container.current != None:
container.current.event()
以main.py的launcher为例,先通过container.reload(),将实例化的launcher放到容器里,也就是reload函数中,reload函数在temp中临时存着。然后在forever函数中,将container.temp中的实例复制给cintainer.current。紧接着将container.current.load(),这里我们看一下launcher.load发生了什么,
还是先创建个分时软定时器,然后在event内加入UI。
在判断container.current不为空时,我们直接调用event,也就是把UI和延时放入msg中去。