minor improvements

This commit is contained in:
david-ajax
2025-03-23 00:05:53 +08:00
parent b86ffd9f5b
commit 84372e6dd8
2 changed files with 81 additions and 15 deletions

View File

@@ -2,6 +2,8 @@
# vgllib.py
import pygame
import uuid
import time
import threading
class Graph:
@staticmethod
@@ -48,12 +50,26 @@ class Graph:
class Frame(object):
components = dict()
components_stat = dict()
thread = None
motion_queue = None
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)
def move(self, subname, direction, duration, effect="linear"): # our powerful move!
# direction: 使用角度制, 以直角笛卡尔坐标系的x正半轴方向为0度, 逆时针为加, 接受负数
# duration: "动画"时间, 为0则即时
# effect: "动画"效果, linear为线性移动, 或许会在未来增加贝塞尔曲线
# TODO: 增加 bezier 曲线
pass
def render(self):
while 1:
self.draw_all()
def show(self, window, position: tuple):
if not self.is_hide:
@@ -68,28 +84,48 @@ class Frame(object):
def register(self, subname="", attr=None):
if subname == "":
subname = uuid.uuid4()
# use percent of frame size instead of pixels :)
attr['pos'] = list(attr['pos'])
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[0])
attr['pos'][1] = round(attr['pos'][1] / 100 * self.size[1])
attr['size'] = list(attr['size'])
attr['size'][0] = round(attr['size'][0] / 100 * self.size[0])
attr['size'][1] = round(attr['size'][1] / 100 * self.size[1])
self.components[subname] = attr
self.components_stat[subname] = 1 # by default, not hiding
def draw(self, attr):
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[1])
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[1])
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[1])
attr['pos'][0] = round(attr['pos'][0] / 100 * self.size[1])
Graph.call(self, **attr)
def set_component_visible(self, subname, newstat):
self.components_stat[subname] = newstat
def drawall(self, attr):
def draw_all(self):
for i in self.components.keys():
if self.components_stat[i]:
self.draw(self.components[i])
def clear(self, color=(0,0,0)):
def refresh(self, color=(0,0,0)):
self.surface.fill(color)
def loads(self, ):
def clear(self):
components = dict()
components_stat = dict()
def loads(self, grap_str):
# TODO: 将会重写 以替代不安全的 eval
self.register(subname="", attr=(eval(grap_str)))
def load(self, file="default.vgld", mode="a"):
# a: 增量加载 (默认)
# w: 覆盖式加载
# 文件扩展名: vgld (矢量图形层描述文件)
if mode == 'w':
self.clear()
with open(file=file, mode="r+", encoding="UTF-8") as f:
for i in f.readlines():
#print(i)
self.loads(i)
# 示例
@@ -97,18 +133,17 @@ if __name__ == "__main__":
pygame.init()
window = pygame.display.set_mode((800, 600))
frame = Frame("Test", (800, 600))
frame.draw({'method':'rect', 'pos':(50, 50), 'size':(200, 100), 'color':(255, 0, 0)}) # 绘制红色矩形
# 主循环
input()
frame.load()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
window.fill((0, 0, 0)) # 清空窗口
frame.show(window, (0, 0)) # 显示帧
pygame.display.flip() # 更新显示
window.fill((0, 0, 0))
frame.draw_all()
frame.show(window, (0, 0))
pygame.display.flip()
pygame.time.delay(10)
pygame.quit()