changeset 121:ae3b8139c7c7

* Applying settings patch by greyghost * See: #274
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 05 Aug 2008 14:44:15 +0000
parents e21b1a26fa5f
children 6b2f3a151f81
files clients/rio_de_hola/gui/changes_require_restart.xml clients/rio_de_hola/gui/rootpanel.xml clients/rio_de_hola/gui/settings.xml clients/rio_de_hola/run.py clients/rio_de_hola/scripts/agents/beekeeper.py clients/rio_de_hola/scripts/agents/cloud.py clients/rio_de_hola/scripts/agents/girl.py clients/rio_de_hola/scripts/agents/hero.py clients/rio_de_hola/scripts/world.py clients/rio_de_hola/settings-dist.xml clients/rio_de_hola/settings.py
diffstat 11 files changed, 235 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clients/rio_de_hola/gui/changes_require_restart.xml	Tue Aug 05 14:44:15 2008 +0000
@@ -0,0 +1,7 @@
+<Window title="Changes require restart">
+	<Label text="Some of your changes require you to restart Rio de hola." />
+	<HBox>
+		<Spacer />
+		<Button name="closeButton" text="Ok" />
+	</HBox>
+</Window>
--- a/clients/rio_de_hola/gui/rootpanel.xml	Mon Aug 04 15:45:07 2008 +0000
+++ b/clients/rio_de_hola/gui/rootpanel.xml	Tue Aug 05 14:44:15 2008 +0000
@@ -2,4 +2,5 @@
 	<Label name="Title" text="Rio de hola 2008.1"/>
 	<Button name="quitButton" text="Quit"/>
 	<Button name="aboutButton" text="About"/>
+	<Button name="optionsButton" text="Options"/>
 </HBox>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clients/rio_de_hola/gui/settings.xml	Tue Aug 05 14:44:15 2008 +0000
@@ -0,0 +1,21 @@
+<Window name="Settings" title="Settings">
+	<Label text="Rio de hola Settings menu!" />
+	<HBox>
+		<VBox>
+			<Label text="Resolution:" />
+			<Label text="Renderer:" />
+		</VBox>
+		<VBox>
+			<DropDown name="screen_resolution" min_size="120,0" />
+			<DropDown name="render_backend" min_size="120,0" />
+		</VBox>
+	</HBox>
+	<CheckBox name="enable_fullscreen" text="Use the full screen mode" />
+	<CheckBox name="enable_sound" text="Enable sound" />
+	<HBox>
+		<Spacer />
+		<Button name="cancelButton" text="Cancel" />
+		<Button name="okButton" text="Ok" />
+		<Button name="defaultButton" text="Defaults" />
+	</HBox>
+</Window>
--- a/clients/rio_de_hola/run.py	Mon Aug 04 15:45:07 2008 +0000
+++ b/clients/rio_de_hola/run.py	Tue Aug 05 14:44:15 2008 +0000
@@ -2,7 +2,7 @@
 # coding: utf-8
 # This is the rio de hola client for FIFE.
 
-import sys, os, re, math, random
+import sys, os, re, math, random, shutil
 
 def _jp(path):
 	return os.path.sep.join(path.split('/'))
@@ -12,14 +12,18 @@
 	if p not in sys.path:
 		sys.path.append(_jp(p))
 
+if not os.path.exists('settings.xml'):
+	shutil.copyfile('settings-dist.xml', 'settings.xml')
+
 import fife, fifelog
 from scripts import world
 from scripts.common import eventlistenerbase
 from basicapplication import ApplicationBase
 import pychan
 import pychan.widgets as widgets
-import settings as TDS
+from settings import Setting
 
+TDS = Setting()
 
 class ApplicationListener(eventlistenerbase.EventListenerBase):
 	def __init__(self, engine, world):
@@ -41,6 +45,7 @@
 		self.rootpanel.mapEvents({ 
 			'quitButton' : self.onQuitButtonPress,
 			'aboutButton' : self.onAboutButtonPress,
+			'optionsButton' : TDS.onOptionsPress
 		})
 		self.rootpanel.show()
 
