From 1e8f1e3fa702f1300a3c5cf350289ad521d1d722 Mon Sep 17 00:00:00 2001 From: elukjanovica Date: Tue, 4 Jun 2024 08:56:33 +0300 Subject: [PATCH] --- main.py | 104 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 79 insertions(+), 25 deletions(-) diff --git a/main.py b/main.py index 3d6e895..d123ce7 100644 --- a/main.py +++ b/main.py @@ -423,7 +423,7 @@ def render_albums(): y = size_to_y[current_size] albums_to_delete = [] - checked = False + checked = {} for hover_index, album in enumerate(list(storage["albums"].keys())): render_text(album, (size[1], y), screen) @@ -435,24 +435,11 @@ def render_albums(): album = handle_album_interaction(album_hover, album) if album: return album - + if delete_mode: - checkbox_rect = pygame.Rect(cons[2], y + cons[3], checkbox[1], checkbox[1]) - checkbox_hover = checkbox_rect.collidepoint(pygame.mouse.get_pos()) - - if checkbox_hover and pygame.mouse.get_pressed()[0]: - checked = not checked - - if checked: - if album not in albums_to_delete: - albums_to_delete.append(album) - checkbox_image = Image((checkbox[0] + checkbox[1] + 3, 0), (cons[2], y + cons[3]), (checkbox[1], checkbox[1])) - else: - if album in albums_to_delete: - albums_to_delete.remove(album) - - checkbox_image = Image((checkbox[0], 0), (cons[2], y + cons[3]), (checkbox[1], checkbox[1])) - checkbox_image.draw(screen) + if album not in checked: + checked[album] = False + delete_albums_mode(album, albums_to_delete, checked, y, album_rect, current_hovered_index) y += cons[0] @@ -461,8 +448,11 @@ def render_albums(): delete_albums(albums_to_delete) delete_mode = False - if delete_mode is False: + if not delete_mode: draw_arrow(current_hovered_index) + + if typing: + album_creation(y) def update_hover_index(current_hovered_index, album_hover, hover_index): @@ -483,13 +473,50 @@ def handle_album_interaction(album_hover, album): return None +def delete_albums_mode(album, albums_to_delete, checked, y, album_rect, current_hovered_index): + checkbox_rect = pygame.Rect(cons[2], y + cons[3], checkbox[1], checkbox[1]) + checkbox_hover = checkbox_rect.collidepoint(pygame.mouse.get_pos()) + + if checkbox_hover and pygame.mouse.get_pressed()[0]: + pygame.time.wait(100) + + cursor_pos = pygame.mouse.get_pos() + adjusted_pos = (cursor_pos[0] + 30, cursor_pos[1]) + print("Adjusted Pos:", adjusted_pos) + print("Album Rect:", album_rect) + + if current_hovered_index is not None and current_hovered_index >= 0: + hovered_album = list(storage["albums"].keys())[current_hovered_index] + if hovered_album == album: + print("Checkbox Clicked for Album:", album) + + checked[album] = not checked[album] + + if checked[album]: + if album not in albums_to_delete: + albums_to_delete.append(album) + print("Album added to delete:", album) + else: + if album in albums_to_delete: + albums_to_delete.remove(album) + print("Album removed from delete:", album) + + checkbox_image = Image((checkbox[0], 0), (cons[2], y + cons[3]), (checkbox[1], checkbox[1])) + if checked[album]: + checkbox_image = Image((checkbox[0] + checkbox[1] + 3, 0), (cons[2], y + cons[3]), (checkbox[1], checkbox[1])) + checkbox_image.draw(screen) + + + def delete_albums(albums_to_delete): for album in albums_to_delete: - del storage["albums"][album] + if album in storage["albums"]: + del storage["albums"][album] with open(DATA_FILE, 'w') as data_file: data_file.write(json.dumps(storage, indent=3)) + def draw_arrow(current_hovered_index): if current_hovered_index == -1: current_hovered_index = 0 @@ -497,9 +524,24 @@ def draw_arrow(current_hovered_index): arrow_image.draw(screen) -def album_creation(): - # Not finished yet - pass +def album_creation(y): + global user_text, typing + + render_text(user_text, (size[1], y), screen) + + if pygame.time.get_ticks() % 1000 < 500: + caret_x = size[1] + pygame.font.SysFont(FONT, FONT_SIZE).size(user_text)[0] - 2 + render_text("|", (caret_x, y), screen) + + mouse_clicked = pygame.mouse.get_pressed()[0] + keys = pygame.key.get_pressed() + if (user_text and mouse_clicked) or (user_text and keys[pygame.K_RETURN]): + if user_text not in storage["albums"]: + storage["albums"][user_text] = {} + with open(DATA_FILE, 'w') as data_file: + data_file.write(json.dumps(storage, indent=3)) + typing = False + user_text = '' # SELECT MODE class ImageRender: @@ -1049,7 +1091,7 @@ do_not_save = False def button_check(pressed_button, run, current_mode, timer_running, delete_mode): - global completed, not_shuffled, current_timer_text, current_moves, real_time, accepted + global completed, not_shuffled, current_timer_text, current_moves, real_time, accepted, typing do_not_save = False if pressed_button == close_button: run = False @@ -1074,7 +1116,8 @@ def button_check(pressed_button, run, current_mode, timer_running, delete_mode): delete_mode = False accepted = False elif pressed_button == new_button: - album_creation() + if current_mode == "menu": + typing = True elif pressed_button == quick_add_button: quick_game() current_mode = "game" @@ -1133,6 +1176,8 @@ pressed_button = None slider_moving = False setting = None arrow = None +user_text = '' +typing = False # Select mode clicked_image = False image_display = None @@ -1261,6 +1306,15 @@ while run: elif event.button == 5: scroll_offset += 20 scroll_offset = min(scroll_offset, max_scroll_offset) + + elif typing and event.type == pygame.KEYDOWN: + + if event.key == pygame.K_BACKSPACE: + user_text = user_text[0:-1] + elif len(user_text) < 15: + user_text += event.unicode + elif event.key == pygame.K_RETURN: + user_text = user_text.strip() if current_mode == "game": if timer_running: