annotate clients/rio_de_hola/settings.py @ 287:fdb1e98fd8b6

* Create an autorelease pool, so autoreleased SDL objects don't leak (Mac OS X specific issue) * Patch by nschum
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 21 Jun 2009 15:43:12 +0000
parents 815354ba295e
children
rev   line source
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
1 import shutil
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
2 import pychan
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
3 import os
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
4 from fife_utils import getUserDataDirectory
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
5 try:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
6 import xml.etree.cElementTree as ET
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
7 except:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
8 import xml.etree.ElementTree as ET
107
860d81602a2a - prepared basicapplication to use WindowTitle / WindowIcon from clients settings.py
chewie@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 6
diff changeset
9
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
10 class Setting(object):
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
11 def __init__(self, *args, **kwargs):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
12 self._appdata = getUserDataDirectory("fife", "rio_de_hola")
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
13
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
14 if not os.path.exists(self._appdata+'/settings.xml'):
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
15 shutil.copyfile('settings-dist.xml', self._appdata+'/settings.xml')
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
16
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
17
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
18 def onOptionsPress(self):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
19 self.changesRequireRestart = False
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
20 self.isSetToDefault = False
122
6b2f3a151f81 * Small fixes (window icon, additional resolutions, svn:ignore)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 121
diff changeset
21 self.Resolutions = ['640x480', '800x600', '1024x768', '1280x800', '1440x900']
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
22 if not hasattr(self, 'OptionsDlg'):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
23 self.OptionsDlg = None
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
24 if not self.OptionsDlg:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
25 self.OptionsDlg = pychan.loadXML('gui/settings.xml')
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
26 self.OptionsDlg.distributeInitialData({
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
27 'screen_resolution' : self.Resolutions,
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
28 'render_backend' : ['OpenGL', 'SDL']
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
29 })
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
30 self.OptionsDlg.distributeData({
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
31 'screen_resolution' : self.Resolutions.index(str(self.readSetting("ScreenWidth")) + 'x' + str(self.readSetting("ScreenHeight"))),
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
32 'render_backend' : 0 if str(self.readSetting("RenderBackend")) == "OpenGL" else 1,
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
33 'enable_fullscreen' : int(self.readSetting("FullScreen")),
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
34 'enable_sound' : int(self.readSetting("PlaySounds"))
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
35 })
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
36 self.OptionsDlg.mapEvents({
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
37 'okButton' : self.saveSettings,
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
38 'cancelButton' : self.OptionsDlg.hide,
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
39 'defaultButton' : self.setDefaults
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
40 })
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
41 self.OptionsDlg.show()
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
43 def setDefaults(self):
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
44 shutil.copyfile('settings-dist.xml', self._appdata+'/settings.xml')
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
45 self.isSetToDefault = True
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
46 self.changesRequireRestart = True
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
48 def readSetting(self, name, type='int', strip=True, text=False):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
49 if not hasattr(self, 'tree'):
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
50 self.tree = ET.parse(self._appdata+'/settings.xml')
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
51 self.root_element = self.tree.getroot()
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
52 element = self.root_element.find(name)
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
53 if element is not None:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
54 element_value = element.text
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
55 if element_value is None:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
56 if type == 'int':
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
57 return 0
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
58 elif type == 'list':
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
59 list = []
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
60 return list
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
61 else:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
62 if type == 'int':
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
63 return element_value.strip() if strip else element_value
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
64 elif type == 'list':
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
65 list = []
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
66 list_s = []
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
67 list = str(element_value.strip()).split(";")
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
68 for item in list:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
69 item = item.strip()
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
70 if text:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
71 item = item.replace('\\n', '\n')
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
72 list_s.append(item)
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
73 return list_s
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
74 elif type == 'bool':
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
75 return False if element_value.strip() == 'False' else True
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
76 else:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
77 print 'Setting,', name, 'does not exist!'
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
78
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
79 def setSetting(self, name, value):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
80 element = self.root_element.find(name)
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
81 if element is not None:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
82 if value is not element.text:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
83 element.text = str(value)
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
84 else:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
85 print 'Setting,', name, 'does not exist!'
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
86
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
87 def saveSettings(self):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
88 screen_resolution, render_backend, enable_fullscreen, enable_sound = self.OptionsDlg.collectData('screen_resolution', 'render_backend', 'enable_fullscreen', 'enable_sound')
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
89 render_backend = 'OpenGL' if render_backend is 0 else 'SDL'
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
90 if render_backend != str(self.readSetting("RenderBackend")):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
91 self.setSetting('RenderBackend', render_backend)
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
92 self.changesRequireRestart = True
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
93 if int(enable_fullscreen) != int(self.readSetting("FullScreen")):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
94 self.setSetting('FullScreen', int(enable_fullscreen))
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
95 self.changesRequireRestart = True
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
96 if int(enable_sound) != int(self.readSetting("PlaySounds")):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
97 self.setSetting('PlaySounds', int(enable_sound))
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
98 self.changesRequireRestart = True
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
99 if screen_resolution != self.Resolutions.index(str(self.readSetting("ScreenWidth")) + 'x' + str(self.readSetting("ScreenHeight"))):
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
100 self.setSetting('ScreenWidth', int(self.Resolutions[screen_resolution].partition('x')[0]))
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
101 self.setSetting('ScreenHeight', int(self.Resolutions[screen_resolution].partition('x')[2]))
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
102 self.changesRequireRestart = True
0
4a0efb7baf70 * Datasets becomes the new trunk and retires after that :-)
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
103
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
104 if not self.isSetToDefault:
273
815354ba295e * Added function to fife_utils: getUserDataDirectory. Returns the proper path to save setting and data files depending on OS.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 122
diff changeset
105 self.tree.write(self._appdata+'/settings.xml')
121
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
106 self.OptionsDlg.hide()
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
107 if self.changesRequireRestart:
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
108 RestartDlg = pychan.loadXML('gui/changes_require_restart.xml')
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
109 RestartDlg.mapEvents({ 'closeButton' : RestartDlg.hide })
ae3b8139c7c7 * Applying settings patch by greyghost
mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 120
diff changeset
110 RestartDlg.show()