@@ -95,19 +100,18 @@
 			self.aboutWindow.distributeData({ 'helpText' : open("misc/infotext.txt").read() })
 		self.aboutWindow.show()
 
-
 class IslandDemo(ApplicationBase):
 	def __init__(self):
 		super(IslandDemo,self).__init__()
-		pychan.init(self.engine, debug=TDS.PychanDebug)
+		pychan.init(self.engine, debug=TDS.readSetting("PychanDebug", type='bool'))
 		self.world = world.World(self.engine)
 		self.listener = ApplicationListener(self.engine, self.world)
-		self.world.load(TDS.MapFile)
+		self.world.load(str(TDS.readSetting("MapFile")))
 
 		self.soundmanager = self.engine.getSoundManager()
 		self.soundmanager.init()
 
-		if TDS.PlaySounds:
+		if int(TDS.readSetting("PlaySounds")):
 			# play track as background music
 			emitter = self.soundmanager.createEmitter()
 			id = self.engine.getSoundClipPool().addResourceFromFile('music/rio_de_hola.ogg')
@@ -115,6 +119,45 @@
 			emitter.setLooping(True)
 			emitter.play()
 
+	def loadSettings(self):
+		"""
+		Load the settings from a python file and load them into the engine.
+		Called in the ApplicationBase constructor.
+		"""
+		import settings
+		self.settings = settings
+
+		engineSetting = self.engine.getSettings()
+		engineSetting.setDefaultFontGlyphs(str(TDS.readSetting("FontGlyphs", strip=False)))
+		engineSetting.setDefaultFontPath(str(TDS.readSetting("Font")))
+		engineSetting.setDefaultFontSize(12)
+		engineSetting.setBitsPerPixel(int(TDS.readSetting("BitsPerPixel")))
+		engineSetting.setInitialVolume(float(TDS.readSetting("InitialVolume")))
+		engineSetting.setSDLRemoveFakeAlpha(int(TDS.readSetting("SDLRemoveFakeAlpha")))
+		engineSetting.setScreenWidth(int(TDS.readSetting("ScreenWidth")))
+		engineSetting.setScreenHeight(int(TDS.readSetting("ScreenHeight")))
+		engineSetting.setRenderBackend(str(TDS.readSetting("RenderBackend")))
+		engineSetting.setFullScreen(int(TDS.readSetting("FullScreen")))
+
+		try:
+			engineSetting.setWindowTitle(str(TDS.readSetting("WindowTitle")))
+			engineSetting.setWindowIcon(str(TDS.readSetting("WindowIcon")))
+		except:
+			pass			
+		try:
+			engineSetting.setImageChunkingSize(int(TDS.readSetting("ImageChunkSize")))
+		except:
+			pass
+
+	def initLogging(self):
+		"""
+		Initialize the LogManager.
+		"""
+		LogModules = TDS.readSetting("LogModules", type='list')
+		self.log = fifelog.LogManager(self.engine, int(TDS.readSetting("LogToPrompt")), int(TDS.readSetting("LogToFile")))
+		if LogModules:
+			self.log.setVisibleModules(*LogModules)
+
 	def createListener(self):
 		pass # already created in constructor
 
@@ -131,7 +174,7 @@
 
 
 if __name__ == '__main__':
-	if TDS.ProfilingOn:
+	if TDS.readSetting("ProfilingOn", type='bool'):
 		import hotshot, hotshot.stats
 		print "Starting profiler"
 		prof = hotshot.Profile("fife.prof")
@@ -143,7 +186,7 @@
 		stats.sort_stats('time', 'calls')
 		stats.print_stats(20)
 	else:
-		if TDS.UsePsyco:
+		if TDS.readSetting("UsePsyco", type='bool'):
 			# Import Psyco if available
 			try:
 				import psyco
