Spaces:
Runtime error
Runtime error
File size: 10,433 Bytes
d82cf6a |
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
from pyglet import gl
from pyglet import app
from pyglet import window
from pyglet import canvas
class Display:
"""A display device supporting one or more screens.
.. versionadded:: 1.2
"""
name = None
"""Name of this display, if applicable.
:type: str
"""
x_screen = None
"""The X11 screen number of this display, if applicable.
:type: int
"""
def __init__(self, name=None, x_screen=None):
"""Create a display connection for the given name and screen.
On X11, :attr:`name` is of the form ``"hostname:display"``, where the
default is usually ``":1"``. On X11, :attr:`x_screen` gives the X
screen number to use with this display. A pyglet display can only be
used with one X screen; open multiple display connections to access
multiple X screens.
Note that TwinView, Xinerama, xrandr and other extensions present
multiple monitors on a single X screen; this is usually the preferred
mechanism for working with multiple monitors under X11 and allows each
screen to be accessed through a single pyglet`~pyglet.canvas.Display`
On platforms other than X11, :attr:`name` and :attr:`x_screen` are
ignored; there is only a single display device on these systems.
:Parameters:
name : str
The name of the display to connect to.
x_screen : int
The X11 screen number to use.
"""
canvas._displays.add(self)
def get_screens(self):
"""Get the available screens.
A typical multi-monitor workstation comprises one :class:`Display`
with multiple :class:`Screen` s. This method returns a list of
screens which can be enumerated to select one for full-screen display.
For the purposes of creating an OpenGL config, the default screen
will suffice.
:rtype: list of :class:`Screen`
"""
raise NotImplementedError('abstract')
def get_default_screen(self):
"""Get the default (primary) screen as specified by the user's operating system
preferences.
:rtype: :class:`Screen`
"""
screens = self.get_screens()
for screen in screens:
if screen.x == 0 and screen.y == 0:
return screen
# No Primary screen found?
return screens[0]
def get_windows(self):
"""Get the windows currently attached to this display.
:rtype: sequence of :class:`~pyglet.window.Window`
"""
return [window for window in app.windows if window.display is self]
class Screen:
"""A virtual monitor that supports fullscreen windows.
Screens typically map onto a physical display such as a
monitor, television or projector. Selecting a screen for a window
has no effect unless the window is made fullscreen, in which case
the window will fill only that particular virtual screen.
The :attr:`width` and :attr:`height` attributes of a screen give the
current resolution of the screen. The :attr:`x` and :attr:`y` attributes
give the global location of the top-left corner of the screen. This is
useful for determining if screens are arranged above or next to one
another.
Use :func:`~Display.get_screens` or :func:`~Display.get_default_screen`
to obtain an instance of this class.
"""
def __init__(self, display, x, y, width, height):
"""
:parameters:
`display` : `~pyglet.canvas.Display`
:attr:`display`
`x` : int
Left edge :attr:`x`
`y` : int
Top edge :attr:`y`
`width` : int
:attr:`width`
`height` : int
:attr:`height`
"""
self.display = display
"""Display this screen belongs to."""
self.x = x
"""Left edge of the screen on the virtual desktop."""
self.y = y
"""Top edge of the screen on the virtual desktop."""
self.width = width
"""Width of the screen, in pixels."""
self.height = height
"""Height of the screen, in pixels."""
def __repr__(self):
return '{}(x={}, y={}, width={}, height={})'.format(self.__class__.__name__, self.x, self.y, self.width, self.height)
def get_best_config(self, template=None):
"""Get the best available GL config.
Any required attributes can be specified in `template`. If
no configuration matches the template,
:class:`~pyglet.window.NoSuchConfigException` will be raised.
:deprecated: Use :meth:`pyglet.gl.Config.match`.
:Parameters:
`template` : `pyglet.gl.Config`
A configuration with desired attributes filled in.
:rtype: :class:`~pyglet.gl.Config`
:return: A configuration supported by the platform that best
fulfils the needs described by the template.
"""
configs = None
if template is None:
for template_config in [gl.Config(double_buffer=True, depth_size=24, major_version=3, minor_version=3),
gl.Config(double_buffer=True, depth_size=16, major_version=3, minor_version=3),
None]:
try:
configs = self.get_matching_configs(template_config)
break
except window.NoSuchConfigException:
pass
else:
configs = self.get_matching_configs(template)
if not configs:
raise window.NoSuchConfigException()
return configs[0]
def get_matching_configs(self, template):
"""Get a list of configs that match a specification.
Any attributes specified in `template` will have values equal
to or greater in each returned config. If no configs satisfy
the template, an empty list is returned.
:deprecated: Use :meth:`pyglet.gl.Config.match`.
:Parameters:
`template` : `pyglet.gl.Config`
A configuration with desired attributes filled in.
:rtype: list of :class:`~pyglet.gl.Config`
:return: A list of matching configs.
"""
raise NotImplementedError('abstract')
def get_modes(self):
"""Get a list of screen modes supported by this screen.
:rtype: list of :class:`ScreenMode`
.. versionadded:: 1.2
"""
raise NotImplementedError('abstract')
def get_mode(self):
"""Get the current display mode for this screen.
:rtype: :class:`ScreenMode`
.. versionadded:: 1.2
"""
raise NotImplementedError('abstract')
def get_closest_mode(self, width, height):
"""Get the screen mode that best matches a given size.
If no supported mode exactly equals the requested size, a larger one
is returned; or ``None`` if no mode is large enough.
:Parameters:
`width` : int
Requested screen width.
`height` : int
Requested screen height.
:rtype: :class:`ScreenMode`
.. versionadded:: 1.2
"""
# Best mode is one with smallest resolution larger than width/height,
# with depth and refresh rate equal to current mode.
current = self.get_mode()
best = None
for mode in self.get_modes():
# Reject resolutions that are too small
if mode.width < width or mode.height < height:
continue
if best is None:
best = mode
# Must strictly dominate dimensions
if (mode.width <= best.width and mode.height <= best.height and
(mode.width < best.width or mode.height < best.height)):
best = mode
# Preferably match rate, then depth.
if mode.width == best.width and mode.height == best.height:
points = 0
if mode.rate == current.rate:
points += 2
if best.rate == current.rate:
points -= 2
if mode.depth == current.depth:
points += 1
if best.depth == current.depth:
points -= 1
if points > 0:
best = mode
return best
def set_mode(self, mode):
"""Set the display mode for this screen.
The mode must be one previously returned by :meth:`get_mode` or
:meth:`get_modes`.
:Parameters:
`mode` : `ScreenMode`
Screen mode to switch this screen to.
"""
raise NotImplementedError('abstract')
def restore_mode(self):
"""Restore the screen mode to the user's default.
"""
raise NotImplementedError('abstract')
class ScreenMode:
"""Screen resolution and display settings.
Applications should not construct `ScreenMode` instances themselves; see
:meth:`Screen.get_modes`.
The :attr:`depth` and :attr:`rate` variables may be ``None`` if the
operating system does not provide relevant data.
.. versionadded:: 1.2
"""
width = None
"""Width of screen, in pixels.
:type: int
"""
height = None
"""Height of screen, in pixels.
:type: int
"""
depth = None
"""Pixel color depth, in bits per pixel.
:type: int
"""
rate = None
"""Screen refresh rate in Hz.
:type: int
"""
def __init__(self, screen):
"""
:parameters:
`screen` : `Screen`
"""
self.screen = screen
def __repr__(self):
return f'{self.__class__.__name__}(width={self.width!r}, height={self.height!r}, depth={self.depth!r}, rate={self.rate})'
class Canvas:
"""Abstract drawing area.
Canvases are used internally by pyglet to represent drawing areas --
either within a window or full-screen.
.. versionadded:: 1.2
"""
def __init__(self, display):
"""
:parameters:
`display` : `Display`
:attr:`display`
"""
self.display = display
"""Display this canvas was created on."""
|