minor VGL improvements

This commit is contained in:
2025-04-05 14:21:31 +08:00
parent bce75a1727
commit 890543e3c1
6 changed files with 185 additions and 8 deletions

Binary file not shown.

View File

@@ -23,7 +23,67 @@ class Aux():
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]))
def eventproc(events_dict, event):
"""events = {
"cursor": {
"position": (15,12),
"relative": (13,11),
"poscale": (0.1,0.5),
"relscale": (0.05,0.1),
},
"wheel": 1, # 远离用户为 1, 靠近为 -1, 无动作为 0
"click": [1, 2], # 被按下的按钮
"focus": 1, # 或 0
"key": ["ctrl", "k"]
}"""
if event.type == pygame.MOUSEMOTION:
events_dict.setdefault("cursor", {})["position"] = event.pos
events_dict.setdefault("cursor", {})["relative"] = event.rel
events_dict.setdefault("cursor", {})["poscale"] = (event.pos[0] / screen_width, event.pos[1] / screen_height)
events_dict.setdefault("cursor", {})["relscale"] = (event.rel[0] / screen_width, event.rel[1] / screen_height)
elif event.type == pygame.MOUSEWHEEL:
if "wheel" not in events_dict:
events_dict["wheel"] = 0
events_dict["wheel"] += event.y # 远离用户为正,靠近为负
elif event.type == pygame.MOUSEBUTTONDOWN:
events_dict.setdefault("click", []).append(event.button)
elif event.type == pygame.MOUSEBUTTONUP:
if "click" in events_dict and event.button in events_dict["click"]:
events_dict["click"].remove(event.button)
elif event.type == pygame.WINDOWEVENT and hasattr(pygame, 'WINDOWEVENT_FOCUS_GAINED') and event.event == pygame.WINDOWEVENT_FOCUS_GAINED:
events_dict["focus"] = 1
elif event.type == pygame.WINDOWEVENT and hasattr(pygame, 'WINDOWEVENT_FOCUS_LOST') and event.event == pygame.WINDOWEVENT_FOCUS_LOST:
events_dict["focus"] = 0
elif event.type == pygame.KEYDOWN:
keys = events_dict.setdefault("key", [])
key_name = pygame.key.name(event.key).lower()
# 简单的修饰键处理,可以根据需要扩展
if key_name in ["left ctrl", "right ctrl"]:
if "ctrl" not in keys:
keys.append("ctrl")
elif key_name in ["left shift", "right shift"]:
if "shift" not in keys:
keys.append("shift")
elif key_name in ["left alt", "right alt"]:
if "alt" not in keys:
keys.append("alt")
elif len(key_name) == 1: # 处理字母和数字
keys.append(key_name)
elif event.type == pygame.KEYUP:
if "key" in events_dict:
key_name = pygame.key.name(event.key).lower()
if key_name in ["left ctrl", "right ctrl"] and "ctrl" in events_dict["key"]:
events_dict["key"].remove("ctrl")
elif key_name in ["left shift", "right shift"] and "shift" in events_dict["key"]:
events_dict["key"].remove("shift")
elif key_name in ["left alt", "right alt"] and "alt" in events_dict["key"]:
events_dict["key"].remove("alt")
elif len(key_name) == 1 and key_name in events_dict["key"]:
events_dict["key"].remove(key_name)
else:
print("不重要事件")
return 0
return 1
class Element(object):
is_hide = False
is_template = True
@@ -81,6 +141,18 @@ class Frame(object):
class Window(object):
frames = dict()
events = {
"cursor": {
"position": (15,12),
"relative": (13,11),
"poscale": (0.1,0.5),
"relscale": (0.05,0.1),
},
"wheel": 1, # 远离用户为 1, 靠近为 -1, 无动作为 0
"click": [1, 2], # 被按下的按钮
"focus": 1, # 或 0
"key": ["ctrl", "k"]
}
def __init__(self, title="Vector Graphic Layer Window", size: tuple=(1024,768)):
self.title = title
self.size = size
@@ -101,8 +173,19 @@ class Window(object):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = 0
if_matters = Aux.eventproc(self.events, event.type)
if if_matters:
pass # 消息式调用
for i in self.frames.values():
i.render()
pygame.display.flip()
pygame.time.delay(10)
pygame.quit()
def observer(self):
def decorator(func):
def wrapper(*args, **kwargs):
print("注册观察者")
result = func(info=self.event, *args, **kwargs) # 调用原始函数
return result
return wrapper
return decorator

View File

@@ -1 +0,0 @@
vgl