Forum Archive

Toggling between drawing freely and straight lines

HT

Hi everyone,

I'm learning pythonista and am trying to work on this example script:
https://gist.github.com/8f81907e4844d579ef8eda2ed5d65d5e

It allows you to draw freely in the bottom view.
I've added a switch, with which I want to change the drawing behaviour from drawing freely to only being able to draw straight lines.
I want the app to move the line following the touch until the touch is ended, which is when the line is supposed to be drawn. I hope this is understandable.

Can anyone give me some pointers?

cvp

@HT If I correctly understand:

    def touch_began(self, touch):
        self.xb, self.yb = touch.location

    def touch_moved(self, touch):
        x, y = touch.location
        self.path = ui.Path()
        self.path.line_width = 2.0
        self.path.line_join_style = ui.LINE_JOIN_ROUND
        self.path.line_cap_style = ui.LINE_CAP_ROUND
        self.path.move_to(self.xb, self.yb)
        self.path.line_to(x, y)
        self.set_needs_display()
cvp

@HT If next line must start from end of previous one:

    def __init__(self, frame):
        self.frame = frame
        self.flex = 'WH'
        self.path = None
        self.action = None
        self.xb = None

    def touch_began(self, touch):
        if not self.xb:
          self.xb, self.yb = touch.location

    def touch_moved(self, touch):
        x, y = touch.location
        self.path = ui.Path()
        self.path.line_width = 2.0
        self.path.line_join_style = ui.LINE_JOIN_ROUND
        self.path.line_cap_style = ui.LINE_CAP_ROUND
        self.path.move_to(self.xb, self.yb)
        self.path.line_to(x, y)
        self.set_needs_display()

    def touch_ended(self, touch):
        self.xb, self.yb = touch.prev_location
         ...
HT

Thanks, that's exactly what I needed!

JonB

For what it's worth, one of my many abandoned projects, a "pen tool"
https://gist.github.com/2d9e24f31f471f629d56912ead624538