def on_enter(self):
        super(PythonInterpreterLayer, self).on_enter()

        vw, vh = cocos.director.director.get_window_size()

        # format the code
        self.document = document.FormattedDocument(self.content)
        self.document.set_style(
            0, len(self.document.text), {
                'font_name': self.cfg['code.font_name'],
                'font_size': self.cfg['code.font_size'],
                'color': self.cfg['code.color'],
            })

        self.batch = graphics.Batch()

        # generate the document
        self.layout = layout.IncrementalTextLayout(self.document,
                                                   vw,
                                                   vh,
                                                   multiline=True,
                                                   batch=self.batch)
        self.layout.anchor_y = 'top'

        self.caret = caret.Caret(self.layout, color=self.cfg['caret.color'])
        self.caret.on_activate()

        self.on_cocos_resize(vw, vh)

        self.start_of_line = len(self.document.text)
示例#2
0
    def __init__(self, doctype, *args, **kwargs):
        super(TestWindow, self).__init__(*args, **kwargs)

        self.batch = graphics.Batch()
        self.document = doctype()
        self.layout = layout.IncrementalTextLayout(self.document,
            self.width, self.height, batch=self.batch)
示例#3
0
    def __init__(self, doctype, *args, **kwargs):
        super(TestWindow, self).__init__(*args, **kwargs)

        self.batch = graphics.Batch()
        self.document = doctype()
        self.layout = layout.IncrementalTextLayout(self.document,
                                                   self.width,
                                                   self.height,
                                                   batch=self.batch)
        self.document.set_style(0, len(self.document.text), {"bold": True})
示例#4
0
    def __init__(self, *args, **kwargs):
        super(TestWindow, self).__init__(*args, **kwargs)

        self.batch = pyglet.graphics.Batch()
        self.document = pyglet.text.decode_html(doctext)
        self.margin = 2
        self.layout = layout.IncrementalTextLayout(self.document,
            self.width - self.margin * 2, self.height - self.margin * 2,
            multiline=True,
            batch=self.batch)
        self.caret = caret.Caret(self.layout)
        self.push_handlers(self.caret)

        self.set_mouse_cursor(self.get_system_mouse_cursor('text'))
示例#5
0
    def __init__(self, *args, **kwargs):
        super(TestWindow, self).__init__(*args, **kwargs)

        self.batch = pyglet.graphics.Batch()
        self.document = pyglet.text.decode_attributed(doctext)
        for i in range(0, len(doctext), 300):
            self.document.insert_element(i, TestElement(60, -10, 70))
        self.margin = 2
        self.layout = layout.IncrementalTextLayout(self.document,
            self.width - self.margin * 2, self.height - self.margin * 2,
            multiline=True,
            batch=self.batch)
        self.caret = caret.Caret(self.layout)
        self.push_handlers(self.caret)

        self.set_mouse_cursor(self.get_system_mouse_cursor('text'))
示例#6
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.batch = graphics.Batch()
        self.document = text.decode_text(doctext)
        self.margin = 2
        self.layout = layout.IncrementalTextLayout(
            self.document,
            self.width - self.margin * 2,
            self.height - self.margin * 2,
            multiline=True,
            batch=self.batch)
        self.layout.content_valign = 'bottom'
        self.caret = caret.Caret(self.layout)
        self.push_handlers(self.caret)

        self.set_mouse_cursor(self.get_system_mouse_cursor('text'))
示例#7
0
    def __init__(self, multiline, wrap_lines, msg, *args, **kwargs):
        super(TestWindow, self).__init__(*args, **kwargs)

        self.batch = graphics.Batch()
        self.document = text.decode_attributed(msg)
        self.margin = 2
        self.layout = layout.IncrementalTextLayout(self.document,
            (self.width - self.margin * 2), 
            self.height - self.margin * 2,
            multiline=multiline,
            wrap_lines=wrap_lines,
            batch=self.batch)
        self.caret = caret.Caret(self.layout)
        self.push_handlers(self.caret)

        self.wrap_lines = wrap_lines

        self.set_mouse_cursor(self.get_system_mouse_cursor('text'))
示例#8
0
def main():
    w = window.Window(vsync=False, resizable=True)
    w.set_mouse_cursor(w.get_system_mouse_cursor('text'))

    @w.event
    def on_key_press(symbol, modifiers):
        if sys.platform == 'darwin':
            accel = key.MOD_COMMAND
        else:
            accel = key.MOD_CTRL

        if modifiers & accel:
            if symbol == key.B:
                toggle_style('bold')
            elif symbol == key.I:
                toggle_style('italic')
            elif symbol in (key.EQUAL, key.NUM_ADD):
                add_font_size(2)
            elif symbol in (key.MINUS, key.NUM_SUBTRACT):
                add_font_size(-2)

        if symbol == key.ESCAPE:
            w.has_exit = True

    def toggle_style(attribute):
        old = caret.get_style(attribute)
        if old == style.INDETERMINATE:
            value = True
        else:
            value = not old
        caret.set_style({attribute: value})

    def add_font_size(size):
        old_size = caret.get_style('font_size')
        if old_size in (style.INDETERMINATE, None):
            old_size = 12
        caret.set_style({'font_size': old_size + size})

    def on_resize(width, height):
        text.x = border
        text.y = height - border
        text.width = width - border * 2
        text.height = height - border * 2
        caret._update()

    w.push_handlers(on_resize)

    if len(sys.argv) > 1:
        content = open(sys.argv[1]).read()
    else:
        content = resource.file('info.att').read()

    # Draw to this border so we can test clipping.
    border = 50

    batch = graphics.Batch()
    doc = attributed(content)
    text = layout.IncrementalTextLayout(doc,
                                        w.width - border * 2,
                                        w.height - border * 2,
                                        multiline=True,
                                        batch=batch)
    caret = caret_module.Caret(text)
    caret.color = (0, 0, 0)
    caret.visible = True
    caret.position = 0
    w.push_handlers(caret)

    fps = clock.ClockDisplay(font=font.load('', 10, dpi=96),
                             color=(0, 0, 0, 1),
                             interval=1.,
                             format='FPS: %(fps)d')
    fps.label.x = 2
    fps.label.y = 15
    stats_text = font.Text(font.load('', 10, dpi=96),
                           '',
                           x=2,
                           y=2,
                           color=(0, 0, 0, 1))

    def update_stats(dt):
        states = list(batch.state_map.values())
        usage = 0.
        blocks = 0
        domains = 0

        fragmentation = 0.
        free_space = 0.
        capacity = 0.

        for state in states:
            for domain in list(state.values()):
                domains += 1
                free_space += domain.allocator.get_free_size()
                fragmentation += domain.allocator.get_fragmented_free_size()
                capacity += domain.allocator.capacity
                blocks += len(domain.allocator.starts)
        if free_space:
            fragmentation /= free_space
        else:
            fragmentation = 0.
        free_space /= capacity
        usage = 1. - free_space
        stats_text.text = \
            'States: %d  Domains: %d  Blocks: %d  Usage: %d%%  Fragmentation: %d%%' % \
            (len(states), domains, blocks, usage * 100, fragmentation * 100)

    clock.schedule_interval(update_stats, 1)

    glClearColor(1, 1, 1, 1)
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE)
    while not w.has_exit:
        clock.tick()
        w.dispatch_events()
        w.clear()
        batch.draw()
        fps.draw()
        stats_text.draw()

        glPushAttrib(GL_CURRENT_BIT)
        glColor3f(0, 0, 0)
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
        glRectf(border - 2, border - 2, w.width - border + 4,
                w.height - border + 4)
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
        glPopAttrib()

        w.flip()

    main()