-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsManager.py
89 lines (67 loc) · 2.68 KB
/
SettingsManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import toml
import pygame
import os
# Import PopupManager for error messages
import Utils
# Global constant to enable/disable dev mode (set this accordingly)
DEV_MODE = False
# Initialize PopupManager
popup = Utils.PopupManager()
class SettingsManager:
MOUSE_MAPPING = {
"LMB": pygame.BUTTON_LEFT,
"RMB": pygame.BUTTON_RIGHT,
"MMB": pygame.BUTTON_MIDDLE
}
def __init__(self, PathToTomlConfigFile: os.PathLike):
self.TomlPath: os.PathLike = PathToTomlConfigFile
self.config = self._LoadConfig()
"""
INTERNAL FUNCTION DO NOT USE
"""
def _LoadConfig(self):
if not os.path.exists(self.TomlPath):
self._handle_error("Config file not found", f"Could not locate: {self.TomlPath}")
return {}
return toml.load(self.TomlPath)
def _handle_error(self, title: str, message: str):
"""
INERNAL DO NOT USE
Handles errors based on DEV_MODE setting.
"""
if DEV_MODE:
print(f"[ERROR] {title}: {message}")
else:
popup.Error(title, message)
def _get_key_code(self, key_name: str, default: str) -> int:
"""
INERNAL DO NOT USE
Fetch a key from the config and convert it to a pygame key constant or mouse button.
Logs errors if an invalid key is used and falls back to default.
"""
key_str = self.config.get("CONTROLS", {}).get(key_name, default)
# Check if it's a mouse button
if key_str in self.MOUSE_MAPPING:
return self.MOUSE_MAPPING[key_str]
# Otherwise, assume it's a keyboard key
try:
return pygame.key.key_code(key_str)
except ValueError:
self._handle_error("Invalid Key Binding", f"'{key_str}' is not a valid key for '{key_name}'. Using default '{default}' instead.")
return pygame.key.key_code(default)
def GetControls_MoveUp(self) -> int:
return self._get_key_code("MoveUp", "w")
def GetControls_MoveDown(self) -> int:
return self._get_key_code("MoveDown", "s")
def GetControls_MoveLeft(self) -> int:
return self._get_key_code("MoveLeft", "a")
def GetControls_MoveRight(self) -> int:
return self._get_key_code("MoveRight", "d")
def GetControls_FireWeapon(self) -> int:
return self._get_key_code("FireWeapon", "LMB")
def GetControls_ReloadWeapon(self) -> int:
return self._get_key_code("ReloadWeapon", "r")
def GetRendering_VSync(self) -> bool:
return self.config.get("RENDERING", {}).get("VSync", False)
def GetRendering_Fullscreen(self) -> bool:
return self.config.get("RENDERING", {}).get("Fullscreen", False)