some pyglet update

This commit is contained in:
shenjack 2022-10-23 19:56:14 +08:00
parent e6cb2fad4e
commit becf65ed4c
31 changed files with 261 additions and 207 deletions

BIN
.nuitka-complie.ps1.swp Normal file

Binary file not shown.

2
DR.py
View File

@ -44,7 +44,7 @@ if __name__ == '__main__':
from Difficult_Rocket.crash import crash
from Difficult_Rocket import DR_option
try:
import pyglet # 导入pyglet
from libs import pyglet # 导入pyglet
pyglet.resource.path = ['/textures/']
pyglet.resource.reindex()

View File

@ -18,9 +18,8 @@ from typing import Union
from decimal import Decimal
# from DR
from utils import translate
from Difficult_Rocket.utils import new_thread
from Difficult_Rocket.command.api import CommandText
from Difficult_Rocket.utils import new_thread, translate
# from libs.pyglet
from libs import pyglet

View File

@ -12,7 +12,6 @@ gitee: @shenjackyuanjie
"""
import inspect
import objprint
from typing import Union

View File

@ -44,7 +44,7 @@ import sys
from typing import TYPE_CHECKING
#: The release version
version = '2.0.b1'
version = '2.0.b2'
__version__ = version
MIN_PYTHON_VERSION = 3, 7
@ -198,7 +198,7 @@ _option_types = {
for key in options:
"""Read defaults for options from environment"""
assert key in _option_types, f"Option '{key}' must have a type set in _option_types."
env = 'PYGLET_%s' % key.upper()
env = f'PYGLET_{key.upper()}'
try:
value = os.environ[env]
if _option_types[key] is tuple:
@ -265,21 +265,21 @@ def _trace_frame(thread, frame, indent):
filename = os.path.join('...', filename)
_trace_filename_abbreviations[path] = filename
location = '(%s:%d)' % (filename, line)
location = f'({filename}:{line})'
if indent:
name = 'Called from %s' % name
print('[%d] %s%s %s' % (thread, indent, name, location))
name = f'Called from {name}'
print(f'[{thread}] {indent}{name} {location}')
if _trace_args:
if is_ctypes:
args = [_trace_repr(arg) for arg in frame.f_locals['args']]
print(' %sargs=(%s)' % (indent, ', '.join(args)))
print(f' {indent}args=({", ".join(args)})')
else:
for argname in code.co_varnames[:code.co_argcount]:
try:
argvalue = _trace_repr(frame.f_locals[argname])
print(' %s%s=%s' % (indent, argname, argvalue))
print(f' {indent}{argname}={argvalue}')
except:
pass
@ -335,7 +335,7 @@ class _ModuleProxy:
if self._module is not None:
raise
import_name = 'pyglet.%s' % self._module_name
import_name = f'pyglet.{self._module_name}'
__import__(import_name)
module = sys.modules[import_name]
object.__setattr__(self, '_module', module)
@ -349,7 +349,7 @@ class _ModuleProxy:
if self._module is not None:
raise
import_name = 'pyglet.%s' % self._module_name
import_name = f'pyglet.{self._module_name}'
__import__(import_name)
module = sys.modules[import_name]
object.__setattr__(self, '_module', module)

View File

@ -340,9 +340,7 @@ class ScreenMode:
self.screen = screen
def __repr__(self):
return '%s(width=%r, height=%r, depth=%r, rate=%r)' % (
self.__class__.__name__,
self.width, self.height, self.depth, self.rate)
return f'{self.__class__.__name__}(width={self.width!r}, height={self.height!r}, depth={self.depth!r}, rate={self.rate})'
class Canvas:

View File

@ -56,7 +56,7 @@ class HeadlessDisplay(Display):
if num_devices.value > 0:
headless_device = pyglet.options['headless_device']
if headless_device < 0 or headless_device >= num_devices.value:
raise ValueError('Invalid EGL devide id: %d' % headless_device)
raise ValueError(f'Invalid EGL devide id: {headless_device}')
devices = (eglext.EGLDeviceEXT * num_devices.value)()
eglext.eglQueryDevicesEXT(num_devices.value, devices, byref(num_devices))
self._display_connection = eglext.eglGetPlatformDisplayEXT(

View File

@ -134,9 +134,7 @@ class Win32ScreenMode(ScreenMode):
self.scaling = mode.dmDisplayFixedOutput
def __repr__(self):
return '%s(width=%r, height=%r, depth=%r, rate=%r, scaling=%r)' % (
self.__class__.__name__,
self.width, self.height, self.depth, self.rate, self.scaling)
return f'{self.__class__.__name__}(width={self.width!r}, height={self.height!r}, depth={self.depth!r}, rate={self.rate}, scaling={self.scaling})'
class Win32Canvas(Canvas):
def __init__(self, display, hwnd, hdc):

View File

@ -120,11 +120,11 @@ class XlibDisplay(XlibSelectDevice, Display):
self._display = xlib.XOpenDisplay(name)
if not self._display:
raise NoSuchDisplayException('Cannot connect to "%s"' % name)
raise NoSuchDisplayException(f'Cannot connect to "{name}"')
screen_count = xlib.XScreenCount(self._display)
if x_screen >= screen_count:
raise NoSuchDisplayException('Display "%s" has no screen %d' % (name, x_screen))
raise NoSuchDisplayException(f'Display "{name}" has no screen {x_screen:d}')
super(XlibDisplay, self).__init__()
self.name = name
@ -274,9 +274,8 @@ class XlibScreen(Screen):
self.set_mode(self._initial_mode)
def __repr__(self):
return 'XlibScreen(display=%r, x=%d, y=%d, ' \
'width=%d, height=%d, xinerama=%d)' % \
(self.display, self.x, self.y, self.width, self.height, self._xinerama)
return f"{self.__class__.__name__}(display={self.display!r}, x={self.x}, y={self.y}, " \
f"width={self.width}, height={self.height}, xinerama={self._xinerama})"

View File

@ -219,7 +219,7 @@ class EventDispatcher:
# Single magically named function
name = obj.__name__
if name not in self.event_types:
raise EventException('Unknown event "%s"' % name)
raise EventException(f'Unknown event "{name}"')
if inspect.ismethod(obj):
yield name, WeakMethod(obj, partial(self._remove_handler, name))
else:
@ -234,7 +234,7 @@ class EventDispatcher:
for name, handler in kwargs.items():
# Function for handling given event (no magic)
if name not in self.event_types:
raise EventException('Unknown event "%s"' % name)
raise EventException(f'Unknown event "{name}"')
if inspect.ismethod(handler):
yield name, WeakMethod(handler, partial(self._remove_handler, name))
else:
@ -398,7 +398,7 @@ class EventDispatcher:
"EventDispatcher.register_event_type('event_name')."
)
assert event_type in self.event_types, \
"%r not found in %r.event_types == %r" % (event_type, self, self.event_types)
f"{event_type} not found in {self}.event_types == {self.event_types}"
invoked = False

View File

@ -254,17 +254,17 @@ def check_palette(palette):
for i, t in enumerate(p):
if len(t) not in (3, 4):
raise ProtocolError(
"palette entry %d: entries must be 3- or 4-tuples." % i)
f"palette entry {i}: entries must be 3- or 4-tuples.")
if len(t) == 3:
seen_triple = True
if seen_triple and len(t) == 4:
raise ProtocolError(
"palette entry %d: all 4-tuples must precede all 3-tuples" % i)
f"palette entry {i}: all 4-tuples must precede all 3-tuples")
for x in t:
if int(x) != x or not(0 <= x <= 255):
raise ProtocolError(
"palette entry %d: "
"values must be integer: 0 <= x <= 255" % i)
f"palette entry {i}: "
"values must be integer: 0 <= x <= 255")
return p
@ -282,12 +282,10 @@ def check_sizes(size, width, height):
"size argument should be a pair (width, height)")
if width is not None and width != size[0]:
raise ProtocolError(
"size[0] (%r) and width (%r) should match when both are used."
% (size[0], width))
f"size[0] ({size[0]}) and width ({width}) should match when both are used.")
if height is not None and height != size[1]:
raise ProtocolError(
"size[1] (%r) and height (%r) should match when both are used."
% (size[1], height))
f"size[1] ({size[1]}) and height ({height}) should match when both are used.")
return size
@ -307,17 +305,17 @@ def check_color(c, greyscale, which):
except TypeError:
c = (c,)
if len(c) != 1:
raise ProtocolError("%s for greyscale must be 1-tuple" % which)
raise ProtocolError(f"{which} for greyscale must be 1-tuple")
if not is_natural(c[0]):
raise ProtocolError(
"%s colour for greyscale must be integer" % which)
f"{which} colour for greyscale must be integer")
else:
if not (len(c) == 3 and
is_natural(c[0]) and
is_natural(c[1]) and
is_natural(c[2])):
raise ProtocolError(
"%s colour must be a triple of integers" % which)
f"{which} colour must be a triple of integers")
return c
@ -553,8 +551,7 @@ class Writer:
valid = is_natural(b) and 1 <= b <= 16
if not valid:
raise ProtocolError(
"each bitdepth %r must be a positive integer <= 16" %
(bitdepth,))
f"each bitdepth {bitdepth} must be a positive integer <= 16")
# Calculate channels, and
# expand bitdepth to be one element per channel.
@ -656,8 +653,7 @@ class Writer:
if wrong_length:
# Note: row numbers start at 0.
raise ProtocolError(
"Expected %d values but got %d values, in row %d" %
(vpr, len(row), i))
f"Expected {vpr} values but got {len(row)} values, in row {i}")
yield row
if self.interlace:
@ -668,8 +664,7 @@ class Writer:
nrows = self.write_passes(outfile, check_rows(rows))
if nrows != self.height:
raise ProtocolError(
"rows supplied (%d) does not match height (%d)" %
(nrows, self.height))
f"rows supplied ({nrows}) does not match height ({self.height})")
return nrows
def write_passes(self, outfile, rows):
@ -780,8 +775,7 @@ class Writer:
if self.rescale:
write_chunk(
outfile, b'sBIT',
struct.pack('%dB' % self.planes,
* [s[0] for s in self.rescale]))
struct.pack(f'{self.planes,* [s[0] for s in self.rescale]}B' ))
# :chunk:order: Without a palette (PLTE chunk),
# ordering is relatively relaxed.
@ -997,7 +991,7 @@ def unpack_rows(rows):
to being a sequence of bytes.
"""
for row in rows:
fmt = '!%dH' % len(row)
fmt = f'!{len(row)}'
yield bytearray(struct.pack(fmt, *row))
@ -1187,8 +1181,7 @@ def from_array(a, mode=None, info={}):
if bitdepth:
if info.get("bitdepth") and bitdepth != info['bitdepth']:
raise ProtocolError(
"bitdepth (%d) should match bitdepth of info (%d)." %
(bitdepth, info['bitdepth']))
f"bitdepth ({bitdepth}) should match bitdepth of info ({info[bitdepth]}).")
info['bitdepth'] = bitdepth
# Fill in and/or check entries in *info*.
@ -1383,19 +1376,17 @@ class Reader:
data = self.file.read(length)
if len(data) != length:
raise ChunkError(
'Chunk %s too short for required %i octets.'
% (type, length))
f'Chunk {type} too short for required {length} octets.')
checksum = self.file.read(4)
if len(checksum) != 4:
raise ChunkError('Chunk %s too short for checksum.' % type)
raise ChunkError(f'Chunk {type} too short for checksum.')
verify = zlib.crc32(type)
verify = zlib.crc32(data, verify)
verify = struct.pack('!I', verify)
if checksum != verify:
(a, ) = struct.unpack('!I', checksum)
(b, ) = struct.unpack('!I', verify)
message = ("Checksum error in %s chunk: 0x%08X != 0x%08X."
% (type.decode('ascii'), a, b))
message = f"Checksum error in {type.decode('ascii')} chunk: 0x{a:08X} != 0x{b:08X}."
if lenient:
warnings.warn(message, RuntimeWarning)
else:
@ -1539,7 +1530,7 @@ class Reader:
return bytearray(bs)
if self.bitdepth == 16:
return array('H',
struct.unpack('!%dH' % (len(bs) // 2), bs))
struct.unpack(f'!{(len(bs) // 2)}H' , bs))
assert self.bitdepth < 8
if width is None:
@ -1634,14 +1625,13 @@ class Reader:
'End of file whilst reading chunk length and type.')
length, type = struct.unpack('!I4s', x)
if length > 2 ** 31 - 1:
raise FormatError('Chunk %s is too large: %d.' % (type, length))
raise FormatError(f'Chunk {type} is too large: {length}.')
# Check that all bytes are in valid ASCII range.
# https://www.w3.org/TR/2003/REC-PNG-20031110/#5Chunk-layout
type_bytes = set(bytearray(type))
if not(type_bytes <= set(range(65, 91)) | set(range(97, 123))):
raise FormatError(
'Chunk %r has invalid Chunk Type.'
% list(type))
f'Chunk {list(type)} has invalid Chunk Type.')
return length, type
def process_chunk(self, lenient=False):
@ -1673,18 +1663,17 @@ class Reader:
if self.compression != 0:
raise FormatError(
"Unknown compression method %d" % self.compression)
f"Unknown compression method {self.compression}")
if self.filter != 0:
raise FormatError(
"Unknown filter method %d,"
f"Unknown filter method {self.filter},"
" see http://www.w3.org/TR/2003/REC-PNG-20031110/#9Filters ."
% self.filter)
)
if self.interlace not in (0, 1):
raise FormatError(
"Unknown interlace method %d, see "
f"Unknown interlace method {self.interlace}, see "
"http://www.w3.org/TR/2003/REC-PNG-20031110/#8InterlaceMethods"
" ."
% self.interlace)
" .")
# Derived values
# http://www.w3.org/TR/PNG/#6Colour-values
@ -1733,7 +1722,7 @@ class Reader:
"PLTE chunk is required before bKGD chunk.")
self.background = struct.unpack('B', data)
else:
self.background = struct.unpack("!%dH" % self.color_planes,
self.background = struct.unpack(f"!{self.color_planes}",
data)
except struct.error:
raise FormatError("bKGD chunk has incorrect length.")
@ -1752,11 +1741,10 @@ class Reader:
else:
if self.alpha:
raise FormatError(
"tRNS chunk is not valid with colour type %d." %
self.color_type)
f"tRNS chunk is not valid with colour type {self.color_type}.")
try:
self.transparent = \
struct.unpack("!%dH" % self.color_planes, data)
struct.unpack(f"!{self.color_planes}", data)
except struct.error:
raise FormatError("tRNS chunk has incorrect length.")
@ -1989,13 +1977,12 @@ class Reader:
pixels = itertrns(pixels)
targetbitdepth = None
if self.sbit:
sbit = struct.unpack('%dB' % len(self.sbit), self.sbit)
sbit = struct.unpack(f'{len(self.sbit)}', self.sbit)
targetbitdepth = max(sbit)
if targetbitdepth > info['bitdepth']:
raise Error('sBIT chunk %r exceeds bitdepth %d' %
(sbit, self.bitdepth))
raise Error(f'sBIT chunk {sbit!r} exceeds bitdepth {self.bitdepth}')
if min(sbit) <= 0:
raise Error('sBIT chunk %r has a 0-entry' % sbit)
raise Error(f'sBIT chunk {sbit} has a 0-entry')
if targetbitdepth:
shift = info['bitdepth'] - targetbitdepth
info['bitdepth'] = targetbitdepth
@ -2181,24 +2168,23 @@ def check_bitdepth_colortype(bitdepth, colortype):
"""
if bitdepth not in (1, 2, 4, 8, 16):
raise FormatError("invalid bit depth %d" % bitdepth)
raise FormatError(f"invalid bit depth {bitdepth}")
if colortype not in (0, 2, 3, 4, 6):
raise FormatError("invalid colour type %d" % colortype)
raise FormatError(f"invalid colour type {colortype}")
# Check indexed (palettized) images have 8 or fewer bits
# per pixel; check only indexed or greyscale images have
# fewer than 8 bits per pixel.
if colortype & 1 and bitdepth > 8:
raise FormatError(
"Indexed images (colour type %d) cannot"
" have bitdepth > 8 (bit depth %d)."
f"Indexed images (colour type {bitdepth}) cannot"
f" have bitdepth > 8 (bit depth {colortype})."
" See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 ."
% (bitdepth, colortype))
)
if bitdepth < 8 and colortype not in (0, 3):
raise FormatError(
"Illegal combination of bit depth (%d)"
" and colour type (%d)."
" See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 ."
% (bitdepth, colortype))
f"Illegal combination of bit depth ({bitdepth})"
f" and colour type ({colortype})."
" See http://www.w3.org/TR/2003/REC-PNG-20031110/#table111 .")
def is_natural(x):

View File

@ -213,7 +213,7 @@ class FreeTypeFont(base.Font):
def _load_font_face_from_system(self):
match = get_fontconfig().find_font(self._name, self.size, self.bold, self.italic)
if not match:
raise base.FontException('Could not match font "%s"' % self._name)
raise base.FontException(f'Could not match font "{self._name}"')
self.face = FreeTypeFace.from_fontconfig(match)
@classmethod
@ -258,7 +258,7 @@ class FreeTypeFace:
return cls(match.face)
else:
if not match.file:
raise base.FontException('No filename for "%s"' % match.name)
raise base.FontException(f'No filename for "{match.name}"')
return cls.from_file(match.file)
@property

View File

@ -402,16 +402,16 @@ class TruetypeInfo:
# a fuckwit.
header = _read_cmap_format4Header(self._data, offset)
seg_count = header.seg_count_x2 // 2
array_size = struct.calcsize('>%dH' % seg_count)
end_count = self._read_array('>%dH' % seg_count,
array_size = struct.calcsize(f'>{seg_count}')
end_count = self._read_array(f'>{seg_count}',
offset + header.size)
start_count = self._read_array('>%dH' % seg_count,
start_count = self._read_array(f'>{seg_count}',
offset + header.size + array_size + 2)
id_delta = self._read_array('>%dh' % seg_count,
id_delta = self._read_array(f'>{seg_count}',
offset + header.size + array_size + 2 + array_size)
id_range_offset_address = \
offset + header.size + array_size + 2 + array_size + array_size
id_range_offset = self._read_array('>%dH' % seg_count,
id_range_offset = self._read_array(f'>{seg_count}',
id_range_offset_address)
character_map = {}
for i in range(0, seg_count):
@ -475,7 +475,7 @@ def _read_table(*entries):
setattr(self, pname, pvalue)
def __repr__(self):
return '{'+', '.join(['%s = %s' % (pname, pvalue) for pname, pvalue in self.pairs])+'}'
return '{'+', '.join([f'{pname} = {pvalue}' for pname, pvalue in self.pairs])+'}'
@staticmethod
def array(data, offset, count):

View File

@ -142,9 +142,9 @@ class GDIGlyphRenderer(Win32GlyphRenderer):
if _debug_font:
_debug('%r.render(%s)' % (self, text))
_debug('abc.abcA = %r' % abc.abcA)
_debug('abc.abcB = %r' % abc.abcB)
_debug('abc.abcC = %r' % abc.abcC)
_debug(f'abc.abcA = {abc.abcA}')
_debug(f'abc.abcB = {abc.abcB}')
_debug(f'abc.abcC = {abc.abcC}')
_debug('width = %r' % width)
_debug('height = %r' % height)
_debug('lsb = %r' % lsb)

View File

@ -515,7 +515,7 @@ if __name__ == '__main__':
print('\n'.join(fonts))
if DEBUG:
print("Total: %s" % len(font_list()))
print(f"Total: {len(font_list())}")
# -- CHAPTER 2: WORK WITH FONT DIMENSIONS --

View File

@ -451,7 +451,7 @@ class TextEntry(WidgetBase):
super().__init__(x, y, width, height)
def _update_position(self):
self._layout.position = self._x, self._y
self._layout.position = self._x, self._y, 0
self._outline.position = self._x - self._pad, self._y - self._pad
@property

View File

@ -79,7 +79,7 @@ def dump_python():
print('os.getcwd():', os.getcwd())
for key, value in os.environ.items():
if key.startswith('PYGLET_'):
print("os.environ['%s']: %s" % (key, value))
print(f"os.environ['{key}']: {value}")
def dump_pyglet():
@ -89,7 +89,7 @@ def dump_pyglet():
print('pyglet.compat_platform:', pyglet.compat_platform)
print('pyglet.__file__:', pyglet.__file__)
for key, value in pyglet.options.items():
print("pyglet.options['%s'] = %r" % (key, value))
print(f"pyglet.options['{key}'] = {value!r}")
def dump_window():
@ -103,10 +103,10 @@ def dump_window():
print('display:', repr(display))
screens = display.get_screens()
for i, screen in enumerate(screens):
print('screens[%d]: %r' % (i, screen))
print(f'screens[{i}]: {screen!r}')
window = pyglet.window.Window(visible=False)
for key, value in window.config.get_gl_attributes():
print("config['%s'] = %r" % (key, value))
print(f"config['{key}'] = {value!r}")
print('context:', repr(window.context))
_heading('window.context._info')
@ -207,9 +207,9 @@ def dump_wintab():
impl_version = wintab.get_implementation_version()
spec_version = wintab.get_spec_version()
print('WinTab: %s %d.%d (Spec %d.%d)' % (interface_name,
impl_version >> 8, impl_version & 0xff,
spec_version >> 8, spec_version & 0xff))
print('WinTab: {0} {1}.{2} (Spec {3}.{4})'.format(interface_name,
impl_version >> 8, impl_version & 0xff,
spec_version >> 8, spec_version & 0xff))
def _try_dump(heading, func):

View File

@ -138,7 +138,7 @@ class LibraryLoader:
if self.platform.startswith('linux'):
for name in names:
libname = self.find_library(name)
platform_names.append(libname or 'lib%s.so' % name)
platform_names.append(libname or f'lib{name}.so')
platform_names.extend(names)
for name in platform_names:
@ -165,7 +165,7 @@ class LibraryLoader:
if _debug_lib:
print(f"Unexpected error loading library {name}: {str(o)}")
raise ImportError('Library "%s" not found.' % names[0])
raise ImportError(f'Library "{names[0]}" not found.')
def find_library(self, name):
return ctypes.util.find_library(name)
@ -272,7 +272,7 @@ class MachOLibraryLoader(LibraryLoader):
lib = _TraceLibrary(lib)
return lib
raise ImportError("Can't find framework %s." % name)
raise ImportError(f"Can't find framework {name}.")
class LinuxLibraryLoader(LibraryLoader):

View File

@ -38,7 +38,7 @@ from ctypes import util
from .cocoatypes import *
__LP64__ = (8*struct.calcsize("P") == 64)
__LP64__ = (8 * struct.calcsize("P") == 64)
__i386__ = (platform.machine() == 'i386')
__arm64__ = (platform.machine() == 'arm64')
@ -332,7 +332,7 @@ objc.object_getClassName.argtypes = [c_void_p]
# Ivar object_getInstanceVariable(id obj, const char *name, void **outValue)
objc.object_getInstanceVariable.restype = c_void_p
objc.object_getInstanceVariable.argtypes=[c_void_p, c_char_p, c_void_p]
objc.object_getInstanceVariable.argtypes = [c_void_p, c_char_p, c_void_p]
# id object_getIvar(id object, Ivar ivar)
objc.object_getIvar.restype = c_void_p
@ -366,8 +366,10 @@ objc.property_getName.argtypes = [c_void_p]
objc.protocol_conformsToProtocol.restype = c_bool
objc.protocol_conformsToProtocol.argtypes = [c_void_p, c_void_p]
class OBJC_METHOD_DESCRIPTION(Structure):
_fields_ = [ ("name", c_void_p), ("types", c_char_p) ]
_fields_ = [("name", c_void_p), ("types", c_char_p)]
# struct objc_method_description *protocol_copyMethodDescriptionList(Protocol *p, BOOL isRequiredMethod, BOOL isInstanceMethod, unsigned int *outCount)
# You must free() the returned array.
@ -407,6 +409,7 @@ objc.sel_isEqual.argtypes = [c_void_p, c_void_p]
objc.sel_registerName.restype = c_void_p
objc.sel_registerName.argtypes = [c_char_p]
######################################################################
def ensure_bytes(x):
@ -414,20 +417,25 @@ def ensure_bytes(x):
return x
return x.encode('ascii')
######################################################################
def get_selector(name):
return c_void_p(objc.sel_registerName(ensure_bytes(name)))
def get_class(name):
return c_void_p(objc.objc_getClass(ensure_bytes(name)))
def get_object_class(obj):
return c_void_p(objc.object_getClass(obj))
def get_metaclass(name):
return c_void_p(objc.objc_getMetaClass(ensure_bytes(name)))
def get_superclass_of_object(obj):
cls = c_void_p(objc.object_getClass(obj))
return c_void_p(objc.class_getSuperclass(cls))
@ -446,6 +454,7 @@ def x86_should_use_stret(restype):
return False
return True
# http://www.sealiesoftware.com/blog/archive/2008/11/16/objc_explain_objc_msgSend_fpret.html
def should_use_fpret(restype):
"""Determine if objc_msgSend_fpret is required to return a floating point type."""
@ -459,6 +468,7 @@ def should_use_fpret(restype):
return True
return False
# By default, assumes that restype is c_void_p
# and that all arguments are wrapped inside c_void_p.
# Use the restype and argtypes keyword arguments to
@ -470,7 +480,7 @@ def send_message(receiver, selName, *args, **kwargs):
receiver = get_class(receiver)
selector = get_selector(selName)
restype = kwargs.get('restype', c_void_p)
#print('send_message', receiver, selName, args, kwargs)
# print('send_message', receiver, selName, args, kwargs)
argtypes = kwargs.get('argtypes', [])
# Choose the correct version of objc_msgSend based on return type.
if should_use_fpret(restype):
@ -489,11 +499,14 @@ def send_message(receiver, selName, *args, **kwargs):
result = c_void_p(result)
return result
class OBJC_SUPER(Structure):
_fields_ = [ ('receiver', c_void_p), ('class', c_void_p) ]
_fields_ = [('receiver', c_void_p), ('class', c_void_p)]
OBJC_SUPER_PTR = POINTER(OBJC_SUPER)
# http://stackoverflow.com/questions/3095360/what-exactly-is-super-in-objective-c
#
# `superclass_name` is optional and can be used to force finding the superclass
@ -521,10 +534,12 @@ def send_super(receiver, selName, *args, superclass_name=None, **kwargs):
result = c_void_p(result)
return result
######################################################################
cfunctype_table = {}
def parse_type_encoding(encoding):
"""Takes a type encoding string and outputs a list of the separated type codes.
Currently does not handle unions or bitfields and strips out any field width
@ -536,7 +551,7 @@ def parse_type_encoding(encoding):
parse_type_encoding('{CGSize=dd}40@0:8{CGSize=dd}16Q32') --> ['{CGSize=dd}', '@', ':', '{CGSize=dd}', 'Q']
"""
type_encodings = []
brace_count = 0 # number of unclosed curly braces
brace_count = 0 # number of unclosed curly braces
bracket_count = 0 # number of unclosed square brackets
typecode = b''
for c in encoding:
@ -555,7 +570,7 @@ def parse_type_encoding(encoding):
elif c == b'}':
typecode += c
brace_count -= 1
assert(brace_count >= 0)
assert (brace_count >= 0)
elif c == b'[':
# Check if this marked the end of previous type code.
if typecode and typecode[-1:] != b'^' and brace_count == 0 and bracket_count == 0:
@ -566,7 +581,7 @@ def parse_type_encoding(encoding):
elif c == b']':
typecode += c
bracket_count -= 1
assert(bracket_count >= 0)
assert (bracket_count >= 0)
elif brace_count or bracket_count:
# Anything encountered while inside braces or brackets gets stuck on.
typecode += c
@ -604,12 +619,12 @@ def cfunctype_for_encoding(encoding):
return cfunctype_table[encoding]
# Otherwise, create a new CFUNCTYPE for the encoding.
typecodes = {b'c':c_char, b'i':c_int, b's':c_short, b'l':c_long, b'q':c_longlong,
b'C':c_ubyte, b'I':c_uint, b'S':c_ushort, b'L':c_ulong, b'Q':c_ulonglong,
b'f':c_float, b'd':c_double, b'B':c_bool, b'v':None, b'*':c_char_p,
b'@':c_void_p, b'#':c_void_p, b':':c_void_p, NSPointEncoding:NSPoint,
NSSizeEncoding:NSSize, NSRectEncoding:NSRect, NSRangeEncoding:NSRange,
PyObjectEncoding:py_object}
typecodes = {b'c': c_char, b'i': c_int, b's': c_short, b'l': c_long, b'q': c_longlong,
b'C': c_ubyte, b'I': c_uint, b'S': c_ushort, b'L': c_ulong, b'Q': c_ulonglong,
b'f': c_float, b'd': c_double, b'B': c_bool, b'v': None, b'*': c_char_p,
b'@': c_void_p, b'#': c_void_p, b':': c_void_p, NSPointEncoding: NSPoint,
NSSizeEncoding: NSSize, NSRectEncoding: NSRect, NSRangeEncoding: NSRange,
PyObjectEncoding: py_object}
argtypes = []
for code in parse_type_encoding(encoding):
if code in typecodes:
@ -627,6 +642,7 @@ def cfunctype_for_encoding(encoding):
cfunctype_table[encoding] = cfunctype
return cfunctype
######################################################################
# After calling create_subclass, you must first register
@ -638,9 +654,11 @@ def create_subclass(superclass, name):
superclass = get_class(superclass)
return c_void_p(objc.objc_allocateClassPair(superclass, ensure_bytes(name), 0))
def register_subclass(subclass):
objc.objc_registerClassPair(subclass)
# types is a string encoding the argument types of the method.
# The first type code of types is the return type (e.g. 'v' if void)
# The second type code must be '@' for id self.
@ -648,8 +666,8 @@ def register_subclass(subclass):
# Additional type codes are for types of other arguments if any.
def add_method(cls, selName, method, types):
type_encodings = parse_type_encoding(types)
assert(type_encodings[1] == b'@') # ensure id self typecode
assert(type_encodings[2] == b':') # ensure SEL cmd typecode
assert (type_encodings[1] == b'@') # ensure id self typecode
assert (type_encodings[2] == b':') # ensure SEL cmd typecode
selector = get_selector(selName)
cfunctype = cfunctype_for_encoding(types)
imp = cfunctype(method)
@ -657,18 +675,22 @@ def add_method(cls, selName, method, types):
objc.class_addMethod(cls, selector, imp, types)
return imp
def add_ivar(cls, name, vartype):
return objc.class_addIvar(cls, ensure_bytes(name), sizeof(vartype), alignment(vartype), encoding_for_ctype(vartype))
def set_instance_variable(obj, varname, value, vartype):
objc.object_setInstanceVariable.argtypes = [c_void_p, c_char_p, vartype]
objc.object_setInstanceVariable(obj, ensure_bytes(varname), value)
def get_instance_variable(obj, varname, vartype):
variable = vartype()
objc.object_getInstanceVariable(obj, ensure_bytes(varname), byref(variable))
return variable.value
######################################################################
class ObjCMethod:
@ -677,13 +699,13 @@ class ObjCMethod:
# Note, need to map 'c' to c_byte rather than c_char, because otherwise
# ctypes converts the value into a one-character string which is generally
# not what we want at all, especially when the 'c' represents a bool var.
typecodes = {b'c':c_byte, b'i':c_int, b's':c_short, b'l':c_long, b'q':c_longlong,
b'C':c_ubyte, b'I':c_uint, b'S':c_ushort, b'L':c_ulong, b'Q':c_ulonglong,
b'f':c_float, b'd':c_double, b'B':c_bool, b'v':None, b'Vv':None, b'*':c_char_p,
b'@':c_void_p, b'#':c_void_p, b':':c_void_p, b'^v':c_void_p, b'?':c_void_p,
NSPointEncoding:NSPoint, NSSizeEncoding:NSSize, NSRectEncoding:NSRect,
NSRangeEncoding:NSRange,
PyObjectEncoding:py_object}
typecodes = {b'c': c_byte, b'i': c_int, b's': c_short, b'l': c_long, b'q': c_longlong,
b'C': c_ubyte, b'I': c_uint, b'S': c_ushort, b'L': c_ulong, b'Q': c_ulonglong,
b'f': c_float, b'd': c_double, b'B': c_bool, b'v': None, b'Vv': None, b'*': c_char_p,
b'@': c_void_p, b'#': c_void_p, b':': c_void_p, b'^v': c_void_p, b'?': c_void_p,
NSPointEncoding: NSPoint, NSSizeEncoding: NSSize, NSRectEncoding: NSRect,
NSRangeEncoding: NSRange,
PyObjectEncoding: py_object}
cfunctype_table = {}
@ -706,7 +728,7 @@ class ObjCMethod:
try:
self.argtypes = [self.ctype_for_encoding(t) for t in self.argument_types]
except:
#print('no argtypes encoding for %s (%s)' % (self.name, self.argument_types))
# print(f'no argtypes encoding for {self.name} ({self.argument_types})')
self.argtypes = None
# Get types for the return type.
try:
@ -717,7 +739,7 @@ class ObjCMethod:
else:
self.restype = self.ctype_for_encoding(self.return_type)
except:
#print('no restype encoding for %s (%s)' % (self.name, self.return_type))
# print(f'no restype encoding for {self.name} ({self.return_type})')
self.restype = None
self.func = None
@ -786,6 +808,7 @@ class ObjCMethod:
'encoding = ' + str(self.encoding))
raise
######################################################################
class ObjCBoundMethod:
@ -804,6 +827,7 @@ class ObjCBoundMethod:
"""Call the method with the given arguments."""
return self.method(self.objc_id, *args)
######################################################################
class ObjCClass:
@ -841,9 +865,9 @@ class ObjCClass:
objc_class = super(ObjCClass, cls).__new__(cls)
objc_class.ptr = ptr
objc_class.name = name
objc_class.instance_methods = {} # mapping of name -> instance method
objc_class.class_methods = {} # mapping of name -> class method
objc_class._as_parameter_ = ptr # for ctypes argument passing
objc_class.instance_methods = {} # mapping of name -> instance method
objc_class.class_methods = {} # mapping of name -> class method
objc_class._as_parameter_ = ptr # for ctypes argument passing
# Store the new class in dictionary of registered classes.
cls._registered_classes[name] = objc_class
@ -927,6 +951,7 @@ class ObjCClass:
# Otherwise, raise an exception.
raise AttributeError('ObjCClass %s has no attribute %s' % (self.name, name))
######################################################################
class ObjCInstance:
@ -1008,6 +1033,7 @@ class ObjCInstance:
# Otherwise raise an exception.
raise AttributeError('ObjCInstance %s has no attribute %s' % (self.objc_class.name, name))
######################################################################
def convert_method_arguments(encoding, args):
@ -1024,6 +1050,7 @@ def convert_method_arguments(encoding, args):
new_args.append(a)
return new_args
# ObjCSubclass is used to define an Objective-C subclass of an existing
# class registered with the runtime. When you create an instance of
# ObjCSubclass, it registers the new subclass with the Objective-C
@ -1120,10 +1147,12 @@ class ObjCSubclass:
typecodes = parse_type_encoding(encoding)
typecodes.insert(1, b'@:')
encoding = b''.join(typecodes)
def decorator(f):
name = f.__name__.replace('_', ':')
self.add_method(f, name, encoding)
return f
return decorator
def method(self, encoding):
@ -1133,6 +1162,7 @@ class ObjCSubclass:
typecodes = parse_type_encoding(encoding)
typecodes.insert(1, b'@:')
encoding = b''.join(typecodes)
def decorator(f):
def objc_method(objc_self, objc_cmd, *args):
py_self = ObjCInstance(objc_self)
@ -1144,9 +1174,11 @@ class ObjCSubclass:
elif isinstance(result, ObjCInstance):
result = result.ptr.value
return result
name = f.__name__.replace('_', ':')
self.add_method(objc_method, name, encoding)
return objc_method
return decorator
def classmethod(self, encoding):
@ -1156,6 +1188,7 @@ class ObjCSubclass:
typecodes = parse_type_encoding(encoding)
typecodes.insert(1, b'@:')
encoding = b''.join(typecodes)
def decorator(f):
def objc_class_method(objc_cls, objc_cmd, *args):
py_cls = ObjCClass(objc_cls)
@ -1167,11 +1200,14 @@ class ObjCSubclass:
elif isinstance(result, ObjCInstance):
result = result.ptr.value
return result
name = f.__name__.replace('_', ':')
self.add_class_method(objc_class_method, name, encoding)
return objc_class_method
return decorator
######################################################################
# Instances of DeallocationObserver are associated with every

View File

@ -87,7 +87,7 @@ def get_audio_driver():
break
except Exception:
if _debug:
print('Error importing driver %s:' % driver_name)
print(f'Error importing driver {driver_name}:')
import traceback
traceback.print_exc()
else:

View File

@ -210,7 +210,7 @@ class PulseAudioPlayer(AbstractAudioPlayer):
def _write_to_stream(self, nbytes=None):
if nbytes is None:
nbytes = self.stream.writable_size
assert _debug('PulseAudioPlayer: Requested to write %d bytes to stream' % nbytes)
assert _debug(f'PulseAudioPlayer: Requested to write {nbytes} bytes to stream')
seek_mode = pa.PA_SEEK_RELATIVE
if self._clear_write:

View File

@ -101,7 +101,7 @@ class MediaThread:
Time to wait, in seconds.
"""
assert _debug('MediaThread.sleep(%r)' % timeout)
assert _debug(f'MediaThread.sleep({timeout!r})')
with self._condition:
if not self._stopped:
self._condition.wait(timeout)

