109 lines
3.5 KiB
Python
109 lines
3.5 KiB
Python
# Vector Graphics Library
|
|
# vgllib.py
|
|
import pygame
|
|
import uuid
|
|
import time
|
|
import threading
|
|
import copy
|
|
|
|
class Aux():
|
|
def gettime():
|
|
return round(time.time(), 1)
|
|
def getprogress(task):
|
|
return (Aux.gettime() - task["start"] / task["duration"])
|
|
def tuple_scale(tup, mul):
|
|
return (tup[0]*mul, tup[1]*mul)
|
|
def pixelizer(arg, base):
|
|
if not isinstance(base, tuple):
|
|
base = base.size
|
|
if isinstance(arg, int):
|
|
return arg * base
|
|
else:
|
|
if len(arg) == 2:
|
|
return (round(arg[0] * base[0]), round(arg[1] * base[1]))
|
|
if len(arg) == 4:
|
|
return (round(arg[0] * base[0]), round(arg[1] * base[1]), round(arg[2] * base[0]), round(arg[3] * base[1]))
|
|
|
|
class Element(object):
|
|
is_hide = False
|
|
is_template = True
|
|
attached = None
|
|
attached_frame = None
|
|
attached_window = None
|
|
tasks = dict()
|
|
def __init__(self, name="Element"):
|
|
self.name = name
|
|
def attach(self, frame_object, clone_name=""):
|
|
if clone_name == "":
|
|
clone_name = str(uuid.uuid4())
|
|
clone = copy.deepcopy(self)
|
|
clone.is_template = False
|
|
frame_object.elements[clone_name] = clone
|
|
clone.attached_frame = frame_object
|
|
clone.attached_window = frame_object.attached_window
|
|
return clone
|
|
def teleport(self, delta: tuple):
|
|
if not self.is_template:
|
|
pass
|
|
def add_task(self, group, start_time, duration, **kwargs):
|
|
if group not in self.tasks():
|
|
self.tasks[group] = list()
|
|
self.tasks[group].append({"start": start_time, "duration":duration, "para":kwargs})
|
|
def render():
|
|
print("未配置渲染器")
|
|
pass
|
|
def update():
|
|
pass
|
|
|
|
class Frame(object):
|
|
surface = None
|
|
elements = dict()
|
|
is_hide = False
|
|
is_template = True
|
|
attached_window = None
|
|
poscale: tuple = (0, 0) # 左上角, 对于每个实例的位置矢量
|
|
def __init__(self, name="Frame"):
|
|
self.name = name
|
|
def attach(self, window_object, poscale, clone_name=""):
|
|
if clone_name == "":
|
|
clone_name = str(uuid.uuid4())
|
|
clone = copy.deepcopy(self)
|
|
clone.surface = pygame.Surface(window_object.size)
|
|
clone.poscale = poscale
|
|
clone.is_template = False
|
|
clone.attached_window = window_object
|
|
window_object.frames[clone_name] = clone
|
|
return window_object.frames[clone_name]
|
|
def render(self):
|
|
for i in self.elements.values():
|
|
i.render()
|
|
self.attached_window.screen.blit(self.surface, self.poscale)
|
|
|
|
class Window(object):
|
|
frames = dict()
|
|
def __init__(self, title="Vector Graphic Layer Window", size: tuple=(1024,768)):
|
|
self.title = title
|
|
self.size = size
|
|
self.running = 1
|
|
pygame.init()
|
|
self.screen = pygame.display.set_mode(size)
|
|
pygame.display.set_caption(title)
|
|
def set_title(self, title):
|
|
self.title = title
|
|
pygame.display.set_caption(title)
|
|
def start(self):
|
|
self.thr = threading.Thread(target=self.main_loop, name="main_loop")
|
|
self.thr.start()
|
|
def kill(self):
|
|
self.running = 0
|
|
def main_loop(self):
|
|
while self.running:
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
self.running = 0
|
|
for i in self.frames.values():
|
|
i.render()
|
|
pygame.display.flip()
|
|
pygame.time.delay(10)
|
|
pygame.quit()
|