def load_button(self, window: surface, objects: dict): # Set mode_window position mode_window.frame.by = objects[ 'ciphertext'].frame.iy + 112 * config.scale_w() # Add mode button to main window pygame_ess.load.surface(window.surface, mode_window)
def run(cipher_type: str = 'affine_cipher'): '''Display cryptography home page''' # Ensure cipher_type exist if cipher_type not in info_screen_data.keys(): logging.error( '[{}] No such cipher type for the info screen.'.format( info_window.name)) return True # Set correct info screen # Set height info_window.frame.by = 0 info_window.frame.h = int(info_screen_data[cipher_type]['height'] * config.scale_w()) # Set button position info_objects['learn_more'].frame.by = int( (info_screen_data[cipher_type]['height'] - 123) * config.scale_w()) info_objects['learn_more'].frame.iy = int( (info_screen_data[cipher_type]['height'] - 123) * config.scale_w()) info_objects['try_now'].frame.by = int( (info_screen_data[cipher_type]['height'] - 123) * config.scale_w()) info_objects['try_now'].frame.iy = int( (info_screen_data[cipher_type]['height'] - 123) * config.scale_w()) # Load screen pygame_ess.load.objects(info_window.surface, info_objects, [cipher_type]) pygame_ess.display.screen(info_window, animate=True) while True: # Check for selection selection_result: dict = pygame_ess.event.selection( info_window, info_objects) # Quit program if selection_result['action_result'] == 'quit' or pygame_ess.buffer( info_window): return 'quit' # Close info window elif selection_result['action_result'] == 'back': return True # Open learn more URL elif selection_result['action_result'] == 'learn_more': webbrowser.open(info_screen_data[cipher_type]['link'])
def run(self, window: surface, objects: dict): # Grap y-axis location of mode button (due to scrolling) mode_window.frame.by = objects[ 'ciphertext'].frame.iy + window.frame.by + 112 * config.scale_w() # Check for mode selection selection_result = pygame_ess.event.selection(mode_window, mode_objects) if selection_result['object_name'] == 'decrypt': # Set mode to encrypt self.current_mode = 'encrypt' # Set to mode self.set_mode(window, objects) elif selection_result['object_name'] == 'encrypt': # Set mode decrypt self.current_mode = 'decrypt' # Set to mode self.set_mode(window, objects)
def __init__(self, text:str = '', font_type:str = None, calculate_font_dir:bool = True, font_size:int = 36, warp_text:int = None, align:str = 'left', colour:set = (0, 0, 0), validation = None): self.text:str = text self.calculate_font_dir:bool = calculate_font_dir self.colour:tuple = colour self.validation = validation self.warp_text:int = warp_text self.align:str = align # Scale font size self.font_size:int = int(font_size * config.scale_w()) # Get font file from fonts folder if calculate_font_dir: # Get font type file font_dir:str = 'font/'+font_type # If in code directory and not root, go back a step if os.path.basename(os.getcwd()) == 'code': font_dir = '../' + font_dir # Save dir of custom font self.font_type:str = font_dir # Save font_type directly else: self.font_type:str = font_type
def __setattr__(self, name, value): if name != 'scale' and self.scale: self.__dict__[name] = int(value * config.scale_w()) else: self.__dict__[name] = value
def __init__(self, window_objects:dict, name:str = 'window', frame:coord = None, fill_screen:bool = True, background_fill:tuple = pygame_ess.colour.gray, load:bool = True, is_alpha:bool = False, scroll:bool = True): # Calculate smart height of window size if no frame defined if frame == None: logging.warn('No frame defined for surface {}, doing smart frame calculation.'.format(name)) frame = coord(bx=0, by=0, w=1024, h=0) for window_object in window_objects.values(): frame.h = max(frame.h, window_object.frame.iy+window_object.frame.h) # Set surface to height of screen if fill_screen and frame.h < config.screen.height: frame.h = config.screen.height / config.scale_w() # Create the surface window = pygame_ess.create.surface(frame.box_size(), background_fill, is_alpha) # Load surface if load: pygame_ess.load.screen(window, window_objects) # Save to class self.name:str = name self.surface = window.convert_alpha() if is_alpha else window.convert() self.frame:coord = frame self.background_fill:tuple = background_fill self.is_alpha:bool = is_alpha self.scroll:bool = scroll # Debug surface logging.debug(self.__str__())
def images(image_page:list, file_type:str = '.png', is_alpha:bool = False) -> dict: '''Load all images in a given directory to pygame surface''' # Define variables images = dict() image_dir = 'images/{}/'.format('/'.join(image_page)) # If in code directory and not root, go back a step if os.path.basename(os.getcwd()) == 'code': image_dir = '../' + image_dir # Get all image file from givent directory image_dir_list = glob.glob(image_dir+"*"+file_type) # Warn on empty dir if len(image_dir_list) == 0: logging.error('No image found in {}'.format(image_dir)) # Load them into pygame for image in image_dir_list: image_name = image.split('/')[-1].split('\\')[-1].split('.')[0] images[image_name] = pygame.image.load(image).convert_alpha() if is_alpha else pygame.image.load(image).convert() images[image_name] = pygame.transform.smoothscale(images[image_name], (int(images[image_name].get_width()*config.scale_w()), int(images[image_name].get_height()*config.scale_w()))) return images