1414# Source.Python Imports
1515# Engines
1616from engines .server import engine_server
17+ # Menus
18+ from menus import PagedMenu
19+ from menus import Option
20+ # Messages
21+ from messages import SayText
1722# Players
1823from players .helpers import playerinfo_from_index
1924from players .helpers import uniqueid_from_playerinfo
2025# Settings
26+ from settings import _settings_strings
2127from 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' ])
2236
2337
2438# =============================================================================
@@ -28,7 +42,7 @@ class _SettingsType(object):
2842
2943 """Class used to store settings with possible values."""
3044
31- def __new__ (cls , name , default , * args ):
45+ def __new__ (cls , name , default , text = None , * args ):
3246 """Verify the name and default value before getting the new object."""
3347 # Was a valid value passed as the name?
3448 if not name .replace ('_' , '' ).replace (' ' , '' ).isalpha ():
@@ -48,6 +62,18 @@ def __new__(cls, name, default, *args):
4862 # Get the new object instance
4963 self = object .__new__ (cls )
5064
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+
5177 # Return the instance
5278 return self
5379
@@ -71,13 +97,20 @@ def prefix(self):
7197 """Return the prefix of the setting."""
7298 return self ._prefix
7399
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+
74110 def get_setting (self , index ):
75111 """Return the setting value for the given player index."""
76- # Get the convar's value
77- convar = self .prefix + self .name .lower ().replace (' ' , '_' )
78-
79112 # 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 )
81114
82115 # Try to typecast the value, suppressing ValueErrors
83116 with suppress (ValueError ):
@@ -98,10 +131,10 @@ def get_setting(self, index):
98131 if uniqueid in _player_settings_storage :
99132
100133 # 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 ]:
102135
103136 # Get the client's value for the convar
104- value = _player_settings_storage [uniqueid ][convar ]
137+ value = _player_settings_storage [uniqueid ][self . convar ]
105138
106139 # Try to typecast the value, suppressing ValueErrors
107140 with suppress (ValueError ):
@@ -118,6 +151,21 @@ def get_setting(self, index):
118151 # Return the default value
119152 return self .default
120153
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+
121169
122170class _NumericalSetting (_SettingsType ):
123171
@@ -126,9 +174,6 @@ class _NumericalSetting(_SettingsType):
126174 def __init__ (
127175 self , name , default , text = None , min_value = None , max_value = None ):
128176 """Store the base attributes on instantiation."""
129- self ._name = name
130- self ._default = default
131- self ._text = text
132177 self ._min = self ._type (min_value ) if min_value is not None else None
133178 self ._max = self ._type (max_value ) if max_value is not None else None
134179
@@ -182,9 +227,6 @@ class _StringSetting(_SettingsType):
182227
183228 def __init__ (self , name , default , text = None ):
184229 """Store the base attributes on instatiation."""
185- self ._name = name
186- self ._default = default
187- self ._text = text
188230 self ._options = OrderedDict ()
189231
190232 @property
@@ -202,7 +244,11 @@ def add_option(self, name, text=None):
202244 'Given name "{0}" is already an option' .format (name ))
203245
204246 # 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 )
206252
207253 def remove_option (self , name ):
208254 """Remove an option from the settings."""
0 commit comments