| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8,10 +8,15 @@ | |||
| 8 | 8 | # Source.Python Imports | |
| 9 | 9 | # Loggers | |
| 10 | 10 | from loggers import _sp_logger | |
| 11 | + # Translations | ||
| 12 | + from translations.strings import LangStrings | ||
| 11 | 13 | ||
| 12 | 14 | ||
| 13 | 15 | # ============================================================================= | |
| 14 | 16 | # >> GLOBAL VARIABLES | |
| 15 | 17 | # ============================================================================= | |
| 16 | 18 | # Get the sp.settings logger | |
| 17 | 19 | settings_logger = _sp_logger.settings | |
| 20 | + | ||
| 21 | + # Get the settings strings | ||
| 22 | + _settings_strings = LangStrings('_core/settings_strings') | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,6 +10,8 @@ | |||
| 10 | 10 | from menus import PagedMenu | |
| 11 | 11 | # Players | |
| 12 | 12 | from players.helpers import index_from_playerinfo | |
| 13 | + # Settings | ||
| 14 | + from settings import _settings_strings | ||
| 13 | 15 | ||
| 14 | 16 | ||
| 15 | 17 | # ============================================================================= | |
@@ -21,7 +23,9 @@ class _AvailableSettings(dict): | |||
| 21 | 23 | ||
| 22 | 24 | def __init__(self): | |
| 23 | 25 | """Create the main settings menu on instantiation.""" | |
| 24 | - self._menu = PagedMenu(select_callback=self._chosen_item) | ||
| 26 | + self._menu = PagedMenu( | ||
| 27 | + select_callback=self._chosen_item, | ||
| 28 | + title=_settings_strings['Main Title']) | ||
| 25 | 29 | ||
| 26 | 30 | @property | |
| 27 | 31 | def menu(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,7 +51,9 @@ def __init__(self, name, text=None): | |||
| 51 | 51 | self._text = text | |
| 52 | 52 | ||
| 53 | 53 | # Create the instance's menu | |
| 54 | - self._menu = PagedMenu(select_callback=self._chosen_item) | ||
| 54 | + self._menu = PagedMenu( | ||
| 55 | + select_callback=self._chosen_item, | ||
| 56 | + title=name if text is None else text) | ||
| 55 | 57 | ||
| 56 | 58 | # Call the super class' __init__ to initialize the OrderedDict | |
| 57 | 59 | super(_SettingsDictionary, self).__init__() | |
@@ -158,7 +160,7 @@ def _chosen_item(menu, index, option): | |||
| 158 | 160 | return | |
| 159 | 161 | ||
| 160 | 162 | # TODO: Placeholder for sending setting specific menus | |
| 161 | - print('You chose {0}'.format(option.text)) | ||
| 163 | + option.value.menu.send(index) | ||
| 162 | 164 | ||
| 163 | 165 | ||
| 164 | 166 | class PlayerSettings(_SettingsDictionary, AutoUnload): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,11 +14,25 @@ | |||
| 14 | 14 | # Source.Python Imports | |
| 15 | 15 | # Engines | |
| 16 | 16 | from engines.server import engine_server | |
| 17 | + # Menus | ||
| 18 | + from menus import PagedMenu | ||
| 19 | + from menus import Option | ||
| 20 | + # Messages | ||
| 21 | + from messages import SayText | ||
| 17 | 22 | # Players | |
| 18 | 23 | from players.helpers import playerinfo_from_index | |
| 19 | 24 | from players.helpers import uniqueid_from_playerinfo | |
| 20 | 25 | # Settings | |
| 26 | + from settings import _settings_strings | ||
| 21 | 27 | from settings.storage import _player_settings_storage | |
| 28 | + # Translations | ||
| 29 | + from translations.strings import TranslationStrings | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + # ============================================================================= | ||
| 33 | + # >> GLOBAL VARIABLES | ||
| 34 | + # ============================================================================= | ||
| 35 | + _message = SayText(message=_settings_strings['Chosen']) | ||
| 22 | 36 | ||
| 23 | 37 | ||
| 24 | 38 | # ============================================================================= | |
@@ -28,7 +42,7 @@ class _SettingsType(object): | |||
| 28 | 42 | ||
| 29 | 43 | """Class used to store settings with possible values.""" | |
| 30 | 44 | ||
| 31 | - def __new__(cls, name, default, *args): | ||
| 45 | + def __new__(cls, name, default, text=None, *args): | ||
| 32 | 46 | """Verify the name and default value before getting the new object.""" | |
| 33 | 47 | # Was a valid value passed as the name? | |
| 34 | 48 | if not name.replace('_', '').replace(' ', '').isalpha(): | |
@@ -48,6 +62,18 @@ def __new__(cls, name, default, *args): | |||
| 48 | 62 | # Get the new object instance | |
| 49 | 63 | self = object.__new__(cls) | |
| 50 | 64 | ||
| 65 | + # Store the base attributes | ||
| 66 | + self._name = name | ||
| 67 | + self._default = default | ||
| 68 | + self._text = text | ||
| 69 | + | ||
| 70 | + # Store a menu for the object | ||
| 71 | + self._menu = PagedMenu( | ||
| 72 | + select_callback=self._chosen_value, | ||
| 73 | + build_callback=self._menu_build, | ||
| 74 | + title=name if text is None else text, | ||
| 75 | + description=_settings_strings['Description']) | ||
| 76 | + | ||
| 51 | 77 | # Return the instance | |
| 52 | 78 | return self | |
| 53 | 79 | ||
@@ -71,13 +97,20 @@ def prefix(self): | |||
| 71 | 97 | """Return the prefix of the setting.""" | |
| 72 | 98 | return self._prefix | |
| 73 | 99 | ||
| 100 | + @property | ||
| 101 | + def convar(self): | ||
| 102 | + """Returns the convar name of the setting.""" | ||
| 103 | + return self.prefix + self.name.lower().replace(' ', '_') | ||
| 104 | + | ||
| 105 | + @property | ||
| 106 | + def menu(self): | ||
| 107 | + """Return the setting's menu.""" | ||
| 108 | + return self._menu | ||
| 109 | + | ||
| 74 | 110 | def get_setting(self, index): | |
| 75 | 111 | """Return the setting value for the given player index.""" | |
| 76 | - # Get the convar's value | ||
| 77 | - convar = self.prefix + self.name.lower().replace(' ', '_') | ||
| 78 | - | ||
| 79 | 112 | # Get the client's convar value | |
| 80 | - value = engine_server.get_client_convar_value(index, convar) | ||
| 113 | + value = engine_server.get_client_convar_value(index, self.convar) | ||
| 81 | 114 | ||
| 82 | 115 | # Try to typecast the value, suppressing ValueErrors | |
| 83 | 116 | with suppress(ValueError): | |
@@ -98,10 +131,10 @@ def get_setting(self, index): | |||
| 98 | 131 | if uniqueid in _player_settings_storage: | |
| 99 | 132 | ||
| 100 | 133 | # Is the convar in the clients's dictionary? | |
| 101 | - if convar in _player_settings_storage[uniqueid]: | ||
| 134 | + if self.convar in _player_settings_storage[uniqueid]: | ||
| 102 | 135 | ||
| 103 | 136 | # Get the client's value for the convar | |
| 104 | - value = _player_settings_storage[uniqueid][convar] | ||
| 137 | + value = _player_settings_storage[uniqueid][self.convar] | ||
| 105 | 138 | ||
| 106 | 139 | # Try to typecast the value, suppressing ValueErrors | |
| 107 | 140 | with suppress(ValueError): | |
@@ -118,6 +151,21 @@ def get_setting(self, index): | |||
| 118 | 151 | # Return the default value | |
| 119 | 152 | return self.default | |
| 120 | 153 | ||
| 154 | + def _menu_build(self, menu, index): | ||
| 155 | + """Set the default value in the menu description.""" | ||
| 156 | + self.menu.description.tokens = {'value': self.get_setting(index)} | ||
| 157 | + | ||
| 158 | + def _chosen_value(self, menu, index, option): | ||
| 159 | + """Store the player's chosen value for the setting.""" | ||
| 160 | + # Get the client's uniqueid | ||
| 161 | + uniqueid = uniqueid_from_playerinfo(playerinfo_from_index(index)) | ||
| 162 | + | ||
| 163 | + # Set the player's setting | ||
| 164 | + _player_settings_storage[uniqueid][self.convar] = option.value | ||
| 165 | + | ||
| 166 | + # Send the player a message about their changed setting | ||
| 167 | + _message.send(index, convar=self.convar, value=option.value) | ||
| 168 | + | ||
| 121 | 169 | ||
| 122 | 170 | class _NumericalSetting(_SettingsType): | |
| 123 | 171 | ||
@@ -126,9 +174,6 @@ class _NumericalSetting(_SettingsType): | |||
| 126 | 174 | def __init__( | |
| 127 | 175 | self, name, default, text=None, min_value=None, max_value=None): | |
| 128 | 176 | """Store the base attributes on instantiation.""" | |
| 129 | - self._name = name | ||
| 130 | - self._default = default | ||
| 131 | - self._text = text | ||
| 132 | 177 | self._min = self._type(min_value) if min_value is not None else None | |
| 133 | 178 | self._max = self._type(max_value) if max_value is not None else None | |
| 134 | 179 | ||
@@ -182,9 +227,6 @@ class _StringSetting(_SettingsType): | |||
| 182 | 227 | ||
| 183 | 228 | def __init__(self, name, default, text=None): | |
| 184 | 229 | """Store the base attributes on instatiation.""" | |
| 185 | - self._name = name | ||
| 186 | - self._default = default | ||
| 187 | - self._text = text | ||
| 188 | 230 | self._options = OrderedDict() | |
| 189 | 231 | ||
| 190 | 232 | @property | |
@@ -202,7 +244,11 @@ def add_option(self, name, text=None): | |||
| 202 | 244 | 'Given name "{0}" is already an option'.format(name)) | |
| 203 | 245 | ||
| 204 | 246 | # Store the option | |
| 205 | - self.options[name] = text if text else name | ||
| 247 | + option = self.options[name] = Option( | ||
| 248 | + name if text is None else text, name) | ||
| 249 | + | ||
| 250 | + # Add the option to the menu | ||
| 251 | + self.menu.append(option) | ||
| 206 | 252 | ||
| 207 | 253 | def remove_option(self, name): | |
| 208 | 254 | """Remove an option from the settings.""" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,8 @@ | |||
| 1 | + [Main Title] | ||
| 2 | + en = "User settings" | ||
| 3 | + | ||
| 4 | + [Description] | ||
| 5 | + en = "Current value: $value" | ||
| 6 | + | ||
| 7 | + [Chosen] | ||
| 8 | + en = "Client setting '\x04$convar\x01' has been updated to '\x04$value\x01'." | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments