def test_disable(self, _init_pygame: None, default_ui_manager: UIManager, _display_surface_return_none: None): text_entry = UITextEntryLine(relative_rect=pygame.Rect( 100, 100, 200, 30), manager=default_ui_manager) text_entry.focus() text_entry.disable() assert text_entry.is_enabled is False # process a mouse button down event processed_key_event = text_entry.process_event( pygame.event.Event(pygame.KEYDOWN, { 'key': pygame.K_d, 'mod': 0, 'unicode': 'd' })) text_entry.process_event( pygame.event.Event(pygame.KEYDOWN, { 'key': pygame.K_a, 'mod': 0, 'unicode': 'a' })) text_entry.process_event( pygame.event.Event(pygame.KEYDOWN, { 'key': pygame.K_n, 'mod': 0, 'unicode': 'n' })) assert processed_key_event is False and text_entry.get_text() == ''
def move(self, squares: Squares, win: pygame.Surface, board): if not isinstance(squares, list): raise TypeError("The squares arguement must be a list(), not " + str(type(squares))[8:-1] + "().") limiting_pos = [[self.min_x, max_x], [self.min_y, self.max_y]] max_length = 1 selected = False direction = 0 max_direction = 8 direction_offset = 0 pygame.font.init() while (self.x in limiting_pos[0] and self.y in limiting_pos[1]): if len(self.pieces) > max_length: font = pygame.font.SysFont("comicsans", 40) text = font.render( "You can't select that piece because you have already selected a piece. You must either move the already selected piece or unselect it." ) win.blit(txt, ((self.max_x - txt.get_width()) / 2, (self.max_y - txt.get_height()) / 2)) for event in pygame.event.get(): if event.type == pygame.K_SPACE or event.type == pygame.K_RETURN: if (self.x, self.y, self.name) in pieces: selected = False self.pieces.pop() else: selected = True self.pieces.append((self.x, self.y, self.name)) keys = pygame.key.get_pressed() if ((keys[pygame.K_RALT] and keys[pygame.K_k]) or (keys[pygame.K_LALT] and keys[pygame.K_k])) and not ( (keys[pygame.K_RALT] and keys[pygame.K_k]) and (keys[pygame.K_LALT] and keys[pygame.K_k])): active = False text_input_line = UITextEntryLine(pygame.pygame.Rect( self.x, self.y, self.square_width, self.square_height), manager=MANAGER) text_input_line.disable() text_input_line.set_allowed_characters( [d for d in string.digits[1:9]] + [l for l in string.ascii_lowercase[:8]]) text_input_line.set_text_length_limit(2) if active: text_input_line.enable() text_input_line.focus() if keys[pygame.K_RETURN]: text = text_input_line.get_text() file = text[0] rank = int(text[1]) move_set = self.get_possible_positions( text[0] + str(text[1]), squares) piece = self.find_piece_from_move_set( move_set, squares) if piece: self.x, self.y = get_window_pos( self.file, self.rank, self.possible_files) original_x, original_y = self.x, self.y else: text = font.render( "You can't move there. There is no knight nearby." ) win.blit(text, (self.x, self.y)) else: text_input_line.disable() text_input_line.unfocus() while direction < max_direction: self.attacked_pieces = self._update_attacked_pieces( self.x, self.y, self.square_width, self.square_height, squares) direction += 1 direction = 0 return self.attacked_pieces, (self.piece_x, self.piece_y), (original_x, original_y), self
class SaveLoadWindow(UIWindow): def __init__(self, path, title, confirm_callback, save): super().__init__( pygame.Rect(SCREEN_WIDTH / 2 - 440 / 2, SCREEN_HEIGHT / 2 - 500 / 2, 440, 500), ui_manager, window_display_title=title, resizable=True ) # set initial path self.path = path self.current_file_list = [] self.confirm_callback = confirm_callback self.save = save # set minimum dimensions minimum_dimensions = (300, 300) self.set_minimum_dimensions(minimum_dimensions) # file browser panel file_selection_rect = pygame.Rect( 10, 20, self.get_container().get_size()[0] - 20, self.get_container().get_size()[1] - 70 ) self.update_file_list() self.file_selection_list = UISelectionList( relative_rect=file_selection_rect, item_list=self.current_file_list, manager=ui_manager, container=self, object_id='#file_display_list', anchors= { 'left': 'left', 'right': 'right', 'top': 'top', 'bottom': 'bottom' } ) register_ui_callback( self.file_selection_list, pygame_gui.UI_SELECTION_LIST_NEW_SELECTION, lambda e: self.file_name_text_line.set_text(e.text) ) # text entry line to write file name text_line_rect = pygame.Rect(10, -40, self.get_container().get_size()[0] - 110, 25) self.file_name_text_line = UITextEntryLine( relative_rect=text_line_rect, manager=self.ui_manager, container=self, object_id='#file_path_text_line', anchors= { 'left': 'left', 'right': 'right', 'top': 'bottom', 'bottom': 'bottom' } ) if not save: self.file_name_text_line.disable() # confirm button confirm_btn_rect = pygame.Rect(-90, -40, 80, 30) self.confirm_btn = UIButton( relative_rect=confirm_btn_rect, text="OK", manager=self.ui_manager, container=self, anchors= { 'left': 'right', 'right': 'right', 'top': 'bottom', 'bottom': 'bottom' } ) register_ui_callback( self.confirm_btn, pygame_gui.UI_BUTTON_PRESSED, lambda e: ( self.on_confirm_btn_click() ) ) self.set_blocking(True) def on_confirm_btn_click(self): target_file = self.file_name_text_line.get_text() if not self.save: files = self.get_files_in_path() if target_file not in files: show_message_box("<b><font face='verdana' color='#FF3333' size=3.5>" "Invalid file name" "</font></b>") self.kill() return self.confirm_callback(target_file), self.kill() def update_file_list(self): #files = [f.name for f in Path(self.path).iterdir() if f.is_file() ] files = self.get_files_in_path() files = sorted(files, key=str.casefold) self.current_file_list = files def get_files_in_path(self): files = file_utils.get_all_files_in_path(self.path, MAPS_EXTENSION) files = [f.split('/')[1] for f in files] return files