Skip to content

Commit

Permalink
Group cache needs to be cleared upon group change for TextLayouts. (#825
Browse files Browse the repository at this point in the history
)

Caret also needs to be able to update layout in-case the group changes.
  • Loading branch information
caffeinepills committed May 4, 2023
1 parent dbd866f commit c0b7e77
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyglet/gui/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ def _check_hit(self, x, y):
def _set_focus(self, value):
self._focus = value
self._caret.visible = value
self._caret.layout = self._layout

def update_groups(self, order):
self._outline.group = Group(order=order + 1, parent=self._user_group)
Expand Down
23 changes: 20 additions & 3 deletions pyglet/text/caret.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ def __init__(self, layout, batch=None, color=(0, 0, 0, 255)):
"""
from pyglet import gl
self._layout = layout
batch = batch or layout.batch
group = layout.foreground_decoration_group

self._custom_batch = True if batch else False
self._batch = batch or layout.batch
self._group = layout.foreground_decoration_group

# Handle both 3 and 4 byte colors
r, g, b, *a = color
Expand All @@ -96,7 +98,7 @@ def __init__(self, layout, batch=None, color=(0, 0, 0, 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 = self._group.program.vertex_list(2, gl.GL_LINES, batch, self._group, colors=('Bn', colors))
self._ideal_x = None
self._ideal_line = None
self._next_attributes = {}
Expand All @@ -105,6 +107,21 @@ def __init__(self, layout, batch=None, color=(0, 0, 0, 255)):

layout.push_handlers(self)

@property
def layout(self):
return self._layout

@layout.setter
def layout(self, layout):
if self._layout == layout and self._group == layout.group:
return

from pyglet import gl
self._layout = layout
batch = self._batch if self._custom_batch else layout.batch
self._group = layout.foreground_decoration_group
self._batch.migrate(self._list, gl.GL_LINES, self._group, batch)

def delete(self):
"""Remove the caret from its batch.
Expand Down
1 change: 1 addition & 0 deletions pyglet/text/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,7 @@ def group(self):
def group(self, group):
self._user_group = group
self._initialize_groups()
self.group_cache.clear()
self._update()

@property
Expand Down

0 comments on commit c0b7e77

Please sign in to comment.