Exemple #1
0
def main(client, setupinfo=None):
    gameid = 'test1'  #setupinfo["request"]["parameters"]["id"]
    height = 720
    width = 1024
    screensize = (width, height)
    screen = pygame.display.set_mode(screensize)
    clock = pygame.time.Clock()
    pygame.mouse.set_visible(0)
    animate = True
    running = True

    background, background_rect = load_image("stars.jpg")
    outer_menu = MenuBar(planet1name, 1, backgroundcolor, True, (90, 0))
    menubar.pdb_image, menubar.pdbrect = load_image("pdbup.png", None)
    menubar.pdbrect.topleft = menubar.pdbboxpostion
    screen.blit(background, background_rect)
    mouse_ptr = MouseCursor("pointer2.png")
    mouse_sel = MouseCursor("selected2.png")
    mouse = pygame.sprite.RenderUpdates((mouse_ptr))
    mouse.draw(screen)
    pygame.display.flip()
    #===========================================================================
    # Object Initialization:
    #===========================================================================
    characterlist = client.root.get_state(
        game_id=gameid, object_type="Character")["response"]["character"]
    planetlist = client.root.get_state(
        game_id=gameid, object_type="Planet")["response"]["planet"]
    environlist = client.root.get_state(
        game_id=gameid, object_type="Environ")["response"]["environ"]
    militarylist = client.root.get_state(
        game_id=gameid, object_type="Unit")["response"]["unit"]
    stacklist = client.root.get_state(game_id=gameid,
                                      object_type="Stack")["response"]["stack"]
    star_system = System(screen, background, animate, characterlist,
                         planetlist, environlist, militarylist, stacklist)
    #print characterlist
    menu = Menu_Buttons.Menu(screen)

    selected_unit = None

    while running:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sysexit()
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                pygame.quit()
                sysexit()
            if not mouse_ptr.down:
                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    if selected_unit:
                        hover_unit = left_mouse_select_check(
                            client, mouse_sel, star_system)
                        if hover_unit != selected_unit:
                            #mergeresponse = client.root.merge_stack(unit.stack_id, selected_unit.stack_id)
                            #if mergeresponse["Success"]:
                            selected_unit.add_unit(hover_unit)
                            star_system.unit_list.remove(hover_unit)
                    print "SPACE BAR"
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        mouse_ptr.pressed = mouse_sel.pressed = True
                        mouse_ptr.released = mouse_sel.released = False
                        selected_unit = left_mouse_select_check(
                            client, mouse_ptr, star_system)
                        # while the mouse button is down, change its cursor
                        mouse.remove(mouse_ptr)
                        mouse.add(mouse_sel)
                        #Update Buttons and menu status
                        menu.update_buttons(mouse_ptr.rect)
                    elif event.button == 3:
                        key_mod = pygame.key.get_mods()
                        if key_mod == 4097 or key_mod == 1:
                            hover_unit = star_system.unit_list.get_sprites_at(
                                mouse_ptr.pos)
                            print "STACK REMOVING FROM", hover_unit
                            if hover_unit:
                                if hover_unit[0].stack_list[-1].charflag:
                                    splitresponse = client.root.split_stack(
                                        stack_id=hover_unit[0].stack_id,
                                        character_id=hover_unit[0].
                                        stack_list[-1].id)
                                else:
                                    splitresponse = client.root.split_stack(
                                        stack_id=hover_unit[0].stack_id,
                                        unit_id=hover_unit[0].stack_list[-1].id
                                    )
                                print splitresponse
                                if splitresponse["request"]["success"]:
                                    sprite = hover_unit[0].remove_unit()
                                    sprite.set_stack_id(
                                        splitresponse["response"]["unit"]
                                        ["stack_id"])
                                    star_system.unit_list.add(sprite)
                            print "SHIFT RIGHT CLICK"
                        else:
                            hover_unit = star_system.unit_list.get_sprites_at(
                                mouse_ptr.pos)
                            if hover_unit:
                                hover_unit[0].cycle_unit()
                            print "RIGHT CLICK"

            if event.type == pygame.MOUSEBUTTONUP:
                mouse_ptr.released = mouse_sel.released = True
                selected_unit = left_mouse_unselect_check(
                    client, mouse, selected_unit, star_system)
                # while the mouse button is up, change its cursor
                mouse.remove(mouse_sel)
                mouse.add(mouse_ptr)

        screen.blit(background, background_rect)
        star_system.update()
        if selected_unit:
            selected_unit.update(True)
        mouse.update()
        star_system.draw()
        #Draw buttons and menus
        menu.draw_buttons(screen, height, width, selected_unit)
        for planet in star_system.planet_list:
            if planet.orient == 'center':
                current_planet = planet
        try:
            outer_menu.update(current_planet.name, current_planet.loyalty,
                              current_planet.pdb_state, "Egrix", False, False)
        except:
            pass
        outer_menu.draw(background, screen, menubar_)
        mouse.draw(screen)
        pygame.display.flip()
Exemple #2
0
def main(client, setupinfo=None):
    gameid = 'test1'#setupinfo["request"]["parameters"]["id"]
    height = 720
    width = 1024
    screensize = (width, height)
    screen = pygame.display.set_mode(screensize)
    clock = pygame.time.Clock()
    pygame.mouse.set_visible(0)
    animate = True
    running = True

    background, background_rect = load_image("stars.jpg")
    outer_menu = MenuBar(planet1name, 1, backgroundcolor, True, (90,0))
    menubar.pdb_image, menubar.pdbrect = load_image("pdbup.png", None)
    menubar.pdbrect.topleft = menubar.pdbboxpostion
    screen.blit(background, background_rect)
    mouse_ptr = MouseCursor("pointer2.png")
    mouse_sel = MouseCursor("selected2.png")
    mouse = pygame.sprite.RenderUpdates((mouse_ptr))
    mouse.draw(screen)
    pygame.display.flip()
    #===========================================================================
    # Object Initialization:
    #===========================================================================
    characterlist = client.root.get_state(game_id = gameid, object_type="Character")["response"]["character"]
    planetlist = client.root.get_state(game_id = gameid, object_type="Planet")["response"]["planet"]
    environlist = client.root.get_state(game_id = gameid, object_type="Environ")["response"]["environ"]
    militarylist = client.root.get_state(game_id = gameid, object_type="Unit")["response"]["unit"]
    stacklist = client.root.get_state(game_id = gameid, object_type="Stack")["response"]["stack"]
    star_system = System(screen, background, animate, characterlist, planetlist, environlist, militarylist, stacklist)
    #print characterlist
    menu = Menu_Buttons.Menu(screen)
    
    
    selected_unit = None

    while running:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sysexit()
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
                pygame.quit()
                sysexit()
            if not mouse_ptr.down:
                if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
                    if selected_unit:
                        hover_unit = left_mouse_select_check(client, mouse_sel, star_system)
                        if hover_unit != selected_unit:
                            #mergeresponse = client.root.merge_stack(unit.stack_id, selected_unit.stack_id)
                            #if mergeresponse["Success"]:
                            selected_unit.add_unit(hover_unit)
                            star_system.unit_list.remove(hover_unit)
                    print "SPACE BAR"
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button == 1:
                        mouse_ptr.pressed = mouse_sel.pressed = True
                        mouse_ptr.released = mouse_sel.released = False
                        selected_unit = left_mouse_select_check(client, mouse_ptr, star_system)
                        # while the mouse button is down, change its cursor
                        mouse.remove(mouse_ptr)
                        mouse.add(mouse_sel)
						#Update Buttons and menu status
                        menu.update_buttons(mouse_ptr.rect)
                    elif event.button == 3:
                        key_mod = pygame.key.get_mods()
                        if key_mod == 4097 or key_mod == 1:
                            hover_unit = star_system.unit_list.get_sprites_at(mouse_ptr.pos)
                            print "STACK REMOVING FROM", hover_unit
                            if hover_unit:
                                if hover_unit[0].stack_list[-1].charflag:
                                    splitresponse = client.root.split_stack(stack_id = hover_unit[0].stack_id, character_id =hover_unit[0].stack_list[-1].id)
                                else:
                                    splitresponse = client.root.split_stack(stack_id = hover_unit[0].stack_id, unit_id =hover_unit[0].stack_list[-1].id)
                                print splitresponse
                                if splitresponse["request"]["success"]:
                                    sprite = hover_unit[0].remove_unit()
                                    sprite.set_stack_id(splitresponse["response"]["unit"]["stack_id"])
                                    star_system.unit_list.add(sprite)
                            print "SHIFT RIGHT CLICK"
                        else:
                            hover_unit = star_system.unit_list.get_sprites_at(mouse_ptr.pos)
                            if hover_unit:
                                hover_unit[0].cycle_unit()
                            print "RIGHT CLICK"

            if event.type == pygame.MOUSEBUTTONUP:
                mouse_ptr.released = mouse_sel.released = True
                selected_unit = left_mouse_unselect_check(client, mouse, selected_unit, star_system)
                # while the mouse button is up, change its cursor
                mouse.remove(mouse_sel)
                mouse.add(mouse_ptr)

        screen.blit(background, background_rect)
        star_system.update()
        if selected_unit:
            selected_unit.update(True)
        mouse.update()
        star_system.draw()
		#Draw buttons and menus
        menu.draw_buttons(screen, height, width, selected_unit)
        for planet in star_system.planet_list:
            if planet.orient == 'center':
                current_planet = planet
        try:
            outer_menu.update(current_planet.name, current_planet.loyalty, current_planet.pdb_state, "Egrix", False, False)
        except:
            pass
        outer_menu.draw(background, screen, menubar_)
        mouse.draw(screen)
        pygame.display.flip()
