From 5cb8101495b4dcdaa69311da755bdabf9526bec7 Mon Sep 17 00:00:00 2001 From: Quinn Date: Wed, 28 Aug 2024 20:10:17 -0400 Subject: [PATCH] added a UI class to better organize UI creation --- tools/animation-tools/UI.py | 44 ++++++++++++++++++------- tools/animation-tools/animation-tool.py | 32 +++++------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/tools/animation-tools/UI.py b/tools/animation-tools/UI.py index 29f5935..5dca482 100644 --- a/tools/animation-tools/UI.py +++ b/tools/animation-tools/UI.py @@ -2,7 +2,6 @@ from AnimationExporter import scene_to_frame from CustomWidgets import ColorPicker from itertools import product from pygame_widgets.button import Button -import pygame_widgets from Scene import Scene from Shapes import * import pygame @@ -15,13 +14,7 @@ class SceneManager: self.window = window self.color_picker = color_picker self._selected_meshes: list[Mesh] = [] - self.make_buttons() - def make_buttons(self): - scr_wdt, scr_hgt = self.window.get_size() - self.save_button = Button(self.window, 20, scr_hgt-40, 60, 30, text="Save",onClick=self.save_frame_to_file) - self.next_frame_button = Button(self.window, scr_wdt-120, scr_hgt-40, 120, 30, text="Next Frame",onClick=self.next_scene) - self.last_frame_button = Button(self.window, scr_wdt-240, scr_hgt-40, 120, 30, text="last Frame",onClick=self.previous_scene) def save_frame_to_file(self): with open("tools/animation-tools/output.txt", 'w') as file: for i, scene in enumerate(self._scenes): @@ -83,9 +76,38 @@ class SceneManager: self._current_scene_idx -= 1 self.get_current_scene().euler_angles = [angle for angle in current_angles] self.deselect_all_mesh() + +class AnimatorUI: + def __init__(self, window): + scr_wdt, scr_hgt = window.get_size() -def create_ui(window, scr_wdt, scr_hgt) -> SceneManager: - colorPicker = ColorPicker(window, 20, 20, 100, 300) - sceneManager = SceneManager(window, colorPicker) + self.window = window + colorPicker: ColorPicker = ColorPicker(self.window, 20, 20, 100, 300) + self.sceneManager: SceneManager = SceneManager(window, colorPicker) + + + self.save_button: Button = Button(self.window, 20, scr_hgt-40, 60, 30, text="Save",onClick=self.sceneManager.save_frame_to_file) + self.next_frame_button: Button = Button(self.window, scr_wdt-120, scr_hgt-40, 120, 30, text="Next Frame",onClick=self.sceneManager.next_scene) + self.last_frame_button: Button = Button(self.window, scr_wdt-240, scr_hgt-40, 120, 30, text="last Frame",onClick=self.sceneManager.previous_scene) + self.trackMouseMotion: bool = False - return sceneManager \ No newline at end of file + def update_interaction(self, game_event): + if not self.trackMouseMotion: + pygame.mouse.get_rel() + + if game_event.type == pygame.MOUSEBUTTONDOWN: + if game_event.button == 2: # middle mouse click + self.trackMouseMotion = True + elif game_event.button == 1: # left click + self.sceneManager.click_mesh(pygame.mouse.get_pos()) + elif game_event.type == pygame.MOUSEBUTTONUP: + if game_event.button == 2: # middle mouse release + self.trackMouseMotion = False + elif self.trackMouseMotion and game_event.type == pygame.MOUSEMOTION: + mouseMovement = pygame.mouse.get_rel() + current_scene = self.sceneManager.get_current_scene() + current_scene.euler_angles[0] -= mouseMovement[1] + current_scene.euler_angles[1] -= mouseMovement[0] + + def draw(self): + self.sceneManager.draw() \ No newline at end of file diff --git a/tools/animation-tools/animation-tool.py b/tools/animation-tools/animation-tool.py index cbcb2ab..8d683c3 100644 --- a/tools/animation-tools/animation-tool.py +++ b/tools/animation-tools/animation-tool.py @@ -2,7 +2,7 @@ import pygame import pygame_widgets from Shapes import * from Scene import Scene -from UI import create_ui, SceneManager +from UI import AnimatorUI WINDOW_W = 500 WINDOW_H = 500 @@ -13,40 +13,26 @@ file_data = "" pygame.init() window = pygame.display.set_mode((WINDOW_W, WINDOW_H)) -sceneManager: SceneManager = create_ui(window, WINDOW_W, WINDOW_H) +ui = AnimatorUI(window) clock = pygame.time.Clock() -selected_meshes = [] run = True -trackMouseMotion = False while run: clock.tick(60) window.fill((255, 255, 255)) - current_scene: Scene = sceneManager.get_current_scene() + events = pygame.event.get() for event in events: if event.type == pygame.QUIT: run = False - elif event.type == pygame.MOUSEBUTTONDOWN: - if event.button == 2: # middle mouse click - trackMouseMotion = True - elif event.button == 1: # left click - sceneManager.click_mesh(pygame.mouse.get_pos()) - elif event.type == pygame.MOUSEBUTTONUP: - if event.button == 2: # middle mouse release - trackMouseMotion = False - elif trackMouseMotion and event.type == pygame.MOUSEMOTION: - mouseMovement = pygame.mouse.get_rel() - current_scene.euler_angles[0] -= mouseMovement[1] - current_scene.euler_angles[1] -= mouseMovement[0] - pygame_widgets.update(events) - - - if not trackMouseMotion: - pygame.mouse.get_rel() + # if the event isn't handled above as a global event, let the ui handle it + else: + ui.update_interaction(event) - sceneManager.draw() + pygame_widgets.update(events) + + ui.draw() pygame.display.flip() pygame.quit() \ No newline at end of file