View File

@ -184,16 +184,16 @@ def get_settings_path(name):
if 'APPDATA' in os.environ:
return os.path.join(os.environ['APPDATA'], name)
else:
return os.path.expanduser('~/%s' % name)
return os.path.expanduser(f'~/{name}')
elif pyglet.compat_platform == 'darwin':
return os.path.expanduser('~/Library/Application Support/%s' % name)
return os.path.expanduser(f'~/Library/Application Support/{name}')
elif pyglet.compat_platform.startswith('linux'):
if 'XDG_CONFIG_HOME' in os.environ:
return os.path.join(os.environ['XDG_CONFIG_HOME'], name)
else:
return os.path.expanduser('~/.config/%s' % name)
return os.path.expanduser(f'~/.config/{name}')
else:
return os.path.expanduser('~/.%s' % name)
return os.path.expanduser(f'~/.{name}')
def get_data_path(name):
@ -225,16 +225,16 @@ def get_data_path(name):
if 'APPDATA' in os.environ:
return os.path.join(os.environ['APPDATA'], name)
else:
return os.path.expanduser('~/%s' % name)
return os.path.expanduser(f'~/{name}')
elif pyglet.compat_platform == 'darwin':
return os.path.expanduser('~/Library/Application Support/%s' % name)
return os.path.expanduser(f'~/Library/Application Support/{name}')
elif pyglet.compat_platform.startswith('linux'):
if 'XDG_DATA_HOME' in os.environ:
return os.path.join(os.environ['XDG_DATA_HOME'], name)
else:
return os.path.expanduser('~/.local/share/%s' % name)
return os.path.expanduser(f'~/.local/share/{name}')
else:
return os.path.expanduser('~/.%s' % name)
return os.path.expanduser(f'~/.{name}')
class Location:

