from kivy.app import Appfrom kivy.lang import Builderfrom kivy.graphics import Translate, Color, Linefrom kivy.uix.relativelayout import RelativeLayoutfrom kivy.properties import StringPropertyfrom kivy.uix.togglebutton import ToggleButton
Builder.load_string('''<Paint>: BoxLayout: BoxLayout: orientation: 'vertical' size_hint_x: .2 ToggleButton: text: 'draw' group: 'paint' on_press: root.state = 'draw' ToggleButton: text: 'translate' group: 'paint' on_press: root.state = 'translate' Widget: id: widget on_touch_down: if self.collide_point(*args[1].pos): root.touch_down(args[1]) on_touch_move: if self.collide_point(*args[1].pos): root.touch_move(args[1]) canvas: Color: rgb: 1, 1, 1 Rectangle: pos: self.pos size: self.size''')
class Paint(RelativeLayout): state = StringProperty('draw')
def touch_down(self, touch): if self.state == 'draw': with self.ids.widget.canvas: Color(0, 0, 0) touch.ud['line'] = Line(points=(touch.x, touch.y), width=10) elif self.state == 'translate': with self.ids.widget.canvas: touch.ud['start'] = (touch.x, touch.y) touch.ud['translate'] = Translate(0, 0)
def touch_move(self, touch): if self.state == 'draw': touch.ud['line'].points += [touch.x, touch.y] elif self.state == 'translate': touch.ud['translate'].xy = (touch.x-touch.ud['start'][0], touch.y-touch.ud['start'][1])
class PaintApp(App): def build(self): return Paint()
if __name__ == '__main__': PaintApp().run()from kivy.app import Appfrom kivy.lang import Builderfrom kivy.graphics import Translate, Color, Line, PushMatrix, PopMatrix PushMatrix() touch.ud['translate'] = Translate(0, 0) PopMatrix()
def touch_move(self, touch): if self.state == 'draw': touch.ud['line'].points += [touch.x, touch.y] elif self.state == 'translate': touch.ud['translate'].xy = (touch.x-touch.ud['start'][0], touch.y-touch.ud['start'][1])
class PaintApp(App): def build(self): return Paint()
if __name__ == '__main__': PaintApp().run()from kivy.app import Appfrom kivy.lang import Builderfrom kivy.graphics import Translate, Color, Line, PushMatrix, PopMatrixfrom kivy.uix.relativelayout import RelativeLayoutfrom kivy.properties import StringPropertyfrom kivy.uix.togglebutton import ToggleButton
Builder.load_string('''<Paint>: BoxLayout: BoxLayout: orientation: 'vertical' size_hint_x: .2 ToggleButton: text: 'draw' group: 'paint' on_press: root.state = 'draw' ToggleButton: text: 'translate' group: 'paint' on_press: root.state = 'translate' Widget: id: widget on_touch_down: if self.collide_point(*args[1].pos): root.touch_down(args[1]) on_touch_move: if self.collide_point(*args[1].pos): root.touch_move(args[1]) canvas.before: Color: rgb: 1, 1, 1 Rectangle: pos: self.pos size: self.size''')
class Paint(RelativeLayout): state = StringProperty('draw')
def __init__(self, **kwargs): super(Paint, self).__init__(**kwargs) with self.ids.widget.canvas.before: PushMatrix() self.translate = Translate(0, 0) with self.ids.widget.canvas.after: PopMatrix()
def touch_down(self, touch): if self.state == 'draw': with self.ids.widget.canvas: Color(0, 0, 0) touch.ud['line'] = Line(points=(touch.x, touch.y), width=10) elif self.state == 'translate': touch.ud['start'] = (touch.x, touch.y) touch.ud['translation'] = self.translate.xy
def touch_move(self, touch): if self.state == 'draw': touch.ud['line'].points += [touch.x, touch.y] elif self.state == 'translate': self.translate.xy = (touch.ud['translation'][0]+(touch.x-touch.ud['start'][0]), touch.ud['translation'][1]+(touch.y-touch.ud['start'][1]))
class PaintApp(App): def build(self): return Paint()
if __name__ == '__main__': PaintApp().run()