Skip to content

Commit

Permalink
#24: create new eraser when surface is completly redrawn (force=True)
Browse files Browse the repository at this point in the history
  • Loading branch information
anxuae committed Jun 1, 2021
1 parent c5486a5 commit 6133839
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
36 changes: 28 additions & 8 deletions pygame_vkeyboard/vkeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,26 @@ def get_rect(self):
rect = rect.union(self.input.get_rect())
return rect

def set_eraser(self, surface):
"""Setup the surface used to hide/clear the keyboard.
"""
self.eraser = surface.copy()
for layout in self.layouts:
layout.sprites.clear(surface, self.eraser)

def draw(self, surface=None, force=False):
"""Draw the virtual keyboard.
This method is optimized to be called at each loop of the
main application. It uses DirtySprite to update only parts
of the screen that need to be refreshed.
The first call to this method will setup the "eraser" surface that
will be used to redraw dirty parts of the screen.
The `force` parameter shall be used if the surface has been redrawn:
it reset the eraser and redraw all sprites.
Parameters
----------
surface:
Expand All @@ -610,15 +623,22 @@ def draw(self, surface=None, force=False):
rects:
List of updated area.
"""
# Setup the surface used to hide/clear the keyboard
if surface and surface != self.eraser:
self.eraser = surface
for layout in self.layouts:
layout.sprites.clear(surface, self.eraser.copy())
surface = surface or self.surface

# Setup eraser
if not self.eraser or force:
self.set_eraser(surface)

# Setup new the surface where to draw
for layout in self.layouts:
if layout.sprites.get_clip() != self.background.rect:
# Changing the clipping area will force update of all
# sprites without using "dirty mechanism"
layout.sprites.set_clip(self.background.rect)

rects = self.layout.sprites.draw(surface or self.surface)
rects += self.input.draw(surface or self.surface, force)
rects = self.layout.sprites.draw(surface)
rects += self.input.draw(surface, force)

if force:
self.layout.sprites.repaint_rect(self.background.rect)
return rects
Expand All @@ -631,7 +651,7 @@ def update(self, events):
events:
List of events to process.
"""
if self.state > 0:
if self.state == 1:
self.layout.sprites.update(events)
self.input.update(events)

Expand Down
31 changes: 21 additions & 10 deletions pygame_vkeyboard/vtextinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def is_enabled(self):

def disable(self):
"""Set this text input as non active."""
self.state = 0
self.state = 0 # Disabled by default
self.cursor.visible = 0
self.background.visible = 0
for line in self.sprites.get_sprites_from_layer(1):
Expand All @@ -323,6 +323,12 @@ def get_rect(self):
"""Return text input rect."""
return self.background.rect

def set_eraser(self, surface):
"""Setup the surface used to hide/clear the keyboard.
"""
self.eraser = surface.copy()
self.sprites.clear(surface, self.eraser)

def draw(self, surface, force):
"""Draw the text input box.
Expand All @@ -333,13 +339,18 @@ def draw(self, surface, force):
force:
Force the drawing of the entire surface (time consuming).
"""
# Setup the surface used to hide/clear the text input
if surface and surface != self.eraser:
self.eraser = surface
self.sprites.clear(surface, self.eraser.copy())
self.sprites.set_clip(pygame.Rect(self.position[0], 0,
self.size[0],
self.position[1] + self.size[1]))
# Setup eraser
if not self.eraser or force:
self.set_eraser(surface)

# Setup new the surface where to draw
clip_rect = pygame.Rect(self.position[0], 0,
self.background.rect.width,
self.background.rect.bottom)
if self.sprites.get_clip() != clip_rect:
# Changing the clipping area will force update of all
# sprites without using "dirty mechanism"
self.sprites.set_clip(clip_rect)

if force:
self.sprites.repaint_rect(self.background.rect)
Expand All @@ -353,7 +364,7 @@ def update(self, events):
events:
List of events to process.
"""
if self.state > 0:
if self.state == 1:
self.sprites.update(events)
for event in events:
if event.type == pygame.KEYUP and self.cursor.selected:
Expand All @@ -378,7 +389,7 @@ def update(self, events):

def update_lines(self):
"""Update lines content with the current text."""
if self.state > 0:
if self.state == 1:
remain = self.text

# Update existing line with text
Expand Down

0 comments on commit 6133839

Please sign in to comment.