minor VGL improvements

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

View File

@ -9,7 +9,6 @@ horizontal_indicator:
ends: [(0.4, 0.5), (0.6, 0.5)] ends: [(0.4, 0.5), (0.6, 0.5)]
color: green color: green
- type: line - type: line
status: static
args: args:
ends: [(0.4, 0.5), (0.6, 0.5)] ends: [(0.4, 0.5), (0.6, 0.5)]
color: green color: green

View File

@ -3,23 +3,33 @@ import time
window = None window = None
def horizontal_indicator(): def horizontal_indicator():
global window global window
frame = vgl.Frame().attach(window, (0, 0), "Horizontal Indicator") frame = vgl.Frame().attach(window_object=window, poscale=(0, 0), clone_name="Horizontal Indicator")
vgl.elements.Line(ends=[(0.4, 0.5), (0.6, 0.5)], color="green").attach(frame) vgl.elements.Line(ends=[(0.4, 0.5), (0.6, 0.5)], color="green").attach(frame_object=frame)
pass pass
def marking_lines():
global window
frame = vgl.Frame().attach(window_object=window, poscale=(0,0), clone_name="Marking Line")
for i in range(0, 24):
vgl.elements.Line(ends=[(0, i/24), (1, i/24)], color="green").attach(frame_object=frame)
for i in range(0, 32):
vgl.elements.Line(ends=[(i/32, 0), (i/32, 1)], color="green").attach(frame_object=frame)
def console(): def console():
print("You've entered Pulsar's command console, an embbedded Python interpreter for debugging & testing") print("You've entered Pulsar's command console, an embbedded Python interpreter for debugging & testing")
while True: while True:
try: try:
exec(input(">>> ")) exec(input(prompt=">>> "))
except: except:
print("An error caused & captured") print("An error caused & captured")
if __name__ == '__main__': if __name__ == '__main__':
print("Welcome to AiraPulsar Client") print("Welcome to AiraPulsar Client")
window = vgl.Window(title="Pulsar", size=(1600, 800)) window = vgl.Window(title="Pulsar", size=(1024, 768))
window.start() window.start()
horizontal_indicator() horizontal_indicator()
marking_lines()
#console() #console()
time.sleep(3) input("任意键以退出")
window.kill() window.kill()
exit() exit()

86
pulsar/t.py Normal file
View File

