1
# ----------------------------------------------------------------------------
3
# Copyright (c) 2008 Daniel Moisset, Ricardo Quesada, Rayentray Tappa, Lucio Torre
6
# Redistribution and use in source and binary forms, with or without
7
# modification, are permitted provided that the following conditions are met:
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
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
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
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.
45
__docformat__ = 'restructuredtext'
48
from pyglet.gl import *
50
from cocos.director import *
51
from base_layers import Layer
52
import cocos.cocosnode
54
__all__ = ['ColorLayer']
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).
61
For example, to create green layer::
63
l = ColorLayer(0, 255, 0, 0 )
65
The size and position can be changed, for example::
67
l = ColorLayer( 0, 255,0,0, width=200, height=400)
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
81
w,h = director.get_window_size()
88
super(ColorLayer, self).on_enter()
89
x, y = self.width, self.height
92
self._vertex_list = self._batch.add(4, pyglet.gl.GL_QUADS, None,
102
super(ColorLayer, self).on_exit()
103
self._vertex_list.delete()
104
self._vertex_list = None
107
super(ColorLayer, self).draw()
110
glPushAttrib(GL_CURRENT_BIT)
115
def _update_color(self):
116
if self._vertex_list:
118
self._vertex_list.colors[:] = [r, g, b, int(self._opacity)] * 4
120
def _set_opacity(self, opacity):
121
self._opacity = opacity
124
opacity = property(lambda self: self._opacity, _set_opacity,
125
doc='''Blend opacity.
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.
131
An opacity of 255 (the default) has no effect. An opacity of 128 will
132
make the sprite appear translucent.
137
def _set_color(self, rgb):
138
self._rgb = map(int, rgb)
141
color = property(lambda self: self._rgb, _set_color,
144
This property sets the color of the layer's vertices. This allows the
145
layer to be drawn with a color tint.
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).
150
:type: (int, int, int)