Ejemplo n.º 1
0
def run():
    import thorpy
    application = thorpy.Application((600, 600), "ThorPy test")

    ##thorpy.theme.set_theme("human")

    #### SIMPLE ELEMENTS ####

    ghost = thorpy.Ghost()
    ghost.finish()

    element = thorpy.Element("Element")
    element.finish()
    thorpy.makeup.add_basic_help(
        element, "Element instance:\nMost simple graphical element.")

    clickable = thorpy.Clickable("Clickable")
    clickable.finish()
    clickable.add_basic_help(
        "Clickable instance:\nCan be hovered and pressed.")

    draggable = thorpy.Draggable("Draggable")
    draggable.finish()
    thorpy.makeup.add_basic_help(draggable,
                                 "Draggable instance:\nYou can drag it.")

    #### SIMPLE Setters ####

    checker_check = thorpy.Checker("Checker")
    checker_check.finish()
    thorpy.makeup.add_basic_help(
        checker_check, "Checker instance:\nHere it is of type 'checkbox'.")

    checker_radio = thorpy.Checker("Radio", typ="radio")
    checker_radio.finish()
    thorpy.makeup.add_basic_help(
        checker_radio, "Checker instance:\nHere it is of type 'radio'.")

    browser = thorpy.Browser("../../", text="Browser")
    browser.finish()
    browser.set_prison()

    browserlauncher = thorpy.BrowserLauncher(browser,
                                             name_txt="Browser",
                                             file_txt="Nothing selected",
                                             launcher_txt="...")
    browserlauncher.finish()
    browserlauncher.scale_to_title()
    thorpy.makeup.add_basic_help(
        browserlauncher,
        "Browser instance:\nA way for user to find a file or" +
        "\na folder on the computer.")

    dropdownlist = thorpy.DropDownListLauncher(
        name_txt="DropDownListLauncher",
        file_txt="Nothing selected",
        titles=[str(i) for i in range(1, 9)])
    dropdownlist.finish()
    dropdownlist.scale_to_title()
    thorpy.makeup.add_basic_help(dropdownlist,
                                 "DropDownList:\nDisplay a list of choices.")

    slider = thorpy.SliderX(120, (5, 12),
                            "Slider: ",
                            typ=float,
                            initial_value=8.4)
    slider.finish()
    thorpy.makeup.add_basic_help(
        slider, "SliderX:\nA way for user to select a value." +
        "\nCan select any type of number (int, float, ..).")
    slider.set_center

    inserter = thorpy.Inserter(name="Inserter: ", value="Write here.")
    inserter.finish()
    thorpy.makeup.add_basic_help(
        inserter, "Inserter:\nA way for user to insert a value.")

    text_title = thorpy.make_text("Test Example", 25, (0, 0, 255))

    central_box = thorpy.Box("", [
        ghost, element, clickable, draggable, checker_check, checker_radio,
        dropdownlist, browserlauncher, slider, inserter
    ])
    central_box.finish()
    central_box.center()
    central_box.add_lift()
    central_box.set_main_color((200, 200, 255, 120))

    background = thorpy.Background(color=(200, 200, 200),
                                   elements=[text_title, central_box])
    background.finish()

    thorpy.store(background)

    menu = thorpy.Menu(background)
    menu.play()

    application.quit()