@ -0,0 +1,86 @@
import pygame
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Pygame All Events Example")
font = pygame.font.Font(None, 30)
event_log = []
running = True
while running:
for event in pygame.event.get():
event_log.insert(0, f"Event: {pygame.event.event_name(event.type)} - {event.__dict__}")
if len(event_log) > 10: # Keep only the last 10 events for display
event_log.pop()
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
print(f"KEYDOWN: Key={pygame.key.name(event.key)}, Modifiers={event.mod}, Unicode='{event.unicode}', Scancode={event.scancode}")
elif event.type == pygame.KEYUP:
print(f"KEYUP: Key={pygame.key.name(event.key)}, Modifiers={event.mod}")
elif event.type == pygame.MOUSEMOTION:
print(f"MOUSEMOTION: Pos={event.pos}, Relative={event.rel}, Buttons={event.buttons}")
elif event.type == pygame.MOUSEBUTTONDOWN:
print(f"MOUSEBUTTONDOWN: Pos={event.pos}, Button={event.button}")
elif event.type == pygame.MOUSEBUTTONUP:
print(f"MOUSEBUTTONUP: Pos={event.pos}, Button={event.button}")
elif hasattr(pygame, 'MOUSEWHEEL') and event.type == pygame.MOUSEWHEEL:
print(f"MOUSEWHEEL:X={event.x}, Y={event.y}, Precise X={event.precise_x}, Precise Y={event.precise_y}, Flipped={event.flipped}")
elif event.type == pygame.ACTIVEEVENT:
print(f"ACTIVEEVENT: Gain={event.gain}, State={event.state}")
elif event.type == pygame.VIDEORESIZE:
print(f"VIDEORESIZE: Size=({event.w}, {event.h})")
screen = pygame.display.set_mode(event.size, pygame.RESIZABLE)
elif event.type == pygame.VIDEOEXPOSE:
print("VIDEOEXPOSE")
elif hasattr(pygame, 'WINDOWEVENT') and event.type == pygame.WINDOWEVENT:
print(f"WINDOWEVENT: Event={pygame.event.event_name(event.event)}, Data1={event.data1}, Data2={event.data2}")
elif pygame.joystick.get_count() > 0:
for i in range(pygame.joystick.get_count()):
joystick = pygame.joystick.Joystick(i)
joystick.init()
if event.type == pygame.JOYAXISMOTION and event.joy == joystick.get_id():
print(f"JOYAXISMOTION (Joystick {joystick.get_id()}): Axis={event.axis}, Value={event.value}")
elif event.type == pygame.JOYBALLMOTION and event.joy == joystick.get_id():
print(f"JOYBALLMOTION (Joystick {joystick.get_id()}): Ball={event.ball}, Relative={event.rel}")
elif event.type == pygame.JOYHATMOTION and event.joy == joystick.get_id():
print(f"JOYHATMOTION (Joystick {joystick.get_id()}): Hat={event.hat}, Value={event.value}")
elif event.type == pygame.JOYBUTTONDOWN and event.joy == joystick.get_id():
print(f"JOYBUTTONDOWN (Joystick {joystick.get_id()}): Button={event.button}")
elif event.type == pygame.JOYBUTTONUP and event.joy == joystick.get_id():
print(f"JOYBUTTONUP (Joystick {joystick.get_id()}): Button={event.button}")
elif event.type >= pygame.USEREVENT:
print(f"USEREVENT: Type={event.type}, Data={event.__dict__}")
elif hasattr(pygame, 'TEXTINPUT') and event.type == pygame.TEXTINPUT:
print(f"TEXTINPUT: Text='{event.text}'")
elif hasattr(pygame, 'TEXTEDITING') and event.type == pygame.TEXTEDITING:
print(f"TEXTEDITING: Text='{event.text}', Start={event.start}, Length={event.length}")
elif hasattr(pygame, 'FINGERDOWN') and event.type == pygame.FINGERDOWN:
print(f"FINGERDOWN: Finger ID={event.finger_id}, Pos={event.pos}, Delta={event.delta}, Pressure={event.pressure}")
elif hasattr(pygame, 'FINGERUP') and event.type == pygame.FINGERUP:
print(f"FINGERUP: Finger ID={event.finger_id}, Pos={event.pos}, Delta={event.delta}, Pressure={event.pressure}")
elif hasattr(pygame, 'FINGERMOTION') and event.type == pygame.FINGERMOTION:
print(f"FINGERMOTION: Finger ID={event.finger_id}, Pos={event.pos}, Delta={event.delta}, Pressure={event.pressure}")
elif hasattr(pygame, 'AUDIODEVICEADDED') and event.type == pygame.AUDIODEVICEADDED:
print(f"AUDIODEVICEADDED: Which={event.which}, Is Capture={event.iscapture}")
elif hasattr(pygame, 'AUDIODEVICEREMOVED') and event.type == pygame.AUDIODEVICEREMOVED:
print(f"AUDIODEVICEREMOVED: Which={event.which}, Is Capture={event.iscapture}")
elif event.type == pygame.SYSWMEVENT:
print(f"SYSWMEVENT: Message={event.msg}, Data1={event.data1}, Data2={event.data2}")
screen.fill((255, 255, 255)) # Fill the screen with white
y_offset = 10
for log_entry in event_log:
text_surface = font.render(log_entry, True, (0, 0, 0))
screen.blit(text_surface, (10, y_offset))
y_offset += 20
pygame.display.flip()
pygame.quit()

Binary file not shown.

View File

@ -23,7 +23,67 @@ class Aux():
return (round(arg[0] * base[0]), round(arg[1] * base[1])) return (round(arg[0] * base[0]), round(arg[1] * base[1]))
if len(arg) == 4: 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])) 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): class Element(object):
is_hide = False is_hide = False
is_template = True is_template = True
@ -81,6 +141,18 @@ class Frame(object):
class Window(object): class Window(object):
frames = dict() 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)): def __init__(self, title="Vector Graphic Layer Window", size: tuple=(1024,768)):
self.title = title self.title = title
self.size = size self.size = size
@ -101,8 +173,19 @@ class Window(object):
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
self.running = 0 self.running = 0
if_matters = Aux.eventproc(self.events, event.type)
if if_matters:
pass # 消息式调用
for i in self.frames.values(): for i in self.frames.values():
i.render() i.render()
pygame.display.flip() pygame.display.flip()
pygame.time.delay(10) pygame.time.delay(10)
pygame.quit() 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