root / core / app / canvas / layer.py @ 40

View | Annotate | Download (4.3 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
class AbstractLayer(specialScatterW):
26
    def __init__(self, **kwargs):
27
        kwargs.setdefault('layer_manager', None)
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)                
31
        self.color = kwargs.get('color')
32
        set_brush('brushes/brush_particle.png',25)
33
        #self.layer_clear()
34
        self.brush_color=(0,0,0,1)
35
        self.id = kwargs.get('id')
36
37
    def layer_clear(self):
38
        self.fbo.bind()
39
        #glClearColor(1,1,1,1)
40
        #glClear(GL_COLOR_BUFFER_BIT)
41
        set_color(self.color)
42
        drawRectangle((0,0),(self.width,self.height))
43
        self.fbo.release()
44
45
    def on_touch_down(self, touches, touchID, x, y):
46
        if self.collide_point(x,y): 
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)
53
            if self.layer_manager.mode == "draw":
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)                
58
            elif self.layer_manager.mode == "zoom":
59
                super(AbstractLayer, self).on_touch_down(touches, touchID, x, y)
60
            return True
61
            
62
    def on_touch_move(self, touches, touchID, x, y):
63
        if touchID in self.touches:
64
            if self.layer_manager.mode == "zoom":
65
                super(AbstractLayer, self).on_touch_move(touches, touchID, x, y)
66
            elif self.layer_manager.mode == "draw":
67
                cur_pos = self.to_local(x,y)
68
                ox,oy = self.touches[touchID]
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()
74
                self.touches[touchID] = self.to_local(x,y)
75
            return True
76
            
77
    def on_touch_up(self, touches, touchID, x,y):
78
        if touchID in self.touches:
79
            del self.touches[touchID]
80
            return True
81
        
82
    def set_brush_color(self,color):
83
        self.brush_color = color
84
            
85
       
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
        
97
    def draw(self):
98
        with gx_matrix:
99
            #glColor4f(*self.color)
100
            if self.moveable == False :
101
                glColor4f(*self.color)
102
                drawRectangle((0,0),(self.width,self.height))
103
            else:
104
                glColor4f(self.color[0],self.color[1],self.color[2],self.color[3])
105
                drawRectangle((0,0),(self.width,self.height))
106
                drawTexturedRectangle(self.fbo.texture, (0,0),(self.width,self.height))