def get_func_exprs():
    """
    Prompt for a list of functions the user wants to see graphed.

    As mentioned previously, anything entered remains for the duration
    of the program, so you can graph something, change the settings, and
    return to find your functions still there.
    """
    def change_colors(item):
        # index new color
        item_index = button_entries.index(item)
        COL_POS_LIST[item_index] += 1
        # reset index if gone too far
        if COL_POS_LIST[item_index] >= len(COLOR_LIST):
            COL_POS_LIST[item_index] = 0
        # create new Button image
        col = item.image.copy()
        col.fill(COLOR_LIST[COL_POS_LIST[item_index]])
        # update Button
        item.set_new_image(col)
        item.color = COLOR_LIST[COL_POS_LIST[item_index]]

    box = gui.Container()
    box.add(gui.Button(10, 10, utils.load_image("cube.bmp", True), "Back"))
    box.add(
        gui.Button(constants.SCR_WIDTH - 70, 10,
                   utils.load_image("cube.bmp", False), "Graph!"))

    box.add(text_entries)
    box.add(button_entries)

    while True:
        if pygame.event.peek(QUIT):
            utils.quit()

        box.update(*pygame.event.get())
        for item in box:
            if isinstance(item, gui.Button) and item.clicked:
                item.clicked = False
                if item.text == "Back":
                    return
                elif item.text == "":
                    change_colors(item)
                elif item.text == "Graph!":
                    exprs = [(x.expr_text, y.color)
                             for x, y in zip(text_entries, button_entries)
                             if x.expr_text]
                    draw_graph(exprs)

        blit_background()
        box.draw(SCREEN)
        pygame.display.update()
def menu():
    box = gui.Container()
    box.add(
        gui.Button(100, 50, utils.load_image("sphere.bmp", True), "Graph", 30,
                   colors.white, get_func_exprs))
    box.add(
        gui.Button(300, 200, utils.load_image("sphere.bmp", True), "Settings",
                   30, colors.white, settings))
    box.add(
        gui.Button(500, 350, utils.load_image("sphere.bmp", True), "Help", 30,
                   colors.white, xhelp))

    while True:
        if pygame.event.peek(QUIT):
            utils.quit()

        events = pygame.event.get()
        box.update(*events)
        blit_background()
        box.draw(SCREEN)
        pygame.display.update()
def draw_graph(exprs):
    """
    Draw all functions in |exprs|: each element should supply a tuple
    (or any iterable thing), first element being the expression
    to be eval'd(), second element being a color.
    """

    utils.cls(SCREEN)
    draw_grid = False
    draw_axes = False

    with utils.load_tfile("settings.txt", "rU") as f:
        for line in f:
            l = line.rstrip("\n").split()
            if l[0] == "GRID_ON" and l[1] == "1":
                draw_grid = True
            elif l[0] == "AXES_ON" and l[1] == "1":
                draw_axes = True
            elif l[0] == "GRAPH_ANIM":
                GRAPH["GRAPH_ANIM"] = bool(int(l[1]))

    if draw_grid:
        draw_funcs.draw_grid(GRAPH, SCREEN, colors.white)
    if draw_axes:
        draw_funcs.draw_axes(GRAPH, SCREEN, colors.silver)
    pygame.display.update()

    for expr, col in exprs:
        # automatically add in multiplication signs
        a = re.findall(r"[0-9]+x", expr)
        for pattern in a:
            expr = expr.replace(pattern, "".join(pattern.split("x") + ["*x"]))
        # change ^ to **
        expr = expr.replace("^", "**")
        draw_funcs.draw_function(GRAPH, SCREEN, expr, col)
    pygame.event.clear()

    # save a copy of the current screen for performance reasons
    # (extremely slow to constantly replot functions)
    graphed_surface = SCREEN.copy()

    box = gui.Container()
    pos_label = gui.Label(0, 0, "(0, 0)", 20, colors.lime)
    pos_label_pos = (0, 0)

    while True:
        if pygame.event.peek(QUIT):
            utils.quit()

        events = pygame.event.get()
        for event in events:
            if event.type == KEYDOWN and event.key == K_ESCAPE:
                return
            elif event.type == KEYUP and event.key == K_BACKQUOTE:
                import time
                t = time.strftime("%b%d %I_%M_%S")
                del time
                pygame.image.save(graphed_surface, "graph_%s.bmp" % (t))

            elif event.type == MOUSEMOTION:
                pos_label_pos = event.pos

            elif event.type == MOUSEBUTTONDOWN and event.button == 1:
                if pos_label in box:
                    box.remove(pos_label)
                else:
                    box.add(pos_label)

        box.update(*events)
        pos_label.x, pos_label.y = pos_label_pos
        pos_label.text = "(%.3f, %.3f)" % (draw_funcs.to_graph(
            GRAPH, *pos_label_pos))

        utils.cls(SCREEN)
        SCREEN.blit(graphed_surface, (0, 0))
        box.draw(SCREEN)
        pygame.display.update()