Exemple #3
0
class App:
    def __init__(self,
                 name,
                 menubar,
                 idle_icon_path=ICON_UNKNOWN,
                 open_icon_path=None,
                 function=None,
                 is_folder=False):
        self.name = name  # Name of app
        self.menubar = menubar  # The parent menubar
        self.function = function

        if is_folder:
            self.function = self.toggle_is_open

            idle_icon_path = ICON_FOLDER
            open_icon_path = ICON_FOLDER_OPEN

        self.idle_rect = pygame.Rect(0, 0, APP_DIMENSION, APP_DIMENSION)
        self.selected_rect = pygame.Rect(0, 0, APP_DIMENSION + UI_PADDING,
                                         APP_DIMENSION + UI_PADDING)
        self.rect = self.idle_rect

        self.idle_icon = pygame.transform.scale(
            pygame.image.load(idle_icon_path), self.idle_rect.size)
        self.selected_icon = pygame.transform.scale(self.idle_icon,
                                                    self.selected_rect.size)
        self.open_icon = pygame.transform.scale(
            pygame.image.load(idle_icon_path if open_icon_path ==
                              None else open_icon_path), self.idle_rect.size)
        self.open_selected_icon = pygame.transform.scale(
            self.open_icon, self.selected_rect.size)
        self.icon = self.idle_icon

        self.sub_apps = []
        self.sub_app_menubar = MenuBar(
            self.menubar.screen, self.menubar.font,
            self.menubar.rect[1] + APP_MENUBAR_OFFSET)

        self.is_selected = False
        self.is_open = False

        self.resize()

    def update(self, mouse_data, key_data):
        if mouse_data[0] and self.is_selected:
            self.run()

            self.is_selected = False
        else:
            self.is_selected = in_bounds(self.rect)

        self.icon = (self.open_selected_icon if self.is_open else
                     self.selected_icon) if self.is_selected else (
                         self.open_icon if self.is_open else self.idle_icon)
        self.rect = self.selected_rect if self.is_selected else self.idle_rect

        if self.is_open:
            self.sub_app_menubar.update(mouse_data, key_data)

            for sub_app in self.sub_apps:
                sub_app.update(mouse_data, key_data)

    def draw(self, screen):
        screen.blit(self.icon, self.rect)

        if self.is_open:
            self.sub_app_menubar.draw()

            for sub_app in self.sub_apps:
                sub_app.draw(screen)

    def set_pos(self, pos):
        self.idle_rect = pygame.Rect(pos, (APP_DIMENSION, APP_DIMENSION))
        self.selected_rect = pygame.Rect(pos[0] - (UI_PADDING / 2.0),
                                         pos[1] - (UI_PADDING / 2.0),
                                         APP_SELECTED_DIMENSION,
                                         APP_SELECTED_DIMENSION)
        self.rect = self.idle_rect

        self.resize()

    def run(self):
        if self.function != None:
            self.function()

            self.is_selected = False

    def resize(self):
        self.sub_app_menubar.resize()

    def toggle_is_open(self):
        self.is_open = not self.is_open

    def add_app(self, app):
        self.sub_app_menubar.add_app(app)
