Add controller UI support from-scratch

This commit is contained in:
csd4ni3l
2025-07-07 15:38:39 +02:00
parent b03bef66c5
commit 5d3f5c157b
4 changed files with 160 additions and 56 deletions

View File

@@ -1,37 +1,36 @@
import json
from utils.constants import game_modes
from utils.utils import FocusView
from ursina import *
class GameModeSelector:
class GameModeSelector(FocusView):
def __init__(self, rpc):
super().__init__(model='cube', color=color.dark_gray, scale=(1.8, 1.2), z=1)
self.rpc = rpc
rpc.update(state='In Game Mode Selector', details='Selecting Game Mode Selector', start=rpc.start_time)
self.data = json.load(open('settings.json'))
self.main = Entity(parent=camera.ui, model='cube', color=color.dark_gray, scale=(1.8, 1.2), z=1)
self.back_button = Button('Back', parent=camera.ui, color=color.gray, scale=(.1, .05), position=(-.8, .45), on_click=self.exit)
self.title_label = Text(text="Select a mode to play.", position=(-0.4, 0.425), scale=3)
self.ui = [self.main, self.back_button, self.title_label]
y = 0.2
for game_mode in game_modes:
button = Button(text=game_mode, scale_x=1, scale_y=0.15, text_size=2, position=(0, y), on_click=lambda game_mode=game_mode: self.play(game_mode))
self.ui.append(button)
y -= 0.16
self.ui.extend([self.back_button, self.title_label, self.main])
self.detect_focusable_widgets()
def play(self, game_mode):
self.hide()
from game.game import Game
Game(self.rpc, game_mode.lower())
def hide(self):
for e in self.ui:
destroy(e)
self.ui.clear()
def exit(self):
self.hide()
from menus.main import Main

View File

@@ -1,17 +1,14 @@
from ursina import *
import pypresence, asyncio, json
from utils.utils import FakePyPresence
from utils.utils import FakePyPresence, MenuButton, FocusView
from utils.constants import discord_presence_id
class MenuButton(Button):
def __init__(self, text='', **kwargs):
super().__init__(text, scale=(.25, .075), highlight_color=color.azure, **kwargs)
for key, value in kwargs.items():
setattr(self, key, value)
class Main():
class Main(FocusView):
def __init__(self, pypresence_client=None) -> None:
super().__init__(y=.15)
self.pypresence_client = pypresence_client
with open("settings.json", "r") as file:
@@ -55,15 +52,17 @@ class Main():
self.pypresence_client.update(state='In Main Menu', details='In Main Menu')
button_spacing = .075 * 1.25
self.menu_parent = Entity(parent=camera.ui, y=.15)
self.main_menu = Entity(parent=self.menu_parent)
self.title_label = Text("Aim Trainer", parent=self.main_menu, y=-0.01 * button_spacing, scale=3, x=-.2)
self.high_score_label = Text(f"High Score: {self.high_score}", parent=self.main_menu, scale=1.25, y=-1 * button_spacing, x=-.12)
self.play_button = MenuButton('Play', on_click=Func(self.play), parent=self.main_menu, y=-2 * button_spacing)
self.settings_button = MenuButton('Settings', on_click=Func(self.settings), parent=self.main_menu, y=-3 * button_spacing)
self.quit_button = MenuButton('Quit', on_click=Sequence(Wait(.01), Func(application.quit)), parent=self.main_menu, y=-4 * button_spacing)
self.title_label = Text("Aim Trainer", parent=self.main, y=-0.01 * button_spacing, scale=3, x=-.2)
self.high_score_label = Text(f"High Score: {self.high_score}", parent=self.main, scale=1.25, y=-1 * button_spacing, x=-.12)
self.play_button = MenuButton('Play', on_click=Func(self.play), parent=self.main, y=-2 * button_spacing)
self.settings_button = MenuButton('Settings', on_click=Func(self.settings), parent=self.main, y=-3 * button_spacing)
self.quit_button = MenuButton('Quit', on_click=Sequence(Wait(.01), Func(application.quit)), parent=self.main, y=-4 * button_spacing)
self.ui = [self.title_label, self.high_score_label, self.play_button, self.settings_button, self.quit_button]
self.detect_focusable_widgets()
def play(self):
self.hide()
@@ -73,11 +72,4 @@ class Main():
def settings(self):
self.hide()
from menus.settings import Settings
Settings(self.pypresence_client)
def hide(self):
destroy(self.play_button)
destroy(self.settings_button)
destroy(self.quit_button)
destroy(self.main_menu)
destroy(self.menu_parent)
Settings(self.pypresence_client)

View File

