diff --git a/testfield/vgl/vgllib.py b/testfield/vgl/vgllib.py index 8d0c579..46b1317 100644 --- a/testfield/vgl/vgllib.py +++ b/testfield/vgl/vgllib.py @@ -4,6 +4,7 @@ import pygame import uuid import time import threading +import math class Graph: @staticmethod @@ -50,26 +51,46 @@ class Graph: class Frame(object): components = dict() components_stat = dict() - thread = None - motion_queue = None + render_thread = None + motion_queue = list() def __init__(self, name: str, size: tuple): self.name = name self.size = size self.surface = pygame.Surface(size, flags=pygame.HWSURFACE) self.is_hide = False print("初始化子模块") - self.thread = threading.Thread(target=self.render) + self.render_thread = threading.Thread(target=self.render) + print("启动图形渲染子线程") + self.render_thread.start() - def move(self, subname, direction, duration, effect="linear"): # our powerful move! + def move(self, subname, direction, length, duration = 0, effect="linear"): # our powerful move! # direction: 使用角度制, 以直角笛卡尔坐标系的x正半轴方向为0度, 逆时针为加, 接受负数 + # length: 百分数 # duration: "动画"时间, 为0则即时 # effect: "动画"效果, linear为线性移动, 或许会在未来增加贝塞尔曲线 # TODO: 增加 bezier 曲线 - pass + if duration == 0: + self.components[subname]["pos"][0] += math.cos(math.radians(direction)) * length / 100 * self.size[0] + self.components[subname]["pos"][1] += math.sin(math.radians(direction)) * length / 100 * self.size[1] + return + self.motion_queue.append({"subname":subname, "direction":direction, "length":length, "start":round(time.time(), 1), "duration":duration, "effect":"linear"}) def render(self): while 1: + rest = list() + while self.motion_queue: + i = self.motion_queue.pop(0) # 从队列的开头移除元素 + self.move(subname=i['subname'], direction=i['direction'], + length=((time.time() - i['start']) / i['duration']) * i['length'], + duration=0) + if ((time.time() - i['start']) / i['duration']) > 0: + i['start'] = time.time() + i['duration'] -= (time.time() - i['start']) + rest.append(i) self.draw_all() + self.motion_queue = rest + time.sleep(0.1) + def show(self, window, position: tuple): if not self.is_hide: @@ -130,20 +151,39 @@ class Frame(object): # 示例 if __name__ == "__main__": - pygame.init() - window = pygame.display.set_mode((800, 600)) - frame = Frame("Test", (800, 600)) - input() - frame.load() - running = True - while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False + frame = None + def grap(): + global frame + pygame.init() + window = pygame.display.set_mode((1200, 900)) + frame = Frame("Test", (1200, 900)) + #frame.load() + frame.register(subname="test", attr={'method': 'rect', 'pos': (33, 33), 'size': (50, 50), 'color': (255, 255, 255)}) + #frame.move(subname='test', direction=0, length=90, duration=1, effect="linear") + running = True + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False - window.fill((0, 0, 0)) - frame.draw_all() - frame.show(window, (0, 0)) - pygame.display.flip() - pygame.time.delay(10) - pygame.quit() + window.fill((0, 0, 0)) + frame.show(window, (0, 0)) + pygame.display.flip() + pygame.time.delay(10) + pygame.quit() + def debug(): + global frame + while 1: + try: + e = input(">>>") + if e == "f": + e = "frame.move(subname='test', direction=0, length=3, duration=1, effect='linear')" + if e == "g": + e = "frame.move(subname='test', direction=180, length=3, duration=1, effect='linear')" + exec(e) + except: + print("ER") + grap_thd = threading.Thread(target=grap) + debug_thd = threading.Thread(target=debug) + grap_thd.start() + debug_thd.start() \ No newline at end of file