| 1 | import pyglet |
| 2 | pyglet.options.debug_gl = False |
| 3 | |
| 4 | window = pyglet.window.Window(800, 600, caption="DPI Test", resizable=True) |
| 5 | batch = pyglet.graphics.Batch() |
| 6 | |
| 7 | print("Window DPI", window.dpi, "Window Scale", window.scale) |
| 8 | |
| 9 | hello_label = pyglet.text.Label('Hello, world', |
| 10 | font_name='Times New Roman', |
| 11 | font_size=36, |
| 12 | x=window.width // 2, y=window.height // 2, |
| 13 | anchor_x='center', anchor_y='center', |
| 14 | batch=batch, |
| 15 | dpi=window.dpi) |
| 16 | |
| 17 | mouse_enter_label = pyglet.text.Label(f"enter: x={0}, y={0}", x=10, y=110, font_size=12, |
| 18 | dpi=window.dpi, batch=batch) |
| 19 | mouse_leave_label = pyglet.text.Label(f"leave: x={0}, y={0}", x=10, y=85, font_size=12, |
| 20 | dpi=window.dpi, batch=batch) |
| 21 | mouse_motion_label = pyglet.text.Label(f"motion: x={0}, y={0} dx={0}, dy={0}", x=10, y=60, font_size=12, |
| 22 | dpi=window.dpi, batch=batch) |
| 23 | mouse_drag_label = pyglet.text.Label(f"drag: x={0}, y={0} dx={0}, dy={0}", x=10, y=35, font_size=12, |
| 24 | dpi=window.dpi, batch=batch) |
| 25 | mouse_press_label = pyglet.text.Label(f"press: x={0}, y={0}", x=10, y=10, font_size=12, |
| 26 | dpi=window.dpi, batch=batch) |
| 27 | labels = [hello_label, mouse_enter_label, mouse_leave_label, mouse_motion_label, mouse_drag_label, mouse_press_label] |
| 28 | |
| 29 | dinosaur = pyglet.resource.animation("programming_guide/dinosaur.gif") |
| 30 | |
| 31 | sprite = pyglet.sprite.Sprite(dinosaur, x=100, y=140, batch=batch) |
| 32 | sprite.scale = window.scale |
| 33 | |
| 34 | |
| 35 | @window.event |
| 36 | def on_draw(): |
| 37 | window.clear() |
| 38 | batch.draw() |
| 39 | |
| 40 | |
| 41 | @window.event |
| 42 | def on_mouse_motion(x, y, dx, dy): |
| 43 | mouse_motion_label.text = f"motion: x={x:.2f}, y={y:.2f} dx={dx:.2f}, dy={dy:.2f}" |
| 44 | |
| 45 | |
| 46 | @window.event |
| 47 | def on_mouse_press(x, y, button, modifier): |
| 48 | mouse_press_label.text = f"press: x={x:.2f}, y={y:.2f}" |
| 49 | |
| 50 | |
| 51 | @window.event |
| 52 | def on_mouse_enter(x, y): |
| 53 | mouse_enter_label.text = f"enter: x={x:.2f}, y={y:.2f}" |
| 54 | |
| 55 | |
| 56 | @window.event |
| 57 | def on_mouse_leave(x, y): |
| 58 | mouse_leave_label.text = f"leave: x={x:.2f}, y={y:.2f}" |
| 59 | |
| 60 | |
| 61 | @window.event |
| 62 | def on_mouse_drag(x, y, dx, dy, button, modifier): |
| 63 | mouse_drag_label.text = f"drag: x={x:.2f}, y={y:.2f} dx={dx:.2f}, dy={dy:.2f}" |
| 64 | |
| 65 | |
| 66 | @window.event |
| 67 | def on_resize(width, height): |
| 68 | hello_label.position = (window.width // 2, window.height // 2, 0) |
| 69 | |
| 70 | |
| 71 | screens = pyglet.display.get_display().get_screens() |
| 72 | selected_screen = screens[0] |
| 73 | |
| 74 | @window.event |
| 75 | def on_key_press(symbol, modifiers): |
| 76 | global selected_screen |
| 77 | if len(screens) > 1: |
| 78 | if symbol == pyglet.window.key._1: |
| 79 | selected_screen = screens[1] |
| 80 | elif symbol == pyglet.window.key._0: |
| 81 | selected_screen = screens[0] |
| 82 | |
| 83 | if symbol == pyglet.window.key.SPACE: |
| 84 | window.set_size(500, 300) |
| 85 | elif symbol == pyglet.window.key.F: |
| 86 | window.set_fullscreen(False, screen=selected_screen) |
| 87 | elif symbol == pyglet.window.key.G: |
| 88 | window.set_fullscreen(True, screen=selected_screen) |
| 89 | |
| 90 | |
| 91 | |
| 92 | @window.event |
| 93 | def on_scale(scale, dpi): |
| 94 | print("--- on scale") |
| 95 | print("SCALE_X", scale, dpi) |
| 96 | print("Window Size:", window.get_size()) |
| 97 | print("Window Scale Ratio:", window.scale) |
| 98 | print("Window Frame Buffer Size:", window.get_framebuffer_size()) |
| 99 | for label in labels: |
| 100 | label.dpi = dpi |
| 101 | sprite.scale = window.scale |
| 102 | |
| 103 | |
| 104 | pyglet.app.run() |