Revision 40
| core/app/canvas/layer.py (revision 40) | ||
|---|---|---|
| 21 | 21 |
#self.bring_to_front() |
| 22 | 22 |
self.touches[touchID] = Vector(x,y) |
| 23 | 23 |
return True |
| 24 |
|
|
| 25 |
|
|
| 26 |
class Layer(specialScatterW): |
|
| 24 |
|
|
| 25 |
class AbstractLayer(specialScatterW): |
|
| 27 | 26 |
def __init__(self, **kwargs): |
| 28 | 27 |
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) |
|
| 28 |
self.layer_manager = kwargs.get('layer_manager')
|
|
| 29 |
super(AbstractLayer, self).__init__(**kwargs) |
|
| 30 |
self.fbo = Fbo(size=(self.width, self.height), with_depthbuffer=False) |
|
| 37 | 31 |
self.color = kwargs.get('color')
|
| 38 |
set_brush('brushes/brush_particle.png')
|
|
| 39 |
self.layer_clear() |
|
| 40 |
set_brush_size(25) |
|
| 32 |
set_brush('brushes/brush_particle.png',25)
|
|
| 33 |
#self.layer_clear() |
|
| 41 | 34 |
self.brush_color=(0,0,0,1) |
| 35 |
self.id = kwargs.get('id')
|
|
| 42 | 36 |
|
| 43 | 37 |
def layer_clear(self): |
| 44 | 38 |
self.fbo.bind() |
| 45 |
glClearColor(1,1,1,1) |
|
| 46 |
glClear(GL_COLOR_BUFFER_BIT) |
|
| 39 |
#glClearColor(1,1,1,1) |
|
| 40 |
#glClear(GL_COLOR_BUFFER_BIT) |
|
| 47 | 41 |
set_color(self.color) |
| 48 | 42 |
drawRectangle((0,0),(self.width,self.height)) |
| 49 | 43 |
self.fbo.release() |
| ... | ... | |
| 51 | 45 |
def on_touch_down(self, touches, touchID, x, y): |
| 52 | 46 |
if self.collide_point(x,y): |
| 53 | 47 |
self.touches[touchID] = self.to_local(x,y) |
| 48 |
if len(touches)==2 : |
|
| 49 |
if touches[touchID].is_double_tap: |
|
| 50 |
self.layer_manager.move_layer_down(self.id) |
|
| 51 |
elif touches[touchID].is_double_tap: |
|
| 52 |
self.layer_manager.move_layer_up(self.id) |
|
| 54 | 53 |
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() |
|
| 54 |
with self.fbo: |
|
| 55 |
set_color(*self.brush_color) |
|
| 56 |
set_brush('brushes/brush_particle.png',25)
|
|
| 57 |
drawCircle(pos=self.to_local(x,y), radius=1) |
|
| 61 | 58 |
elif self.layer_manager.mode == "zoom": |
| 62 |
super(Layer, self).on_touch_down(touches, touchID, x, y) |
|
| 63 |
|
|
| 59 |
super(AbstractLayer, self).on_touch_down(touches, touchID, x, y) |
|
| 64 | 60 |
return True |
| 65 | 61 |
|
| 66 | 62 |
def on_touch_move(self, touches, touchID, x, y): |
| 67 | 63 |
if touchID in self.touches: |
| 68 | 64 |
if self.layer_manager.mode == "zoom": |
| 69 |
super(Layer, self).on_touch_move(touches, touchID, x, y) |
|
| 65 |
super(AbstractLayer, self).on_touch_move(touches, touchID, x, y) |
|
| 70 | 66 |
elif self.layer_manager.mode == "draw": |
| 71 | 67 |
cur_pos = self.to_local(x,y) |
| 72 | 68 |
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() |
|
| 69 |
with self.fbo: |
|
| 70 |
set_color(*self.brush_color) |
|
| 71 |
set_brush('brushes/brush_particle.png',25)
|
|
| 72 |
paintLine((ox,oy,cur_pos[0],cur_pos[1])) |
|
| 73 |
self.fbo.release() |
|
| 79 | 74 |
self.touches[touchID] = self.to_local(x,y) |
| 80 | 75 |
return True |
| 81 | 76 |
|
| ... | ... | |
| 83 | 78 |
if touchID in self.touches: |
| 84 | 79 |
del self.touches[touchID] |
| 85 | 80 |
return True |
| 81 |
|
|
| 82 |
def set_brush_color(self,color): |
|
| 83 |
self.brush_color = color |
|
| 86 | 84 |
|
| 85 |
|
|
| 87 | 86 |
|
| 87 |
class NormalLayer(AbstractLayer): |
|
| 88 |
def __init__(self, **kwargs): |
|
| 89 |
kwargs.setdefault('layer_manager', None)
|
|
| 90 |
self.moveable = kwargs.get('moveable')
|
|
| 91 |
if self.moveable == False : |
|
| 92 |
kwargs.setdefault('do_scale', False)
|
|
| 93 |
kwargs.setdefault('do_rotation', False)
|
|
| 94 |
kwargs.setdefault('do_translation', False)
|
|
| 95 |
super(NormalLayer, self).__init__(**kwargs) |
|
| 96 |
|
|
| 88 | 97 |
def draw(self): |
| 89 | 98 |
with gx_matrix: |
| 90 | 99 |
#glColor4f(*self.color) |
| 91 | 100 |
if self.moveable == False : |
| 92 | 101 |
glColor4f(*self.color) |
| 93 | 102 |
drawRectangle((0,0),(self.width,self.height)) |
| 94 |
with gx_blending: |
|
| 95 |
glColor4f(self.color[0],self.color[1],self.color[2],0.5) |
|
| 103 |
else: |
|
| 104 |
glColor4f(self.color[0],self.color[1],self.color[2],self.color[3]) |
|
| 96 | 105 |
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 |
|
| 106 |
drawTexturedRectangle(self.fbo.texture, (0,0),(self.width,self.height)) |
|
| core/app/canvas/canvas_new.py (revision 40) | ||
|---|---|---|
| 6 | 6 |
class Canvas(MTScatterWidget): |
| 7 | 7 |
def __init__(self, **kwargs): |
| 8 | 8 |
super(Canvas, self).__init__(**kwargs) |
| 9 |
self.canvas_area = CanvasArea(pos=(10,10),size=(500,400)) |
|
| 9 |
self.canvas_area = MTStencilContainer(pos=(20,20),size=(500,400)) |
|
| 10 | 10 |
self.add_widget(self.canvas_area) |
| 11 |
self.layer_manager = LayerManager(pos=(20,20),canvas=self) |
|
| 12 |
self.canvas_area.add_widget(self.layer_manager) |
|
| 13 |
self.fbo = Fbo(size=(self.width, self.height), with_depthbuffer=False) |
|
| 11 | 14 |
|
| 12 | 15 |
def draw(self): |
| 13 | 16 |
with gx_matrix: |
| 14 | 17 |
glColor4f(0,0,0,1) |
| 15 | 18 |
drawRectangle((0,0),(self.width,self.height)) |
| 16 |
|
|
| 19 |
|
|
| 17 | 20 |
def set_mode(self,mode): |
| 18 |
self.canvas_area.layer_manager.set_mode(mode) |
|
| 21 |
self.layer_manager.set_mode(mode) |
|
| 19 | 22 |
|
| 20 |
class CanvasArea(MTStencilContainer): |
|
| 21 |
def __init__(self, **kwargs): |
|
| 22 |
super(CanvasArea, self).__init__(**kwargs) |
|
| 23 |
self.layer_manager = LayerManager(pos=(10,10),canvas=self) |
|
| 24 |
self.add_widget(self.layer_manager) |
|
| 23 |
def create_layer(self,pos=(0,0),size=(200,200),color=(0,0,0,0.5)): |
|
| 24 |
self.layer_manager.create_layer(pos=pos,size=size,color=color) |
|
| 25 | 25 |
|
| 26 |
#def draw(self): |
|
| 27 |
# pass |
|
| 28 |
#self.draw() |
|
| 26 |
def save_image(self): |
|
| 27 |
with self.fbo: |
|
| 28 |
self.layer_manager.background.dispatch_event('on_draw')
|
|
| 29 |
for layer in self.layer_manager.layer_list : |
|
| 30 |
layer.dispatch_event('on_draw')
|
|
| 31 |
data = (self.fbo.texture).get_image_data() |
|
| 32 |
data.save(file='test.png') |
|
| 29 | 33 |
|
| 30 |
|
|
| 31 |
|
|
| 32 | 34 |
if __name__ == '__main__': |
| 33 | 35 |
w = MTWindow() |
| 34 |
canvas = Canvas(size=(520,420),pos=(w.width/2-260,w.height/2-120)) |
|
| 36 |
canvas = Canvas(size=(540,440),pos=(w.width/2-260,w.height/2-120)) |
|
| 35 | 37 |
w.add_widget(canvas) |
| 36 | 38 |
draw_but = MTButton(label="Painting") |
| 37 | 39 |
w.add_widget(draw_but) |
| ... | ... | |
| 39 | 41 |
def on_press(touchID, x, y): |
| 40 | 42 |
canvas.set_mode(mode='draw') |
| 41 | 43 |
zoom_but = MTButton(label="Layering",pos=(draw_but.width+5,0)) |
| 44 |
w.add_widget(zoom_but) |
|
| 42 | 45 |
@zoom_but.event |
| 43 | 46 |
def on_press(touchID, x, y): |
| 44 | 47 |
canvas.set_mode(mode='zoom') |
| 45 |
w.add_widget(zoom_but) |
|
| 48 |
|
|
| 49 |
add_but = MTButton(label="Save",pos=(draw_but.width+zoom_but.width+10,0)) |
|
| 50 |
@add_but.event |
|
| 51 |
def on_press(touchID, x, y): |
|
| 52 |
canvas.save_image() |
|
| 53 |
w.add_widget(add_but) |
|
| 54 |
|
|
| 55 |
canvas.create_layer(pos=(100,100),size=(200,200),color=(1,0,0,0.8)) |
|
| 56 |
canvas.create_layer(size=(300,200),color=(0,1,0,0.8)) |
|
| 57 |
canvas.create_layer(size=(250,150),color=(0,0,1,0.8)) |
|
| 46 | 58 |
runTouchApp() |
| 47 | 59 |
|
| 48 | 60 |
|
| core/app/canvas/layermanager.py (revision 40) | ||
|---|---|---|
| 13 | 13 |
self.canvas = kwargs.get('canvas')
|
| 14 | 14 |
self.size = self.canvas.size |
| 15 | 15 |
self.layer_list = [] |
| 16 |
self.background = Layer(size=self.canvas.size,color=(1,1,1,1),moveable=False,layer_manager=self) |
|
| 16 |
self.background = NormalLayer(size=self.canvas.size,color=(1,1,1,1),moveable=False,layer_manager=self) |
|
| 17 | 17 |
self.add_widget(self.background) |
| 18 |
self.layer1 = Layer(pos=(100,100),size=(200,200),color=(1,0,0,0.5),layer_manager=self) |
|
| 19 |
self.add_widget(self.layer1) |
|
| 20 |
self.layer2 = Layer(size=(300,200),color=(0,1,0,0.5),layer_manager=self) |
|
| 21 |
self.add_widget(self.layer2) |
|
| 22 |
self.layer3 = Layer(size=(250,150),color=(0,0,1,0.5),layer_manager=self) |
|
| 23 |
self.add_widget(self.layer3) |
|
| 18 |
|
|
| 24 | 19 |
|
| 25 |
self.layer_list.append(self.layer1) |
|
| 26 |
self.layer_list.append(self.layer2) |
|
| 27 |
self.layer_list.append(self.layer3) |
|
| 28 |
|
|
| 29 | 20 |
def set_mode(self,value): |
| 30 | 21 |
self.mode = value |
| 31 | 22 |
|
| 23 |
def move_layer_up(self,layer_id): #double tapp on the layer to move up one layer at a time |
|
| 24 |
if layer_id < len(self.layer_list)-1: |
|
| 25 |
a = self.layer_list[layer_id] |
|
| 26 |
b = self.layer_list[layer_id+1] |
|
| 27 |
a.id = layer_id+1 |
|
| 28 |
b.id = layer_id |
|
| 29 |
|
|
| 30 |
self.layer_list[layer_id] = b |
|
| 31 |
self.layer_list[layer_id+1] = a |
|
| 32 |
|
|
| 33 |
for layer in self.layer_list: |
|
| 34 |
self.remove_widget(layer) |
|
| 35 |
|
|
| 36 |
for layer in self.layer_list: |
|
| 37 |
self.add_widget(layer) |
|
| 38 |
|
|
| 39 |
def move_layer_down(self,layer_id): #hold one finger down and double tapp with another on the layer to move down one layer at a time |
|
| 40 |
if layer_id > 0: |
|
| 41 |
a = self.layer_list[layer_id] |
|
| 42 |
b = self.layer_list[layer_id-1] |
|
| 43 |
a.id = layer_id-1 |
|
| 44 |
b.id = layer_id |
|
| 45 |
|
|
| 46 |
self.layer_list[layer_id] = b |
|
| 47 |
self.layer_list[layer_id-1] = a |
|
| 48 |
|
|
| 49 |
for layer in self.layer_list: |
|
| 50 |
self.remove_widget(layer) |
|
| 51 |
|
|
| 52 |
for layer in self.layer_list: |
|
| 53 |
self.add_widget(layer) |
|
| 54 |
|
|
| 55 |
|
|
| 56 |
def create_layer(self,pos=(0,0),size=(200,200),color=(0,0,0,0.5)): |
|
| 57 |
layer = NormalLayer(id=len(self.layer_list),pos=pos,size=size,color=color,layer_manager=self) |
|
| 58 |
self.add_widget(layer) |
|
| 59 |
self.layer_list.append(layer) |
|
| 32 | 60 |
|
| 61 |
|
|
| 62 |
|
|
| 33 | 63 |
|
| 64 |
|
|
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 34 | 68 |
|
Also available in: Unified diff
