add controller support for ui

This commit is contained in:
csd4ni3l
2025-06-24 19:38:03 +02:00
parent dbdee7d054
commit 1672f35211
4 changed files with 26 additions and 11 deletions

View File

@@ -3,6 +3,8 @@ import arcade, arcade.gui
from utils.constants import button_style, iter_fractals from utils.constants import button_style, iter_fractals
from utils.preload import button_texture, button_hovered_texture from utils.preload import button_texture, button_hovered_texture
from arcade.gui.experimental.focus import UIFocusGroup
class FractalChooser(arcade.gui.UIView): class FractalChooser(arcade.gui.UIView):
def __init__(self, pypresence_client): def __init__(self, pypresence_client):
super().__init__() super().__init__()
@@ -10,14 +12,15 @@ class FractalChooser(arcade.gui.UIView):
self.pypresence_client = pypresence_client self.pypresence_client = pypresence_client
self.iter_fractal_buttons = [] self.iter_fractal_buttons = []
def on_show_view(self): self.root = self.add_widget(UIFocusGroup())
super().on_show_view() self.anchor = self.root.add(arcade.gui.UIAnchorLayout(size_hint=(1, 1)))
self.anchor = self.add_widget(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.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") 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.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) 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 = 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.sierpinsky_carpet_button.on_click = lambda event: self.sierpinsky_carpet()
self.root.detect_focusable_widgets()
def main_exit(self): def main_exit(self):
from menus.main import Main from menus.main import Main
self.window.show_view(Main(self.pypresence_client)) self.window.show_view(Main(self.pypresence_client))

View File

@@ -2,12 +2,13 @@ import arcade, arcade.gui, asyncio, pypresence, time, copy, json
from utils.preload import button_texture, button_hovered_texture from utils.preload import button_texture, button_hovered_texture
from utils.constants import big_button_style, discord_presence_id from utils.constants import big_button_style, discord_presence_id
from utils.utils import FakePyPresence from utils.utils import FakePyPresence
from arcade.gui.experimental.focus import UIFocusGroup
class Main(arcade.gui.UIView): class Main(arcade.gui.UIView):
def __init__(self, pypresence_client=None): def __init__(self, pypresence_client=None):
super().__init__() 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.box = self.anchor.add(arcade.gui.UIBoxLayout(space_between=10), anchor_x='center', anchor_y='center')
self.pypresence_client = pypresence_client 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 = 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.settings_button.on_click = lambda event: self.settings()
self.root.detect_focusable_widgets()
def play(self): def play(self):
from menus.fractal_chooser import FractalChooser from menus.fractal_chooser import FractalChooser
self.window.show_view(FractalChooser(self.pypresence_client)) self.window.show_view(FractalChooser(self.pypresence_client))

View File

@@ -1,4 +1,4 @@
import copy, pypresence, json, os import copy, pypresence, json
import arcade, arcade.gui import arcade, arcade.gui
@@ -30,7 +30,8 @@ class Settings(arcade.gui.UIView):
self.modified_settings = {} self.modified_settings = {}
def create_layouts(self): 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.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) 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.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 = 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() 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.top_box.add(category_button)
self.root.detect_focusable_widgets()
def display_category(self, category): def display_category(self, category):
if hasattr(self, 'apply_button'): if hasattr(self, 'apply_button'):
self.anchor.remove(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.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.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): def apply_settings(self):
for config_key, value in self.modified_settings.items(): for config_key, value in self.modified_settings.items():
self.settings_dict[config_key] = value self.settings_dict[config_key] = value
@@ -208,7 +213,6 @@ class Settings(arcade.gui.UIView):
file.write(json.dumps(self.settings_dict, indent=4)) file.write(json.dumps(self.settings_dict, indent=4))
def update(self, setting=None, button_state=None, setting_type="bool"): 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"] config_key = settings[self.current_category][setting]["config_key"]
if setting_type == "option": if setting_type == "option":
@@ -268,6 +272,8 @@ class Settings(arcade.gui.UIView):
self.key_layout.add(self.credits_label) self.key_layout.add(self.credits_label)
self.root.detect_focusable_widgets()
def set_highlighted_style(self, element): def set_highlighted_style(self, element):
element.texture = button_hovered_texture element.texture = button_hovered_texture
element.texture_hovered = button_texture element.texture_hovered = button_texture

3
run.py
View File

@@ -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.utils import get_closest_resolution, print_debug_info, on_exception
from utils.constants import log_dir, menu_background_color from utils.constants import log_dir, menu_background_color
from menus.main import Main from menus.main import Main
from arcade.experimental.controller_window import ControllerWindow
sys.excepthook = on_exception sys.excepthook = on_exception
@@ -66,7 +67,7 @@ else:
with open("settings.json", "w") as file: with open("settings.json", "w") as file:
file.write(json.dumps(settings)) 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: if vsync:
window.set_vsync(True) window.set_vsync(True)