Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/rayito/RtoObject.py
blob: 1c731f3e3777901e926cd3e030008c2d277eee6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#! /usr/bin/env python
# Rayito objects
# Copyright (C) 2011 Gabriel Eirea
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Contact information:
# Gabriel Eirea geirea@gmail.com
# Ceibal Jam http://ceibaljam.org

import pygame

class RtoObject():
    """ Class for rayito objects.

    position: (x, y) of upper-left corner in pixels
    layer: layer number, high is up, low is down
    """

    def __init__(self, position, layer = 0):
        self._position = [position[0], position[1]]
        self._layer = layer
        self._visible = False
        self._moving = False
        self._velocity = (0, 0)
        self._last_update = 0

    def hide(self):
        self._visible = False

    def show(self):
        self._visible = True

    def move_to(self, newpos):
        self._position = [newpos[0], newpos[1]]

    def move_by(self, relpos):
        self._position[0] += relpos[0]
        self._position[1] += relpos[1]

    def change_layer(self, new_layer):
        self._layer = new_layer

    def get_layer(self):
        return self._layer

    def move_start(self, vel):
        # vel should be in pixels per millisecond
        self._moving = True
        self._velocity = vel

    def move_stop(self):
        self._moving = False

    def update(self, time):
        pixels_x =  int(self._velocity[0]*(time - self._last_update))
        pixels_y =  int(self._velocity[1]*(time - self._last_update))
        if self._moving:
            self._position[0] += pixels_x
            self._position[1] += pixels_y
        self._last_update = time
        self.update_specific(time)

    def update_specific(self, time):
        pass

    def render(self, screen):
        pass

    def is_pressed(self, pos):
        return False


class RtoImage(RtoObject):
    """ Class for rayito images.

    image: pygame surface with the image
    position: (x, y) of upper-left corner in pixels
    layer: layer number, high is up, low is down
    """

    def __init__(self, image, position, layer = 0):
        RtoObject.__init__(self, position, layer)
        self._image = image

    def scale_to(self, target_size):
        w = self._image.get_width()
        h = self._image.get_height()
        center = (self._position[0]+int(w/2), self._position[1]+int(h/2))
        tmp = pygame.transform.scale(self._image, target_size)
        wtmp = tmp.get_width()
        htmp = tmp.get_height()
        self._position = [center[0]-int(wtmp/2), center[1]-int(htmp/2)]
        self._image = tmp
        del tmp

    def scale_by(self, ratio):
        w = self._image.get_width()
        h = self._image.get_height()
        center = (self._position[0]+int(w/2), self._position[1]+int(h/2))
        tmp = pygame.transform.scale(self._image,
                          (int(self._image.get_width()*ratio),
                           int(self._image.get_height()*ratio)))
        wtmp = tmp.get_width()
        htmp = tmp.get_height()
        self._position = [center[0]-int(wtmp/2), center[1]-int(htmp/2)]
        self._image = tmp
        del tmp

    def flip_hor(self):
        tmp = pygame.transform.flip(self._image, True, False)
        self._image = tmp
        del tmp

    def flip_ver(self):
        tmp = pygame.transform.scale(self._image, False, True)
        self._image = tmp
        del tmp

    def rotate(self, angle):
        w = self._image.get_width()
        h = self._image.get_height()
        center = (self._position[0]+int(w/2), self._position[1]+int(h/2))
        tmp = pygame.transform.rotate(self._image, angle)
        wtmp = tmp.get_width()
        htmp = tmp.get_height()
        self._position = [center[0]-int(wtmp/2), center[1]-int(htmp/2)]
        self._image = tmp
        del tmp

    def render(self, screen):
        if self._visible:
            screen.blit(self._image,self._position)


