Beispiel #1
0
    def __init__(self):
        self.window = window()
        self.window.on('paint', self.frame)
        self.window.on('keydown', self.keydown)
        self.window.show()
        
        self.font = get('font/proggy_tiny', 'font')
        self.border = get('assets/border.png', 'patch-9')

        self.plugins = load_all_plugins([default_plugin_directory], self)

        self.file0 = Buffer(
            filename = (argv[1] if len(argv) > 1 else None),
        )
        self.selection = Selection(self.file0, (), self.file0.document, starmarker(self.file0.document.holes[0], 0, 0))
        self.hook('loadbuffer', self, self.file0)
Beispiel #2
0
class Editor(object):
    def __init__(self):
        self.window = window()
        self.window.on('paint', self.frame)
        self.window.on('keydown', self.keydown)
        self.window.show()
        
        self.font = get('font/proggy_tiny', 'font')
        self.border = get('assets/border.png', 'patch-9')

        self.plugins = load_all_plugins([default_plugin_directory], self)

        self.file0 = Buffer(
            filename = (argv[1] if len(argv) > 1 else None),
        )
        self.selection = Selection(self.file0, (), self.file0.document, starmarker(self.file0.document.holes[0], 0, 0))
        self.hook('loadbuffer', self, self.file0)

    def hook(self, name, *a, **kw):
        for plugin in self.plugins:
            if hasattr(plugin, name):
                res = getattr(plugin, name)(*a, **kw)
                if res is not None:
                    return res

    def frame(self, screen, dt):
        screen(color(0x00, 0x00, 0x00))

        root = self.file0.render(self.layout_hook)

        ngin = engine()
        ngin.guide(root.left == 50)
        ngin.guide(root.top == 50)
        root.configure(ngin)
        root.render(screen)

        self.highlight(screen, self.selection)

        # should be bound to mode
        caption, iconname = pygame.display.get_caption()
        if self.selection.buffer.title != caption:
            pygame.display.set_caption(self.file0.title, 'upi')

    def keydown(self, key, mod, ch):
        key = keybindings.get(key)
        modifiers = set()
        for mask, ident in modbindings:
            if mod & mask != 0:
                modifiers.add(ident)

        if self.hook('keyboard', self, key, modifiers, ch):
            return

        selection = self.selection
        shift = 'shift' in modifiers
        if 'ctrl' in modifiers:
            if key == "q":
                exit(0)
            if key == "t":
                print self.last_highlight
            if key == "a":
                selection.expand()
            if key == "s":
                selection.buffer.save()
#            if key == "h":
#                self.selection = selection.move(-1, shift)
#            if key == "l":
#                self.selection = selection.move(+1, shift)
#            if key == "j" and selection.descendable(selection.cursor):
#                self.selection = selection.descend(selection.cursor)
#            if key == "e":
#                self.selection = selection.build(shift)
#            if key == "c" and selection.ascendable:
#                self.selection = selection.collapse()
#            if key == "k" and selection.ascendable:
#                self.selection = selection.ascend
#            if key == "b":
#                self.selection = selection.move(selection.bounds[0], shift, relative=False)
#            if key == "w":
#                self.selection = selection.move(selection.bounds[1], shift, relative=False)
#            if key == "m":
#                start, stop = selection.textbounds
#                selection = selection.grasp(start, stop)
#                kw = dict(name=''.join(selection.yank))
#                self.selection = selection.splice([]).modify(kw)
#
#                #self.selection = selection.splice([]).modify({'name':name})
#        elif 'enter' == key:
#            if shift:
#                self.selection = selection.walk_backward()
#            else:
#                self.selection = selection.walk_forward()
        elif 'tab' == key:
            name = choice(['toy-object', 'reindeer', 'baaz', 'santa', 'foo', 'guux', 'lol'])
            holes = [choice([star, dot]) for _ in range(randint(0, 5))]
            selection.replace([empty_template(name, *holes)], branch_in=True)
        elif 'backspace' == key:
            if selection.empty: selection.move(-1, True)
            selection.remove()
        elif 'delete' == key:
            if selection.empty: selection.move(+1, True)
            selection.remove()
        elif 'left' == key:
            selection.move(-1, shift)
        elif 'right' == key:
            selection.move(+1, shift)
        elif 'enter' == key and shift:
            selection.walk_backwards()
        elif 'enter' == key:
            selection.walk()
        elif 'up' == key:
            selection.move(selection.bounds[0], shift, relative=False)
        elif 'down' == key:
            selection.move(selection.bounds[1], shift, relative=False)
        elif len(ch) == 1:
            selection.replace([ch])
#            self.selection = selection.splice([ch])

    def layout_hook(self, obj, context=()):
        frame = self.hook('layout', self, obj, context)
        if frame is None:
            frame = self.default_layout_hook(obj, context)
        return frame
    
    def default_layout_hook(self, obj, context):
        if obj is None:
            return group([string(self.font('none'))], padding = (5, 5, 5, 5))
        if not iselement(obj):
            return string(self.font(obj))

        # this element is UNKNOWN, therefore we are doing this...
        name = string(self.font('{%s}' % obj.name).mul(red))
        holes = []
        for index, hole in enumerate(obj.holes):
            if isinstance(hole, dot):
                holes.append(self.layout_dot(obj, index, context))
            if isinstance(hole, star):
                frames = self.layout_star(obj, index, context)
                holes.append(group(delimit(frames, yglue, 4,0),
                    padding = (5, 5, 5, 5),
                ))

        return group(delimit([name] + holes, yglue, 0, 0)
        )


#        frames = self.layout_recurse(obj, context)
#        if obj.get('which') == 'scratch':
#            return group(delimit(frames, yglue, 8, 2) or [string(self.font('nil').mul(cyan))],
#                background = self.border,
#                padding = (10,10,10,10),
#            )
#        name = string(self.font('{%s}' % obj.get('name')).mul(red))
#        return group(delimit([name] + frames, xglue, 8, 12),
#            background=self.border,
#            padding=(10,10,10,10),
#        )

    def layout_dot(self, obj, index, context):
        context = push(context, (obj, index))
        hole = obj.holes[index]
        frame = self.layout_hook(hole.a, context)
        frame.hole = hole
        return frame

    @makelist
    def layout_star(self, obj, index, context, plac="*"):
        context = push(context, (obj, index))
        hole = obj.holes[index]
        for partial in hole.partials:
            frame = self.layout_hook(partial.a, context)
            frame.hole = partial
            yield frame
        if len(hole.a) == 0:
            frame = string(self.font(plac).mul(gray))
            frame.hole = hole
            yield frame
        
#                if len(frames) == 0:
#                    #frames = [string(self.font("*").mul(gray))]

    def highlight(self, screen, selection):
        marker = selection.marker
        frames = selection.buffer.visual
        areas = []

        if isinstance(marker, dotmarker):
            for frame in frames.find():
                if marker.test(frame.hole):
                    area = frame.area
                    areas.append(area)
                    screen.mul(yellow, area)
                    break

        if isinstance(marker, starmarker):
            start, stop = marker.start, marker.stop
            for frame in frames.find():
                if marker.test(frame.hole):
                    area = frame.highlight(start, stop)
                    if area:
                        areas.append(area)
                        screen.add(gray, area)
                if marker.test2(frame.hole):
                    area = padding(frame.area, (3,3,3,3))
                    screen.add(gray, area)
                    areas.append(area)
        self.last_highlight = areas