Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/block.py
blob: 7564b6d87041454c6fd716af26334537fe1d76be (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
# -*- coding: utf-8 -*-
#Copyright (c) 2010 Walter Bender

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

import pygtk
pygtk.require('2.0')
import gtk
import gobject
from constants import *
import block_factory
import sprites

#
# A class for the list of blocks and everything they share in common
#
class Blocks:
    def __init__(self, sprites):
        self.list = []
        self.sprites = sprites

    def get_block(self, i):
        if i < 0 or i > len(self.list)-1:
            return(None)
        else:
            return(self.list[i])

    def length_of_list(self):
        return(len(self.list))

    def append_to_list(self,block):
        self.list.append(block)

    def insert_in_list(self,block,i):
        if i < 0:
            self.list.insert(0, block)
        elif i > len(self.list)-1:
            self.list.append(block)
        else:
            self.list.insert(i, block)

    def remove_from_list(self, block):
        if block in self.list:
            self.list.remove(block)

    #
    # block and spr utilities
    #
    def spr_to_block(self, spr):
        for b in self.list:
            if spr == b.spr:
                return b

#
# A class for the individual blocks
#
class Block:
    def __init__(self, blocks, proto_name, x, y, labels=[], 
                 colors=["#00FF00","#00A000"], scale=2.0):
        self.blocks = blocks
        self.spr = None
        self._new_block_from_prototype(proto_name, labels, colors, scale, x, y)
        self.blocks.append_to_list(self)
        #
        # TODO:
        # save arguments
        # dock and connection info
        # highlight image
        # Logo code
        # HTML code
        # debug code
        # etc.

    def _new_block_from_prototype(self, name, labels, colors, scale, x, y):
        if len(labels) == 0:
            print "%s (%d %d)" % (name, x, y)
        else:
            print "%s %s (%d %d)" % (name, labels[0], x, y)
        if name in BASIC_STYLE:
            svg = block_factory.SVG()
            svg.set_scale(scale)
            svg.expand(20,0)
            svg.set_innie([False])
            svg.set_outie(False)
            svg.set_tab(True)
            svg.set_slot(True)
            svg.set_gradiant(True)
            svg.set_colors(colors)
            self.spr = sprites.Sprite(self.blocks.sprites, x, y,
                                      svg_str_to_pixbuf(svg.basic_block()))
            print "created new basic block: %s" % (str(self.spr))
            self.spr.set_layer(2000)
            self.spr.draw()
            self.spr.set_label(labels[0])
        elif name in BASIC_STYLE_HEAD:
            svg = block_factory.SVG()
            svg.set_scale(scale)
            svg.expand(20,0)
            svg.set_innie([False])
            svg.set_outie(False)
            svg.set_tab(False)
            svg.set_slot(True)
            svg.set_gradiant(True)
            svg.set_colors(colors)
            self.spr = sprites.Sprite(self.blocks.sprites, x, y,
                                      svg_str_to_pixbuf(svg.basic_block()))
            print "created new basic block: %s" % (str(self.spr))
            self.spr.set_layer(2000)
            self.spr.draw()
            self.spr.set_label(labels[0])
        elif name in BASIC_STYLE_TAIL:
            svg = block_factory.SVG()
            svg.set_scale(scale)
            svg.expand(20,0)
            svg.set_innie([False])
            svg.set_outie(False)
            svg.set_tab(True)
            svg.set_slot(False)
            svg.set_gradiant(True)
            svg.set_colors(colors)
            self.spr = sprites.Sprite(self.blocks.sprites, x, y,
                                      svg_str_to_pixbuf(svg.basic_block()))
            print "created new basic block: %s" % (str(self.spr))
            self.spr.set_layer(2000)
            self.spr.draw()
            self.spr.set_label(labels[0])
        elif name in BASIC_STYLE_1ARG:
            svg = block_factory.SVG()
            svg.set_scale(scale)
            svg.expand(20,0)
            svg.set_innie([True])
            svg.set_outie(False)
            svg.set_tab(True)
            svg.set_slot(True)
            svg.set_gradiant(True)
            svg.set_colors(colors)
            self.spr = sprites.Sprite(self.blocks.sprites, x, y,
                                      svg_str_to_pixbuf(svg.basic_block()))
            print "created new basic block: %s" % (str(self.spr))
            self.spr.set_layer(2000)
            self.spr.draw()
            self.spr.set_label(labels[0])
        elif name in BASIC_STYLE_2ARG:
            svg = block_factory.SVG()
            svg.set_scale(scale)
            svg.expand(20,0)
            svg.set_innie([True,True])
            svg.set_outie(False)
            svg.set_tab(True)
            svg.set_slot(True)
            svg.set_gradiant(True)
            svg.set_colors(colors)
            self.spr = sprites.Sprite(self.blocks.sprites, x, y,
                                      svg_str_to_pixbuf(svg.basic_block()))
            print "created new basic block: %s" % (str(self.spr))
            self.spr.set_layer(2000)
            self.spr.draw()
            self.spr.set_label(labels[0])
        elif name in BOX_STYLE:
            svg = block_factory.SVG()
            svg.set_scale(scale)
            svg.expand(60,0)
            svg.set_gradiant(True)
            svg.set_colors(colors)
            self.spr = sprites.Sprite(self.blocks.sprites, x, y,
                                      svg_str_to_pixbuf(svg.basic_box()))
            print "created new box block: %s" % (str(self.spr))
        else:
            print "don't know how to create a block for %s" % (name)
            return

        for label in labels:
            self.spr.set_label(label, labels.index(label))

#
# Load pixbuf from SVG string
#
def svg_str_to_pixbuf(svg_string):
    pl = gtk.gdk.PixbufLoader('svg')
    pl.write(svg_string)
    pl.close()
    pixbuf = pl.get_pixbuf()
    return pixbuf