View File

@ -148,7 +148,7 @@ def get_decoder(filename, mimetype=None):
from pyglet.text.formats import attributed
return attributed.AttributedTextDecoder()
else:
raise DocumentDecodeException('Unknown format "%s"' % mimetype)
raise DocumentDecodeException(f'Unknown format "{mimetype}"')
def load(filename, file=None, mimetype=None):
@ -234,7 +234,7 @@ class DocumentLabel(layout.TextLayout):
"""
def __init__(self, document=None,
x=0, y=0, width=None, height=None,
x=0, y=0, z=0, width=None, height=None,
anchor_x='left', anchor_y='baseline',
multiline=False, dpi=None, batch=None, group=None, rotation=0):
"""Create a label for a given document.
@ -246,6 +246,8 @@ class DocumentLabel(layout.TextLayout):
X coordinate of the label.
`y` : int
Y coordinate of the label.
`z` : int
Z coordinate of the label.
`width` : int
Width of the label in pixels, or None
`height` : int
@ -274,6 +276,7 @@ class DocumentLabel(layout.TextLayout):
super().__init__(document, width, height, multiline, dpi, batch, group)
self._x = x
self._y = y
self._z = z
self._rotation = rotation
self._anchor_x = anchor_x
self._anchor_y = anchor_y
@ -415,7 +418,7 @@ class Label(DocumentLabel):
def __init__(self, text='',
font_name=None, font_size=None, bold=False, italic=False, stretch=False,
color=(255, 255, 255, 255),
x=0, y=0, width=None, height=None,
x=0, y=0, z=0, width=None, height=None,
anchor_x='left', anchor_y='baseline',
align='left',
multiline=False, dpi=None, batch=None, group=None, rotation=0):
@ -441,6 +444,8 @@ class Label(DocumentLabel):
X coordinate of the label.
`y` : int
Y coordinate of the label.
`z` : int
Z coordinate of the label.
`width` : int
Width of the label in pixels, or None
`height` : int
@ -471,7 +476,7 @@ class Label(DocumentLabel):
"""
doc = decode_text(text)
super().__init__(doc, x, y, width, height, anchor_x, anchor_y, multiline, dpi, batch, group, rotation)
super().__init__(doc, x, y, z, width, height, anchor_x, anchor_y, multiline, dpi, batch, group, rotation)
self.document.set_style(0, len(self.document.text), {
'font_name': font_name,
@ -492,7 +497,7 @@ class HTMLLabel(DocumentLabel):
"""
def __init__(self, text='', location=None,
x=0, y=0, width=None, height=None,
x=0, y=0, z=0, width=None, height=None,
anchor_x='left', anchor_y='baseline',
multiline=False, dpi=None, batch=None, group=None, rotation=0):
"""Create a label with an HTML string.
@ -507,6 +512,8 @@ class HTMLLabel(DocumentLabel):
X coordinate of the label.
`y` : int
Y coordinate of the label.
`z` : int
Z coordinate of the label.
`width` : int
Width of the label in pixels, or None
`height` : int
@ -535,7 +542,7 @@ class HTMLLabel(DocumentLabel):
self._text = text
self._location = location
doc = decode_html(text, location)
super().__init__(doc, x, y, width, height, anchor_x, anchor_y, multiline, dpi, batch, group, rotation)
super().__init__(doc, x, y, z, width, height, anchor_x, anchor_y, multiline, dpi, batch, group, rotation)
@property
def text(self):

View File

@ -385,6 +385,7 @@ class Caret:
else:
self._ideal_line = line
x, y = self._layout.get_point_from_position(self._position, line)
z = self._layout.z
if update_ideal_x:
self._ideal_x = x
@ -392,7 +393,7 @@ class Caret:
y += self._layout.y + self._layout.height
font = self._layout.document.get_font(max(0, self._position - 1))
self._list.position[:] = [x, y + font.descent, x, y + font.ascent]
self._list.position[:] = [x, y + font.descent, z, x, y + font.ascent, z]
if self._mark is not None:
self._layout.set_selection(min(self._position, self._mark), max(self._position, self._mark))

View File

@ -487,7 +487,7 @@ class AbstractDocument(event.EventDispatcher):
for element in self._elements:
if element._position == position:
return element
raise RuntimeError('No element at position %d' % position)
raise RuntimeError(f'No element at position {position}')
def set_style(self, start, end, attributes):
"""Set text style of some or all of the document.

