Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt/tapaletteview.py
blob: 213ee178f0168b9341b6adcc4171873196768b41 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# -*- coding: utf-8 -*-
#Copyright (c) 2014, 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.

_SKIN_IMAGE = 1
_MARGIN = 5
_BUTTON_SIZE = 32

from tautils import find_group, debug_output
from tablock import Block
from tapalette import (palette_names, palette_blocks, hidden_proto_blocks,
                       block_styles)
from taconstants import (PALETTE_SCALE, ICON_SIZE, PYTHON_SKIN, XO1,
                         HORIZONTAL_PALETTE, PALETTE_WIDTH, PALETTE_HEIGHT,
                         CATEGORY_LAYER, TOP_LAYER, PROTO_LAYER)
from tasprite_factory import SVG, svg_str_to_pixbuf
from sprites import Sprite


def _width_and_height(blk):
    ''' What are the width and height of a stack? '''
    minx = 10000
    miny = 10000
    maxx = -10000
    maxy = -10000
    for gblk in find_group(blk):
        (x, y) = gblk.spr.get_xy()
        w, h = gblk.spr.get_dimensions()
        if x < minx:
            minx = x
        if y < miny:
            miny = y
        if x + w > maxx:
            maxx = x + w
        if y + h > maxy:
            maxy = y + h
    return(maxx - minx, maxy - miny)