--- a/clients/rio_de_hola/scripts/agents/beekeeper.py	Mon Aug 04 15:45:07 2008 +0000
+++ b/clients/rio_de_hola/scripts/agents/beekeeper.py	Tue Aug 05 14:44:15 2008 +0000
@@ -1,5 +1,4 @@
 from agent import Agent
-import settings as TDS
 import fife, random
 
 _STATE_NONE, _STATE_TALK = 0, 1
--- a/clients/rio_de_hola/scripts/agents/cloud.py	Mon Aug 04 15:45:07 2008 +0000
+++ b/clients/rio_de_hola/scripts/agents/cloud.py	Tue Aug 05 14:44:15 2008 +0000
@@ -1,5 +1,4 @@
 from agent import Agent
-import settings as TDS
 import fife
 import random
 
@@ -46,4 +45,4 @@
 	def move(self):
 		self.state = _STATE_FLOATING
 		self.agent.act('default', self.loc, False)
-	
\ No newline at end of file
+	
--- a/clients/rio_de_hola/scripts/agents/girl.py	Mon Aug 04 15:45:07 2008 +0000
+++ b/clients/rio_de_hola/scripts/agents/girl.py	Tue Aug 05 14:44:15 2008 +0000
@@ -1,10 +1,12 @@
 from agent import Agent
-import settings as TDS
 import fife
+from settings import Setting
+
+TDS = Setting()
 
 _STATE_NONE, _STATE_IDLE, _STATE_RUN, _STATE_FOLLOW = 0, 1, 2, 3
 
-GIRL_SPEED = 3 * TDS.TestAgentSpeed
+GIRL_SPEED = 3 * float(TDS.readSetting("TestAgentSpeed"))
 class Girl(Agent):
 	def __init__(self, model, agentName, layer, uniqInMap=True):
 		super(Girl, self).__init__(model, agentName, layer, uniqInMap)
--- a/clients/rio_de_hola/scripts/agents/hero.py	Mon Aug 04 15:45:07 2008 +0000
+++ b/clients/rio_de_hola/scripts/agents/hero.py	Tue Aug 05 14:44:15 2008 +0000
@@ -1,6 +1,8 @@
 import random
 from agent import Agent
-import settings as TDS
+from settings import Setting
+
+TDS = Setting()
 
 _STATE_NONE, _STATE_IDLE, _STATE_RUN, _STATE_KICK, _STATE_TALK = xrange(5)
 
@@ -17,8 +19,9 @@
 		else:
 			self.idlecounter += 1
 		if self.idlecounter % 7 == 0:
-			txtindex = random.randint(0, len(TDS.heroTexts) - 1)
-			instance.say(TDS.heroTexts[txtindex], 2500)
+			heroTexts = TDS.readSetting("heroTexts", type='list', text=True)
+			txtindex = random.randint(0, len(heroTexts) - 1)
+			instance.say(heroTexts[txtindex], 2500)
 	
 	def start(self):
 		self.idle()
@@ -29,7 +32,7 @@
 		
 	def run(self, location):
 		self.state = _STATE_RUN
-		self.agent.move('run', location, 4 * TDS.TestAgentSpeed)
+		self.agent.move('run', location, 4 * float(TDS.readSetting("TestAgentSpeed")))
 	
 	def kick(self, target):
 		self.state = _STATE_KICK
--- a/clients/rio_de_hola/scripts/world.py	Mon Aug 04 15:45:07 2008 +0000
+++ b/clients/rio_de_hola/scripts/world.py	Tue Aug 05 14:44:15 2008 +0000
@@ -10,7 +10,9 @@
 from agents.cloud import Cloud
 from agents.beekeeper import Beekeeper
 from agents.agent import create_anonymous_agents
-import settings as TDS
+from settings import Setting
+
+TDS = Setting()
 
 class MapListener(fife.MapChangeListener):
 	def __init__(self, map):
@@ -123,7 +125,7 @@
 				
 		self.view.resetRenderers()
 		renderer = fife.FloatingTextRenderer.getInstance(self.cameras['main'])
