Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/block.py
blob: 4da2b0b56712baddcb9d42b40523cacd4c5ab8ed (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
# -*- 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.

from constants import *
import sprite_factory
import sprites
from gettext import gettext as _

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

    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 remove_from_list(self, block):
        if block in self.list:
            self.list.remove(block)

    def print_list(self):
        for i, block in enumerate(self.list):
            print "%d: %s" % (i, block.name)


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

#
# A class for the individual blocks
#
class Block:
    def __init__(self, block_list, sprite_list, proto_name, x, y, labels=[], 
                 colors=["#00FF00","#00A000"], scale=2.0):
        self.spr = None
        self.shape = None
        self.selected_shape = None
        self.name = proto_name
        self.docks = None
        self.connections = None
        self._new_block_from_prototype(sprite_list, proto_name, labels, colors,
                                       scale, x, y)
        block_list.append_to_list(self)
        #
        # TODO:
        # Logo code
        # HTML code
        # debug code
        # etc.

    def _new_block_from_prototype(self, sprite_list, name, labels, colors,
                                  scale, x, y):
        if len(labels) == 0:
            print "new block: %s (%d %d)" % (name, x, y)
        else:
            print "new block: %s %s (%d %d)" % (name, labels[0], x, y)

        svg = sprite_factory.SVG()
        if name in TURTLE_PALETTE:
            svg.set_colors(TURTLE_COLORS)
        elif name in PEN_PALETTE:
            svg.set_colors(PEN_COLORS)
        elif name in NUMBER_PALETTE:
            svg.set_colors(NUMBER_COLORS)
        elif name in BLOCKS_PALETTE:
            svg.set_colors(BLOCKS_COLORS)
        elif name in MISC_PALETTE:
            svg.set_colors(MISC_COLORS)
        elif name in FLOW_PALETTE:
            svg.set_colors(FLOW_COLORS)
        elif name in PORTFOLIO_PALETTE:
            svg.set_colors(PORTFOLIO_COLORS)
        svg.set_scale(scale)
        svg.set_gradiant(True)
        svg.set_innie([False])
        svg.set_outie(False)
        svg.set_tab(True)
        svg.set_slot(True)
        xoff = INNIE*scale-2
        yoff = SLOTY*scale-1
        if name in BASIC_STYLE:
            svg.expand(40,0)
            self._make_basic_block(sprite_list, svg, x, y)
            self.docks = (('flow',True,0,2),('flow',False,0,self.height-yoff))
        elif name in BASIC_STYLE_HEAD:
            svg.expand(40,0)
            svg.set_slot(False)
            self._make_basic_block(sprite_list, svg, x, y)
            self.docks = (('start',True,0,2),('flow',False,0,self.height-yoff))
        elif name in BASIC_STYLE_HEAD_1ARG:
            svg.expand(40,0)
            svg.set_slot(False)
            self._make_basic_block(sprite_list, svg, x, y)
            self.docks = (('start',True,0,0), ('string',False,self.width,12),
                          ('flow',False,0,self.height-yoff))
        elif name in BASIC_STYLE_TAIL:
            svg.expand(40,0)
            svg.set_tab(False)
            self._make_basic_block(sprite_list, svg, x, y)
            self.docks = (('flow',True,0,2),('unavailable',False,0,0))
        elif name in BASIC_STYLE_1ARG:
            svg.expand(25,0)
            svg.set_innie([True])
            self._make_basic_block(sprite_list, svg, x, y)
            self.docks = (('flow',True,0,2), ('num',False,self.width-xoff,12),
                          ('flow',False,0,self.height-yoff))
        elif name in BASIC_STYLE_2ARG:
            svg.expand(25,0)
            svg.set_innie([True,True])
            self._make_basic_block(sprite_list, svg, x, y)
            self.docks = (('flow',True,0,2), ('num',False,self.width-xoff,12),
                          ('num',False,self.width-xoff,54), 
                          ('flow',False,0,self.height-yoff))
        elif name in BOX_STYLE:
            svg.expand(40,0)
            self._make_basic_box(sprite_list, svg, x, y)
            self.docks = (('num',True,0,12),('unavailable',False,0,12))
        else:
            svg.expand(40,0)
            self._make_basic_block(sprite_list, svg, x, y)
            self.docks = (('flow',True,0,2),('flow',False,0,self.height-yoff))
            print "don't know how to create a block for %s" % (name)

        if len(labels) > 0:
            self.spr.set_label(_(labels[0]))
            for label in labels:
                self.spr.set_label(label, labels.index(label))

        self.type = 'block'
        if DEFAULTS.has_key(name):
            self.defaults = DEFAULTS[name]
        else:
            self.defaults = []

    def _make_basic_block(self, sprite_list, svg, x, y):
        self.shape = sprite_factory.svg_str_to_pixbuf(svg.basic_block())
        self.width = svg.get_width()
        self.height = svg.get_height()
        svg.set_stroke_width(SELECTED_STROKE_WIDTH)
        svg.set_stroke_color(SELECTED_COLOR)
        self.selected_shape =\
                     sprite_factory.svg_str_to_pixbuf(svg.basic_block())
        self.spr = sprites.Sprite(sprite_list, x, y, self.shape)

    def _make_basic_box(self, sprite_list, svg, x, y):
        self.shape = sprite_factory.svg_str_to_pixbuf(svg.basic_box())
        self.width = svg.get_width()
        self.height = svg.get_height()
        svg.set_stroke_width(SELECTED_STROKE_WIDTH)
        svg.set_stroke_color(SELECTED_COLOR)
        self.selected_shape = sprite_factory.svg_str_to_pixbuf(svg.basic_box())
        self.spr = sprites.Sprite(sprite_list, x, y, self.shape)