view clients/rio_de_hola/settings.py @ 300:1465536aec94

* Reverting revisions 2937 - 2947; these have been pathfinding related changes that partially broke some FIFE-based 3rd party games * trunk has been copied to branches/active/pathfinding before the revert, so you can continue to work on the pathfinding changes without the risk of breaking trunk by accident
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 06 Jul 2009 19:51:22 +0000
parents 815354ba295e
children
line wrap: on
line source

import shutil
import pychan
import os
from fife_utils import getUserDataDirectory
try:
	import xml.etree.cElementTree as ET
except:
	import xml.etree.ElementTree as ET

class Setting(object):
	def __init__(self, *args, **kwargs):
		self._appdata = getUserDataDirectory("fife", "rio_de_hola")
		
		if not os.path.exists(self._appdata+'/settings.xml'):
			shutil.copyfile('settings-dist.xml', self._appdata+'/settings.xml')
		

	def onOptionsPress(self):	
		self.changesRequireRestart = False
		self.isSetToDefault = False
		self.Resolutions = ['640x480', '800x600', '1024x768', '1280x800', '1440x900']
		if not hasattr(self, 'OptionsDlg'):
			self.OptionsDlg = None
		if not self.OptionsDlg:
			self.OptionsDlg = pychan.loadXML('gui/settings.xml')
			self.OptionsDlg.distributeInitialData({
				'screen_resolution' : self.Resolutions,
				'render_backend' : ['OpenGL', 'SDL']
			})
			self.OptionsDlg.distributeData({
				'screen_resolution' : self.Resolutions.index(str(self.readSetting("ScreenWidth")) + 'x' + str(self.readSetting("ScreenHeight"))),
				'render_backend' : 0 if str(self.readSetting("RenderBackend")) == "OpenGL" else 1,
				'enable_fullscreen' : int(self.readSetting("FullScreen")),
				'enable_sound' : int(self.readSetting("PlaySounds"))
			})
			self.OptionsDlg.mapEvents({
				'okButton' : self.saveSettings,
				'cancelButton' : self.OptionsDlg.hide,
				'defaultButton' : self.setDefaults
			})
		self.OptionsDlg.show()

	def setDefaults(self):
		shutil.copyfile('settings-dist.xml', self._appdata+'/settings.xml')
		self.isSetToDefault = True
		self.changesRequireRestart = True

	def readSetting(self, name, type='int', strip=True, text=False):
		if not hasattr(self, 'tree'):
			self.tree = ET.parse(self._appdata+'/settings.xml')
			self.root_element = self.tree.getroot()
		element = self.root_element.find(name)
		if element is not None:
			element_value = element.text
			if element_value is None:
				if type == 'int':
					return 0
				elif type == 'list':
					list = []
					return list
			else:
				if type == 'int':
					return element_value.strip() if strip else element_value
				elif type == 'list':
					list = []
					list_s = []
					list = str(element_value.strip()).split(";")
					for item in list:
						item = item.strip()
						if text:
							item = item.replace('\\n', '\n')
						list_s.append(item)
					return list_s
				elif type == 'bool':
					return False if element_value.strip() == 'False' else True
		else:
			print 'Setting,', name, 'does not exist!'

	def setSetting(self, name, value):
		element = self.root_element.find(name)
		if element is not None:
			if value is not element.text:
				element.text = str(value)
		else:
			print 'Setting,', name, 'does not exist!'

	def saveSettings(self):
		screen_resolution, render_backend, enable_fullscreen, enable_sound = self.OptionsDlg.collectData('screen_resolution', 'render_backend', 'enable_fullscreen', 'enable_sound')
		render_backend = 'OpenGL' if render_backend is 0 else 'SDL'
		if render_backend != str(self.readSetting("RenderBackend")):
			self.setSetting('RenderBackend', render_backend)
			self.changesRequireRestart = True
		if int(enable_fullscreen) != int(self.readSetting("FullScreen")):
			self.setSetting('FullScreen', int(enable_fullscreen))
			self.changesRequireRestart = True
		if int(enable_sound) != int(self.readSetting("PlaySounds")):
			self.setSetting('PlaySounds', int(enable_sound))
			self.changesRequireRestart = True
		if screen_resolution != self.Resolutions.index(str(self.readSetting("ScreenWidth")) + 'x' + str(self.readSetting("ScreenHeight"))):
			self.setSetting('ScreenWidth', int(self.Resolutions[screen_resolution].partition('x')[0]))
			self.setSetting('ScreenHeight', int(self.Resolutions[screen_resolution].partition('x')[2]))
			self.changesRequireRestart = True

		if not self.isSetToDefault:
			self.tree.write(self._appdata+'/settings.xml')
		self.OptionsDlg.hide()
		if self.changesRequireRestart:
			RestartDlg = pychan.loadXML('gui/changes_require_restart.xml')
			RestartDlg.mapEvents({ 'closeButton' : RestartDlg.hide })
			RestartDlg.show()