Ejemplo n.º 2
0
def apploop(DISPLAY):
    global display_height, display_width
    #main app loo
    pygame.display.set_caption('Planetbox')
    pygame.display.set_icon(ico)

    display_width, display_height = DISPLAY.get_size()
    DISPLAY.fill(RICHBLUE)

    #display background
    Bg = Background('../imgs/bg.jpg', [0, 0])
    DISPLAY.blit(
        pygame.transform.scale(Bg.image, (display_width, display_height)),
        Bg.rect)

    #display sky preview
    Skyprev = SkyPrev(DISPLAY)

    #update
    pygame.display.update()

    #thorpy elements

    #logo
    logo = pygame.image.load('../imgs/logo300.png')

    def radius_check(radius, type):
        if (type == "terrestrial"):
            if (radius < 300):
                return 0
            else:
                return 1
        elif (type == "ice" or type == "gas"):
            if (radius < 200):
                return 0
            else:
                return 1

    # checks if the density is ok
    def density_check(ptype, pmass, prad):
        # pmass: 10^22 kg, prad: km
        rad = prad * 100000  # radius in cm
        vol = (4 / 3) * math.pi * rad * rad * rad  # cm3
        mass = pmass * pow(10, 25)  # g
        g = mass / vol  # g/cm3

        if ptype == "terrestrial":
            if (g < 3.6):  # not sure about this one
                return 0
            elif (g > 28):
                return 1
            else:
                return 2
        elif ptype == "gas" or ptype == "ice":
            if (g < 0.2):  # not sure about this one
                return 0
            elif (g > 17):
                return 1
            else:
                return 2

    #Reading inputs functions

    def read_inserter_func(
            event):  #Reactions functions must take an event as first arg
        global prad, pmass, pname, pdist
        try:
            if (event.el == p_rad):
                prad = event.el.get_value()
                prad = int(prad)
            elif (event.el == p_dist):
                pdist = event.el.get_value()
                pdist = float(pdist)
            elif (event.el == p_mass):
                pmass = event.el.get_value()
                pmass = int(pmass)
            elif (event.el == p_name):
                pname = event.el.get_value()
            elif (event.el == p_kind):
                ptype = event.el.get_value()
        except:  # handling errors
            AlertBox.AlertBox(1, "valueError")
            p_rad.set_value("")
            p_dist.set_value("")
            p_mass.set_value("")
            p_rad.unblit_and_reblit()
            p_dist.unblit_and_reblit()
            p_mass.unblit_and_reblit()

    def reset_simulation():
        simulation.Planets = []
        simulation.PlanetsCord = []
        SkyPrev(DISPLAY)

    # pressing add planet btn reaction
    def readPlanet():
        global prad, pmass, pname, ptype, simulation, pdist
        ptype = p_kind.get_selected().get_text()
        den_val = density_check(ptype, pmass, prad)
        rad_val = radius_check(prad, ptype)
        if den_val == 0 or den_val == 1:  # wrong density
            AlertBox.AlertBox(den_val, "density")
        elif rad_val == 0:  # radius too small
            AlertBox.AlertBox(rad_val, "radius")
        else:
            #print(ptype)
            # create a new planet
            # clean the inserters
            p_rad.set_value("")
            p_rad.unblit_and_reblit()

            p_dist.set_value("")
            p_dist.unblit_and_reblit()

            p_mass.set_value("")
            p_mass.unblit_and_reblit()

            p_name.set_value("")
            p_name.unblit_and_reblit()

            planet = Planet.Planet(prad, pmass, ptype, pdist, pname)
            simulation.AddPlanet(planet)
            # update preview
            SkyPrev(DISPLAY)

    def startExplorer():
        #print("Starting explorer...")
        planetExp.pe_main()

    def startSimulation():
        #simulation.CreateMoons()
        #print("Starting simulation...")
        print(simulation.PrintPlanets())
        simulation_gui.create(simulation)

    # add button: adds planet to a list and updates prev
    addBtn = thorpy.make_button("Add planet", func=readPlanet)
    addBtn.set_size((120, 20))
    addBtn.set_main_color(RICHBLUE)
    addBtn.set_font_color(WHITE)

    # new window: starts simulation
    startBtn = thorpy.make_button("Start simulation", func=startSimulation)
    startBtn.set_size((120, 20))
    startBtn.set_main_color(RICHBLUE)
    startBtn.set_font_color(WHITE)

    # explore planets: lets u choose a planet and prints info about it and moons simulation
    prevBtn = thorpy.make_button("Explore the planets", func=startExplorer)
    prevBtn.set_size((120, 20))
    prevBtn.set_main_color(RICHBLUE)
    prevBtn.set_font_color(WHITE)

    # reset simulation
    resBtn = thorpy.make_button("Reset simulation", func=reset_simulation)
    resBtn.set_size((120, 20))
    resBtn.set_main_color(RICHBLUE)
    resBtn.set_font_color(WHITE)

    # radius input
    p_rad_txt = thorpy.OneLineText(text="Radius of the planet(km):")
    p_rad = thorpy.Inserter(name="", value="", size=(100, 20))

    radReact = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT,
                               reac_func=read_inserter_func,
                               event_args={
                                   "id": thorpy.constants.EVENT_INSERT,
                                   "el": p_rad
                               })

    p_rad.add_reaction(radReact)

    # distance to the sun input
    p_dist_txt = thorpy.OneLineText(text="Distance to the sun (AU):")
    p_dist = thorpy.Inserter(name="", value="", size=(100, 20))

    distReact = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT,
                                reac_func=read_inserter_func,
                                event_args={
                                    "id": thorpy.constants.EVENT_INSERT,
                                    "el": p_dist
                                })

    p_dist.add_reaction(distReact)

    # name input
    p_name_txt = thorpy.OneLineText(text="Name of the planet: ")
    p_name = thorpy.Inserter(name="", value="", size=(100, 20))
    nameReact = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT,
                                reac_func=read_inserter_func,
                                event_args={
                                    "id": thorpy.constants.EVENT_INSERT,
                                    "el": p_name
                                })

    p_name.add_reaction(nameReact)

    # mass input
    p_mass_txt = thorpy.OneLineText(text="Mass of the planet (10^22 kg):")
    p_mass = thorpy.Inserter(name="", value="", size=(100, 20))
    massReact = thorpy.Reaction(reacts_to=thorpy.constants.THORPY_EVENT,
                                reac_func=read_inserter_func,
                                event_args={
                                    "id": thorpy.constants.EVENT_INSERT,
                                    "el": p_mass
                                })

    p_mass.add_reaction(massReact)

    # type of planet input
    radio_txt = thorpy.make_text("Type of the planet: ")
    radios = [
        thorpy.Checker("gas", type_="radio"),
        thorpy.Checker("ice", type_="radio"),
        thorpy.Checker("terrestrial", type_="radio")
    ]
    p_kind = thorpy.RadioPool(radios, first_value=radios[1], always_value=True)

    # title above the preview
    prev_txt = thorpy.make_text("Preview of the planetary system: ", 24, WHITE)
    prev_txt.set_font("Ubuntu.ttf")
    prev_txt.set_topleft((420, 200))

    # blit thingies
    entries = [
        p_rad_txt, p_rad, p_mass_txt, p_mass, p_dist_txt, p_dist, p_name_txt,
        p_name
    ]
    txts = [radio_txt]
    buttons = [addBtn, prevBtn, startBtn, resBtn]
    elements = entries + txts + radios + buttons
    boxBtn = thorpy.Box.make(elements=elements)
    boxBtn.set_main_color(WHITE)
    boxBtn.set_size((260, 500))

    thorpy.store(boxBtn, elements, align="center")

    menu = thorpy.Menu(elements=[prev_txt, boxBtn])

    for element in menu.get_population():
        element.surface = DISPLAY

    boxBtn.set_topleft((40, 50))
    boxBtn.blit()
    boxBtn.update()
    prev_txt.blit()
    prev_txt.update()
    DISPLAY.blit(logo, (390, 20))
    pygame.display.flip()

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.VIDEORESIZE:
                DISPLAY = pygame.display.set_mode(
                    event.dict['size'],
                    pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)
                DISPLAY.blit(
                    pygame.transform.scale(Bg.image, event.dict['size']),
                    (0, 0))
                display_width, display_height = event.dict['size']
                apploop(DISPLAY)
                pygame.display.flip()
            menu.react(event)
