Exemplo n.º 1
0
 def crop(self, *args):
     if args and isinstance(args[0], Rect):
         return _terminal.crop(
             args[0].origin.x, args[0].origin.y,
             args[0].size.width, args[0].size.height)
     else:
         return _terminal.crop(*args)
Exemplo n.º 2
0
def test_formatted_log():
    dragging_scrollbar = False
    dragging_scrollbar_offset = 0

    blt.set("window: title='Omni: message log', resizeable=true, minimum-size=20x8; font: default")
    blt.set("input: filter='keyboard, mouse+', precise-mouse=true")
    if blt.set("runic font: ../Media/Tigrex3drunes_16x16_437.PNG, size=16x16, codepage=437, spacing=2x1, transparent=auto"):
        alternative_fonts.append("runic")        
    if blt.set("stone font: ../Media/Aesomatica_16x16_437.png, size=16x16, codepage=437, spacing=2x1, transparent=#FF00FF"):
        alternative_fonts.append("stone")
    if blt.set("curvy font: ../Media/Cheepicus_16x16_437.png, size=16x16, codepage=437, spacing=2x1, transparent=auto"):
        alternative_fonts.append("curvy")

    prompt = \
        "Use arrow keys or mouse wheel to scroll the list up and down. " \
        "Try to resize the window.\n\n--- --- ---"
    messages.append(prompt)

    # Add a dozen of random messages
    for i in range(10):
        messages.append(generate_random_message())

    # Initial update
    frame.update_geometry(
        padding_left,
        padding_top,
        blt.state(blt.TK_WIDTH) - (padding_left + padding_right + 1),
        blt.state(blt.TK_HEIGHT) - (padding_top + padding_bottom))

    while True:
        blt.clear()
        frame.draw()
        blt.color("white")

        blt.layer(1)
        current_line = 0
        for text, height in messages:
            if current_line + height >= frame.offset:
                # stop when message is below frame
                if current_line - frame.offset > frame.height: break
                # drawing message
                blt.puts(padding_left, padding_top + current_line - frame.offset, text, frame.width)
            current_line += height + 1

        blt.crop(padding_left, padding_top, frame.width, frame.height)

        # Render
        blt.refresh()

        key = blt.read()

        if key in (blt.TK_CLOSE, blt.TK_ESCAPE):
            break

        elif key == blt.TK_UP:
            frame.scroll(-1)
            
        elif key == blt.TK_DOWN:
            frame.scroll(1)

        elif key == blt.TK_MOUSE_SCROLL:
            # Mouse wheel scroll
            frame.scroll(mouse_scroll_step * blt.state(blt.TK_MOUSE_WHEEL))

        elif key == blt.TK_MOUSE_LEFT and blt.state(blt.TK_MOUSE_X) == frame.scrollbar_column:
            py = blt.state(blt.TK_MOUSE_PIXEL_Y)
            if frame.scrollbar_offset <= py <= frame.scrollbar_offset + frame.scrollbar_height * blt.state(blt.TK_CELL_HEIGHT):
                # Clicked on the scrollbar handle: start dragging
                dragging_scrollbar = True
                dragging_scrollbar_offset = py - frame.scrollbar_offset
            else:
                # Clicked outside of the handle: jump to position
                frame.scroll_to_pixel(blt.state(blt.TK_MOUSE_PIXEL_Y) - frame.scrollbar_height * blt.state(blt.TK_CELL_HEIGHT) // 2)

        elif key == blt.TK_MOUSE_LEFT | blt.TK_KEY_RELEASED :
            dragging_scrollbar = False

        elif key == blt.TK_MOUSE_MOVE:
            if dragging_scrollbar:
                frame.scroll_to_pixel(blt.state(blt.TK_MOUSE_PIXEL_Y) - dragging_scrollbar_offset)            

            while blt.peek() == blt.TK_MOUSE_MOVE:
                blt.read()

        elif key == blt.TK_RESIZED:
            frame.update_geometry(
                padding_left,
                padding_top,
                blt.state(blt.TK_WIDTH) - (padding_left + padding_right + 1),
                blt.state(blt.TK_HEIGHT) - (padding_top + padding_bottom))

    blt.set("window: resizeable=false")
    blt.set("runic font: none; stone font: none; curvy font: none")
    blt.set("input.precise-mouse=false;")
Exemplo n.º 3
0
 def render(self):
     terminal.layer(self.layer)
     terminal.crop(self.x, self.y, self.w, self.h)
Exemplo n.º 4
0
def show_msg_history(message_log, name, viewport_w, viewport_h):
    messages = MessageList()
    frame = FrameWithScrollbar(messages)
    message_log.reverse()

    for message in message_log:
        messages.append(message.msg)

    # Initial update
    frame.update_geometry(padding_left + 1, padding_top,
                          viewport_w + 5 - (padding_left + padding_right),
                          viewport_h - (padding_top + padding_bottom))

    prompt = \
        "Message history: \n"

    if name == "Inventory":
        prompt = \
            "Inventory: \n"

    while True:

        frame.draw()
        blt.color("white")

        blt.layer(0)
        w = viewport_w
        h = viewport_h
        i = 0
        while i < 5:
            blt.layer(i)
            blt.clear_area(1, 1, w, h)
            i += 1
        current_line = 0
        blt.puts(padding_left, padding_top - frame.offset, prompt, frame.width)
        for text, height in messages:
            if current_line + height >= frame.offset:
                # stop when message is below frame
                if current_line - frame.offset > frame.height:
                    break
                # drawing message
                blt.puts(padding_left,
                         padding_top + current_line - frame.offset + 5, text,
                         frame.width)
            current_line += height + 1

        blt.crop(padding_left, padding_top, frame.width, frame.height)

        # Render
        blt.refresh()

        key = blt.read()

        if key in (blt.TK_CLOSE, blt.TK_ESCAPE, blt.TK_M):
            blt.clear()
            break

        elif key == blt.TK_I and name == "Inventory":
            blt.clear()
            break

        elif key == blt.TK_UP:
            frame.scroll(-1)

        elif key == blt.TK_DOWN:
            frame.scroll(1)

        elif key == blt.TK_RESIZED:
            frame.update_geometry(
                padding_left, padding_top,
                blt.state(blt.TK_WIDTH) - (padding_left + padding_right + 1),
                blt.state(blt.TK_HEIGHT) - (padding_top + padding_bottom))