def load_lists(self, v14_lists, kurt_target):
        for v14_list in v14_lists.values():
            kurt_list = kurt.List(map(unicode, v14_list.list_items))
            kurt_target.lists[v14_list.name] = kurt_list

            kurt_watcher = kurt.Watcher(
                kurt_target, kurt.Block("contentsOfList:", v14_list.name))
            kurt_watcher.is_visible = bool(v14_list.owner)

            (x, y, w, h) = v14_list.bounds.value
            if not kurt_watcher.is_visible:
                x -= 534
                y -= 71
            kurt_watcher.pos = (x, y)
            self.project.actors.append(kurt_watcher)
示例#2
0
    def load_scriptable(self, sd, is_stage=False):
        if is_stage:
            scriptable = kurt.Stage(self.project)
        elif 'objName' in sd:
            scriptable = kurt.Sprite(self.project, sd["objName"])
        else:
            return self.load_watcher(sd)

        # costumes
        for cd in sd.get("costumes", []):
            image = self.read_image(cd['baseLayerID'])
            rotation_center = (cd['rotationCenterX'], cd['rotationCenterY'])

            if cd['bitmapResolution'] != 1:
                (w, h) = image.size
                w /= cd['bitmapResolution']
                h /= cd['bitmapResolution']
                image = image.resize((w, h))

                (x, y) = rotation_center
                x /= cd['bitmapResolution']
                y /= cd['bitmapResolution']
                rotation_center = (x, y)

            if 'text' in cd:
                text_layer = self.read_image(cd['textLayerID'])
                if text_layer:
                    image = image.paste(text_layer)

            scriptable.costumes.append(
                kurt.Costume(cd['costumeName'], image, rotation_center))

        # sounds
        for snd in sd.get("sounds", []):
            scriptable.sounds.append(
                kurt.Sound(
                    snd['soundName'],
                    self.read_waveform(snd['soundID'], snd['rate'],
                                       snd['sampleCount'])))

        # vars & lists
        target = self.project if is_stage else scriptable

        for vd in sd.get("variables", []):
            var = kurt.Variable(vd['value'], vd['isPersistent'])
            target.variables[vd['name']] = var

        for ld in sd.get("lists", []):
            name = ld['listName']
            target.lists[name] = kurt.List(ld['contents'], ld['isPersistent'])
            self.list_watchers.append(
                kurt.Watcher(target,
                             kurt.Block("contentsOfList:", name),
                             is_visible=ld['visible'],
                             pos=(ld['x'], ld['y'])))

        # custom blocks first
        for script_array in sd.get("scripts", []):
            if script_array[2]:
                block_array = script_array[2][0]
                if block_array[0] == 'procDef':
                    (_, spec, input_names, defaults, is_atomic) = block_array
                    cb = custom_block(spec, input_names, defaults)
                    cb.is_atomic = is_atomic
                    self.custom_blocks[spec] = cb

        # scripts
        for script_array in sd.get("scripts", []):
            scriptable.scripts.append(self.load_script(script_array))

        # comments
        blocks_by_id = []
        for script in scriptable.scripts:
            for block in list(get_blocks_by_id(script)):
                blocks_by_id.append(block)

        for comment_array in sd.get("scriptComments", []):
            (x, y, w, h, expanded, block_id, text) = comment_array
            if block_id > -1:
                blocks_by_id[block_id].comment = text
            else:
                scriptable.scripts.append(kurt.Comment(text, (x, y)))

        # sprite only
        if not is_stage:
            scriptable.position = (sd['scratchX'], sd['scratchY'])
            scriptable.direction = sd['direction']
            scriptable.rotation_style = str(sd['rotationStyle'])
            scriptable.is_draggable = sd['isDraggable']
            scriptable.is_visible = sd['visible']
            scriptable.size = sd['scale'] * 100.0

        return scriptable
for font_path in FONT_PATHS:
    font = ImageFont.truetype(font_path, FONT_SIZE)
    (_, font_name) = os.path.split(font_path)
    if "." in font_name:
        font_name = ".".join(font_name.split(".")[:-1])
    font_name = font_name.replace("_", "")
    if font_name in project.sprites:
        continue
    print font_name

    sprite = kurt.Sprite(project, name=font_name)

    for name in ("i", "j", "word x", "message"):
        sprite.variables[name] = kurt.Variable(0)

    sprite.lists["widths"] = kurt.List()

    error = False
    for char in CHARACTERS:
        pil_image = text_to_image(font, char, TEXT_COLOR)
        (width, height) = pil_image.size
        if char == " " and width == 0 or height == 0:
            width = max(1, width)
            height = max(1, width)
            pil_image = Image.new("RGB", (width, height), (255, 255, 255))

        try:
            costume = kurt.Costume(char, kurt.Image(pil_image), (0, 0))
        except SystemError:
            error = True
            break
import os
import sys

# try and find kurt directory
path_to_file = os.path.join(os.getcwd(), __file__)
path_to_lib = os.path.split(os.path.split(path_to_file)[0])[0]
sys.path.append(path_to_lib)
import kurt

project = kurt.Project()
testcontext = project.stage
testcontext.variables['foo'] = kurt.Variable()
testcontext.lists['positions'] = kurt.List()
project.sprites.append(kurt.Sprite(project, 'Sprite1'))

p = lambda x: kurt.text.parse(x + "\n", testcontext)
pb = lambda x: kurt.text.parse(x, testcontext)[0]

try:
    pb("a")
except SyntaxError:
    pass
else:
    raise

print pb("set foo to 4")

assert pb("set foo to 10") == kurt.Block("setVar:to:", "foo", u'10')
assert pb("set x to 4") == kurt.Block("xpos:", u'4')
assert pb("set foo to 10") == kurt.Block("setVar:to:", "foo", u'10')
assert pb("set foo to 1 * 2") == kurt.Block("setVar:to:", "foo",