Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 202 lines (166 sloc) 7.119 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
import collections
import os
from pymt import *
from OpenGL.GL import *
from glob import glob

# PYMT Plugin integration
IS_PYMT_PLUGIN = True
PLUGIN_TITLE = 'Paint Sandbox'
PLUGIN_AUTHOR = 'Thomas Hansen'
PLUGIN_EMAIL = 'thomas.hansen@gmail.com'
PLUGIN_DESCRIPTION = 'This is a simple paint canvas.'

current_dir = os.path.dirname(__file__)

class PaintBrushLayout(MTBoxLayout):
    def draw(self):
        set_color(0, 0, 0, 0.7)
        drawRectangle(self.pos, self.size)


class MTPaintColorPicker(MTWidget):
    def __init__(self, **kwargs):
        kwargs.setdefault('min', 0)
        kwargs.setdefault('max', 255)
        kwargs.setdefault('target', [])
        super(MTPaintColorPicker, self).__init__(**kwargs)
        self.targets = kwargs.get('targets')
        self.sliders = [ MTSlider(min=kwargs.get('min'), max=kwargs.get('max'), size=(30,200), style = {'slider-color': (1,0,0,1)}),
                        MTSlider(min=kwargs.get('min'), max=kwargs.get('max'), size=(30,200), style = {'slider-color': (0,1,0,1)}),
                        MTSlider(min=kwargs.get('min'), max=kwargs.get('max'), size=(30,200), style = {'slider-color': (0,0,1,1)}),
                        MTSlider(min=kwargs.get('min'), max=kwargs.get('max'), size=(30,200), style = {'slider-color': (1,1,1,.7)}),
                        MTSlider(min=kwargs.get('min'), max=kwargs.get('max'), size=(30,200), style = {'slider-color': (1,1,1,.7)}) ]
        for slider in self.sliders:
            slider.value = 200
        self.update_color()
        self.touch_positions = {}
        self.brush_size = (self.sliders[4].value/4) + 1
        set_brush_size(self.brush_size)

    def draw(self):
        set_color(0.2,0.2,0.2,0.5)
        drawRoundedRectangle(pos=(self.x, self.y),
                             size=(self.width, self.height),
                             radius=15)

        set_color(*self.current_color)
        drawRectangle(pos=(self.x+10, self.y+220), size=(110,60))

        pos = self.x + 170, self.y + 250
        set_brush_size(self.brush_size)
        paintLine(pos+pos)


        for i in range(len(self.sliders)):
            self.sliders[i].x = 10 + self.x + i*42
            self.sliders[i].y = 10 + self.y
            self.sliders[i].draw()

    def update_color(self):
        r = self.sliders[0].value/255.0
        g = self.sliders[1].value/255.0
        b = self.sliders[2].value/255.0
        a = self.sliders[3].value/255.0
        for w in self.targets:
            w.color = (r,g,b,a)
        self.current_color = (r,g,b,a)
        self.brush_size = (self.sliders[4].value/4) + 1
        set_brush_size(self.brush_size)

    def on_touch_down(self, touch):
        for s in self.sliders:
            if s.on_touch_down(touch):
                self.update_color()
                return True

        if self.collide_point(touch.x, touch.y):
            self.touch_positions[touch.id] = (touch.x, touch.y, touch.id)
            return True

    def on_touch_move(self, touch):
        for s in self.sliders:
            if s.on_touch_move(touch):
                self.update_color()
                return True
        if touch.id in self.touch_positions:
            self.x += touch.x - self.touch_positions[touch.id][0]
            self.y += touch.y - self.touch_positions[touch.id][1]
            self.touch_positions[touch.id] = (touch.x, touch.y, touch.id)
            return True

    def on_touch_up(self, touch):
        for s in self.sliders:
            if s.on_touch_up(touch):
                self.update_color()
                return True
        if touch.id in self.touch_positions:
            del self.touch_positions[touch.id]



class Canvas(MTWidget):
    def __init__(self, min=0, max=100, pos=(0,0), size=(640,480)):
        MTWidget.__init__(self, pos=pos, size=size)
        self.touch_positions = {}
        self.fbo = Fbo(size=(self.width, self.height), with_depthbuffer=False)
        self.bgcolor = (0,0,0,1)
        self.color = (0,1,0,1.0)
        set_brush(os.path.join(current_dir, 'brushes', 'brush_particle.png'))
        self.paint_queue = collections.deque()
        self.do_paint_queue = False
        self.clear()

    def clear(self):
        self.fbo.bind()
        glClearColor(*self.bgcolor)
        glClear(GL_COLOR_BUFFER_BIT)
        glClearColor(1,0,1,1)
        self.fbo.release()

    def draw(self):
        # draw the whole queue
        if self.do_paint_queue:
            self.fbo.bind()
            while True:
                try:
                    item = self.paint_queue.pop()
                except IndexError:
                    break
                color, positions = item
                set_color(*color)
                paintLine(positions)
            self.fbo.release()
            self.do_paint_queue = False
        set_color(1,1,1,1)
        drawTexturedRectangle(self.fbo.texture, size=(self.width, self.height))

    def on_resize(self, w, h):
        if self.fbo.size == (w, h):
            return
        del self.fbo
        self.fbo = Fbo(size=(w, h), push_viewport=False)

    def on_touch_down(self, touch):
        self.paint_queue.appendleft((self.color, (touch.x,touch.y,touch.x,touch.y)))
        self.do_paint_queue = True
        self.touch_positions[touch.id] = (touch.x, touch.y)

    def on_touch_move(self, touch):
        if self.touch_positions.has_key(touch.id):
            ox,oy = self.touch_positions[touch.id]
            self.paint_queue.appendleft((self.color, (ox,oy,touch.x,touch.y)))
            self.do_paint_queue = True
            self.touch_positions[touch.id] = (touch.x, touch.y)

    def on_touch_up(self, touch):
        if self.touch_positions.has_key(touch.id):
            del self.touch_positions[touch.id]

def update_brush(brush, size, *largs):
    set_brush(brush, size=size)

def clear_canvas(canvas, *largs):
    canvas.clear()

def pymt_plugin_activate(root, ctx):

    ctx.canvas = Canvas(pos=(40,40),size=(root.width,root.height))
    def resizeCanvas(w,h):
        ctx.canvas.size = (w, h)
        ctx.btnclear.pos = (0, root.height - 50)
    root.push_handlers(on_resize=resizeCanvas)

    root.add_widget(ctx.canvas)
    ctx.slider = MTPaintColorPicker(size=(220,290), targets=[ctx.canvas])
    root.add_widget(ctx.slider)

    ctx.btnclear = MTButton(label='Clear', size=(50,50), pos=(0, root.height-50))
    ctx.btnclear.push_handlers(on_press=curry(clear_canvas, ctx.canvas))
    root.add_widget(ctx.btnclear)

    ctx.brushes = PaintBrushLayout(pos=(300, 0), size_hint=(None, None))
    for brush in glob(os.path.join(current_dir, 'brushes', '*.png')):
        button = MTImageButton(filename=brush)
        button.push_handlers(on_press=curry(update_brush, brush,
                                            ctx.slider.brush_size))
        ctx.brushes.add_widget(button)
    root.add_widget(ctx.brushes)



def pymt_plugin_deactivate(root, ctx):
    root.remove_widget(ctx.canvas)
    root.remove_widget(ctx.slider)

if __name__ == '__main__':
    w = MTWindow()
    ctx = MTContext()
    pymt_plugin_activate(w, ctx)
    runTouchApp()
    pymt_plugin_deactivate(w, ctx)
Something went wrong with that request. Please try again.