def settings():
    """
    Options for graphing.

    New settings are only saved after you press Accept. This way,
    we can also have a Cancel button (that discards all changes made).
    """
    def save():
        # the file is assumed to be small enough to
        # fit in memory all at once (which it should)
        with utils.load_tfile("settings.txt", "rU") as f:
            stuff = f.read()

        # read old data and modify our new copy
        new_lines = []
        for line in stuff.split("\n"):
            l = line.split()
            if l[0] == "GRID_ON":
                l[1] = str(int(grid_setting))
            elif l[0] == "AXES_ON":
                l[1] = str(int(axis_setting))
            elif l[0] == "GRAPH_ANIM":
                l[1] = str(int(graph_anim_setting))
            elif l[0].startswith("x") or l[0].startswith("y"):
                for t_entry in xy_t_entries:
                    if t_entry.text.startswith(l[0]):
                        l[1] = t_entry.expr_text
                        GRAPH[l[0]] = float(l[1])
                        break
            new_lines.append(" ".join(l))
        GRAPH["x_dist"] = GRAPH["x_right"] - GRAPH["x_left"]
        GRAPH["y_dist"] = GRAPH["y_bottom"] - GRAPH["y_top"]

        # now overwrite the old file with our new copy
        with utils.load_tfile("settings.txt", "w") as f:
            f.write("\n".join(new_lines))

    box = gui.Container()
    box.add(gui.Button(10, 10, utils.load_image("cube.bmp", True), "Cancel"))
    box.add(
        gui.Button(constants.SCR_WIDTH - 70, 10,
                   utils.load_image("cube.bmp", True), "Accept"))

    xy_settings = []
    with utils.load_tfile("settings.txt", "rU") as f:
        for line in f:
            l = line.rstrip("\n")
            if l.startswith("GRID_ON"):
                grid_setting = bool(int(l.split()[1]))
            elif l.startswith("AXES_ON"):
                axis_setting = bool(int(l.split()[1]))
            elif l.startswith("GRAPH_ANIM"):
                graph_anim_setting = bool(int(l.split()[1]))
            elif l.startswith("x") or l.startswith("y"):
                xy_settings.append(l)

    # grid
    box.add(gui.Label(10, 120, "Grid:"))
    grid_button = \
        gui.Button(70, 100, utils.load_image("cube.bmp", True),
                   "On" if grid_setting else "Off")
    box.add(grid_button)

    # axes
    box.add(gui.Label(210, 120, "Axes:"))
    axis_button = \
        gui.Button(270, 100, utils.load_image("cube.bmp", True),
                   "On" if axis_setting else "Off")
    box.add(axis_button)

    # graph animation
    box.add(gui.Label(410, 120, "Graph animation: "))
    graph_anim_button = \
                      gui.Button(570, 100, utils.load_image("cube.bmp", True),
                                 "On" if graph_anim_setting else "Off")
    box.add(graph_anim_button)

    # x/y settings
    y = 200
    xy_t_entries = []
    for item in xy_settings:
        t_entry = \
            gui.TextEntry(10, y, item.split()[0] + ": ", 30, colors.white, 300)
        box.add(t_entry)
        t_entry.text += item.split()[1]
        t_entry.expr_text = item.split()[1]
        xy_t_entries.append(t_entry)
        y += 50

    while True:
        if pygame.event.peek(QUIT):
            utils.quit()

        box.update(*pygame.event.get())
        for item in box:
            if isinstance(item, gui.Button) and item.clicked:
                item.clicked = False
                if item is grid_button:
                    grid_setting = not grid_setting
                    item.text = "On" if grid_setting else "Off"
                elif item is axis_button:
                    axis_setting = not axis_setting
                    item.text = "On" if axis_setting else "Off"
                elif item is graph_anim_button:
                    graph_anim_setting = not graph_anim_setting
                    item.text = "On" if graph_anim_setting else "Off"
                elif item.text == "Cancel":
                    # changes are merely discarded
                    return
                elif item.text == "Accept":
                    save()
                    return

        blit_background()
        box.draw(SCREEN)
        pygame.display.update()
def xhelp():
    """
    Show help. Prefixed with an "x" to avoid confusion with builtin.
    """
    def load():
        """load help text files"""
        for i in range(5):
            parts.append([])
            with utils.load_tfile("help%d.txt" % (i + 1), "rU") as f:
                for line in f:
                    parts[i].append(line.rstrip("\n"))

    def update():
        """update with new text"""
        help_labels.empty()
        y = 100
        help_labels.add(gui.Label(100, 10, parts[current_part][0], 30))
        for t in parts[current_part][2:]:
            help_labels.add(gui.Label(10, y, t, 20))
            y += 20

    box = gui.Container()
    help_labels = gui.Container()

    box.add(gui.Button(10, 10, utils.load_image("cube.bmp", True), "Back"))
    box.add(
        gui.Button(constants.SCR_WIDTH - 70, 10,
                   utils.load_image("cube.bmp", True), "Next"))

    parts = []
    current_part = 0
    load()
    update()

    while True:
        if pygame.event.peek(QUIT):
            utils.quit()

        events = pygame.event.get()
        box.update(*events)
        help_labels.update(*events)

        for item in box:
            if isinstance(item, gui.Button) and item.clicked:
                item.clicked = False
                if item.text == "Back":
                    current_part -= 1
                    if current_part < 0:
                        return
                    update()

                elif item.text == "Next":
                    current_part += 1
                    if current_part >= len(parts):
                        return
                    update()

        blit_background()
        box.draw(SCREEN)
        help_labels.draw(SCREEN)
        pygame.display.update()
