major improvements

This commit is contained in:
2025-04-04 22:19:27 +08:00
parent 551ed9a4ce
commit bce75a1727
17 changed files with 158 additions and 208 deletions

1
vgl/README.md Normal file
View File

@@ -0,0 +1 @@
# VGL - 矢量图形渲染库

7
vgl/__init__.py Normal file
View File

@@ -0,0 +1,7 @@
import os
os.environ["PYGAME_HIDE_SUPPORT_PROMPT"] = "True"
from .main import *
from . import basic_elements as elements
version = '0.1.0'
print(f"Powered by Vector Graphic Layer, version {version}")

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

93
vgl/basic_elements.py Normal file
View File

@@ -0,0 +1,93 @@
from vgl.main import *
import pygame
import time
class Line(Element):
def __init__(self, ends = [(0.1, 0.1), (0.2, 0.2)], color = "#ffffff"):
self.color = color
self.ends = ends
def move(self, delta, duration):
self.add_task(group = "move", start_time = Aux.gettime(), duration = duration, delta = delta)
def update(self):
for i in self.tasks.get("move", []):
self.teleport(Aux.tuple_scale(tup = i['delta'], mul = Aux.getprogress(i)))
def render(self):
pygame.draw.aaline(self.attached_frame.surface, self.color, Aux.pixelizer(self.ends[0], self.attached_window), Aux.pixelizer(self.ends[1], self.attached_window))
class Rect(Element):
def __init__(self, nw_posc=(0.1, 0.1), se_posc=(0.2,0.2), color = "#114514", width = 0): # non-0 for "frame"
self.color = color
self.width = width
self.rect = (nw_posc[0], nw_posc[1], se_posc[0], se_posc[1])
def move(self, delta, duration):
self.add_task(group = "move", start_time = Aux.gettime(), duration = duration, delta = delta)
def update(self):
for i in self.tasks.get("move", []):
self.teleport(Aux.tuple_scale(tup = i['delta'], mul = Aux.getprogress(i)))
def render(self):
pygame.draw.rect(surface=self.attached_frame.surface, color=self.color, rect=Aux.pixelizer(self.rect, self.attached_window), width=self.width)
class Graph:
@staticmethod
def rect(frame, pos: tuple = (0, 0), size: tuple = (1, 1), color: tuple = (255, 255, 255), width: int = 0):
pygame.draw.rect(frame.surface, color=color, rect=(pos[0], pos[1], size[0], size[1]), width=width)
@staticmethod
def line(frame, start_pos: tuple, end_pos: tuple, color: tuple = (255, 255, 255)):
pygame.draw.aaline(frame.surface, color, start_pos, end_pos)
@staticmethod
def circle(frame, center: tuple, radius: int, color: tuple = (255, 255, 255), width: int = 0):
pygame.draw.circle(frame.surface, color, center, radius, width)
@staticmethod
def ellipse(frame, pos: tuple = (0, 0), size: tuple = (1, 1), color: tuple = (255, 255, 255), width: int = 0):
rect=(pos[0], pos[1], size[0], size[1])
pygame.draw.ellipse(frame.surface, color, rect, width)
@staticmethod
def polygon(frame, pointlist: list, color: tuple = (255, 255, 255), width: int = 0):
pygame.draw.polygon(frame.surface, color, pointlist, width)
@staticmethod
def arc(frame, pos: tuple = (0, 0), size: tuple = (1, 1), color: tuple = (255, 255, 255), start_angle: float = 0, stop_angle: float = 3.14, width: int = 1):
rect=(pos[0], pos[1], size[0], size[1])
pygame.draw.arc(frame.surface, color, rect, start_angle, stop_angle, width)
@staticmethod
def point(frame, pos: tuple, color: tuple = (255, 255, 255)):
pygame.draw.point(frame.surface, color, pos)
@staticmethod
def lines(frame, pointlist: list, color: tuple = (255, 255, 255)):
pygame.draw.aalines(frame.surface, color, closed=False, points=pointlist)
@staticmethod
def call(frame, method, **kwargs):
if hasattr(Graph, method):
getattr(Graph, method)(frame, **kwargs)
else:
print(f"方法 {method} 不存在")

108
vgl/main.py Normal file
View File

@@ -0,0 +1,108 @@
# 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()

28
vgl/test.py Normal file
View File

@@ -0,0 +1,28 @@
import pygame
import sys
# 初始化 Pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((800, 600))
# 设置颜色 (RGB)
background_color = (255, 255, 255) # 白色背景
line_color = (255, 0, 0) # 红色线条
# 主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# 填充背景
screen.fill(background_color)
# 绘制抗锯齿线条
pygame.draw.aaline(screen, line_color, (100, 100), (700, 500))
# 更新显示
pygame.display.flip()

1
vgl/vgl Symbolic link
View File

@@ -0,0 +1 @@
vgl