sync pyglet update

This commit is contained in:
shenjack 2023-04-18 21:07:58 +08:00
parent b7651e72b4
commit 7589a3d09b
2 changed files with 39 additions and 15 deletions

View File

@ -419,13 +419,13 @@ class ShapeBase(ABC):
return return
if batch is not None and self._batch is not None: if batch is not None and self._batch is not None:
self._batch.migrate(self._vertex_list, self._draw_mode, self._batch.migrate(self._vertex_list, self._draw_mode, self._group, batch)
self._group, batch)
self._batch = batch self._batch = batch
else: else:
self._vertex_list.delete() self._vertex_list.delete()
self._batch = batch self._batch = batch
self._create_vertex_list() self._create_vertex_list()
self._update_vertices()
class Arc(ShapeBase): class Arc(ShapeBase):
@ -1703,7 +1703,6 @@ class Polygon(ShapeBase):
self._create_vertex_list() self._create_vertex_list()
self._update_vertices() self._update_vertices()
self._update_color()
def _create_vertex_list(self): def _create_vertex_list(self):
self._vertex_list = self._group.program.vertex_list( self._vertex_list = self._group.program.vertex_list(

View File

@ -67,7 +67,7 @@ class Caret:
_mark = None _mark = None
def __init__(self, layout, batch=None, color=(0, 0, 0)): def __init__(self, layout, batch=None, color=(0, 0, 0, 255)):
"""Create a caret for a layout. """Create a caret for a layout.
By default the layout's batch is used, so the caret does not need to By default the layout's batch is used, so the caret does not need to
@ -78,15 +78,23 @@ class Caret:
Layout to control. Layout to control.
`batch` : `~pyglet.graphics.Batch` `batch` : `~pyglet.graphics.Batch`
Graphics batch to add vertices to. Graphics batch to add vertices to.
`color` : (int, int, int) `color` : (int, int, int, int)
RGB tuple with components in range [0, 255]. An RGBA or RGB tuple with components in the range [0, 255].
RGB colors will be treated as having an opacity of 255.
""" """
from pyglet import gl from pyglet import gl
self._layout = layout self._layout = layout
batch = batch or layout.batch batch = batch or layout.batch
group = layout.foreground_decoration_group group = layout.foreground_decoration_group
colors = (*color, 255, *color, 255)
# Handle both 3 and 4 byte colors
r, g, b, *a = color
# The alpha value when not in a hidden blink state
self._visible_alpha = a[0] if a else 255
colors = r, g, b, self._visible_alpha, r, g, b, self._visible_alpha
self._list = group.program.vertex_list(2, gl.GL_LINES, batch, group, colors=('Bn', colors)) self._list = group.program.vertex_list(2, gl.GL_LINES, batch, group, colors=('Bn', colors))
self._ideal_x = None self._ideal_x = None
@ -108,10 +116,13 @@ class Caret:
def _blink(self, dt): def _blink(self, dt):
if self.PERIOD: if self.PERIOD:
self._blink_visible = not self._blink_visible self._blink_visible = not self._blink_visible
if self._visible and self._active and self._blink_visible: if self._visible and self._active and self._blink_visible:
alpha = 255 alpha = self._visible_alpha
else: else:
alpha = 0 alpha = 0
# Only set the alpha rather than entire colors
self._list.colors[3] = alpha self._list.colors[3] = alpha
self._list.colors[7] = alpha self._list.colors[7] = alpha
@ -140,19 +151,33 @@ class Caret:
@property @property
def color(self): def color(self):
"""Caret color. """An RGBA tuple of the current caret color
The default caret color is ``[0, 0, 0]`` (black). Each RGB color When blinking off, the alpha channel will be set to ``0``. The
component is in the range 0 to 255. default caret color when visible is ``(0, 0, 0, 255)`` (opaque black).
:type: (int, int, int) You may set the color to an RGBA or RGB color tuple.
.. warning:: This setter can fail for a short time after layout / window init!
Use ``__init__``'s ``color`` keyword argument instead if you
run into this problem.
Each color channel must be between 0 and 255, inclusive. If the color
set to an RGB color, the previous alpha channel value will be used.
:type: (int, int, int, int)
""" """
return self._list.colors[:3] return self._list.colors[:4]
@color.setter @color.setter
def color(self, color): def color(self, color):
self._list.colors[:3] = color r, g, b, *_a = color
self._list.colors[4:7] = color
# Preserve alpha when setting an RGB color
a = _a[0] if _a else self._list.colors[3]
self._list.colors[:] = r, g, b, a, r, g, b, a
@property @property
def position(self): def position(self):