Ejemplo n.º 3
0
def run():
    application = thorpy.Application((800, 600), "ThorPy Overview")

    element = thorpy.Element("Element")
    thorpy.makeup.add_basic_help(element,
                                 "Element:\nMost simple graphical element.")

    clickable = thorpy.Clickable("Clickable")
    thorpy.makeup.add_basic_help(clickable,
                                 "Clickable:\nCan be hovered and pressed.")

    draggable = thorpy.Draggable("Draggable")
    thorpy.makeup.add_basic_help(draggable, "Draggable:\nYou can drag it.")

    checker_check = thorpy.Checker("Checker")

    checker_radio = thorpy.Checker("Radio", type_="radio")

    browser = thorpy.Browser("../../", text="Browser")

    browserlauncher = thorpy.BrowserLauncher.make(browser,
                                                  const_text="Choose file:",
                                                  var_text="")
    browserlauncher.max_chars = 20  #limit size of browser launcher

    dropdownlist = thorpy.DropDownListLauncher(
        const_text="Choose number:",
        var_text="",
        titles=[str(i) * i for i in range(1, 9)])
    dropdownlist.scale_to_title()
    dropdownlist.max_chars = 20  #limit size of drop down list

    slider = thorpy.SliderX(80, (5, 12),
                            "Slider: ",
                            type_=float,
                            initial_value=8.4)

    inserter = thorpy.Inserter(name="Inserter: ", value="Write here.")

    quit = thorpy.make_button("Quit", func=thorpy.functions.quit_menu_func)

    title_element = thorpy.make_text("Overview example", 22, (255, 255, 0))

    elements = [
        element, clickable, draggable, checker_check, checker_radio,
        dropdownlist, browserlauncher, slider, inserter, quit
    ]
    central_box = thorpy.Box(elements=elements)
    central_box.fit_children(margins=(30, 30))  #we want big margins
    central_box.center()  #center on screen
    central_box.add_lift()  #add a lift (useless since box fits children)
    central_box.set_main_color(
        (220, 220, 220, 180))  #set box color and opacity

    background = thorpy.Background.make(image=thorpy.style.EXAMPLE_IMG,
                                        elements=[title_element, central_box])
    thorpy.store(background)

    menu = thorpy.Menu(background)
    menu.play()

    application.quit()