class RtoDialog(RtoObject):
    """Class for rayito dialogs 

    text: list with text to display; 
          each element can have \n to separate lines
    font: pygame font to use
    bg_color: background color (R, G, B)
    fg_color: text color (R, G, B)
    position: (x, y) of upper-left corner in pixels
    layer: layer number, high is up, low is down
    """

    def __init__(self, text, font, bg_color, fg_color, position, layer = 0):
        RtoObject.__init__(self, position, layer)
        self._font = font
        self._text = text
        self._ntexts = len(text)
        self._current_text = 0
        self._bg_color = bg_color
        self._fg_color = fg_color
        # compute size
        self._width = 0
        self._height = 0
        for t in self._text:
            lines = t.split("\n")
            lh = 0
            for l in lines:
                (w, h) = self._font.size(l)
                if w > self._width:
                    self._width = w
                lh += h
            if lh > self._height:
                self._height = lh
        self._width += 10
        self._height += 10
        # create empty background
        self._bg = pygame.Surface((self._width, self._height))
        self._bg.fill(self._bg_color)
        # render first line of text
        self.next_text()

    def next_text(self):
        if self._current_text == self._ntexts:
            self.hide()
            return False
        self._image = self._bg.copy()
        yline = 5
        for l in self._text[self._current_text].split("\n"):
            text_img = self._font.render(l, 1, self._fg_color)
            textrect = text_img.get_rect()
            textrect.left = 5
            textrect.top = yline
            self._image.blit(text_img, textrect)
            yline += self._font.get_height()
        self._current_text += 1
        return True

    def render(self, screen):
        if self._visible:
            screen.blit(self._image, self._position)

    def restart(self):
        self._current_text = 0
        self.next_text()


class RtoButton(RtoObject):
    """Class for rayito buttons 

    text: text to display; can have \n to separate lines
    font: pygame font to use
    bg_color: background color (R, G, B)
    fg_color: text color (R, G, B)
    position: (x, y) of upper-left corner in pixels
    layer: layer number, high is up, low is down
    """

    def __init__(self, text, font, bg_color, fg_color, position, layer = 0):
        RtoObject.__init__(self, position, layer)
        self._font = font
        self._text = text
        self._bg_color = bg_color
        self._fg_color = fg_color
        # compute size
        self._width = 0
        self._height = 0
        lines = self._text.split("\n")
        for l in lines:
            (w, h) = self._font.size(l)
            if w > self._width:
                self._width = w
            self._height += h
        self._width += 10
        self._height += 10
        # create empty background
        self._image = pygame.Surface((self._width, self._height))
        self._image.fill(self._bg_color)
        yline = 5
        for l in self._text.split("\n"):
            text_img = self._font.render(l, 1, self._fg_color)
            textrect = text_img.get_rect()
            textrect.left = 5
            textrect.top = yline
            self._image.blit(text_img, textrect)
            yline += h

    def render(self, screen):
        if self._visible:
            screen.blit(self._image, self._position)

    def is_pressed(self, pos):
        if pos[0] > self._position[0] \
                and pos[0] < self._position[0] + self._width \
                and pos[1] > self._position[1] \
                and pos[1] < self._position[1] + self._height:
            return True
        else:
            return False


class RtoSprite(RtoObject):
    """Class for rayito sprites.

    images: list with sprite images (each one a pygame surface)
    frames: list with image# for every frame
    next_frames: list with next frame from current one
    dxs: list with dx for current frame (in pixels)
    dys: list with dy for current frame (in pixels)
    position: (x, y) of upper-left corner in pixels
    layer: layer number, high is up, low is down
    """

    def __init__(self, images, frames, next_frames, dxs, dys,
                 position, layer = 0):
        RtoObject.__init__(self, position, layer)
        self._images = images
        self._frames = frames
        self._next_frames = next_frames
        self._dxs = dxs
        self._dys = dys
        self._frame = 0
        self._image = self._images[self._frames[self._frame]]

    def update_specific(self, time):
        self._position[0] += self._dxs[self._frame]
        self._position[1] += self._dys[self._frame]
        self._frame = self._next_frames[self._frame]
        self._image = self._images[self._frames[self._frame]]
        self._last_update = time

    def render(self, screen):
        screen.blit(self._image, self._position)

    def update_frame(self, fr):
        self._frame = fr


class RtoSound():
    """Class for rayito sounds.

    sound: pygame.mixer.Sound object
    volume: volume (between 0 and 1)
    """

    def __init__(self, sound, volume = 1):
        self._sound = sound
        self._volume = volume

    def play(self):
        self._sound.play()
        
    def set_volume(self, vol):
        if vol >= 0 and vol <= 1:
            self._sound.set_volume(vol)

    def update(self, time):
        pass