class PaletteView():
    ''' Palette View class abstraction  '''

    def __init__(self, turtle_window, n):
        '''
        This class handles the display of block palettes
        '''
        self.blocks = []
        self.backgrounds = [None, None]

        self._turtle_window = turtle_window
        self._palette_index = n
        self._visible = False
        self._populated = False

        if not n < len(palette_names):
            # Shouldn't happen, but hey...
            debug_output('palette index %d is out of range' % n,
                         self._turtle_window.running_sugar)
            self._name = 'undefined'
        else:
            self._name = palette_names[n]

    def create(self, regenerate=True, show=False):
        if not self._name == 'undefined':
            # Create proto blocks for each palette entry
            self._create_proto_blocks()

            save_selected = self._turtle_window.selected_palette
            self.layout_palette(regenerate=regenerate,
                                show=(show or
                                      save_selected == self._palette_index))

    def show(self):
        ''' Show palette background and proto blocks. If needed, display
            shift button. '''
        orientation = self._turtle_window.orientation
        if self.backgrounds[orientation] is not None:
            self.backgrounds[orientation].set_layer(CATEGORY_LAYER)

        for blk in self.blocks:
            if blk.get_visibility():
                blk.spr.set_layer(PROTO_LAYER)
            else:
                blk.spr.hide()

        self.display_palette_shift_buttons()

    def hide(self):
        ''' Hide the palette. '''
        for background in self.backgrounds:
            if background is not None:
                background.hide()

        for blk in self.blocks:
            blk.spr.hide()

        self._hide_palette_shift_buttons()

        if self._trash_palette():
            for blk in self._turtle_window.trash_stack:
                for gblk in find_group(blk):
                    gblk.spr.hide()

    def move(self, x, y):
        ''' Move the palette. '''
        buttons = self._turtle_window.palette_button

        for blk in self.blocks:
            blk.spr.move((x + blk.spr.save_xy[0], y + blk.spr.save_xy[1]))

        for button in buttons:
            button.move((x + button.save_xy[0], y + button.save_xy[1]))

        for spr in self.backgrounds:
            if spr is not None:
                spr.move((x + spr.save_xy[0], y + spr.save_xy[1]))

        if self._trash_palette():
            for blk in self._turtle_window.trash_stack:
                for gblk in find_group(blk):
                    gblk.spr.move((x + gblk.spr.save_xy[0],
                                   y + gblk.spr.save_xy[1]))

    def shift(self):
        ''' Shift blocks on the palette. '''
        buttons = self._turtle_window.palette_button
        orientation = self._turtle_window.orientation

        x, y = self.backgrounds[orientation].get_xy()
        w, h = self.backgrounds[orientation].get_dimensions()
        bx, by = self.blocks[0].spr.get_xy()
        if orientation == 0:
            width = self._turtle_window.width

            if bx != _BUTTON_SIZE:
                dx = w - width
            else:
                dx = width - w
            dy = 0
        else:
            height = self._turtle_window.height
            offset = self._turtle_window.toolbar_offset

            dx = 0
            if by != offset + _BUTTON_SIZE + _MARGIN:
                dy = h - height + ICON_SIZE
            else:
                dy = height - h - ICON_SIZE

        for blk in self.blocks:
            if blk.get_visibility():
                blk.spr.move_relative((dx, dy))

        buttons[orientation].set_layer(TOP_LAYER)
        if dx < 0 or dy < 0:
            buttons[orientation + 5].set_layer(TOP_LAYER)
            buttons[orientation + 3].hide()
        else:
            buttons[orientation + 5].hide()
            buttons[orientation + 3].set_layer(TOP_LAYER)

    def _create_proto_blocks(self):
        '''
        Create the proto blocks that will populate this palette.
        Reload the palette, but reuse the existing blocks.
        If a block doesn't exist, add it.
        '''
        for blk in self.blocks:
            blk.spr.hide()

        preexisting_blocks = self.blocks[:]
        self.blocks = []
        for name in palette_blocks[self._palette_index]:
            # Did we already create this block?
            preexisting_block = False
            for blk in preexisting_blocks:
                if blk.name == name:
                    self.blocks.append(blk)
                    preexisting_block = True
                    break

            # If not, create it now.
            if not preexisting_block:
                self.blocks.append(Block(self._turtle_window.block_list,
                                         self._turtle_window.sprite_list,
                                         name, 0, 0, 'proto', [],
                                         PALETTE_SCALE))
                if name in hidden_proto_blocks:
                    self.blocks[-1].set_visibility(False)
                else:
                    self.blocks[-1].spr.set_layer(PROTO_LAYER)
                    self.blocks[-1].unhighlight()
                    self.blocks[-1].resize()

                # Some proto blocks get a skin.
                if name in block_styles['box-style-media']:
                    self._proto_skin(name + 'small', self.blocks[-1].spr)
                elif name in PYTHON_SKIN:
                    self._proto_skin('pythonsmall', self.blocks[-1].spr)
                elif len(self.blocks[-1].spr.labels) > 0:
                    self.blocks[-1].refresh()

    def _proto_skin(self, name, spr):
        ''' Utility for creating proto block skins '''
        x, y = self._turtle_window._calc_image_offset(name, spr)
        spr.set_image(self._turtle_window.media_shapes[name], _SKIN_IMAGE,
                      x, y)

    def _float_palette(self, spr):
        ''' We sometimes let the palette move with the canvas. '''
        if self._turtle_window.running_sugar and \
           not self._turtle_window.hw in [XO1]:
            spr.move_relative(
                (self._turtle_window.activity.hadj_value,
                 self._turtle_window.activity.vadj_value))

    def _trash_palette(self):
        return 'trash' in palette_names and \
            self._palette_index == palette_names.index('trash')

    def layout_palette(self, regenerate=False, show=True):
        ''' Layout prototypes in a palette. '''

        offset = self._turtle_window.toolbar_offset
        buttons = self._turtle_window.palette_button
        orientation = self._turtle_window.orientation
        w = PALETTE_WIDTH
        h = PALETTE_HEIGHT

        if orientation == HORIZONTAL_PALETTE:
            x, y, max_w = self._horizontal_layout(
                _BUTTON_SIZE, offset + _MARGIN, self.blocks)
            if self._trash_palette():
                blocks = []  # self.blocks[:]
                for blk in self._turtle_window.trash_stack:
                    blocks.append(blk)
                x, y, max_w = self._horizontal_layout(x + max_w, y, blocks)
            w = x + max_w + _BUTTON_SIZE + _MARGIN
            if show:
                buttons[2].move((w - _BUTTON_SIZE, offset))
                buttons[4].move((_BUTTON_SIZE, offset))
                buttons[6].move((_BUTTON_SIZE, offset))
        else:
            x, y, max_h = self._vertical_layout(
                _MARGIN, offset + _BUTTON_SIZE + _MARGIN, self.blocks)
            if self._trash_palette():
                blocks = []  # self.blocks[:]
                for blk in self._turtle_window.trash_stack:
                    blocks.append(blk)
                x, y, max_h = self._vertical_layout(x, y + max_h, blocks)
            h = y + max_h + _BUTTON_SIZE + _MARGIN - offset
            if show:
                buttons[2].move((PALETTE_WIDTH - _BUTTON_SIZE, offset))
                buttons[3].move((0, offset + _BUTTON_SIZE))
                buttons[5].move((0, offset + _BUTTON_SIZE))

        self._make_background(0, offset, w, h, regenerate)

        if show:
            for blk in self.blocks:
                if blk.get_visibility():
                    blk.spr.set_layer(PROTO_LAYER)
                else:
                    blk.spr.hide()

            buttons[2].save_xy = buttons[2].get_xy()
            self._float_palette(buttons[2])
            self.backgrounds[orientation].set_layer(CATEGORY_LAYER)
            self.display_palette_shift_buttons()

            if self._trash_palette():
                for blk in self._turtle_window.trash_stack:
                    for gblk in find_group(blk):
                        gblk.spr.set_layer(PROTO_LAYER)

                svg = SVG()
                self.backgrounds[orientation].set_shape(
                    svg_str_to_pixbuf(svg.palette(w, h)))

    def _make_background(self, x, y, w, h, regenerate=False):
        ''' Make the background sprite for the palette. '''
        orientation = self._turtle_window.orientation

        if regenerate and not self.backgrounds[orientation] is None:
            self.backgrounds[orientation].hide()
            self.backgrounds[orientation] = None

        if self.backgrounds[orientation] is None:
            svg = SVG()
            self.backgrounds[orientation] = \
                Sprite(self._turtle_window.sprite_list, x, y,
                       svg_str_to_pixbuf(svg.palette(w, h)))
            self.backgrounds[orientation].save_xy = (x, y)

            self._float_palette(self.backgrounds[orientation])

            if orientation == 0 and w > self._turtle_window.width:
                self.backgrounds[orientation].type = \
                    'category-shift-horizontal'
            elif orientation == 1 and \
                 h > self._turtle_window.height - ICON_SIZE:
                self.backgrounds[orientation].type = \
                    'category-shift-vertical'
            else:
                self.backgrounds[orientation].type = 'category'

            '''
            if self._trash_palette():
                svg = SVG()
                self.backgrounds[orientation].set_shape(
                    svg_str_to_pixbuf(svg.palette(w, h)))
            '''

    def _horizontal_layout(self, x, y, blocks):
        ''' Position prototypes in a horizontal palette. '''
        offset = self._turtle_window.toolbar_offset
        max_w = 0

        for blk in blocks:
            if not blk.get_visibility():
                continue

            w, h = _width_and_height(blk)
            if y + h > PALETTE_HEIGHT + offset:
                x += int(max_w + 3)
                y = offset + 3
                max_w = 0

            (bx, by) = blk.spr.get_xy()
            dx = x - bx
            dy = y - by
            for g in find_group(blk):
                g.spr.move_relative((int(dx), int(dy)))
                g.spr.save_xy = g.spr.get_xy()
                self._float_palette(g.spr)
            y += int(h + 3)
            if w > max_w:
                max_w = w

        return x, y, max_w

    def _vertical_layout(self, x, y, blocks):
        ''' Position prototypes in a vertical palette. '''
        row = []
        row_w = 0
        max_h = 0

        for blk in blocks:
            if not blk.get_visibility():
                continue

            w, h = _width_and_height(blk)
            if x + w > PALETTE_WIDTH:
                # Recenter row.
                dx = int((PALETTE_WIDTH - row_w) / 2)
                for r in row:
                    for g in find_group(r):
                        g.spr.move_relative((dx, 0))
                        g.spr.save_xy = (g.spr.save_xy[0] + dx,
                                         g.spr.save_xy[1])
                row = []
                row_w = 0
                x = 4
                y += int(max_h + 3)
                max_h = 0

            row.append(blk)
            row_w += (4 + w)

            (bx, by) = blk.spr.get_xy()
            dx = int(x - bx)
            dy = int(y - by)
            for g in find_group(blk):
                g.spr.move_relative((dx, dy))
                g.spr.save_xy = g.spr.get_xy()
                self._float_palette(g.spr)

            x += int(w + 4)
            if h > max_h:
                max_h = h

        # Recenter last row.
        dx = int((PALETTE_WIDTH - row_w) / 2)
        for r in row:
            for g in find_group(r):
                g.spr.move_relative((dx, 0))
                g.spr.save_xy = (g.spr.save_xy[0] + dx, g.spr.save_xy[1])

        return x, y, max_h

    def _hide_palette_shift_buttons(self):
        buttons = self._turtle_window.palette_button
        for i in range(4):
            buttons[i + 3].hide()

    def display_palette_shift_buttons(self):
        ''' Palettes too wide (or tall) for the screen get a shift button. '''
        self._hide_palette_shift_buttons()

        buttons = self._turtle_window.palette_button
        orientation = self._turtle_window.orientation

        if self.backgrounds[orientation].type == 'category-shift-horizontal':
            buttons[3].set_layer(CATEGORY_LAYER)
        elif self.backgrounds[orientation].type == 'category-shift-vertical':
            buttons[4].set_layer(CATEGORY_LAYER)