Exemple #4
0
def main():
    ''' The main method :o '''

    global PI_CLIENT
    global PI_CLIENT_CONNECTED
    global IS_RUNNING

    joysticks = [
        pygame.joystick.Joystick(x) for x in range(pygame.joystick.get_count())
    ]
    PRINT('Found ' + ENC_VALUE(len(joysticks)) + ' joysticks.', INFO)

    # Initialize detected joysticks
    if len(joysticks) > 0:
        for joystick in joysticks:
            joystick.init()
            PRINT(
                '| ' + ENC_VALUE(joystick.get_id()) + ' ' +
                ENC_VALUE(joystick.get_name()), INFO)

    screen = pygame.display.set_mode(SCREEN_DIMENSION, pygame.RESIZABLE)
    screen.set_alpha(None)
    pygame.display.set_caption('LoggerheadROV Driver Station | ' +
                               random.choice(QUOTES))
    pygame.display.set_icon(
        pygame.image.load('gui/images/loggerhead_logo.png'))

    clock = pygame.time.Clock()
    horizon = Horizon(
        screen.get_size()[0] - GAUGE_DIMENSION - UI_SCREEN_PADDING - UI_BORDER,
        screen.get_size()[1] - GAUGE_DIMENSION - UI_SCREEN_PADDING - UI_BORDER)

    menubar = MenuBar(screen, FONT)
    menubar.add_app(App("Do Thing", menubar))
    menubar.add_app(App("Do Other Thing", menubar))
    folder = App("Image Recognition", menubar, is_folder=True)
    folder.add_app(App("More Things", folder.sub_app_menubar))
    menubar.add_app(folder)

    # Create thread for the connection handler loop
    connection_handler = Thread(target=connection_listener, args=(joysticks, ))
    connection_handler.start()

    while IS_RUNNING:
        mouse_data = [False] * len(pygame.mouse.get_pressed())
        key_data = [False] * len(pygame.key.get_pressed())

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                shutdown()

            if event.type == pygame.VIDEORESIZE:
                screen = pygame.display.set_mode((event.w, event.h),
                                                 pygame.RESIZABLE)
                horizon = Horizon(
                    screen.get_size()[0] - GAUGE_DIMENSION -
                    UI_SCREEN_PADDING - UI_BORDER,
                    screen.get_size()[1] - GAUGE_DIMENSION -
                    UI_SCREEN_PADDING - UI_BORDER)
                menubar.resize()

            if event.type == pygame.KEYDOWN:
                key_data = pygame.key.get_pressed()

            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_data = pygame.mouse.get_pressed()

        mouse_pos = pygame.mouse.get_pos()

        screen.fill(UI_COLOR_4)

        if PI_CLIENT_CONNECTED:
            try:
                # Get the video frame from the client and decode it
                frame = cv2.imdecode(PI_CLIENT.recv_data[DATA_IDX_VIDEO],
                                     cv2.IMREAD_COLOR)
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                frame = np.rot90(frame)
                frame = cv2.flip(frame, 1)
                frame = pygame.surfarray.make_surface(frame)

                frame = pygame.transform.scale(frame, screen.get_size())

                screen.blit(frame, (0, 0))

                # cv2.imshow('frame', frame)
            except:
                pass

        menubar.update(mouse_data, key_data)
        # horizon.update(screen, mouse_pos[0] - (screen.get_size()[0] / 2), mouse_pos[1] - (screen.get_size()[1] / 2)) # Draws gauge too

        menubar.draw()
        draw_text(screen, FONT,
                  str(round(clock.get_fps(), 3)) + ' FPS',
                  (screen.get_size()[0] -
                   (UI_SCREEN_PADDING * 8), UI_SCREEN_PADDING), False)

        pygame.display.update()
        clock.tick(FPS)

    cv2.destroyAllWindows()
    connection_handler.join()

    PRINT('Quit.', SUCCESS)