From 1672f352114df0148c0d6b81073a77a7df0561d7 Mon Sep 17 00:00:00 2001 From: csd4ni3l Date: Tue, 24 Jun 2025 19:38:03 +0200 Subject: [PATCH] add controller support for ui --- menus/fractal_chooser.py | 13 +++++++++---- menus/main.py | 7 +++++-- menus/settings.py | 14 ++++++++++---- run.py | 3 ++- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/menus/fractal_chooser.py b/menus/fractal_chooser.py index fd2164c..b56c519 100644 --- a/menus/fractal_chooser.py +++ b/menus/fractal_chooser.py @@ -3,6 +3,8 @@ import arcade, arcade.gui from utils.constants import button_style, iter_fractals from utils.preload import button_texture, button_hovered_texture +from arcade.gui.experimental.focus import UIFocusGroup + class FractalChooser(arcade.gui.UIView): def __init__(self, pypresence_client): super().__init__() @@ -10,14 +12,15 @@ class FractalChooser(arcade.gui.UIView): self.pypresence_client = pypresence_client self.iter_fractal_buttons = [] - def on_show_view(self): - super().on_show_view() - - self.anchor = self.add_widget(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) + self.root = self.add_widget(UIFocusGroup()) + self.anchor = self.root.add(arcade.gui.UIAnchorLayout(size_hint=(1, 1))) self.grid = self.add_widget(arcade.gui.UIGridLayout(row_count=4, column_count=3, horizontal_spacing=10, vertical_spacing=10)) self.anchor.add(self.grid, anchor_x="center", anchor_y="center") + def on_show_view(self): + super().on_show_view() + self.title_label = self.anchor.add(arcade.gui.UILabel(text="Choose a fractal to view.", font_name="Roboto", font_size=32), anchor_x="center", anchor_y="top", align_y=-50) self.back_button = arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50) @@ -37,6 +40,8 @@ class FractalChooser(arcade.gui.UIView): self.sierpinsky_carpet_button = self.grid.add(arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='Sierpinsky Carpet', style=button_style, width=200, height=200), row=row, column=col) self.sierpinsky_carpet_button.on_click = lambda event: self.sierpinsky_carpet() + self.root.detect_focusable_widgets() + def main_exit(self): from menus.main import Main self.window.show_view(Main(self.pypresence_client)) diff --git a/menus/main.py b/menus/main.py index c514be3..9794949 100644 --- a/menus/main.py +++ b/menus/main.py @@ -2,12 +2,13 @@ import arcade, arcade.gui, asyncio, pypresence, time, copy, json from utils.preload import button_texture, button_hovered_texture from utils.constants import big_button_style, discord_presence_id from utils.utils import FakePyPresence - +from arcade.gui.experimental.focus import UIFocusGroup class Main(arcade.gui.UIView): def __init__(self, pypresence_client=None): super().__init__() - self.anchor = self.add_widget(arcade.gui.UIAnchorLayout()) + self.root = self.add_widget(UIFocusGroup()) + self.anchor = self.root.add(arcade.gui.UIAnchorLayout()) self.box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=10), anchor_x='center', anchor_y='center') self.pypresence_client = pypresence_client @@ -58,6 +59,8 @@ class Main(arcade.gui.UIView): self.settings_button = self.box.add(arcade.gui.UITextureButton(text="Settings", texture=button_texture, texture_hovered=button_hovered_texture, width=self.window.width / 2, height=150, style=big_button_style)) self.settings_button.on_click = lambda event: self.settings() + self.root.detect_focusable_widgets() + def play(self): from menus.fractal_chooser import FractalChooser self.window.show_view(FractalChooser(self.pypresence_client)) diff --git a/menus/settings.py b/menus/settings.py index f44bc0c..105a110 100644 --- a/menus/settings.py +++ b/menus/settings.py @@ -1,4 +1,4 @@ -import copy, pypresence, json, os +import copy, pypresence, json import arcade, arcade.gui @@ -30,7 +30,8 @@ class Settings(arcade.gui.UIView): self.modified_settings = {} def create_layouts(self): - self.anchor = self.add_widget(UIAnchorLayout(size_hint=(1, 1))) + self.root = self.add_widget(UIFocusGroup()) + self.anchor = self.root.add(UIAnchorLayout(size_hint=(1, 1))) self.box = UIBoxLayout(space_between=50, align="center", vertical=False) self.anchor.add(self.box, anchor_x="center", anchor_y="top", align_x=10, align_y=-75) @@ -46,7 +47,7 @@ class Settings(arcade.gui.UIView): self.create_layouts() - self.ui.push_handlers(self) + self.ui.push_handlers(self) # required for controller stuff self.back_button = arcade.gui.UITextureButton(texture=button_texture, texture_hovered=button_hovered_texture, text='<--', style=button_style, width=100, height=50) self.back_button.on_click = lambda e: self.main_exit() @@ -67,6 +68,8 @@ class Settings(arcade.gui.UIView): self.top_box.add(category_button) + self.root.detect_focusable_widgets() + def display_category(self, category): if hasattr(self, 'apply_button'): self.anchor.remove(self.apply_button) @@ -136,6 +139,8 @@ class Settings(arcade.gui.UIView): self.apply_button.on_click = lambda event: self.apply_settings() self.anchor.add(self.apply_button, anchor_x="right", anchor_y="bottom", align_x=-10, align_y=10) + self.root.detect_focusable_widgets() + def apply_settings(self): for config_key, value in self.modified_settings.items(): self.settings_dict[config_key] = value @@ -208,7 +213,6 @@ class Settings(arcade.gui.UIView): file.write(json.dumps(self.settings_dict, indent=4)) def update(self, setting=None, button_state=None, setting_type="bool"): - setting_dict = settings[self.current_category][setting] config_key = settings[self.current_category][setting]["config_key"] if setting_type == "option": @@ -268,6 +272,8 @@ class Settings(arcade.gui.UIView): self.key_layout.add(self.credits_label) + self.root.detect_focusable_widgets() + def set_highlighted_style(self, element): element.texture = button_hovered_texture element.texture_hovered = button_texture diff --git a/run.py b/run.py index 009164a..52e9eeb 100644 --- a/run.py +++ b/run.py @@ -7,6 +7,7 @@ import logging, datetime, os, json, sys, arcade from utils.utils import get_closest_resolution, print_debug_info, on_exception from utils.constants import log_dir, menu_background_color from menus.main import Main +from arcade.experimental.controller_window import ControllerWindow sys.excepthook = on_exception @@ -66,7 +67,7 @@ else: with open("settings.json", "w") as file: file.write(json.dumps(settings)) -window = arcade.Window(width=resolution[0], height=resolution[1], title='Fractal Viewer', samples=antialiasing, antialiasing=antialiasing > 0, fullscreen=fullscreen, vsync=vsync, resizable=False, style=style) +window = ControllerWindow(width=resolution[0], height=resolution[1], title='Fractal Viewer', samples=antialiasing, antialiasing=antialiasing > 0, fullscreen=fullscreen, vsync=vsync, resizable=False, style=style) if vsync: window.set_vsync(True)