mirror of
https://github.com/Yonokid/PyTaiko.git
synced 2026-02-04 03:30:13 +01:00
fix double free bug, add logging, update to python 3.14
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import logging
|
||||
import pyray as ray
|
||||
|
||||
from libs.audio import audio
|
||||
from libs.screen import Screen
|
||||
from libs.utils import (
|
||||
global_data,
|
||||
is_l_don_pressed,
|
||||
@@ -10,10 +12,12 @@ from libs.utils import (
|
||||
save_config,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class SettingsScreen:
|
||||
def __init__(self):
|
||||
self.screen_init = False
|
||||
|
||||
class SettingsScreen(Screen):
|
||||
def on_screen_start(self):
|
||||
super().on_screen_start()
|
||||
self.config = global_data.config
|
||||
self.headers = list(self.config.keys())
|
||||
self.headers.append('Exit')
|
||||
@@ -23,13 +27,7 @@ class SettingsScreen:
|
||||
self.editing_key = False
|
||||
self.editing_gamepad = False
|
||||
|
||||
def on_screen_start(self):
|
||||
if not self.screen_init:
|
||||
audio.list_host_apis()
|
||||
self.screen_init = True
|
||||
|
||||
def on_screen_end(self):
|
||||
self.screen_init = False
|
||||
def on_screen_end(self, next_screen: str):
|
||||
save_config(self.config)
|
||||
global_data.config = self.config
|
||||
audio.close_audio_device()
|
||||
@@ -41,7 +39,8 @@ class SettingsScreen:
|
||||
audio.buffer_size = global_data.config["audio"]["buffer_size"]
|
||||
audio.volume_presets = global_data.config["volume"]
|
||||
audio.init_audio_device()
|
||||
return "ENTRY"
|
||||
logger.info("Settings saved and audio device re-initialized")
|
||||
return next_screen
|
||||
|
||||
def get_current_settings(self):
|
||||
"""Get the current section's settings as a list"""
|
||||
@@ -53,6 +52,7 @@ class SettingsScreen:
|
||||
def handle_boolean_toggle(self, section, key):
|
||||
"""Toggle boolean values"""
|
||||
self.config[section][key] = not self.config[section][key]
|
||||
logger.info(f"Toggled boolean setting: {section}.{key} -> {self.config[section][key]}")
|
||||
|
||||
def handle_numeric_change(self, section, key, increment):
|
||||
"""Handle numeric value changes"""
|
||||
@@ -81,6 +81,7 @@ class SettingsScreen:
|
||||
new_value = valid_sizes[new_idx]
|
||||
|
||||
self.config[section][key] = new_value
|
||||
logger.info(f"Changed numeric setting: {section}.{key} -> {new_value}")
|
||||
|
||||
def handle_string_cycle(self, section, key):
|
||||
"""Cycle through predefined string values"""
|
||||
@@ -98,10 +99,12 @@ class SettingsScreen:
|
||||
self.config[section][key] = values[new_idx]
|
||||
except ValueError:
|
||||
self.config[section][key] = values[0]
|
||||
logger.info(f"Cycled string setting: {section}.{key} -> {self.config[section][key]}")
|
||||
|
||||
def handle_key_binding(self, section, key):
|
||||
"""Handle key binding changes"""
|
||||
self.editing_key = True
|
||||
logger.info(f"Started key binding edit for: {section}.{key}")
|
||||
|
||||
def update_key_binding(self):
|
||||
"""Update key binding based on input"""
|
||||
@@ -116,11 +119,14 @@ class SettingsScreen:
|
||||
setting_key, _ = settings[self.setting_index]
|
||||
self.config[current_header][setting_key] = [new_key]
|
||||
self.editing_key = False
|
||||
logger.info(f"Key binding updated: {current_header}.{setting_key} -> {new_key}")
|
||||
elif key_pressed == ray.KeyboardKey.KEY_ESCAPE:
|
||||
self.editing_key = False
|
||||
logger.info("Key binding edit cancelled")
|
||||
|
||||
def handle_gamepad_binding(self, section, key):
|
||||
self.editing_gamepad = True
|
||||
logger.info(f"Started gamepad binding edit for: {section}.{key}")
|
||||
|
||||
def update_gamepad_binding(self):
|
||||
"""Update gamepad binding based on input"""
|
||||
@@ -132,11 +138,13 @@ class SettingsScreen:
|
||||
setting_key, _ = settings[self.setting_index]
|
||||
self.config[current_header][setting_key] = [button_pressed]
|
||||
self.editing_gamepad = False
|
||||
logger.info(f"Gamepad binding updated: {current_header}.{setting_key} -> {button_pressed}")
|
||||
if ray.is_key_pressed(ray.KeyboardKey.KEY_ESCAPE):
|
||||
self.editing_gamepad = False
|
||||
logger.info("Gamepad binding edit cancelled")
|
||||
|
||||
def update(self):
|
||||
self.on_screen_start()
|
||||
super().update()
|
||||
|
||||
# Handle key binding editing
|
||||
if self.editing_key:
|
||||
@@ -151,18 +159,22 @@ class SettingsScreen:
|
||||
|
||||
# Exit handling
|
||||
if current_header == 'Exit' and (is_l_don_pressed() or is_r_don_pressed()):
|
||||
return self.on_screen_end()
|
||||
logger.info("Exiting settings screen")
|
||||
return self.on_screen_end("ENTRY")
|
||||
|
||||
# Navigation between sections
|
||||
if not self.in_setting_edit:
|
||||
if is_r_kat_pressed():
|
||||
self.header_index = (self.header_index + 1) % len(self.headers)
|
||||
self.setting_index = 0
|
||||
logger.info(f"Navigated to next section: {self.headers[self.header_index]}")
|
||||
elif is_l_kat_pressed():
|
||||
self.header_index = (self.header_index - 1) % len(self.headers)
|
||||
self.setting_index = 0
|
||||
logger.info(f"Navigated to previous section: {self.headers[self.header_index]}")
|
||||
elif (is_l_don_pressed() or is_r_don_pressed()) and current_header != 'Exit':
|
||||
self.in_setting_edit = True
|
||||
logger.info(f"Entered section edit: {current_header}")
|
||||
else:
|
||||
# Navigation within settings
|
||||
settings = self.get_current_settings()
|
||||
@@ -172,8 +184,10 @@ class SettingsScreen:
|
||||
|
||||
if is_r_kat_pressed():
|
||||
self.setting_index = (self.setting_index + 1) % len(settings)
|
||||
logger.info(f"Navigated to next setting: {settings[self.setting_index][0]}")
|
||||
elif is_l_kat_pressed():
|
||||
self.setting_index = (self.setting_index - 1) % len(settings)
|
||||
logger.info(f"Navigated to previous setting: {settings[self.setting_index][0]}")
|
||||
elif is_r_don_pressed():
|
||||
# Modify setting value
|
||||
setting_key, setting_value = settings[self.setting_index]
|
||||
@@ -209,6 +223,7 @@ class SettingsScreen:
|
||||
|
||||
elif ray.is_key_pressed(ray.KeyboardKey.KEY_ESCAPE):
|
||||
self.in_setting_edit = False
|
||||
logger.info("Exited section edit")
|
||||
|
||||
def draw(self):
|
||||
# Draw title
|
||||
@@ -237,7 +252,8 @@ class SettingsScreen:
|
||||
display_value = ', '.join(map(str, value))
|
||||
else:
|
||||
display_value = str(value)
|
||||
|
||||
if key == 'device_type':
|
||||
display_value = f'{display_value} ({audio.get_host_api_name(value)})'
|
||||
ray.draw_text(f'{key}: {display_value}', 250, i*25 + 70, 20, color)
|
||||
|
||||
# Draw instructions
|
||||
@@ -255,5 +271,3 @@ class SettingsScreen:
|
||||
else:
|
||||
# Draw exit instruction
|
||||
ray.draw_text("Press Don to exit settings", 250, 100, 20, ray.GREEN)
|
||||
def draw_3d(self):
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user