1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
import math
import random
import moderngl_window
class WindowEvents(moderngl_window.WindowConfig):
"""
Demonstrates handling mouse, keyboard, render and resize events
"""
gl_version = (3, 3)
title = "Window Events"
cursor = True
vsync = True
def __init__(self, **kwargs):
super().__init__(**kwargs)
# self.wnd.exit_key = None
def on_render(self, time: float, frametime: float):
self.ctx.clear(
(math.sin(time) + 1.0) / 2,
(math.sin(time + 2) + 1.0) / 2,
(math.sin(time + 3) + 1.0) / 2,
)
# We can also check if a key is in press state here
if self.wnd.is_key_pressed(self.wnd.keys.SPACE):
print("User is holding SPACE button")
def on_resize(self, width: int, height: int):
print("Window was resized. buffer size is {} x {}".format(width, height))
def on_close(self):
print("Window is closing")
def on_iconify(self, iconify: bool):
"""Window hide/minimize and restore"""
print("Window was iconified:", iconify)
def on_key_event(self, key, action, modifiers):
keys = self.wnd.keys
# Key presses
if action == keys.ACTION_PRESS:
if key == keys.ESCAPE:
print("ESCAPE key was pressed")
if key == keys.SPACE:
print("SPACE key was pressed")
# Using modifiers (shift and ctrl)
if key == keys.Z and modifiers.shift:
print("Shift + Z was pressed")
if key == keys.Z and modifiers.ctrl:
print("ctrl + Z was pressed")
if key == keys.Z and modifiers.alt:
print("alt + Z was pressed")
# Key releases
elif action == self.wnd.keys.ACTION_RELEASE:
if key == keys.SPACE:
print("SPACE key was released")
if action == keys.ACTION_PRESS:
# Move the window around with AWSD
if key == keys.A:
self.wnd.position = self.wnd.position[0] - 20, self.wnd.position[1]
if key == keys.D:
self.wnd.position = self.wnd.position[0] + 20, self.wnd.position[1]
if key == keys.W:
self.wnd.position = self.wnd.position[0], self.wnd.position[1] - 20
if key == keys.S:
self.wnd.position = self.wnd.position[0], self.wnd.position[1] + 20
# Resize window around with Shift + AWSD
if self.wnd.modifiers.shift and key == keys.A:
self.wnd.size = self.wnd.size[0] - 50, self.wnd.size[1]
if self.wnd.modifiers.shift and key == keys.D:
self.wnd.size = self.wnd.size[0] + 50, self.wnd.size[1]
if self.wnd.modifiers.shift and key == keys.W:
self.wnd.size = self.wnd.size[0], self.wnd.size[1] - 50
if self.wnd.modifiers.shift and key == keys.S:
self.wnd.size = self.wnd.size[0], self.wnd.size[1] + 50
# toggle cursor
if key == keys.C:
self.wnd.cursor = not self.wnd.cursor
# Shuffle window tittle
if key == keys.T:
title = list(self.wnd.title)
random.shuffle(title)
self.wnd.title = "".join(title)
# Toggle mouse exclusivity
if key == keys.M:
self.wnd.mouse_exclusivity = not self.wnd.mouse_exclusivity
# Check number vs. numpad
if key == keys.NUMBER_0:
print("NUMBER 0")
if key == keys.NUMPAD_0:
print("NUMPAD 0")
def on_mouse_position_event(self, x, y, dx, dy):
print("Mouse position pos={} {} delta={} {}".format(x, y, dx, dy))
def on_mouse_drag_event(self, x, y, dx, dy):
print("Mouse drag pos={} {} delta={} {}".format(x, y, dx, dy))
def on_mouse_scroll_event(self, x_offset, y_offset):
print("mouse_scroll_event", x_offset, y_offset)
def on_mouse_press_event(self, x, y, button):
print("Mouse button {} pressed at {}, {}".format(button, x, y))
print("Mouse states:", self.wnd.mouse_states)
def on_mouse_release_event(self, x: int, y: int, button: int):
print("Mouse button {} released at {}, {}".format(button, x, y))
print("Mouse states:", self.wnd.mouse_states)
def on_unicode_char_entered(self, char):
print("unicode_char_entered:", char)
if __name__ == "__main__":
moderngl_window.run_window_config(WindowEvents)
|