Ejemplo n.º 4
0
 def fill_radio_buttons(self):
     for txt in self.radio_butt_text:
         rad = thorpy.Checker(txt, type_="radio")
         rad.finish()
         self.radio_buttons.append(rad)
Ejemplo n.º 5
0
text = thorpy.make_text("Some text", 12, (0, 0, 255))
line = thorpy.Line(200, "h")  #horizontal line of width = 200px

element = thorpy.Element("Element")
thorpy.makeup.add_basic_help(element,
                             "Element:\nMost simple graphical element.")

clickable = thorpy.Clickable("Clickable")
thorpy.makeup.add_basic_help(clickable,
                             "Clickable:\nCan be hovered and pressed.")

draggable = thorpy.Draggable("Draggable")
thorpy.makeup.add_basic_help(draggable, "Draggable:\nYou can drag it.")

checker_check = thorpy.Checker("Checker")

checker_radio = thorpy.Checker("Radio", type_="radio")

browser = thorpy.Browser("../../", text="Browser")

browserlauncher = thorpy.BrowserLauncher(browser,
                                         const_text="Choose file:",
                                         var_text="")
browserlauncher.max_chars = 15  #limit size of browser launcher

dropdownlist = thorpy.DropDownListLauncher(
    const_text="Choose:",
    var_text="",
    titles=[str(i) * i for i in range(1, 9)])
dropdownlist.scale_to_title()
Ejemplo n.º 6
0
import thorpy

ap = thorpy.Application((300, 300))

#format of a Pool : thorpy.Pool(elements, first_value, always_value)
#   <first_value> can be None, and is the element that is 'on' at the beginning
#   <always_value>=True means that there must always be an element which is 'on'

#make radios
radios = [thorpy.Checker("radio" + str(i), type_="radio") for i in range(4)]
radio_pool = thorpy.RadioPool(radios, first_value=radios[2], always_value=True)

#make togglable buttons
buttons = [thorpy.Togglable("togglable" + str(i)) for i in range(4)]
togglable_pool = thorpy.TogglablePool(buttons,
                                      first_value=buttons[1],
                                      always_value=False)

#cosmetic separation line
line = thorpy.Line(200, "h")

bck = thorpy.Background.make((220, 220, 255),
                             elements=radios + [line] + buttons)
thorpy.store(bck)
menu = thorpy.Menu(bck)
menu.play()
ap.quit()
Ejemplo n.º 7
0
 def add_checker(self, nombre):
     self.checkers[nombre] = thorpy.Checker(nombre, value=False)