comparison demos/rio_de_hola/settings.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children 70697641fca3
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 import shutil
2 from fife.extensions import pychan
3 import os
4 from fife.extensions.fife_utils import getUserDataDirectory
5 try:
6 import xml.etree.cElementTree as ET
7 except:
8 import xml.etree.ElementTree as ET
9
10 class Setting(object):
11 def __init__(self, *args, **kwargs):
12 self._appdata = getUserDataDirectory("fife", "rio_de_hola")
13
14 if not os.path.exists(self._appdata+'/settings.xml'):
15 shutil.copyfile('settings-dist.xml', self._appdata+'/settings.xml')
16
17
18 def onOptionsPress(self):
19 self.changesRequireRestart = False
20 self.isSetToDefault = False
21 self.Resolutions = ['640x480', '800x600', '1024x768', '1280x800', '1440x900']
22 if not hasattr(self, 'OptionsDlg'):
23 self.OptionsDlg = None
24 if not self.OptionsDlg:
25 self.OptionsDlg = pychan.loadXML('gui/settings.xml')
26 self.OptionsDlg.distributeInitialData({
27 'screen_resolution' : self.Resolutions,
28 'render_backend' : ['OpenGL', 'SDL']
29 })
30 self.OptionsDlg.distributeData({
31 'screen_resolution' : self.Resolutions.index(str(self.readSetting("ScreenWidth")) + 'x' + str(self.readSetting("ScreenHeight"))),
32 'render_backend' : 0 if str(self.readSetting("RenderBackend")) == "OpenGL" else 1,
33 'enable_fullscreen' : int(self.readSetting("FullScreen")),
34 'enable_sound' : int(self.readSetting("PlaySounds"))
35 })
36 self.OptionsDlg.mapEvents({
37 'okButton' : self.saveSettings,
38 'cancelButton' : self.OptionsDlg.hide,
39 'defaultButton' : self.setDefaults
40 })
41 self.OptionsDlg.show()
42
43 def setDefaults(self):
44 shutil.copyfile('settings-dist.xml', self._appdata+'/settings.xml')
45 self.isSetToDefault = True
46 self.changesRequireRestart = True
47
48 def readSetting(self, name, type='int', strip=True, text=False):
49 if not hasattr(self, 'tree'):
50 self.tree = ET.parse(self._appdata+'/settings.xml')
51 self.root_element = self.tree.getroot()
52 element = self.root_element.find(name)
53 if element is not None:
54 element_value = element.text
55 if element_value is None:
56 if type == 'int':
57 return 0
58 elif type == 'list':
59 list = []
60 return list
61 else:
62 if type == 'int':
63 return element_value.strip() if strip else element_value
64 elif type == 'list':
65 list = []
66 list_s = []
67 list = str(element_value.strip()).split(";")
68 for item in list:
69 item = item.strip()
70 if text:
71 item = item.replace('\\n', '\n')
72 list_s.append(item)
73 return list_s
74 elif type == 'bool':
75 return False if element_value.strip() == 'False' else True
76 else:
77 print 'Setting,', name, 'does not exist!'
78
79 def setSetting(self, name, value):
80 element = self.root_element.find(name)
81 if element is not None:
82 if value is not element.text:
83 element.text = str(value)
84 else:
85 print 'Setting,', name, 'does not exist!'
86
87 def saveSettings(self):
88 screen_resolution, render_backend, enable_fullscreen, enable_sound = self.OptionsDlg.collectData('screen_resolution', 'render_backend', 'enable_fullscreen', 'enable_sound')
89 render_backend = 'OpenGL' if render_backend is 0 else 'SDL'
90 if render_backend != str(self.readSetting("RenderBackend")):
91 self.setSetting('RenderBackend', render_backend)
92 self.changesRequireRestart = True
93 if int(enable_fullscreen) != int(self.readSetting("FullScreen")):
94 self.setSetting('FullScreen', int(enable_fullscreen))
95 self.changesRequireRestart = True
96 if int(enable_sound) != int(self.readSetting("PlaySounds")):
97 self.setSetting('PlaySounds', int(enable_sound))
98 self.changesRequireRestart = True
99 if screen_resolution != self.Resolutions.index(str(self.readSetting("ScreenWidth")) + 'x' + str(self.readSetting("ScreenHeight"))):
100 self.setSetting('ScreenWidth', int(self.Resolutions[screen_resolution].partition('x')[0]))
101 self.setSetting('ScreenHeight', int(self.Resolutions[screen_resolution].partition('x')[2]))
102 self.changesRequireRestart = True
103
104 if not self.isSetToDefault:
105 self.tree.write(self._appdata+'/settings.xml')
106 self.OptionsDlg.hide()
107 if self.changesRequireRestart:
108 RestartDlg = pychan.loadXML('gui/changes_require_restart.xml')
109 RestartDlg.mapEvents({ 'closeButton' : RestartDlg.hide })
110 RestartDlg.show()