-		textfont = self.engine.getGuiManager().createFont('fonts/rpgfont.png', 0, TDS.FontGlyphs);
+		textfont = self.engine.getGuiManager().createFont('fonts/rpgfont.png', 0, str(TDS.readSetting("FontGlyphs", strip=False)));
 		renderer.changeDefaultFont(textfont)
 		
 		renderer = fife.FloatingTextRenderer.getInstance(self.cameras['small'])
@@ -131,13 +133,13 @@
 		
 		renderer = self.cameras['main'].getRenderer('CoordinateRenderer')
 		renderer.clearActiveLayers()
-		renderer.addActiveLayer(self.map.getLayer(TDS.CoordinateLayerName))
+		renderer.addActiveLayer(self.map.getLayer(str(TDS.readSetting("CoordinateLayerName"))))
 		
 		renderer = self.cameras['main'].getRenderer('QuadTreeRenderer')
 		renderer.setEnabled(True)
 		renderer.clearActiveLayers()
-		if TDS.QuadTreeLayerName:
-			renderer.addActiveLayer(self.map.getLayer(TDS.QuadTreeLayerName))
+		if str(TDS.readSetting("QuadTreeLayerName")):
+			renderer.addActiveLayer(self.map.getLayer(str(TDS.readSetting("QuadTreeLayerName"))))
 		
 		self.cameras['small'].getLocationRef().setExactLayerCoordinates( fife.ExactModelCoordinate( 40.0, 40.0, 0.0 ))
 		self.initial_cam2_x = self.cameras['small'].getLocation().getExactLayerCoordinates().x
@@ -237,11 +239,13 @@
 		instance = self.instancemenu.instance
 		self.hero.talk(instance.getLocationRef())
 		if instance.getObject().getId() == 'beekeeper':
-			txtindex = random.randint(0, len(TDS.beekeeperTexts) - 1)
-			instance.say(TDS.beekeeperTexts[txtindex], 5000)
+			beekeeperTexts = TDS.readSetting("beekeeperTexts", type='list', text=True)
+			txtindex = random.randint(0, len(beekeeperTexts) - 1)
+			instance.say(beekeeperTexts[txtindex], 5000)
 		if instance.getObject().getId() == 'girl':
-			txtindex = random.randint(0, len(TDS.girlTexts) - 1)
-			instance.say(TDS.girlTexts[txtindex], 5000)
+			girlTexts = TDS.readSetting("girlTexts", type='list', text=True)
+			txtindex = random.randint(0, len(girlTexts) - 1)
+			instance.say(girlTexts[txtindex], 5000)
 	
 	def onKickButtonPress(self):
 		self.hide_instancemenu()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clients/rio_de_hola/settings-dist.xml	Tue Aug 05 14:44:15 2008 +0000
