Embarrassingly despite searching for an answer to problem 1 and finding nothing, Right after I posted this I started looking into a second issue and on an unrelated search I found someone with a problem similar to mine. They were advised to get the latest code from SVN. So I did and it fixes problem 1 and problem 2 as well.
The mess of 3 continues, as does the incorrect function of 4. though 4 is easier to see just try drawing a 45 degree line slowly. But no show stoppers now :-) Sorry for the trouble. john xyloidhead wrote: > 3) My initial code just added one line after another to the same > canvas but as more lines were added it got slower and slower, line > being drawn a long way behind the mouse. My solution was to create a > new draw canvas for every line. > > Now this works fine, or appears to but it seems untidy. Is there a > better way? > > 4) Draw a few lines, preferably with shart direction changes, and > you'll see that there are gaps generated in either the endcaps or > discontinuities on the joins. > > I've included my code below. I've simplified it as much as I can to > show the problems. It assumes pyglet and cocos are installed. It works > for > pyglet 1.1.1 > cocos 0.3.0 > Python 2.5.2.2 (Active Python) > On Windows XP > > I hope there aren't to many stupid mistakes but I've no doubt there > are some. I'm happy for comments on better ways to do things. > > Any help or advice appreciated. > > Regards > > john > > ----------------- code below here --------------- > # Draw vector lines with the mouse > # > # Creates a new layer for each line in an attempt to remove the lag > when drawing > # many lines. > > import cocos > from cocos.director import director > from cocos.sprite import Sprite > from cocos import draw > import pyglet > > import random > ri = random.randint > > class vline(object): > "Line class that has a border of a different color to the interior > of the line" > def __init__(self, width, innerColor, borderColor, endcap = > draw.ROUND_CAP, join = draw.ROUND_JOIN): > self._width = width > self._borderColor = borderColor > self._innerColor = innerColor > self._endcap = endcap > self._join = join > self._points = [] > > def addPoint(self, pt): > # add a point to the line > self._points.append(pt) > > def draw(self, canvas): > # set the line constants > canvas.set_endcap( self._endcap ) > canvas.set_join( self._join ) > > # draw the outer line > canvas.set_color( (self._borderColor) ) > canvas.set_stroke_width( self._width ) > > # draw lines > canvas.move_to( self._points[0] ) > for p in self._points[1:]: > canvas.line_to( p ) > > # draw the inner line > canvas.set_color( (self._innerColor) ) > canvas.set_stroke_width( self._width/2 ) > > # draw lines > canvas.move_to( self._points[0] ) > for p in self._points[1:]: > canvas.line_to( p ) > > def __len__(self): > return len(self._points) > > def __getitem__(self,key): > return self._points[key] > > class TestFigure(draw.Canvas): > def __init__(self): > super( TestFigure, self ).__init__() > self._lines = [] > self._borderColor = (255, 255, 0, 128) > self._innerColor = (0, 255, 128, 255) > self._currLine = None > # canvas appears to require something on it or it may > generates an exception. > # so add a 1 pixel long line > self._currWidth = 1 > line = vline(self._currWidth, self._innerColor, > self._borderColor) > line.addPoint((0,0)) > line.addPoint((0, 1)) > self._lines.append (line) > > # set the current width to something sensible > self._currWidth = 20 > > def render(self): > if self._currLine is not None: > if len (self._currLine) > 1: > self._currLine.draw(self) > > for l in self._lines: > l.draw(self) > > def addPointClean(self, pt,): > "Add a point without marking the canvas as dirty" > if self._currLine is not None: > self._currLine.addPoint(pt) > > def addPoint(self, pt,): > if self._currLine is not None: > self._currLine.addPoint(pt) > self._dirty = True > > def startLine(self, startPt): > # called when a new line is started > self._currLine = vline(self._currWidth, self._innerColor, > self._borderColor) > self.addPoint(startPt) > > def endLine(self, endPt): > # called when a new line is started > if endPt != self._currLine[len(self._currLine) - 1]: > self.addPointClean(endPt) > self._lines.append(self._currLine) > self._currLine = None > self._dirty = True > > class TestLayer(cocos.layer.Layer): > is_event_handler = True > > def __init__(self): > super( TestLayer, self ).__init__() > self._figures = [] > > def addFigure(self): > self._currFigure = TestFigure() > self.add( self._currFigure ) > self._figures.append(self._currFigure) > > def on_mouse_press(self, x, y, button, modifiers): > if button == pyglet.window.mouse.LEFT: > # create a new Test figure each time we start a new line > # otherwise the lines get further and further behind the > mouse > # movements as more and more lines are drawn each refresh. > self.addFigure() > self._currFigure.startLine((x,y)) > > def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers): > self._currFigure.addPoint((x,y)) > > def on_mouse_release(self, x, y, button, modifiers): > if button == pyglet.window.mouse.LEFT: > self._currFigure.endLine((x,y)) > > > if __name__ == "__main__": > director.init() > test_layer = TestLayer () > main_scene = cocos.scene.Scene (test_layer) > director.run (main_scene) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "cocos2d discuss" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cocos-discuss?hl=en -~----------~----~----~----~------~----~------~--~---