View File

@ -236,7 +236,7 @@ class OrderedListBuilder(ListBuilder):
mark = '?'
if self.numbering == 'A':
mark = mark.upper()
return '%s%s%s' % (self.prefix, mark, self.suffix)
return f'{self.prefix}{mark}{self.suffix}'
elif self.numbering in 'iI':
try:
mark = _int_to_roman(value)
@ -244,9 +244,9 @@ class OrderedListBuilder(ListBuilder):
mark = '?'
if self.numbering == 'i':
mark = mark.lower()
return '%s%s%s' % (self.prefix, mark, self.suffix)
return f'{self.prefix}{mark}{self.suffix}'
else:
return '%s%d%s' % (self.prefix, value, self.suffix)
return f'{self.prefix}{value}{self.suffix}'
class StructuredTextDecoder(pyglet.text.DocumentDecoder):

View File

@ -180,7 +180,7 @@ def _parse_distance(distance, dpi):
return int(distance)
match = _distance_re.match(distance)
assert match, 'Could not parse distance %s' % distance
assert match, f'Could not parse distance {distance}'
if not match:
return 0
@ -199,7 +199,7 @@ def _parse_distance(distance, dpi):
elif unit == 'cm':
return int(value * dpi * 0.393700787)
else:
assert False, 'Unknown distance unit %s' % unit
assert False, f'Unknown distance unit {unit}'
class _Line:
@ -225,7 +225,7 @@ class _Line:
self.boxes = []
def __repr__(self):
return '_Line(%r)' % self.boxes
return f'_Line({self.boxes})'
def add_box(self, box):
self.boxes.append(box)
@ -285,7 +285,7 @@ class _AbstractBox:
self.advance = advance
self.length = length
def place(self, layout, i, x, y, rotation, anchor_x, anchor_y, context):
def place(self, layout, i, x, y, z, rotation, anchor_x, anchor_y, context):
raise NotImplementedError('abstract')
def delete(self, layout):
@ -322,7 +322,7 @@ class _GlyphBox(_AbstractBox):
self.glyphs = glyphs
self.advance = advance
def place(self, layout, i, x, y, rotation, anchor_x, anchor_y, context):
def place(self, layout, i, x, y, z, rotation, anchor_x, anchor_y, context):
assert self.glyphs
program = get_default_layout_shader()
@ -347,7 +347,7 @@ class _GlyphBox(_AbstractBox):
v2 += x1
v1 += y + baseline
v3 += y + baseline
vertices.extend(map(round, [v0, v1, v2, v1, v2, v3, v0, v3]))
vertices.extend(map(round, [v0, v1, z, v2, v1, z, v2, v3, z, v0, v3, z]))
t = glyph.tex_coords
tex_coords.extend(t)
x1 += glyph.advance
@ -372,7 +372,6 @@ class _GlyphBox(_AbstractBox):
tex_coords=('f', tex_coords),
rotation=('f', ((rotation,) * 4) * n_glyphs),
anchor=('f', ((anchor_x, anchor_y) * 4) * n_glyphs))
context.add_list(vertex_list)
# Decoration (background color and underline)
@ -397,16 +396,16 @@ class _GlyphBox(_AbstractBox):
if bg is not None:
if len(bg) != 4:
raise ValueError("Background color requires 4 values (R, G, B, A). Value received: {}".format(bg))
raise ValueError(f"Background color requires 4 values (R, G, B, A). Value received: {bg}")
background_vertices.extend([x1, y1, x2, y1, x2, y2, x1, y2])
background_vertices.extend([x1, y1, z, x2, y1, z, x2, y2, z, x1, y2, z])
background_colors.extend(bg * 4)
if underline is not None:
if len(underline) != 4:
raise ValueError("Underline color requires 4 values (R, G, B, A). Value received: {}".format(underline))
raise ValueError(f"Underline color requires 4 values (R, G, B, A). Value received: {underline}")
underline_vertices.extend([x1, y + baseline - 2, x2, y + baseline - 2])
underline_vertices.extend([x1, y + baseline - 2, z, x2, y + baseline - 2, z])
underline_colors.extend(underline * 2)
x1 = x2
@ -419,21 +418,22 @@ class _GlyphBox(_AbstractBox):
background_indices.extend([element + (bg_idx * 4) for element in [0, 1, 2, 0, 2, 3]])
background_list = decoration_program.vertex_list_indexed(bg_count * 4, GL_TRIANGLES, background_indices,
layout.batch, layout.background_decoration_group,
position=('f', background_vertices),
colors=('Bn', background_colors),
rotation=('f', (rotation,) * 4),
anchor=('f', (anchor_x, anchor_y) * 4))
layout.batch, layout.background_decoration_group,
position=('f', background_vertices),
colors=('Bn', background_colors),
rotation=('f', (rotation,) * 4),
anchor=('f', (anchor_x, anchor_y) * 4))
context.add_list(background_list)
if underline_vertices:
ul_count = len(underline_vertices) // 3
decoration_program = get_default_decoration_shader()
underline_list = decoration_program.vertex_list(len(underline_vertices) // 2, GL_LINES,
layout.batch, layout.foreground_decoration_group,
position=('f',underline_vertices),
colors=('Bn', underline_colors),
rotation=('f', (rotation,) * 4),
anchor=('f', (anchor_x, anchor_y) * 4))
underline_list = decoration_program.vertex_list(ul_count, GL_LINES,
layout.batch, layout.foreground_decoration_group,
position=('f', underline_vertices),
colors=('Bn', underline_colors),
rotation=('f', (rotation,) * ul_count),
anchor=('f', (anchor_x, anchor_y) * ul_count))
context.add_list(underline_list)
def delete(self, layout):
@ -460,7 +460,7 @@ class _GlyphBox(_AbstractBox):
return position
def __repr__(self):
return '_GlyphBox(%r)' % self.glyphs
return f'_GlyphBox({self.glyphs})'
class _InlineElementBox(_AbstractBox):
@ -471,8 +471,8 @@ class _InlineElementBox(_AbstractBox):
self.element = element
self.placed = False
def place(self, layout, i, x, y, rotation, anchor_x, anchor_y, context):
self.element.place(layout, x, y)
def place(self, layout, i, x, y, z, rotation, anchor_x, anchor_y, context):
self.element.place(layout, x, y, z)
self.placed = True
context.add_box(self)
@ -495,7 +495,7 @@ class _InlineElementBox(_AbstractBox):
return 1
def __repr__(self):
return '_InlineElementBox(%r)' % self.element
return f'_InlineElementBox({self.element})'
class _InvalidRange:
@ -539,10 +539,10 @@ class _InvalidRange:
layout_vertex_source = """#version 330 core
in vec2 position;
in vec3 position;
in vec4 colors;
in vec3 tex_coords;
in vec2 translation;
in vec3 translation;
in vec2 anchor;
in float rotation;
@ -571,9 +571,9 @@ layout_vertex_source = """#version 330 core
m_rotation[1][0] = -sin(-radians(rotation));
m_rotation[1][1] = cos(-radians(rotation));
gl_Position = window.projection * window.view * m_anchor * m_rotation * m_neganchor * vec4(position + translation, 0.0, 1.0);
gl_Position = window.projection * window.view * m_anchor * m_rotation * m_neganchor * vec4(position + translation, 1.0);
vert_position = vec4(position + translation, 0.0, 1.0);
vert_position = vec4(position + translation, 1.0);
text_colors = colors;
texture_coords = tex_coords.xy;
}
@ -603,9 +603,9 @@ layout_fragment_source = """#version 330 core
"""
decoration_vertex_source = """#version 330 core
in vec2 position;
in vec3 position;
in vec4 colors;
in vec2 translation;
in vec3 translation;
in vec2 anchor;
in float rotation;
@ -633,9 +633,9 @@ decoration_vertex_source = """#version 330 core
m_rotation[1][0] = -sin(-radians(rotation));
m_rotation[1][1] = cos(-radians(rotation));
gl_Position = window.projection * window.view * m_anchor * m_rotation * m_neganchor * vec4(position + translation, 0.0, 1.0);
gl_Position = window.projection * window.view * m_anchor * m_rotation * m_neganchor * vec4(position + translation, 1.0);
vert_position = vec4(position + translation, 0.0, 1.0);
vert_position = vec4(position + translation, 1.0);
vert_colors = colors;
}
"""
@ -876,6 +876,7 @@ class TextLayout:
_x = 0
_y = 0
_z = 0
_rotation = 0
_width = None
_height = None
@ -1035,7 +1036,7 @@ class TextLayout:
dx = x - self._x
for vertex_list in self._vertex_lists:
vertices = vertex_list.position[:]
vertices[::2] = [x + dx for x in vertices[::2]]
vertices[::3] = [x + dx for x in vertices[::3]]
vertex_list.position[:] = vertices
self._x = x
@ -1061,10 +1062,34 @@ class TextLayout:
dy = y - self._y
for vertex_list in self._vertex_lists:
vertices = vertex_list.position[:]
vertices[1::2] = [y + dy for y in vertices[1::2]]
vertices[1::3] = [y + dy for y in vertices[1::3]]
vertex_list.position[:] = vertices
self._y = y
@property
def z(self):
"""Z coordinate of the layout.
:type: int
"""
return self._z
@z.setter
def z(self, z):
self._set_z(z)
def _set_z(self, z):
if self._boxes:
self._z = z
self._update()
else:
dz = z - self.z
for vertex_list in self._vertex_lists:
vertices = vertex_list.position[:]
vertices[2::3] = [z + dz for z in vertices[2::3]]
vertex_list.position[:] = vertices
self._z = z
@property
def rotation(self):
"""Rotation of the layout.
@ -1083,32 +1108,36 @@ class TextLayout:
@property
def position(self):
"""The (X, Y) coordinates of the layout, as a tuple.
"""The (X, Y, Z) coordinates of the layout, as a tuple.
See also :py:attr:`~pyglet.text.layout.TextLayout.anchor_x`,
and :py:attr:`~pyglet.text.layout.TextLayout.anchor_y`.
:type: (int, int)
:type: (int, int, int)
"""
return self._x, self._y
return self._x, self._y, self._z
@position.setter
def position(self, values):
x, y = values
x, y, z = values
if self._boxes:
self._x = x
self._y = y
self._z = z
self._update()
else:
dx = x - self._x
dy = y - self._y
dz = z - self._z
for vertex_list in self._vertex_lists:
vertices = vertex_list.position[:]
vertices[::2] = [x + dx for x in vertices[::2]]
vertices[1::2] = [y + dy for y in vertices[1::2]]
vertices[2::3] = [z + dz for z in vertices[2::3]]
vertex_list.position[:] = vertices
self._x = x
self._y = y
self._z = z
@property
def visible(self):
@ -1353,7 +1382,7 @@ class TextLayout:
context = _StaticLayoutContext(self, self._document, colors_iter, background_iter)
for line in lines:
self._create_vertex_lists(left + line.x, top + line.y, line.start, line.boxes, context)
self._create_vertex_lists(left + line.x, top + line.y, self._z, line.start, line.boxes, context)
def _update_color(self):
colors_iter = self._document.get_style_runs('color')
@ -1830,9 +1859,9 @@ class TextLayout:
return line_index
def _create_vertex_lists(self, x, y, i, boxes, context):
def _create_vertex_lists(self, x, y, z, i, boxes, context):
for box in boxes:
box.place(self, i, x, y, self._rotation, self._x, self._y, context)
box.place(self, i, x, y, z, self._rotation, self._x, self._y, context)
x += box.advance
i += box.length
@ -1894,11 +1923,11 @@ class ScrollableTextLayout(TextLayout):
@property
def position(self):
return self._x, self._y
return self._x, self._y, self._z
@position.setter
def position(self, position):
self.x, self.y = position
self.x, self.y, self.z = position
@property
def anchor_x(self):
@ -1924,7 +1953,7 @@ class ScrollableTextLayout(TextLayout):
def _update_translation(self):
for _vertex_list in self._vertex_lists:
_vertex_list.translation[:] = (-self._translate_x, -self._translate_y) * _vertex_list.count
_vertex_list.translation[:] = (-self._translate_x, -self._translate_y, 0) * _vertex_list.count
@property
def view_x(self):
@ -2309,7 +2338,7 @@ class IncrementalTextLayout(TextLayout, EventDispatcher):
elif y + line.ascent < self._translate_y - self.height:
break
self._create_vertex_lists(left + line.x, top + y, line.start, line.boxes, context)
self._create_vertex_lists(left + line.x, top + y, self._z, line.start, line.boxes, context)
@property
def x(self):
@ -2335,11 +2364,11 @@ class IncrementalTextLayout(TextLayout, EventDispatcher):
@property
def position(self):
return self._x, self._y
return self._x, self._y, self._z
@position.setter
def position(self, position):
self._x, self._y = position
self._x, self._y, self._z = position
self._uninit_document()
self._init_document()
self._update_scissor_area()
@ -2412,7 +2441,7 @@ class IncrementalTextLayout(TextLayout, EventDispatcher):
def _update_translation(self):
for line in self.lines:
for vlist in line.vertex_lists:
vlist.translation[:] = (-self._translate_x, -self._translate_y) * vlist.count
vlist.translation[:] = (-self._translate_x, -self._translate_y, 0) * vlist.count
@property
def view_x(self):
@ -2620,7 +2649,7 @@ class IncrementalTextLayout(TextLayout, EventDispatcher):
:rtype: int
"""
x -= self._translate_x
y -= self._translate_y
y -= self._translate_y + self._height
line_index = 0
for line in self.lines:

View File

@ -44,7 +44,7 @@ class _Run:
self.count = count
def __repr__(self):
return 'Run(%r, %d)' % (self.value, self.count)
return f'Run({self.value}, {self.count})'
class RunList:

1
nuitka-complie.ps1 Normal file
View File

@ -0,0 +1 @@
python3.8.exe -m nuitka --mingw64 --show-memory --show-progress --output-dir=build/nuitka --enable-plugin=numpy --nofollow-import-to=objprint,numpy,http,urllib,ssl,socket,html,email --follow-import-to=Difficult_Rocket,libs.pyglet --standalone .\DR.py

1
nuitka1-complie.ps1 Normal file
View File

@ -0,0 +1 @@
python3.8.exe -m nuitka --mingw64 --show-memory --show-progress --output-dir=build/nuitka --enable-plugin=numpy --nofollow-import-to=objprint,numpy,http,urllib,ssl,socket,html,email,pyglet --standalone .\DR.py