File: image.py

package info (click to toggle)
singularity 0.30b-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,020 kB
  • ctags: 856
  • sloc: python: 6,443; makefile: 14; sh: 3
file content (73 lines) | stat: -rw-r--r-- 2,328 bytes parent folder | download | duplicates (2)
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
#file: image.py
#Copyright (C) 2008 FunnyMan3595
#This file is part of Endgame: Singularity.

#Endgame: Singularity 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 2 of the License, or
#(at your option) any later version.

#Endgame: Singularity 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 Endgame: Singularity; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#This file contains the image widget.

import pygame

import constants
import g
import widget

def scale(*args, **kwargs):
    try:
        return pygame.transform.smoothscale(*args, **kwargs)
    except Exception:
        return pygame.transform.scale(*args, **kwargs)

class Image(widget.Widget):
    image = widget.causes_rebuild("_image")

    def __init__(self, parent, pos, size = (1, 1),
                 anchor = constants.TOP_LEFT, image = None):
        super(Image, self).__init__(parent, pos, size, anchor)

        self.old_size = None

        if image:
            if type(image) is str:
                image = pygame.image.load(image)

            self.image = image.convert_alpha()

    def _calc_size(self):
        size = list( super(Image, self)._calc_size() )
        if size[0] == size[1] == 0:
            raise ValueError, "One image dimension must be specified!"

        image_size = self.image.get_size()
        ratio = image_size[0] / float(image_size[1])
        if size[0] == 0:
            size[0] = int(size[1] * ratio)
        elif size[1] == 0:
            size[1] = int(size[0] / ratio)

        return tuple(size)

    def rescale(self):
        self.scaled_image = scale(self.image, self.real_size)

    def resize(self):
        super(Image, self).resize()
        if self.real_size != self.old_size:
            self.rescale()
            self.old_size = self.real_size

    def redraw(self):
        super(Image, self).redraw()
        self.surface.blit(self.scaled_image, (0,0))