From 23e18cea676ad400dafd01031e2a33cebe2c5c24 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Fri, 16 Feb 2018 12:44:58 +1000 Subject: [PATCH] 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. --- tuhi/drawing.py | 23 ++++++++++++++++++++++- tuhi/wacom.py | 3 ++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/tuhi/drawing.py b/tuhi/drawing.py index 381c56d..b7e4a68 100644 --- a/tuhi/drawing.py +++ b/tuhi/drawing.py @@ -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 diff --git a/tuhi/wacom.py b/tuhi/wacom.py index da6f32a..94b462c 100644 --- a/tuhi/wacom.py +++ b/tuhi/wacom.py @@ -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()