Exemple #6
0
def main():

    pygame.init()
    screen = pygame.display.set_mode((800, 600))

    input = gui.Input()

    the_gui = gui.Gui(input)

    input.addMouseListener(the_gui)
    input.addKeyboardListener(the_gui)

    box = gui.Box()
    box.setPosition(0, 0)
    box.setDimensions(10, 10)

    box.setPaddings(Pad(1, 2, 3, 4))
    box.setBorders(Pad(10, 10, 4, 4))
    box.setMargins(Pad(9, 10, 11, 12))

    container = gui.Container()
    the_gui.addWidget(container)
    container.addWidget(box)

    content_area = box.getContentArea()
    padded_area = box.getPaddedArea()
    bordered_area = box.getBorderedArea()
    whole_area = box.getWholeArea()

    # Testing parenting:
    frame = gui.Frame()
    frame.setPosition(0, 0)
    #frame.setMargins(EqualPad(4))
    frame.setBorders(EqualPad(1))
    frame.setPaddings(Pad(2, 2, 20, 2))
    frame.setDimensions(200, 200)
    #frame.setLayout(gui.GridLayout(3,4))
    #frame.setLayout(gui.GridLayout(2, 4, Align.TOP))
    frame.setLayout(gui.VerticalLayout())
    the_gui.addWidget(frame)

    for i in range(0, 8):

        box = gui.Box()
        box.setPosition(0, 0)
        #size = 10 + random.randint(0,4)

        width = i * 5
        height = i * 5

        box.setBorders(Pad(1, 1, 1, 1))
        box.setMargins(Pad(5, 5, 5, 5))
        frame.addWidget(box)
        box.setDimensions(width, height)

    # Testing clipping:
    frame = gui.Frame()
    frame.setPosition(340, 0)

    frame.setMargins(Pad(4, 4, 4, 4))
    frame.setBorders(Pad(1, 1, 1, 1))
    frame.setDimensions(80, 200)
    frame.setPaddings(EqualPad(15))
    the_gui.addWidget(frame)

    inner_frame = gui.Frame()
    inner_frame.setPosition(40, 0)
    inner_frame.setMargins(Pad(4, 4, 4, 4))
    inner_frame.setBorders(Pad(1, 1, 1, 1))
    inner_frame.setDimensions(180, 100)
    frame.addWidget(inner_frame)

    inner_frame.setLayout(gui.GridLayout(3, 5))

    for i in range(0, 12):
        col = i % 3
        row = i / 3

        box2 = gui.Box()
        box2.setPosition(0, 0)
        #size = 10 + random.randint(0,4)

        width = col * 10
        height = row * 5

        box2.setDimensions(width, height)
        box2.setBorders(Pad(1, 1, 1, 1))
        box2.setMargins(Pad(5, 5, 5, 5))
        inner_frame.addWidget(box2)

    img = gui.loadImage("res/aircraft.png")
    image_widget = gui.Image(img)
    image_widget.setPosition(0, 0)
    image_widget.setBorders(Pad(3, 3, 3, 3))
    image_widget.setDimensions(100, 100)
    inner_frame.addWidget(image_widget)

    # Testing ImageButton
    img_default = gui.loadImage("res/truck.png")
    img_pressed = gui.loadImage("res/truck_bnw.png")
    img_button = gui.ImageButton(img_default, img_pressed)
    img_button.setPosition(20, 200)
    img_button.setDimensions(30, 30)
    the_gui.addWidget(img_button)

    # Testing elements alignment:

    frame = gui.Frame()
    frame.setDimensions(200, 200)
    frame.setLayout(gui.GridLayout(2, 6))
    frame.setPosition(0, 300)
    the_gui.addWidget(frame)

    run = True
    while (run):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            input.handleEvent(event)

        screen.fill((255, 255, 255))
        the_gui.draw(screen)
        pygame.display.flip()

    pygame.quit()
Exemple #7
0
import pygame
from pygame.locals import *
import gui

screen = pygame.display.set_mode(
    (640, 480), FULLSCREEN)  # try adding DOUBLEBUF | HWSURFACE
# pygame.mouse.set_visible(0)

app = gui.App()
c = gui.Container(width=640, height=480)

##
## dialog 1
##

t1 = gui.Table()
t1.tr()
t1.add(gui.Label("Gal Test"))

t2 = gui.Table()

t2.tr()
t2.add(gui.Label("Gui Widgets"))
t2.add(gui.Input())

t2.tr()
t2.add(gui.Label("Button"))
t2.add(gui.Button("Click Me!"))

d1 = gui.Dialog(t1, t2)
c.add(d1, 50, 150)