@@ -0,0 +1,35 @@
+<Settings>
+	<FullScreen> 0 </FullScreen>
+	<PlaySounds> 1 </PlaySounds>
+	<RenderBackend> OpenGL </RenderBackend>
+	<ScreenWidth> 1024 </ScreenWidth>
+	<ScreenHeight> 768 </ScreenHeight>
+	<BitsPerPixel> 0 </BitsPerPixel>
+	<InitialVolume> 5.0 </InitialVolume>
+	<SDLRemoveFakeAlpha> 1 </SDLRemoveFakeAlpha>
+	<WindowTitle> FIFE - Rio de hola </WindowTitle>
+	<WindowIcon />
+
+	<MapFile> maps/shrine.xml </MapFile>
+	<Font> fonts/samanata.ttf </Font>
+	<FontGlyphs> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&amp;`'*#=[]\"</FontGlyphs>
+	<LogModules> controller </LogModules>
+	<PychanDebug> False </PychanDebug>
+	<LogToPrompt> 1 </LogToPrompt>
+	<LogToFile> 0 </LogToFile>
+	<UsePsyco> False </UsePsyco>
+	<ProfilingOn> False </ProfilingOn>
+	<ImageChunkSize> 256 </ImageChunkSize>
+
+	<CoordinateLayerName> TechdemoMapTileLayer </CoordinateLayerName>
+	<QuadTreeLayerName> <!--TechdemoMapObjectLayer--> </QuadTreeLayerName>
+
+	<heroTexts> I wonder when the guys will \n get this world ready for me ; Bring it on baby, \n I'm ready for action! ; humm di dum.... </heroTexts>
+	<girlTexts> Why I am running? Haven't you heard \n that the bees are coming?! ; Heeelp!!! The bees are going to get us! </girlTexts>
+	<beekeeperTexts> Some idiot has grown mutated bees around here ; Want to take a look at my farm? ; Did you know that I actually cannot do anything \n else than stand still and greet everybody? </beekeeperTexts>
+
+<!-- Test Settings -->
+	<TestCameraPlacement> 0 </TestCameraPlacement>
+	<TestRotationLayerName> TechdemoMapObjectLayer <!-- TechdemoMapTileLayer --> </TestRotationLayerName>
+	<TestAgentSpeed> 0.5 </TestAgentSpeed>
+</Settings>
--- a/clients/rio_de_hola/settings.py	Mon Aug 04 15:45:07 2008 +0000
+++ b/clients/rio_de_hola/settings.py	Tue Aug 05 14:44:15 2008 +0000
@@ -1,49 +1,101 @@
-BitsPerPixel        = 0
-FullScreen          = 0
-InitialVolume       = 5.0
-PlaySounds          = 1
-RenderBackend       = "OpenGL"
-SDLRemoveFakeAlpha  = 1
-ScreenWidth         = 1024
-ScreenHeight        = 768
-WindowTitle         = 'FIFE - Rio de hola'
-WindowIcon          = 'gui/icons/boy.png'
+import shutil
+import pychan
+try:
+	import xml.etree.cElementTree as ET
+except:
+	import xml.etree.ElementTree as ET
 
-MapFile             = 'maps/shrine.xml'
-Font                = 'fonts/samanata.ttf'
-FontGlyphs          = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""
+class Setting(object):
+	def onOptionsPress(self):	
+		self.changesRequireRestart = False
+		self.isSetToDefault = False
+		self.Resolutions = ['800x600', '1024x768', '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()
 
-LogModules          = ['controller']
-PychanDebug         = False
-LogToPrompt         = 1
-LogToFile           = 0
-UsePsyco            = False
-ImageChunkSize      = 256
-ProfilingOn         = False
+	def setDefaults(self):
+		shutil.copyfile('settings-dist.xml', 'settings.xml')
+		self.isSetToDefault = True
+		self.changesRequireRestart = True
 
-CoordinateLayerName = "TechdemoMapTileLayer"
-QuadTreeLayerName = "" #"TechdemoMapObjectLayer"
-
-heroTexts = (
-	'I wonder when the guys will\n' + 'get this world ready for me',
-	"Bring it on baby,\nI'm ready for action!",
-	'humm di dum....',
-)
+	def readSetting(self, name, type='int', strip=True, text=False):
+		if not hasattr(self, 'tree'):
+			self.tree = ET.parse('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!'
 
-girlTexts = (
-	"Why I am running? Haven't you heard\nthat the bees are coming?!",
-	'Heeelp!!! The bees are going to get us!',
-)
+	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!'
 
-beekeeperTexts = (
-	"Some idiot has grown mutated bees around here",
-	'Want to take a look at my farm?',
-	'Did you know that I actually cannot do anything\nelse than stand still and greet everybody?',
-)
+	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
 
-
-# test settings
-TestCameraPlacement = 0
-TestRotationLayerName = "TechdemoMapObjectLayer" # or "TechdemoMapTileLayer"
-TestAgentSpeed      = 0.5
-
+		if not self.isSetToDefault:
+			self.tree.write('settings.xml')
+		self.OptionsDlg.hide()
+		if self.changesRequireRestart:
+			RestartDlg = pychan.loadXML('gui/changes_require_restart.xml')
+			RestartDlg.mapEvents({ 'closeButton' : RestartDlg.hide })
+			RestartDlg.show()