"锚定点" 功能更新

This commit is contained in:
Wang Zhiyu 2025-04-12 22:27:30 +08:00
parent fd67663868
commit 24c1a94f36
9 changed files with 75 additions and 102 deletions

View File

@ -0,0 +1,24 @@
import vgl
name = "Marking Lines"
def main(window):
frame = vgl.Frame().attach(window_object=window, poscale=(0,0), clone_name="Marking Line")
lines = list()
for i in range(0, 24):
lines.append(vgl.elements.Line(ends=[(0, i/24), (1, i/24)], color="green").attach(frame_object=frame))
for i in range(0, 32):
lines.append(vgl.elements.Line(ends=[(i/32, 0), (i/32, 1)], color="green").attach(frame_object=frame))
# We monitor events like this.
@window.observerize
def observer(info):
if info["delta"] == "key":
print(info)
if info["delta"] == "click":
if info["click"]:
print(info["cursor"]["poscale"])
# We make changes like this.
for i in lines:
i.set_color('#' + str((info['cursor']['position'][0] + info['cursor']['position'][1]*100 + info['cursor']['position'][0]*10000) % 999999).zfill(6))
observer()

Binary file not shown.

View File

@ -15,6 +15,9 @@ def main(window):
def observer(info):
if info["delta"] == "key":
print(info)
if info["delta"] == "click":
if info["click"]:
print(info["cursor"]["poscale"])
# We make changes like this.
for i in lines:
i.set_color('#' + str((info['cursor']['position'][0] + info['cursor']['position'][1]*100 + info['cursor']['position'][0]*10000) % 999999).zfill(6))

View File

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

View File

@ -1,22 +1,48 @@
import vgl
import colorama
import os
import importlib.util
def run_components():
window = vgl.Window(title="Pulsar", size=(1024, 768))
def loader():
components_dir = "components"
for filename in os.listdir(components_dir):
if filename.endswith(".py") and filename != "__init__.py":
module_name = filename[:-3] # 去掉 .py 后缀
module_path = os.path.join(components_dir, filename)
for i in os.listdir(components_dir):
if i.endswith(".py") and (not i.startswith("__")):
module_name = i[:-3]
module_path = os.path.join(components_dir, i)
spec = importlib.util.spec_from_file_location(module_name, module_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
try:
if hasattr(module, "main") and callable(module.main):
print(f"[ .... ] Starting {module.name} module")
try:
module.main(window=window)
print(f"[{colorama.Fore.GREEN} OK {colorama.Style.RESET_ALL}] Started {module.name} module")
except Exception as e:
print(f"[{colorama.Fore.RED}FAILED{colorama.Style.RESET_ALL}] Failed to start {module_name} module: {e}")
except:
print(f"Unsupported module: {module_name}.py")
if hasattr(module, "main") and callable(module.main):
print(f"正在调用 {module_name}.main()")
module.main()
else:
print(f"{module_name}.py 中没有找到 main 函数或 main 不是可调用对象。")
def console():
print("You've entered Pulsar's command console, an embbedded Python interpreter for debugging & testing")
while True:
try:
i = input(">>> ")
if i == "q":
return
exec(i)
except:
print("An error occured & captured")
if __name__ == "__main__":
run_components()
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.abspath(__file__)))
print("Tester")
window.start()
loader()
console()
print("Quitting")
window.kill()
exit()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 992 KiB

After

Width:  |  Height:  |  Size: 1.7 MiB

Binary file not shown.

View File

@ -90,9 +90,14 @@ class Aux():
class Anchor(object):
"""
"锚定点" 对象
镜像
@: 矩阵乘法
*: 中心/任意参照点缩放
中点/比例点
"""
poscale = (0, 0)
fixed = 3
poscale = (0, 0) # 位置
fixed = 3 # 小数点后精度
def __init__(self, *args):
if isinstance(args[0], tuple) or isinstance(args[0], Anchor):
if isinstance(args[0], Anchor):
@ -106,6 +111,7 @@ class Anchor(object):
def y(self):
return self.poscale[1]
def __add__(self, other):
"矩阵加法"
if isinstance(other, tuple):
return Anchor((self.poscale[0] + other[0], self.poscale[1] + other[1]))
elif isinstance(other, Anchor):
@ -113,7 +119,7 @@ class Anchor(object):
else:
raise TypeError("不支持的加法操作数类型")
def __mul__(self, other):
"向屏幕中心倍增/倍缩"
"中心倍增/倍缩"
if isinstance(other, tuple):
return Anchor((0.5 + other[0] * (self.poscale[0] - 0.5), 0.5 + other[1] * (self.poscale[1] - 0.5)))
elif isinstance(other, (int, float)):
@ -221,7 +227,7 @@ class Window(object):
"click": [], # 被按下的按钮
"focus": 1, # 或 0
"key": [],
"delta": [] # 变化的键值, 避免多次调用
"delta": "" # 变化的键值, 避免多次调用
}
"""{
"cursor": {