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

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
        with self.fbo:
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
44
    def on_touch_down(self, touches, touchID, x, y):
45
        if self.collide_point(x,y): 
46
            self.touches[touchID] = self.to_local(x,y)
47
            if len(touches)==2 :                
48
                if touches[touchID].is_double_tap:
49
                    self.layer_manager.move_layer_down(self.id)
50
            elif touches[touchID].is_double_tap:
51
               self.layer_manager.move_layer_up(self.id)
52
            if self.layer_manager.mode == "draw":
53
                with self.fbo:
54
                    set_color(*self.brush_color)
55
                    set_brush('brushes/brush_particle.png',25)
56
                    drawCircle(pos=self.to_local(x,y), radius=1)                
57
            elif self.layer_manager.mode == "zoom":
58
                super(AbstractLayer, self).on_touch_down(touches, touchID, x, y)
59
            return True
60
            
61
    def on_touch_move(self, touches, touchID, x, y):
62
        if touchID in self.touches:
63
            if self.layer_manager.mode == "zoom":
64
                super(AbstractLayer, self).on_touch_move(touches, touchID, x, y)
65
            elif self.layer_manager.mode == "draw":
66
                cur_pos = self.to_local(x,y)
67
                ox,oy = self.touches[touchID]
68
                with self.fbo:
69
                    set_color(*self.brush_color)
70
                    set_brush('brushes/brush_particle.png',25)
71
                    paintLine((ox,oy,cur_pos[0],cur_pos[1]))                    
72
                self.touches[touchID] = self.to_local(x,y)
73
            return True
74
            
75
    def on_touch_up(self, touches, touchID, x,y):
76
        if touchID in self.touches:
77
            del self.touches[touchID]
78
            return True
79
        
80
    def set_brush_color(self,color):
81
        self.brush_color = color
82
            
83
       
84
85
class NormalLayer(AbstractLayer):
86
    def __init__(self, **kwargs):
87
        kwargs.setdefault('layer_manager', None)
88
        self.moveable = kwargs.get('moveable')
89
        if self.moveable == False :
90
            kwargs.setdefault('do_scale', False)
91
            kwargs.setdefault('do_rotation', False)
92
            kwargs.setdefault('do_translation', False)
93
        super(NormalLayer, self).__init__(**kwargs)
94
        
95
    def draw(self):
96
        with gx_matrix:
97
            #glColor4f(*self.color)
98
            if self.moveable == False :
99
                glColor4f(*self.color)
100
                drawRectangle((0,0),(self.width,self.height))
101
            else:
102
                glColor4f(self.color[0],self.color[1],self.color[2],self.color[3])
103
                drawRectangle((0,0),(self.width,self.height))
104
                drawTexturedRectangle(self.fbo.texture, (0,0),(self.width,self.height))