drawing: add a stroke.seal() functionality

Makes end-of-stroke handling easier, we can call seal() on the current stroke
and it'll make drawing.current_stroke() None, which is something we can deal
with.
This commit is contained in:
Peter Hutterer 2018-02-16 12:44:58 +10:00
parent 155a11104c
commit 23e18cea67
2 changed files with 24 additions and 2 deletions

View File

@ -41,8 +41,18 @@ class Stroke(GObject.Object):
self.points = []
self._position = (0, 0)
self._pressure = 0
self._is_sealed = False
@GObject.property
def sealed(self):
return self._is_sealed
def seal(self):
self._is_sealed = True
def new_rel(self, position=None, pressure=None):
assert not self._is_sealed
p = Point(self)
if position is not None:
x, y = self._position
@ -55,6 +65,8 @@ class Stroke(GObject.Object):
self.points.append(p)
def new_abs(self, position=None, pressure=None):
assert not self._is_sealed
p = Point(self)
if position is not None:
self._position = position
@ -88,18 +100,27 @@ class Drawing(GObject.Object):
def seal(self):
# Drop empty strokes
for s in self.strokes:
s.seal()
self.strokes = [s for s in self.strokes if s.points]
# The way we're building drawings, we don't need to change the current
# stroke at runtime, so this is read-ony
@GObject.Property
def current_stroke(self):
return self.strokes[self._current_stroke]
if self._current_stroke < 0:
return None
s = self.strokes[self._current_stroke]
return s if not s.sealed else None
def new_stroke(self):
'''
Create a new stroke and make it the current stroke
'''
if self.current_stroke is not None:
self.current_stroke.seal()
s = Stroke(self)
self.strokes.append(s)
self._current_stroke += 1

View File

@ -636,9 +636,10 @@ class WacomProtocolBase(WacomProtocolLowLevelComm):
continue
if bytes(args) == b'\x00\x00\xff\xff\xff\xff\xff\xff':
logger.info(f'end of stroke')
stroke = None
stroke.seal()
continue
stroke = drawing.current_stroke
if stroke is None:
stroke = drawing.new_stroke()