~facundo/enjuewemela/trunk

« back to all changes in this revision

Viewing changes to cocos/layer/util_layers.py

  • Committer: facundo at com
  • Date: 2010-11-20 01:42:31 UTC
  • mfrom: (62.1.3 lint-issues)
  • Revision ID: facundo@taniquetil.com.ar-20101120014231-b2tkyc3mwr84ggcc
Project reorder and lint issues

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# ----------------------------------------------------------------------------
 
2
# cocos2d
 
3
# Copyright (c) 2008 Daniel Moisset, Ricardo Quesada, Rayentray Tappa, Lucio Torre
 
4
# All rights reserved.
 
5
#
 
6
# Redistribution and use in source and binary forms, with or without
 
7
# modification, are permitted provided that the following conditions are met:
 
8
#
 
9
#   * Redistributions of source code must retain the above copyright
 
10
#     notice, this list of conditions and the following disclaimer.
 
11
#   * Redistributions in binary form must reproduce the above copyright
 
12
#     notice, this list of conditions and the following disclaimer in
 
13
#     the documentation and/or other materials provided with the
 
14
#     distribution.
 
15
#   * Neither the name of cocos2d nor the names of its
 
16
#     contributors may be used to endorse or promote products
 
17
#     derived from this software without specific prior written
 
18
#     permission.
 
19
#
 
20
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 
23
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 
24
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 
25
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
26
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 
27
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 
28
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
29
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 
30
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
31
# POSSIBILITY OF SUCH DAMAGE.
 
32
# ----------------------------------------------------------------------------
 
33
"""Layer class and subclasses
 
34
 
 
35
A `Layer` has as size the whole drawable area (window or screen),
 
36
and knows how to draw itself. It can be semi transparent (having holes
 
37
and/or partial transparency in some/all places), allowing to see other layers
 
38
behind it. Layers are the ones defining appearance and behavior, so most
 
39
of your programming time will be spent coding Layer subclasses that do what
 
40
you need. The layer is where you define event handlers.
 
41
Events are propagated to layers (from front to back) until some layer catches
 
42
the event and accepts it.
 
43
"""
 
44
 
 
45
__docformat__ = 'restructuredtext'
 
46
 
 
47
import pyglet
 
48
from pyglet.gl import *
 
49
 
 
50
from cocos.director import *
 
51
from base_layers import Layer
 
52
import cocos.cocosnode
 
53
 
 
54
__all__ = ['ColorLayer']
 
55
 
 
56
 
 
57
class ColorLayer(Layer):
 
58
    """Creates a layer of a certain color.
 
59
    The color shall be specified in the format (r,g,b,a).
 
60
 
 
61
    For example, to create green layer::
 
62
 
 
63
        l = ColorLayer(0, 255, 0, 0 )
 
64
 
 
65
    The size and position can be changed, for example::
 
66
 
 
67
        l = ColorLayer( 0, 255,0,0, width=200, height=400)
 
68
        l.position = (50,50)
 
69
 
 
70
    """
 
71
    def __init__(self, r,g,b,a, width=None, height=None):
 
72
        super(ColorLayer, self).__init__()
 
73
        self._batch = pyglet.graphics.Batch()
 
74
        self._vertex_list = None
 
75
        self._rgb = r,g,b
 
76
        self._opacity = a
 
77
 
 
78
        self.width = width
 
79
        self.height = height
 
80
 
 
81
        w,h = director.get_window_size()
 
82
        if not self.width:
 
83
            self.width = w
 
84
        if not self.height:
 
85
            self.height = h
 
86
 
 
87
    def on_enter(self):
 
88
        super(ColorLayer, self).on_enter()
 
89
        x, y = self.width, self.height
 
90
        ox, oy = 0, 0
 
91
 
 
92
        self._vertex_list = self._batch.add(4, pyglet.gl.GL_QUADS, None,
 
93
            ('v2i', ( ox, oy,
 
94
                      ox, oy + y,
 
95
                      ox+x, oy+y,
 
96
                      ox+x, oy)),
 
97
            'c4B')
 
98
 
 
99
        self._update_color()
 
100
 
 
101
    def on_exit(self):
 
102
        super(ColorLayer, self).on_exit()
 
103
        self._vertex_list.delete()
 
104
        self._vertex_list = None
 
105
 
 
106
    def draw(self):
 
107
        super(ColorLayer, self).draw()
 
108
        glPushMatrix()
 
109
        self.transform()
 
110
        glPushAttrib(GL_CURRENT_BIT)
 
111
        self._batch.draw()
 
112
        glPopAttrib()
 
113
        glPopMatrix()
 
114
 
 
115
    def _update_color(self):
 
116
        if self._vertex_list:
 
117
            r, g, b = self._rgb
 
118
            self._vertex_list.colors[:] = [r, g, b, int(self._opacity)] * 4
 
119
 
 
120
    def _set_opacity(self, opacity):
 
121
        self._opacity = opacity
 
122
        self._update_color()
 
123
 
 
124
    opacity = property(lambda self: self._opacity, _set_opacity,
 
125
                       doc='''Blend opacity.
 
126
 
 
127
    This property sets the alpha component of the colour of the layer's
 
128
    vertices.  This allows the layer to be drawn with fractional opacity,
 
129
    blending with the background.
 
130
 
 
131
    An opacity of 255 (the default) has no effect.  An opacity of 128 will
 
132
    make the sprite appear translucent.
 
133
 
 
134
    :type: int
 
135
    ''')
 
136
 
 
137
    def _set_color(self, rgb):
 
138
        self._rgb = map(int, rgb)
 
139
        self._update_color()
 
140
 
 
141
    color = property(lambda self: self._rgb, _set_color,
 
142
                       doc='''Blend color.
 
143
 
 
144
    This property sets the color of the layer's vertices. This allows the
 
145
    layer to be drawn with a color tint.
 
146
 
 
147
    The color is specified as an RGB tuple of integers ``(red, green, blue)``.
 
148
    Each color component must be in the range 0 (dark) to 255 (saturated).
 
149
 
 
150
    :type: (int, int, int)
 
151
    ''')