root / core / app / canvas / layer.py @ 39
View | Annotate | Download (3.9 KB)
| 1 | from __future__ import with_statement |
|---|---|
| 2 | from pymt import * |
| 3 | from pyglet.gl import * |
| 4 | |
| 5 | class specialScatterW(MTScatterWidget): |
| 6 | def __init__(self, **kwargs): |
| 7 | super(specialScatterW, self).__init__(**kwargs) |
| 8 | |
| 9 | def on_touch_down(self, touches, touchID, x, y): |
| 10 | # if the touch isnt on the widget we do nothing |
| 11 | if not self.collide_point(x,y): |
| 12 | return False |
| 13 | |
| 14 | # let the child widgets handle the event if they want |
| 15 | lx, ly = self.to_local(x,y) |
| 16 | if super(MTScatterWidget, self).on_touch_down(touches, touchID, lx, ly): |
| 17 | return True |
| 18 | |
| 19 | # if the children didnt handle it, we bring to front & keep track |
| 20 | # of touches for rotate/scale/zoom action |
| 21 | #self.bring_to_front() |
| 22 | self.touches[touchID] = Vector(x,y) |
| 23 | return True |
| 24 | |
| 25 | |
| 26 | class Layer(specialScatterW): |
| 27 | def __init__(self, **kwargs): |
| 28 | kwargs.setdefault('layer_manager', None)
|
| 29 | self.moveable = kwargs.get('moveable')
|
| 30 | self.layer_manager = kwargs.get('layer_manager')
|
| 31 | if self.moveable == False : |
| 32 | kwargs.setdefault('do_scale', False)
|
| 33 | kwargs.setdefault('do_rotation', False)
|
| 34 | kwargs.setdefault('do_translation', False)
|
| 35 | super(Layer, self).__init__(**kwargs) |
| 36 | self.fbo = Fbo(size=(self.width, self.height), with_depthbuffer=False) |
| 37 | self.color = kwargs.get('color')
|
| 38 | set_brush('brushes/brush_particle.png')
|
| 39 | self.layer_clear() |
| 40 | set_brush_size(25) |
| 41 | self.brush_color=(0,0,0,1) |
| 42 | |
| 43 | def layer_clear(self): |
| 44 | self.fbo.bind() |
| 45 | glClearColor(1,1,1,1) |
| 46 | glClear(GL_COLOR_BUFFER_BIT) |
| 47 | set_color(self.color) |
| 48 | drawRectangle((0,0),(self.width,self.height)) |
| 49 | self.fbo.release() |
| 50 | |
| 51 | def on_touch_down(self, touches, touchID, x, y): |
| 52 | if self.collide_point(x,y): |
| 53 | self.touches[touchID] = self.to_local(x,y) |
| 54 | if self.layer_manager.mode == "draw": |
| 55 | self.fbo.bind() |
| 56 | set_color(*self.brush_color) |
| 57 | set_brush('brushes/brush_particle.png')
|
| 58 | set_brush_size(25) |
| 59 | drawCircle(pos=self.to_local(x,y), radius=1) |
| 60 | self.fbo.release() |
| 61 | elif self.layer_manager.mode == "zoom": |
| 62 | super(Layer, self).on_touch_down(touches, touchID, x, y) |
| 63 | |
| 64 | return True |
| 65 | |
| 66 | def on_touch_move(self, touches, touchID, x, y): |
| 67 | if touchID in self.touches: |
| 68 | if self.layer_manager.mode == "zoom": |
| 69 | super(Layer, self).on_touch_move(touches, touchID, x, y) |
| 70 | elif self.layer_manager.mode == "draw": |
| 71 | cur_pos = self.to_local(x,y) |
| 72 | ox,oy = self.touches[touchID] |
| 73 | self.fbo.bind() |
| 74 | set_color(*self.brush_color) |
| 75 | set_brush('brushes/brush_particle.png')
|
| 76 | set_brush_size(25) |
| 77 | paintLine((ox,oy,cur_pos[0],cur_pos[1])) |
| 78 | self.fbo.release() |
| 79 | self.touches[touchID] = self.to_local(x,y) |
| 80 | return True |
| 81 | |
| 82 | def on_touch_up(self, touches, touchID, x,y): |
| 83 | if touchID in self.touches: |
| 84 | del self.touches[touchID] |
| 85 | return True |
| 86 | |
| 87 | |
| 88 | def draw(self): |
| 89 | with gx_matrix: |
| 90 | #glColor4f(*self.color) |
| 91 | if self.moveable == False : |
| 92 | glColor4f(*self.color) |
| 93 | drawRectangle((0,0),(self.width,self.height)) |
| 94 | with gx_blending: |
| 95 | glColor4f(self.color[0],self.color[1],self.color[2],0.5) |
| 96 | drawRectangle((0,0),(self.width,self.height)) |
| 97 | drawTexturedRectangle(self.fbo.texture, (-6,-6),(self.width+12,self.height+12)) |
| 98 | |
| 99 | def set_mode(self,mode): |
| 100 | self.mode = mode |
| 101 | |
| 102 | def set_brush_color(self,color): |
| 103 | self.brush_color = color |