@@ -4,28 +4,28 @@ from ursina.prefabs.dropdown_menu import DropdownMenuButton
from ursina.prefabs.button_group import ButtonGroup
import pypresence, json, copy
from utils.utils import FakePyPresence, Dropdown, FileManager, is_float
from utils.utils import FakePyPresence, Dropdown, FileManager, FocusView, is_float
from utils.constants import discord_presence_id, settings, settings_start_category
from utils.preload import music_sound
class Settings:
class Settings(FocusView):
def __init__(self, rpc):
super().__init__(model='cube', color=color.dark_gray, scale=(1.8, 1.2), z=1)
self.rpc = rpc
rpc.update(state='In Settings', details='Modifying Settings', start=rpc.start_time)
self.data = json.load(open('settings.json'))
self.edits = {}
self.category = settings_start_category
self.main = Entity(parent=camera.ui, model='cube', color=color.dark_gray, scale=(1.8, 1.2), z=1)
self.back_button = Button('Back', parent=camera.ui, color=color.gray, scale=(.1, .05), position=(-.8, .45), on_click=self.exit)
self.ui = [self.main]
self.category_group = ButtonGroup(tuple(settings.keys()), default=self.category, spacing=(.25, 0, 0))
self.category_group.on_value_changed = lambda: self.show(self.category_group.value)
self.category_group.position = (-.6, .4)
self.ui = [self.main, self.back_button, self.category_group]
self.ui.append(self.category_group)
self.weapon_dmg_inputs = {}
self.weapon_atk_speed_inputs = {}
@@ -39,8 +39,14 @@ class Settings:
self.enemy_img_path_buttons = {}
self.enemy_remove_buttons = {}
self.back_button = Button('Back', parent=camera.ui, color=color.gray, scale=(.1, .05), position=(-.8, .45), on_click=self.exit)
self.show(self.category)
self.ui.append(self.back_button)
self.detect_focusable_widgets()
def show(self, category):
self.clear()
self.category = category
@@ -66,13 +72,13 @@ class Settings:
if type == 'bool':
bool_button_group = ButtonGroup(('OFF', 'ON'), default='ON' if val else 'OFF', spacing=(.1, 0, 0))
bool_button_group.position = (.2, y)
bool_button_group.on_value_changed = lambda bool_button_group=bool_button_group, n=name: self.update(n, bool_button_group.value == 'ON')
bool_button_group.on_value_changed = lambda bool_button_group=bool_button_group, n=name: self.update_settings(n, bool_button_group.value == 'ON')
self.ui.append(bool_button_group)
elif type == 'slider':
slider = ThinSlider(text=name, min=info['min'], max=info['max'], default=val, dynamic=True)
slider = ThinSlider(min=info['min'], max=info['max'], default=val, dynamic=True)
slider.position = (.2, y)
slider.on_value_changed = lambda slider=slider, n=name: self.update(n, int(slider.value))
slider.on_value_changed = lambda slider=slider, n=name: self.update_settings(n, int(slider.value))
self.ui.append(slider)
elif type == "option":
@@ -100,10 +106,12 @@ class Settings:
self.apply_button = Button('Apply', parent=camera.ui, color=color.green, scale=(.15, .08), position=(.6, -.4), on_click=self.apply_changes)
self.ui.append(self.apply_button)
self.detect_focusable_widgets()
def directory_selected(self, btn, name, value):
btn.text = f"Select Directory ({value})"
self.update(name, value)
self.update_settings(name, value)
def select_directory(self, btn, name):
self.dir_file_manager = FileManager(return_folders=True, z=-1)
@@ -123,9 +131,9 @@ class Settings:
def dropdown_update(self, n, dropdown_menu, btn):
dropdown_menu.text = btn.text
self.update(n, btn.text)
self.update_settings(n, btn.text)
def update(self, name, value):
def update_settings(self, name, value):
self.edits[settings[self.category][name]['config_key']] = value
def apply_changes(self):
@@ -255,6 +263,8 @@ class Settings:
self.apply_button = Button('Apply', parent=camera.ui, color=color.green, scale=(.15, .08), position=(.6, -.4), on_click=self.apply_changes)
self.ui.append(self.apply_button)
self.detect_focusable_widgets()
def weapons(self):
y = .3
@@ -295,16 +305,15 @@ class Settings:
self.apply_button = Button('Apply', parent=camera.ui, color=color.green, scale=(.15, .08), position=(.6, -.4), on_click=self.apply_changes)
self.ui.append(self.apply_button)
self.detect_focusable_widgets()
def clear(self):
for e in list(self.ui):
if e not in (self.main, self.back_button, self.category_group):
destroy(e)
self.ui.remove(e)
def hide(self):
for e in self.ui:
destroy(e)
self.ui.clear()
self.detect_focusable_widgets()
def exit(self):
self.hide()
@@ -339,4 +348,6 @@ class Settings:
self.credits_label = Text(text=text, parent=camera.ui, position=(0, 0), origin=(0, 0), scale=font_size, color=color.white)
self.credits_label.type = 'credits_text'
self.ui.append(self.credits_label)
self.ui.append(self.credits_label)
self.detect_focusable_widgets()