def __init__(self, size: Tuple[int, int], font): super().__init__(size) self._text_component = TextArea(font, WHITE, size, padding=16, style=Style(border_color=LIGHT_GRAY)) self._image_component = Surface(None, style=Style(border_color=LIGHT_GRAY)) self._seekbar = Seekbar((size[0] - 8, 16)) self._seekbar.set_visible(False)
def button(font, size: Tuple[int, int], callback: Callable[[], Any], label: str, hotkey: Optional[int] = None, hold: Optional[HoldDownBehavior] = None): return Button(size=size, callback=callback, label=StaticText(font, WHITE, label), behavior=hold if hold else SingleClickBehavior(), hotkey=hotkey, style=Style(background_color=BUTTON_BACKGROUND_COLOR, border_color=Color(150, 150, 150)), style_hovered=Style(background_color=BUTTON_BACKGROUND_COLOR, border_color=Color(210, 210, 210), border_width=2), style_onclick=Style(background_color=BUTTON_BACKGROUND_COLOR, border_color=MATRIX_GREEN, border_width=3))
def __init__(self): pygame.init() screen = pygame.display.set_mode(SCREEN_RESOLUTION) pygame.display.set_caption("FILE BROWSER") clock = Clock() font = Font('resources/consola.ttf', 14) font_small = Font('resources/consola.ttf', 14) background_color = (0, 0, 0) grid_dimensions = (3, 10) self.buttons = [blank_button(font) for _ in range(grid_dimensions[0] * grid_dimensions[1])] grid = GridContainer(children=self.buttons, dimensions=grid_dimensions, padding=5, margin=1, style=Style(background_color=KEYBOARD_BACKGROUND_COLOR, border_color=LIGHT_GRAY)) width = SCREEN_RESOLUTION[0] - PADDING * 2 grid_container = EvenSpacingContainer(width, "fit_contents", [grid], padding=0) dir_path = os.path.dirname(os.path.realpath(__file__)) self.text_current_dir = StaticText(font, WHITE, dir_path, style=Style(background_color=Color(50, 50, 50))) self.file_names = os.listdir(".") self.preview = FilePreview((width, 230), font_small) container = AbsolutePosContainer(SCREEN_RESOLUTION, [(Vector2(PADDING, PADDING), self.text_current_dir), (Vector2(PADDING, 80), self.preview), (Vector2(PADDING, 330), grid_container)]) container.set_pos(Vector2(0, 0)) self.setup_keys() while True: for event in pygame.event.get(): handle_exit(event) if event.type == pygame.MOUSEBUTTONDOWN: container.handle_mouse_was_clicked(pygame.mouse.get_pos()) elif event.type == pygame.MOUSEBUTTONUP: container.handle_mouse_was_released() elif event.type == pygame.MOUSEMOTION: container.handle_mouse_motion(pygame.mouse.get_pos()) elif event.type == pygame.KEYDOWN: container.handle_key_was_pressed(event.key) elif event.type == pygame.KEYUP: container.handle_key_was_released(event.key) elapsed_time = clock.tick() container.update(elapsed_time) screen.fill(background_color) container.render(screen) pygame.display.flip()
def checkbox(font, size: Tuple[int, int], callback: Callable[[bool], Any], label: str, checked: bool = False): return Checkbox(size=size, callback=callback, label=StaticText(font, COLOR_WHITE, label), checked=checked, style=Style(background_color=Color(50, 50, 100), border_color=Color(150, 150, 150)), style_hovered=Style(background_color=Color(80, 80, 120), border_color=Color(180, 180, 180)), style_onclick=Style(background_color=Color(80, 80, 120), border_color=Color(200, 255, 200), border_width=2))
def image_surface(file_path: str, size: Tuple[int, int]) -> Surface: image = load_and_scale_image(file_path, size) return Surface(image, style=Style(border_color=Color(255, 255, 255)))
def main(): pygame.init() screen = pygame.display.set_mode(SCREEN_RESOLUTION) clock = Clock() set_timer(USEREVENT_EACH_SECOND, 1000) font = Font('resources/Arial Rounded Bold.ttf', 14) background_color = (0, 0, 0) grid = BackgroundGrid(SCREEN_RESOLUTION, Color(20, 20, 20), 32) fps_text = FormattedText(font, COLOR_WHITE, "FPS: %i", 0) debug_texts = [ fps_text, StaticText(font, COLOR_WHITE, "debug: 2"), StaticText(font, COLOR_WHITE, "debug: 3"), ] debug_window = ListContainer(width=200, height="fit_contents", children=debug_texts, margin=5, padding=5, orientation=Orientation.VERTICAL, style=Style(border_color=COLOR_WHITE)) left_buttons = [ button(font, (200, 48), callback=lambda: print("hello"), label="click"), button(font, (200, 48), callback=lambda: print("hello"), label="click"), button(font, (200, 48), callback=lambda: print("hello"), label="click"), ] counter = Counter((50, 50), FormattedText(font, COLOR_WHITE, "%i", 0), style=Style(background_color=Color(100, 100, 100))) right_buttons = [ button(font, (200, 32), callback=lambda: counter.increment(), label="Increment (I)", hotkey=pygame.K_i), button(font, (200, 32), callback=lambda: counter.decrement(), label="Decrement (D)", hotkey=pygame.K_d), checkbox(font, (200, 32), callback=lambda checked: debug_window.set_visible(checked), label="Show debug", checked=debug_window.is_visible()), checkbox(font, (200, 32), callback=lambda checked: print("B: %s" % checked), label="B"), checkbox(font, (200, 32), callback=lambda checked: print("C: %s" % checked), label="C"), checkbox(font, (200, 32), callback=lambda checked: print("D: %s" % checked), label="D"), checkbox(font, (200, 32), callback=lambda checked: print("E: %s" % checked), label="E"), checkbox(font, (200, 32), callback=lambda checked: print("F: %s" % checked), label="F"), ] left_menu_bar = ListContainer(width="fit_contents", height="fill_parent", children=left_buttons, margin=5, padding=5, orientation=Orientation.VERTICAL, style=Style(background_color=Color( 150, 150, 255), border_color=COLOR_WHITE)) right_menu_bar = ScrollContainer(height=166, children=right_buttons, margin=5, padding=5, orientation=Orientation.VERTICAL, style=Style(background_color=Color( 150, 210, 255), border_color=COLOR_WHITE)) text_field = TextArea(font, COLOR_WHITE, (100, 100), padding=5, style=Style(border_color=COLOR_WHITE)) icon_background = load_and_scale_image('resources/stone_tile.png', (32, 32)) grid_children = [ number_button(font, text_field, "1", pygame.K_1), number_button(font, text_field, "2", pygame.K_2), number_button(font, text_field, "3", pygame.K_3), number_button(font, text_field, "4", pygame.K_4), number_button(font, text_field, "5", pygame.K_5), number_button(font, text_field, "6", pygame.K_6), icon(font, (32, 32), icon_background, "A", pygame.K_a), icon(font, (32, 32), icon_background, "B", pygame.K_b), icon(font, (32, 32), icon_background, "C", pygame.K_c), number_button(font, text_field, "0", pygame.K_0), backspace_button(font, text_field) ] grid_container = GridContainer(children=grid_children, dimensions=(3, 4), padding=5, margin=2, style=Style(background_color=Color( 150, 130, 100), border_color=COLOR_WHITE)) img = image_surface('resources/stone_tile.png', (100, 100)) hud = ListContainer( width=800, height=200, children=[right_menu_bar, counter, grid_container, text_field, img], margin=5, padding=5, orientation=Orientation.HORIZONTAL, style=Style(border_color=COLOR_WHITE, background_color=Color(0, 0, 150))) container = AbsolutePosContainer(SCREEN_RESOLUTION, [(Vector2(5, 5), debug_window), (Vector2(0, 400), hud)]) container.set_pos(Vector2(0, 0)) while True: for event in pygame.event.get(): handle_exit(event) if event.type == pygame.MOUSEBUTTONDOWN: container.handle_mouse_was_clicked(pygame.mouse.get_pos()) elif event.type == pygame.MOUSEBUTTONUP: container.handle_mouse_was_released() elif event.type == pygame.MOUSEMOTION: container.handle_mouse_motion(pygame.mouse.get_pos()) elif event.type == USEREVENT_EACH_SECOND: fps_text.format_text(int(clock.get_fps())) elif event.type == pygame.KEYDOWN: container.handle_key_was_pressed(event.key) elif event.type == pygame.KEYUP: container.handle_key_was_released(event.key) elapsed_time = clock.tick() container.update(elapsed_time) screen.fill(background_color) grid.render(screen) container.render(screen) pygame.display.flip()
def main(): pygame.init() screen = pygame.display.set_mode(SCREEN_RESOLUTION) pygame.display.set_caption("Keyboard & Terminal") clock = Clock() font = Font('resources/Arial Rounded Bold.ttf', 18) font_large = Font('resources/consola.ttf', 32) background_color = (0, 0, 0) terminal = TextArea(font_large, MATRIX_GREEN, (SCREEN_RESOLUTION[0] - PADDING * 2, 300), padding=16, blinking_cursor=BlinkingCursor(800), style=Style(border_color=WHITE)) key_components = [ keyboard_button(font, terminal, pygame.K_q), keyboard_button(font, terminal, pygame.K_w), keyboard_button(font, terminal, pygame.K_e), keyboard_button(font, terminal, pygame.K_r), keyboard_button(font, terminal, pygame.K_t), keyboard_button(font, terminal, pygame.K_y), keyboard_button(font, terminal, pygame.K_u), keyboard_button(font, terminal, pygame.K_i), keyboard_button(font, terminal, pygame.K_o), keyboard_button(font, terminal, pygame.K_p), keyboard_button(font, terminal, pygame.K_a), keyboard_button(font, terminal, pygame.K_s), keyboard_button(font, terminal, pygame.K_d), keyboard_button(font, terminal, pygame.K_f), keyboard_button(font, terminal, pygame.K_g), keyboard_button(font, terminal, pygame.K_h), keyboard_button(font, terminal, pygame.K_j), keyboard_button(font, terminal, pygame.K_k), keyboard_button(font, terminal, pygame.K_l), return_button(font, terminal), blank_button(font), keyboard_button(font, terminal, pygame.K_z), keyboard_button(font, terminal, pygame.K_x), keyboard_button(font, terminal, pygame.K_c), keyboard_button(font, terminal, pygame.K_v), keyboard_button(font, terminal, pygame.K_b), keyboard_button(font, terminal, pygame.K_n), keyboard_button(font, terminal, pygame.K_m), space_button(font, terminal), backspace_button(font, terminal) ] keyboard = GridContainer(children=key_components, dimensions=(10, 3), padding=5, margin=5, style=Style(background_color=KEYBOARD_BACKGROUND_COLOR, border_color=WHITE)) keyboard_container = EvenSpacingContainer(SCREEN_RESOLUTION[0] - PADDING * 2, 300, [keyboard], padding=0) container = AbsolutePosContainer(SCREEN_RESOLUTION, [(Vector2(PADDING, PADDING), terminal), (Vector2(PADDING, 360), keyboard_container)]) container.set_pos(Vector2(0, 0)) while True: for event in pygame.event.get(): handle_exit(event) if event.type == pygame.MOUSEBUTTONDOWN: container.handle_mouse_was_clicked(pygame.mouse.get_pos()) elif event.type == pygame.MOUSEBUTTONUP: container.handle_mouse_was_released() elif event.type == pygame.MOUSEMOTION: container.handle_mouse_motion(pygame.mouse.get_pos()) elif event.type == pygame.KEYDOWN: container.handle_key_was_pressed(event.key) elif event.type == pygame.KEYUP: container.handle_key_was_released(event.key) elapsed_time = clock.tick() container.update(elapsed_time) screen.fill(background_color) container.render(screen) pygame.display.flip()