Revision 42
| core/app/canvas/canvas_new.py (revision 42) | ||
|---|---|---|
| 1 |
from __future__ import with_statement |
|
| 2 |
from pymt import * |
|
| 3 |
from pyglet.gl import * |
|
| 4 |
from layermanager import * |
|
| 5 |
|
|
| 6 |
class Canvas(MTScatterWidget): |
|
| 7 |
def __init__(self, **kwargs): |
|
| 8 |
super(Canvas, self).__init__(**kwargs) |
|
| 9 |
self.canvas_area = MTStencilContainer(pos=(20,20),size=(500,400)) |
|
| 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) |
|
| 14 |
|
|
| 15 |
def draw(self): |
|
| 16 |
with gx_matrix: |
|
| 17 |
glColor4f(0,0,0,1) |
|
| 18 |
drawRectangle((0,0),(self.width,self.height)) |
|
| 19 |
|
|
| 20 |
def set_mode(self,mode): |
|
| 21 |
self.layer_manager.set_mode(mode) |
|
| 22 |
|
|
| 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 |
|
|
| 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') |
|
| 33 |
|
|
| 34 |
if __name__ == '__main__': |
|
| 35 |
w = MTWindow() |
|
| 36 |
canvas = Canvas(size=(540,440),pos=(w.width/2-260,w.height/2-120)) |
|
| 37 |
w.add_widget(canvas) |
|
| 38 |
draw_but = MTButton(label="Painting") |
|
| 39 |
w.add_widget(draw_but) |
|
| 40 |
@draw_but.event |
|
| 41 |
def on_press(touchID, x, y): |
|
| 42 |
canvas.set_mode(mode='draw') |
|
| 43 |
zoom_but = MTButton(label="Layering",pos=(draw_but.width+5,0)) |
|
| 44 |
w.add_widget(zoom_but) |
|
| 45 |
@zoom_but.event |
|
| 46 |
def on_press(touchID, x, y): |
|
| 47 |
canvas.set_mode(mode='zoom') |
|
| 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)) |
|
| 58 |
runTouchApp() |
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| core/app/canvas/canvas_old.py (revision 42) | ||
|---|---|---|
| 1 |
from __future__ import with_statement |
|
| 2 |
from pymt import * |
|
| 3 |
from pyglet.gl import * |
|
| 4 |
from numpy import * |
|
| 5 |
import string |
|
| 6 |
|
|
| 7 |
class Canvas(MTScatterWidget): |
|
| 8 |
def __init__(self, **kwargs): |
|
| 9 |
super(Canvas, self).__init__(**kwargs) |
|
| 10 |
self.width = 500 |
|
| 11 |
self.height = 400 |
|
| 12 |
self.fbo = Fbo(size=(self.width, self.height), with_depthbuffer=False) |
|
| 13 |
self.color = (0,1,0,1.0) |
|
| 14 |
set_brush('brushes/brush_particle.png')
|
|
| 15 |
self.layer_clear() |
|
| 16 |
self.touch_positions = {}
|
|
| 17 |
self.mode = "zoom" |
|
| 18 |
set_brush_size(25) |
|
| 19 |
self.brush_color=(0,0,0,1) |
|
| 20 |
|
|
| 21 |
def layer_clear(self): |
|
| 22 |
self.fbo.bind() |
|
| 23 |
glClearColor(0,0,0,0) |
|
| 24 |
glClear(GL_COLOR_BUFFER_BIT) |
|
| 25 |
self.fbo.release() |
|
| 26 |
|
|
| 27 |
def on_touch_down(self, touches, touchID, x, y): |
|
| 28 |
if self.collide_point(x,y): |
|
| 29 |
self.touch_positions[touchID] = self.to_local(x,y) |
|
| 30 |
if self.mode == "draw": |
|
| 31 |
self.fbo.bind() |
|
| 32 |
set_color(*self.brush_color) |
|
| 33 |
set_brush('brushes/brush_particle.png')
|
|
| 34 |
set_brush_size(25) |
|
| 35 |
drawCircle(pos=self.to_local(x,y), radius=1) |
|
| 36 |
self.fbo.release() |
|
| 37 |
elif self.mode == "zoom": |
|
| 38 |
super(Canvas, self).on_touch_down(touches, touchID, x, y) |
|
| 39 |
elif self.mode == "smudge": |
|
| 40 |
self.do_smudge(self.touch_positions[touchID][0],self.touch_positions[touchID][1]) |
|
| 41 |
|
|
| 42 |
return True |
|
| 43 |
|
|
| 44 |
def on_touch_move(self, touches, touchID, x, y): |
|
| 45 |
if self.touch_positions.has_key(touchID): |
|
| 46 |
if self.mode == "zoom": |
|
| 47 |
super(Canvas, self).on_touch_move(touches, touchID, x, y) |
|
| 48 |
elif self.mode == "draw": |
|
| 49 |
cur_pos = self.to_local(x,y) |
|
| 50 |
ox,oy = self.touch_positions[touchID] |
|
| 51 |
self.fbo.bind() |
|
| 52 |
set_color(*self.brush_color) |
|
| 53 |
set_brush('brushes/brush_particle.png')
|
|
| 54 |
set_brush_size(25) |
|
| 55 |
paintLine((ox,oy,cur_pos[0],cur_pos[1])) |
|
| 56 |
self.fbo.release() |
|
| 57 |
self.touch_positions[touchID] = self.to_local(x,y) |
|
| 58 |
return True |
|
| 59 |
|
|
| 60 |
def draw(self): |
|
| 61 |
with gx_matrix: |
|
| 62 |
glColor4f(1,1,1,1) |
|
| 63 |
drawRectangle((-6,-6),(self.width+12,self.height+12)) |
|
| 64 |
glScaled(float(self.width)/500, float(self.height)/400, 2.0) |
|
| 65 |
with gx_blending: |
|
| 66 |
drawTexturedRectangle(self.fbo.texture, (-6,-6),(self.width+12,self.height+12)) |
|
| 67 |
|
|
| 68 |
def set_mode(self,mode): |
|
| 69 |
self.mode = mode |
|
| 70 |
|
|
| 71 |
def set_brush_color(self,color): |
|
| 72 |
self.brush_color = color |
|
| 73 |
|
|
| 74 |
|
|
| 75 |
def do_smudge(self, x, y): |
|
| 76 |
# First, extract region from texture |
|
| 77 |
region = self.fbo.texture.get_region(int(x) - 16, int(y) - 16, 32, 32) |
|
| 78 |
data = region.get_image_data() |
|
| 79 |
# Extract pixels |
|
| 80 |
format = 'RGB' |
|
| 81 |
pitch = 32 * len(format) |
|
| 82 |
pixel_data = data.get_data(format, pitch) |
|
| 83 |
pixels_list = map(ord, list(pixel_data)) |
|
| 84 |
|
|
| 85 |
pixels = zeros((32,32,3), float) |
|
| 86 |
z = 0 |
|
| 87 |
for i in range(0,31): |
|
| 88 |
for j in range(0,31): |
|
| 89 |
for k in range(0,2): |
|
| 90 |
pixels[i,j,k] = pixels_list[z] |
|
| 91 |
z += 1 |
|
| 92 |
#work |
|
| 93 |
state=zeros((32,32,3), float) |
|
| 94 |
rate = 0.5 |
|
| 95 |
|
|
| 96 |
#i = 32 * 32; |
|
| 97 |
for i in range (32*32,0,-1) : |
|
| 98 |
iy = i >> 5 |
|
| 99 |
ix = i & 0x1f |
|
| 100 |
# is it not on the circle of radius sqrt(120) at location 16,16? |
|
| 101 |
if (ix - 16) * (ix - 16) + (iy - 16) * (iy - 16) > 120 : |
|
| 102 |
continue |
|
| 103 |
# it is on the circle, so grab it |
|
| 104 |
|
|
| 105 |
# Get color |
|
| 106 |
r = float(pixels[ix,iy,0])/float(255) |
|
| 107 |
g = float(pixels[ix,iy,1])/float(255) |
|
| 108 |
b = float(pixels[ix,iy,02])/float(255) |
|
| 109 |
#print r,g,b |
|
| 110 |
|
|
| 111 |
state[ix,iy,0] = rate * state[ix,iy,0] + (1.0 - rate) * r |
|
| 112 |
state[ix,iy,1] = rate * state[ix,iy,1] + (1.0 - rate) * g |
|
| 113 |
state[ix,iy,2] = rate * state[ix,iy,2] + (1.0 - rate) * b |
|
| 114 |
|
|
| 115 |
#put back pixel |
|
| 116 |
z = 0 |
|
| 117 |
for i in range(0,31): |
|
| 118 |
for j in range(0,31): |
|
| 119 |
for k in range(0,2): |
|
| 120 |
pixels_list[z] = pixels[i,j,k] |
|
| 121 |
z += 1 |
|
| 122 |
|
|
| 123 |
data.set_data(format, pitch, ''.join(map(chr, pixels_list))) |
|
| 124 |
texture = data.get_texture() |
|
| 125 |
|
|
| 126 |
# Draw texture on Fbo |
|
| 127 |
with self.fbo: |
|
| 128 |
drawTexturedRectangle(texture, pos=(x - 16, y - 16), size=(32, 32)) |
|
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| core/app/canvas/canvas.py (revision 42) | ||
|---|---|---|
| 1 | 1 |
from __future__ import with_statement |
| 2 | 2 |
from pymt import * |
| 3 | 3 |
from pyglet.gl import * |
| 4 |
from numpy import * |
|
| 5 |
import string |
|
| 4 |
from layermanager import * |
|
| 6 | 5 |
|
| 7 | 6 |
class Canvas(MTScatterWidget): |
| 8 | 7 |
def __init__(self, **kwargs): |
| 9 | 8 |
super(Canvas, self).__init__(**kwargs) |
| 10 |
self.width = 500 |
|
| 11 |
self.height = 400 |
|
| 9 |
self.canvas_area = MTStencilContainer(pos=(20,20),size=(self.width-40,self.height-40)) |
|
| 10 |
self.add_widget(self.canvas_area) |
|
| 11 |
self.layer_manager = LayerManager(pos=(20,20),canvas=self,size=(self.width-40,self.height-40)) |
|
| 12 |
self.canvas_area.add_widget(self.layer_manager) |
|
| 12 | 13 |
self.fbo = Fbo(size=(self.width, self.height), with_depthbuffer=False) |
| 13 |
self.color = (0,1,0,1.0) |
|
| 14 |
set_brush('brushes/brush_particle.png')
|
|
| 15 |
self.layer_clear() |
|
| 16 |
self.touch_positions = {}
|
|
| 17 |
self.mode = "zoom" |
|
| 18 |
set_brush_size(25) |
|
| 19 |
self.brush_color=(0,0,0,1) |
|
| 20 |
|
|
| 21 |
def layer_clear(self): |
|
| 22 |
self.fbo.bind() |
|
| 23 |
glClearColor(0,0,0,0) |
|
| 24 |
glClear(GL_COLOR_BUFFER_BIT) |
|
| 25 |
self.fbo.release() |
|
| 26 |
|
|
| 27 |
def on_touch_down(self, touches, touchID, x, y): |
|
| 28 |
if self.collide_point(x,y): |
|
| 29 |
self.touch_positions[touchID] = self.to_local(x,y) |
|
| 30 |
if self.mode == "draw": |
|
| 31 |
self.fbo.bind() |
|
| 32 |
set_color(*self.brush_color) |
|
| 33 |
set_brush('brushes/brush_particle.png')
|
|
| 34 |
set_brush_size(25) |
|
| 35 |
drawCircle(pos=self.to_local(x,y), radius=1) |
|
| 36 |
self.fbo.release() |
|
| 37 |
elif self.mode == "zoom": |
|
| 38 |
super(Canvas, self).on_touch_down(touches, touchID, x, y) |
|
| 39 |
elif self.mode == "smudge": |
|
| 40 |
self.do_smudge(self.touch_positions[touchID][0],self.touch_positions[touchID][1]) |
|
| 41 |
|
|
| 42 |
return True |
|
| 43 |
|
|
| 44 |
def on_touch_move(self, touches, touchID, x, y): |
|
| 45 |
if self.touch_positions.has_key(touchID): |
|
| 46 |
if self.mode == "zoom": |
|
| 47 |
super(Canvas, self).on_touch_move(touches, touchID, x, y) |
|
| 48 |
elif self.mode == "draw": |
|
| 49 |
cur_pos = self.to_local(x,y) |
|
| 50 |
ox,oy = self.touch_positions[touchID] |
|
| 51 |
self.fbo.bind() |
|
| 52 |
set_color(*self.brush_color) |
|
| 53 |
set_brush('brushes/brush_particle.png')
|
|
| 54 |
set_brush_size(25) |
|
| 55 |
paintLine((ox,oy,cur_pos[0],cur_pos[1])) |
|
| 56 |
self.fbo.release() |
|
| 57 |
self.touch_positions[touchID] = self.to_local(x,y) |
|
| 58 |
return True |
|
| 59 |
|
|
| 14 |
|
|
| 60 | 15 |
def draw(self): |
| 61 | 16 |
with gx_matrix: |
| 62 |
glColor4f(1,1,1,1) |
|
| 63 |
drawRectangle((-6,-6),(self.width+12,self.height+12)) |
|
| 64 |
glScaled(float(self.width)/500, float(self.height)/400, 2.0) |
|
| 65 |
with gx_blending: |
|
| 66 |
drawTexturedRectangle(self.fbo.texture, (-6,-6),(self.width+12,self.height+12)) |
|
| 67 |
|
|
| 17 |
glColor4f(0,0,0,1) |
|
| 18 |
drawRectangle((0,0),(self.width,self.height)) |
|
| 19 |
|
|
| 68 | 20 |
def set_mode(self,mode): |
| 69 |
self.mode = mode |
|
| 21 |
self.layer_manager.set_mode(mode) |
|
| 22 |
|
|
| 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) |
|
| 70 | 25 |
|
| 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') |
|
| 33 |
|
|
| 71 | 34 |
def set_brush_color(self,color): |
| 72 |
self.brush_color = color |
|
| 35 |
self.layer_manager.set_brush_color(color) |
|
| 36 |
|
|
| 37 |
def set_brush(self,sprite,size): |
|
| 38 |
self.layer_manager.set_brush(sprite,size) |
|
| 73 | 39 |
|
| 74 |
|
|
| 75 |
def do_smudge(self, x, y): |
|
| 76 |
# First, extract region from texture |
|
| 77 |
region = self.fbo.texture.get_region(int(x) - 16, int(y) - 16, 32, 32) |
|
| 78 |
data = region.get_image_data() |
|
| 79 |
# Extract pixels |
|
| 80 |
format = 'RGB' |
|
| 81 |
pitch = 32 * len(format) |
|
| 82 |
pixel_data = data.get_data(format, pitch) |
|
| 83 |
pixels_list = map(ord, list(pixel_data)) |
|
| 40 |
def collide_point(self, x,y): |
|
| 41 |
local_coords = self.to_local(x,y) |
|
| 42 |
if local_coords[0] > 0 and local_coords[0] < self.width \ |
|
| 43 |
and local_coords[1] > 0 and local_coords[1] < self.height: |
|
| 44 |
return True |
|
| 45 |
else: |
|
| 46 |
return False |
|
| 84 | 47 |
|
| 85 |
pixels = zeros((32,32,3), float) |
|
| 86 |
z = 0 |
|
| 87 |
for i in range(0,31): |
|
| 88 |
for j in range(0,31): |
|
| 89 |
for k in range(0,2): |
|
| 90 |
pixels[i,j,k] = pixels_list[z] |
|
| 91 |
z += 1 |
|
| 92 |
#work |
|
| 93 |
state=zeros((32,32,3), float) |
|
| 94 |
rate = 0.5 |
|
| 95 |
|
|
| 96 |
#i = 32 * 32; |
|
| 97 |
for i in range (32*32,0,-1) : |
|
| 98 |
iy = i >> 5 |
|
| 99 |
ix = i & 0x1f |
|
| 100 |
# is it not on the circle of radius sqrt(120) at location 16,16? |
|
| 101 |
if (ix - 16) * (ix - 16) + (iy - 16) * (iy - 16) > 120 : |
|
| 102 |
continue |
|
| 103 |
# it is on the circle, so grab it |
|
| 104 |
|
|
| 105 |
# Get color |
|
| 106 |
r = float(pixels[ix,iy,0])/float(255) |
|
| 107 |
g = float(pixels[ix,iy,1])/float(255) |
|
| 108 |
b = float(pixels[ix,iy,02])/float(255) |
|
| 109 |
#print r,g,b |
|
| 110 |
|
|
| 111 |
state[ix,iy,0] = rate * state[ix,iy,0] + (1.0 - rate) * r |
|
| 112 |
state[ix,iy,1] = rate * state[ix,iy,1] + (1.0 - rate) * g |
|
| 113 |
state[ix,iy,2] = rate * state[ix,iy,2] + (1.0 - rate) * b |
|
| 114 |
|
|
| 115 |
#put back pixel |
|
| 116 |
z = 0 |
|
| 117 |
for i in range(0,31): |
|
| 118 |
for j in range(0,31): |
|
| 119 |
for k in range(0,2): |
|
| 120 |
pixels_list[z] = pixels[i,j,k] |
|
| 121 |
z += 1 |
|
| 122 |
|
|
| 123 |
data.set_data(format, pitch, ''.join(map(chr, pixels_list))) |
|
| 124 |
texture = data.get_texture() |
|
| 125 |
|
|
| 126 |
# Draw texture on Fbo |
|
| 127 |
with self.fbo: |
|
| 128 |
drawTexturedRectangle(texture, pos=(x - 16, y - 16), size=(32, 32)) |
|
| 48 |
if __name__ == '__main__': |
|
| 49 |
w = MTWindow() |
|
| 50 |
canvas = Canvas(size=(540,440),pos=(w.width/2-260,w.height/2-120)) |
|
| 51 |
w.add_widget(canvas) |
|
| 52 |
draw_but = MTButton(label="Painting") |
|
| 53 |
w.add_widget(draw_but) |
|
| 54 |
@draw_but.event |
|
| 55 |
def on_press(touchID, x, y): |
|
| 56 |
canvas.set_mode(mode='draw') |
|
| 57 |
zoom_but = MTButton(label="Layering",pos=(draw_but.width+5,0)) |
|
| 58 |
w.add_widget(zoom_but) |
|
| 59 |
@zoom_but.event |
|
| 60 |
def on_press(touchID, x, y): |
|
| 61 |
canvas.set_mode(mode='zoom') |
|
| 129 | 62 |
|
| 63 |
add_but = MTButton(label="Save",pos=(draw_but.width+zoom_but.width+10,0)) |
|
| 64 |
@add_but.event |
|
| 65 |
def on_press(touchID, x, y): |
|
| 66 |
canvas.save_image() |
|
| 67 |
w.add_widget(add_but) |
|
| 130 | 68 |
|
| 131 |
|
|
| 69 |
canvas.create_layer(pos=(100,100),size=(200,200),color=(1,0,0,0.8)) |
|
| 70 |
canvas.create_layer(size=(300,200),color=(0,1,0,0.8)) |
|
| 71 |
canvas.create_layer(size=(250,150),color=(0,0,1,0.8)) |
|
| 72 |
runTouchApp() |
|
| 73 |
|
|
| 74 |
|
|
| 75 |
|
|
| core/app/canvas/layer.py (revision 42) | ||
|---|---|---|
| 28 | 28 |
self.layer_manager = kwargs.get('layer_manager')
|
| 29 | 29 |
super(AbstractLayer, self).__init__(**kwargs) |
| 30 | 30 |
self.fbo = Fbo(size=(self.width, self.height), with_depthbuffer=False) |
| 31 |
self.color = kwargs.get('color')
|
|
| 32 |
set_brush('brushes/brush_particle.png',25)
|
|
| 31 |
self.color = kwargs.get('color')
|
|
| 33 | 32 |
self.layer_clear() |
| 34 |
self.brush_color=(0,0,0,1) |
|
| 35 | 33 |
self.id = kwargs.get('id')
|
| 36 | 34 |
|
| 37 | 35 |
def layer_clear(self): |
| ... | ... | |
| 51 | 49 |
self.layer_manager.move_layer_up(self.id) |
| 52 | 50 |
if self.layer_manager.mode == "draw": |
| 53 | 51 |
with self.fbo: |
| 54 |
set_color(*self.brush_color) |
|
| 55 |
set_brush('brushes/brush_particle.png',25)
|
|
| 52 |
set_color(*self.layer_manager.brush_color) |
|
| 53 |
set_brush(self.layer_manager.brush_sprite,self.layer_manager.brush_size) |
|
| 56 | 54 |
drawCircle(pos=self.to_local(x,y), radius=1) |
| 57 | 55 |
elif self.layer_manager.mode == "zoom": |
| 58 | 56 |
super(AbstractLayer, self).on_touch_down(touches, touchID, x, y) |
| ... | ... | |
| 66 | 64 |
cur_pos = self.to_local(x,y) |
| 67 | 65 |
ox,oy = self.touches[touchID] |
| 68 | 66 |
with self.fbo: |
| 69 |
set_color(*self.brush_color) |
|
| 70 |
set_brush('brushes/brush_particle.png',25)
|
|
| 67 |
set_color(*self.layer_manager.brush_color) |
|
| 68 |
set_brush(self.layer_manager.brush_sprite,self.layer_manager.brush_size) |
|
| 71 | 69 |
paintLine((ox,oy,cur_pos[0],cur_pos[1])) |
| 72 | 70 |
self.touches[touchID] = self.to_local(x,y) |
| 73 | 71 |
return True |
| ... | ... | |
| 76 | 74 |
if touchID in self.touches: |
| 77 | 75 |
del self.touches[touchID] |
| 78 | 76 |
return True |
| 79 |
|
|
| 80 |
def set_brush_color(self,color): |
|
| 81 |
self.brush_color = color |
|
| 77 |
|
|
| 82 | 78 |
|
| 83 | 79 |
|
| 84 | 80 |
|
| ... | ... | |
| 98 | 94 |
if self.moveable == False : |
| 99 | 95 |
glColor4f(*self.color) |
| 100 | 96 |
drawRectangle((0,0),(self.width,self.height)) |
| 97 |
drawTexturedRectangle(self.fbo.texture, (0,0),(self.width,self.height)) |
|
| 101 | 98 |
else: |
| 102 | 99 |
glColor4f(self.color[0],self.color[1],self.color[2],self.color[3]) |
| 103 | 100 |
drawRectangle((0,0),(self.width,self.height)) |
| core/app/canvas/layermanager.py (revision 42) | ||
|---|---|---|
| 11 | 11 |
super(LayerManager, self).__init__(**kwargs) |
| 12 | 12 |
self.mode = "zoom" |
| 13 | 13 |
self.canvas = kwargs.get('canvas')
|
| 14 |
self.size = self.canvas.size |
|
| 14 |
#self.size = self.canvas.size |
|
| 15 | 15 |
self.layer_list = [] |
| 16 |
self.background = NormalLayer(size=self.canvas.size,color=(1,1,1,1),moveable=False,layer_manager=self) |
|
| 16 |
self.brush_color = (0,0,0,1) |
|
| 17 |
self.brush_sprite = "brushes/brush_particle.png" |
|
| 18 |
self.brush_size = 25 |
|
| 19 |
self.background = NormalLayer(size=self.size,color=(1,1,1,1),moveable=False,layer_manager=self) |
|
| 17 | 20 |
self.add_widget(self.background) |
| 18 | 21 |
|
| 22 |
|
|
| 19 | 23 |
|
| 20 | 24 |
def set_mode(self,value): |
| 21 | 25 |
self.mode = value |
| 22 | 26 |
|
| 27 |
def set_brush_color(self,color): |
|
| 28 |
self.brush_color = color |
|
| 29 |
|
|
| 30 |
def set_brush(self,sprite,size): |
|
| 31 |
self.brush_sprite = sprite |
|
| 32 |
self.brush_size = size |
|
| 33 |
|
|
| 23 | 34 |
def move_layer_up(self,layer_id): #double tapp on the layer to move up one layer at a time |
| 24 | 35 |
if layer_id < len(self.layer_list)-1: |
| 25 | 36 |
a = self.layer_list[layer_id] |
| core/ui/circularmenu/circularmenu.py (revision 42) | ||
|---|---|---|
| 1 | 1 |
from __future__ import with_statement |
| 2 | 2 |
from pymt import * |
| 3 | 3 |
from pyglet.gl import * |
| 4 |
from math import degrees,sqrt,acos,pi,cos,sin |
|
| 4 |
from math import cos,sin,radians,pi |
|
| 5 |
from glob import glob |
|
| 5 | 6 |
|
| 7 |
class MTCircularItem(MTButton): |
|
| 8 |
def __init__(self, **kwargs): |
|
| 9 |
kwargs.setdefault('scale', 1.0)
|
|
| 10 |
kwargs.setdefault('filename', None)
|
|
| 11 |
if kwargs.get('filename') is None:
|
|
| 12 |
raise Exception('No filename given to Item')
|
|
| 13 |
super(MTCircularItem, self).__init__(**kwargs) |
|
| 14 |
self.action_handler = kwargs.get('handler')
|
|
| 15 |
self.action_value = kwargs.get('value')
|
|
| 16 |
self.filename = kwargs.get('filename')
|
|
| 17 |
self.scale = kwargs.get('scale')
|
|
| 18 |
self.image.scale = self.scale |
|
| 19 |
self.size = (self.image.width, self.image.height) |
|
| 20 |
self.x = self.x-self.width/2 |
|
| 21 |
self.y = self.y-self.height/2 |
|
| 22 |
self.image.x = self.x |
|
| 23 |
self.image.y = self.y |
|
| 24 |
|
|
| 25 |
def collide_point(self, x, y): |
|
| 26 |
if( x > self.x-self.width/2 and x < self.x+self.width/2 and |
|
| 27 |
y > self.y-self.height/2 and y < self.y+self.height/2 ): |
|
| 28 |
return True |
|
| 29 |
|
|
| 30 |
def on_press(self, touchID, x, y): |
|
| 31 |
self.action_handler(sprite=self.action_value,size=25) |
|
| 32 |
|
|
| 33 |
def _get_filename(self): |
|
| 34 |
return self._filename |
|
| 35 |
|
|
| 36 |
def _set_filename(self, filename): |
|
| 37 |
self._filename = filename |
|
| 38 |
img = pyglet.image.load(filename) |
|
| 39 |
self.image = pyglet.sprite.Sprite(img) |
|
| 40 |
filename = property(_get_filename, _set_filename) |
|
| 6 | 41 |
|
| 7 |
class MTCircularMenu(MTWidget): |
|
| 42 |
def on_draw(self): |
|
| 43 |
self.image.x = self.x-self.width/2 |
|
| 44 |
self.image.y = self.y-self.height/2 |
|
| 45 |
self.image.scale = self.scale |
|
| 46 |
self.size = (self.image.width, self.image.height) |
|
| 47 |
self.image.draw() |
|
| 48 |
|
|
| 49 |
class MTCircularMenu_Manager(MTWidget): |
|
| 8 | 50 |
def __init__(self, **kwargs): |
| 9 |
kwargs.setdefault('radius', 200)
|
|
| 51 |
super(MTCircularMenu_Manager, self).__init__(**kwargs) |
|
| 52 |
self.canvas = kwargs.get('canvas')
|
|
| 53 |
self.pos = kwargs.get('pos')
|
|
| 54 |
self.radius = kwargs.get('radius')
|
|
| 55 |
|
|
| 56 |
kt = MTKinetic(velstop=5.0) |
|
| 57 |
self.circular_menu = MTCircularMenu(pos=self.pos,radius=self.radius) |
|
| 58 |
kt.add_widget(self.circular_menu) |
|
| 59 |
self.add_widget(kt) |
|
| 60 |
|
|
| 61 |
brush_list = [] |
|
| 62 |
#by default generate a brushes list in circular menu |
|
| 63 |
for brush in glob('brushes/*.png'):
|
|
| 64 |
brush_list.append([brush,self.canvas.set_brush,brush]) |
|
| 65 |
|
|
| 66 |
self.set_list(list=brush_list) |
|
| 67 |
|
|
| 68 |
|
|
| 69 |
def set_list(self,list): |
|
| 70 |
for item in list: |
|
| 71 |
im = MTCircularItem(filename=item[0],handler=item[1],value=item[2]) |
|
| 72 |
self.circular_menu.add_widget(im) |
|
| 73 |
|
|
| 74 |
class MTCircularMenu(MTScatterWidget): |
|
| 75 |
def __init__(self, **kwargs): |
|
| 76 |
kwargs.setdefault('do_scale', False)
|
|
| 77 |
kwargs.setdefault('do_rotation', True)
|
|
| 78 |
kwargs.setdefault('do_translation', False)
|
|
| 10 | 79 |
super(MTCircularMenu, self).__init__(**kwargs) |
| 11 |
self.radius = kwargs.get('radius')
|
|
| 12 |
self.first_touch = (0, 0) |
|
| 13 |
self.last_touch = (0, 0) |
|
| 14 |
self.angle = 0.0 |
|
| 15 |
self.rotation = 0.0 |
|
| 16 |
|
|
| 80 |
self.radius = kwargs.get('radius')
|
|
| 81 |
self.size = (self.radius*2,self.radius*2) |
|
| 82 |
self.pos = kwargs.get('pos')
|
|
| 83 |
super(MTCircularMenu, self).init_transform(self.pos, 0, 1) |
|
| 84 |
|
|
| 17 | 85 |
def collide_point(self, x, y): |
| 18 | 86 |
return Vector(self.pos).distance((x, y)) <= self.radius |
| 87 |
|
|
| 88 |
def add_widget(self, widget, do_layout=True): |
|
| 89 |
super(MTCircularMenu, self).add_widget(widget) |
|
| 90 |
self.need_layout = True |
|
| 91 |
if do_layout: |
|
| 92 |
self.do_layout() |
|
| 93 |
|
|
| 94 |
def do_layout(self): |
|
| 95 |
x = int((self.radius-40)*cos(radians(20*(len(self.children)-1)))) |
|
| 96 |
y = int((self.radius-40)*sin(radians(20*(len(self.children)-1)))) |
|
| 97 |
self.children[len(self.children)-1].x = x |
|
| 98 |
self.children[len(self.children)-1].y = y |
|
| 99 |
|
|
| 100 |
def rotate_zoom_move(self, touchID, x, y): |
|
| 101 |
# we definitly have one point |
|
| 102 |
p1_start = Vector(*self.touches[touchID]) |
|
| 103 |
p1_now = Vector(x, y) |
|
| 19 | 104 |
|
| 20 |
def on_touch_down(self, touches, touchID, x, y): |
|
| 21 |
if self.collide_point(x, y): |
|
| 22 |
self.first_touch = (x - self.pos[0], y - self.pos[1]) |
|
| 23 |
return True |
|
| 24 |
|
|
| 25 |
def on_touch_up(self, touches, touchID, x, y): |
|
| 26 |
if self.collide_point(x, y): |
|
| 27 |
self.rotation += self.angle |
|
| 28 |
self.angle = 0.0 |
|
| 105 |
# if we have a second point, do the scale/rotate/move thing |
|
| 106 |
# find intersection between lines...the point around which to rotate |
|
| 107 |
intersect = Vector(self.pos) |
|
| 108 |
|
|
| 109 |
# compute rotation angle |
|
| 110 |
old_line = p1_start - intersect |
|
| 111 |
new_line = p1_now - intersect |
|
| 112 |
rotation = -1.0 * old_line.angle(new_line) |
|
| 113 |
|
|
| 114 |
scale = 1.0 |
|
| 115 |
|
|
| 116 |
# just comnpute a translation component if we only have one point |
|
| 117 |
trans = p1_now - p1_start |
|
| 118 |
|
|
| 119 |
# apply to our transformation matrix |
|
| 120 |
self.apply_angle_scale_trans(rotation, scale, trans, intersect) |
|
| 121 |
|
|
| 122 |
# save new position of the current touch |
|
| 123 |
self.touches[touchID] = Vector(x,y) |
|
| 124 |
|
|
| 125 |
def draw(self): |
|
| 126 |
#set_color(0,1,0,1) |
|
| 127 |
#drawCircle((0,0), 10) |
|
| 128 |
set_color(*self.style.get('bg-color'))
|
|
| 129 |
#drawRectangle((0,0),self.size) |
|
| 130 |
drawCircle((0,0), self.radius) |
|
| 131 |
#drawCircle(self.pos, self.radius-64) |
|
| 132 |
#drawTriangle(self.pos, w=40, h=100) |
|
| 29 | 133 |
|
| 30 |
def on_touch_move(self, touches, touchID, x, y): |
|
| 31 |
if self.collide_point(x, y): |
|
| 32 |
self.last_touch = (x - self.pos[0], y - self.pos[1]) |
|
| 33 |
self.calculate_angle() |
|
| 34 |
return True |
|
| 35 | 134 |
|
| 36 |
def calculate_angle(self): |
|
| 37 |
self.angle = Vector(self.last_touch).angle(self.first_touch) |
|
| 38 |
|
|
| 39 |
def on_draw(self): |
|
| 40 |
#set_color((1,1,1,1)) |
|
| 41 |
#drawCSSRectangle(pos=(self.pos[0]-self.radius,self.pos[1]-self.radius), size=(self.radius*2,self.radius*2), style=self.style) |
|
| 42 |
with gx_matrix_identity: |
|
| 43 |
set_color(*self.style.get('bg-color'))
|
|
| 44 |
glTranslated(self.pos[0], self.pos[1], 0) |
|
| 45 |
glRotatef(self.angle+self.rotation, 0, 0, 1) |
|
| 46 |
drawCircle((0, 0), self.radius) |
|
| 47 |
drawTriangle(pos=(0, 0), w=40, h=100) |
|
| 48 |
for w in self.children: |
|
| 49 |
with gx_matrix: |
|
| 50 |
angle = Vector(w.pos).angle((0,self.radius)) |
|
| 51 |
glTranslatef(w.x, w.y, 0) |
|
| 52 |
glRotatef(angle, 0, 0, 1) |
|
| 53 |
glTranslatef(-w.x, -w.y, 0) |
|
| 54 |
w.dispatch_event('on_draw')
|
|
| 55 |
|
|
| 56 | 135 |
if __name__ == '__main__': |
| 57 | 136 |
w = MTWindow() |
| 58 |
cm = MTCircularMenu(pos=(w.width/2,w.height/2),radius=150) |
|
| 59 |
w.add_widget(cm) |
|
| 137 |
cm = MTCircularMenu_Manager(pos=(0,0)) |
|
| 138 |
w.add_widget(cm) |
|
| 60 | 139 |
for i in range (12): |
| 61 | 140 |
teta = float((360/12)*i*(pi/180)) |
| 62 | 141 |
x = int(90*cos(teta)) |
| nuipaint.py (revision 42) | ||
|---|---|---|
| 8 | 8 |
if __name__ == '__main__': |
| 9 | 9 |
w = MTWindow() |
| 10 | 10 |
#Canvas |
| 11 |
cv = Canvas(pos=(w.width/2,w.height/2)) |
|
| 12 |
w.add_widget(cv) |
|
| 11 |
cv = Canvas(size=(540,440),pos=(w.width/2-260,w.height/2-120)) |
|
| 12 |
w.add_widget(cv) |
|
| 13 |
#cv.create_layer(pos=(100,100),size=(200,200),color=(1,0,0,0.2)) |
|
| 14 |
#cv.create_layer(size=(300,200),color=(0,1,0,0.2)) |
|
| 15 |
#cv.create_layer(size=(250,150),color=(0,0,1,0.2)) |
|
| 13 | 16 |
#File Browser |
| 14 | 17 |
fb = MTFileBrowser(pos=(100,500),size=(400,300)) |
| 15 | 18 |
w.add_widget(fb) |
| 16 | 19 |
fb.hide() |
| 20 |
|
|
| 17 | 21 |
#Bottom Toolbar |
| 18 | 22 |
tb = toolbar(win=w,canvas=cv) |
| 19 |
w.add_widget(tb) |
|
| 23 |
w.add_widget(tb) |
|
| 20 | 24 |
#Top Toolbar |
| 21 | 25 |
topb = topBar(win=w,canvas=cv,filebrowser=fb) |
| 22 | 26 |
w.add_widget(topb) |
| 27 |
|
|
| 23 | 28 |
#Side Ciruclar Menu |
| 24 |
kt = MTKinetic(velstop=5.0) |
|
| 25 |
cm = MTCircularMenu(pos=(0,0),radius=200) |
|
| 26 |
kt.add_widget(cm) |
|
| 27 |
for i in range (18): |
|
| 28 |
teta = float((360/18)*i*(pi/180)) |
|
| 29 |
x = int(143*cos(teta)) |
|
| 30 |
y = int(143*sin(teta)) |
|
| 31 |
im = MTImageButton(filename="gfx/icons/flickr.png",pos=(x,y)) |
|
| 32 |
cm.add_widget(im) |
|
| 33 |
w.add_widget(kt) |
|
| 29 |
cm = MTCircularMenu_Manager(pos=(-225,-225),radius=225,canvas=cv) |
|
| 30 |
w.add_widget(cm) |
|
| 34 | 31 |
|
| 35 | 32 |
cs = MTColorSelector(pos=(w.width-200,0),size=(200,200),win=w,canvas=cv) |
| 36 | 33 |
w.add_widget(cs) |
| 34 |
|
|
| 37 | 35 |
runTouchApp() |
| 38 | 36 |
|
Also available in: Unified diff
