Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/char.py
blob: 59a44607852a9428a7538e0239f3583f0d0ff41e (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
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import gtk
import glob
from gettext import gettext as _

import theme
from utils import pixbuf, pixbuf2str

def load():
    from document import Document

    custom = THEMES[-1]
    for i, f in enumerate(
            [i for i in set(Document.tape) if not i.empty() and i.custom()]):
        custom.frames[i] = f

class Frame:
    def __init__(self, image, type):
        self._type =  type
        self._thumb = None
        self._orig = None
        self._filename = None

        if type == theme.RESTORED:
            tmpfile = os.path.join(theme.SESSION_PATH, '.tmp.png')
            file(tmpfile, 'w').write(image)
            self._orig = theme.pixbuf(tmpfile)
            os.unlink(tmpfile)
        elif type == theme.CUSTOM:
            self._thumb = theme.CUSTOM_FRAME_THUMB
        elif type == theme.EMPTY:
            self._type = theme.PREINSTALLED
            self._thumb = theme.EMPTY_THUMB
            self._orig = theme.EMPTY_ORIG
        else:
            self._filename = image

    def read(self):
        if not self._orig:
            return ''
        else:
            return pixbuf2str(self._orig)

    def empty(self):
        return self._thumb == theme.EMPTY_THUMB

    def custom(self):
        return self._type != theme.PREINSTALLED

    def thumb(self):
        if self._thumb == None:
            self._thumb = theme.scale(self.orig())
        return self._thumb

    def orig(self):
        if self._orig == None:
            if self._type != theme.PREINSTALLED:
                return theme.EMPTY_ORIG
            self._orig = theme.pixbuf(self._filename)
        return self._orig

    def select(self):
        if self._type != theme.CUSTOM or self._orig:
            return True;
        self._orig = theme.choose(lambda t, file: theme.pixbuf(file))
        self._thumb = theme.scale(self.orig())
        return self._orig != None

    def filename(self):
        return self._filename

class Char:
    def __init__(self, name, thumbfile, dir):
        self.name = name
        self.frames = []

        if dir:
            for i in sorted(glob.glob(theme.path(dir, '*'))):
                self.frames.append(Frame(
                        os.path.join(dir, os.path.basename(i)),
                        theme.PREINSTALLED))
            for i in range(len(self.frames)-1,
                    theme.FRAME_ROWS*theme.FRAME_COLS):
                self.frames.append(Frame(None, theme.EMPTY))
            self._thumb = theme.pixbuf(thumbfile, theme.THUMB_SIZE)
        else:
            for i in range(0, theme.FRAME_ROWS*theme.FRAME_COLS):
                self.frames.append(Frame(None, theme.CUSTOM))
            self._thumb = theme.CUSTOM_FRAME_THUMB

    def thumb(self):
        return self._thumb

    def clean(self, index):
        if self.frames[index].custom():
            self.frames[index] = Frame(None, theme.CUSTOM)

THEMES = (
    Char(_('Elephant'),     'images/pics/Elephant/bigelephant0.gif',
                            'images/pics/Elephant'),
    Char(_('Space Blob'),   'images/pics/SpaceBlob/bigblob8.gif',
                            'images/pics/SpaceBlob'),
    Char(_('Turkey'),       'images/pics/Turkey/bigturkey1.gif',
                            'images/pics/Turkey'),
    None,
    Char(_('Custom'),       None, None))