changeset 496:987307d12235

Added the fife_settings.py extension. This provides a small utility class for loading and saving settings along with a dialog box for setting screen resolution and other things. Updated the shooter demo and rio_de_hola to use the new settings extension.
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Wed, 12 May 2010 16:09:46 +0000
parents ae9f5383f5b1
children 559a26347730
files demos/rio_de_hola/gui/changes_require_restart.xml demos/rio_de_hola/gui/settings.xml demos/rio_de_hola/run.py demos/rio_de_hola/scripts/agents/girl.py demos/rio_de_hola/scripts/agents/hero.py demos/rio_de_hola/scripts/world.py demos/rio_de_hola/settings.py demos/shooter/gui/mainmenu.xml demos/shooter/run.py demos/shooter/scripts/gui/guis.py demos/shooter/scripts/world.py demos/shooter/settings-dist.xml engine/python/fife/extensions/fifelog.py
diffstat 13 files changed, 73 insertions(+), 186 deletions(-) [+]
line wrap: on
line diff
--- a/demos/rio_de_hola/gui/changes_require_restart.xml	Tue May 11 21:30:55 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-<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/demos/rio_de_hola/gui/settings.xml	Tue May 11 21:30:55 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-<Window name="Settings" title="Settings">
-	<Label text="Rio de hola Settings menu!" />
-	<HBox>
-		<VBox>
-			<Label text="Resolution:" />
-			<Label text="Renderer:" />
-		</VBox>
-		<VBox min_size="120,60">
-			<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/demos/rio_de_hola/run.py	Tue May 11 21:30:55 2010 +0000
+++ b/demos/rio_de_hola/run.py	Wed May 12 16:09:46 2010 +0000
@@ -39,10 +39,10 @@
 from fife.extensions.basicapplication import ApplicationBase
 from fife.extensions import pychan
 from fife.extensions.pychan import widgets
-from settings import Setting
+from fife.extensions.fife_settings import Setting
 from fife.extensions.fife_utils import getUserDataDirectory
 
-TDS = Setting()
+TDS = Setting(app_name="rio_de_hola")
 
 class ApplicationListener(eventlistenerbase.EventListenerBase):
 	def __init__(self, engine, world):
@@ -128,8 +128,6 @@
 		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)))
--- a/demos/rio_de_hola/scripts/agents/girl.py	Tue May 11 21:30:55 2010 +0000
+++ b/demos/rio_de_hola/scripts/agents/girl.py	Wed May 12 16:09:46 2010 +0000
@@ -23,9 +23,9 @@
 
 from agent import Agent
 from fife import fife
-from settings import Setting
+from fife.extensions.fife_settings import Setting
 
-TDS = Setting()
+TDS = Setting(app_name="rio_de_hola")
 
 _STATE_NONE, _STATE_IDLE, _STATE_RUN, _STATE_FOLLOW = 0, 1, 2, 3
 
--- a/demos/rio_de_hola/scripts/agents/hero.py	Tue May 11 21:30:55 2010 +0000
+++ b/demos/rio_de_hola/scripts/agents/hero.py	Wed May 12 16:09:46 2010 +0000
@@ -23,9 +23,9 @@
 
 import random
 from agent import Agent
-from settings import Setting
+from fife.extensions.fife_settings import Setting
 
-TDS = Setting()
+TDS = Setting(app_name="rio_de_hola")
 
 _STATE_NONE, _STATE_IDLE, _STATE_RUN, _STATE_KICK, _STATE_TALK = xrange(5)
 
--- a/demos/rio_de_hola/scripts/world.py	Tue May 11 21:30:55 2010 +0000
+++ b/demos/rio_de_hola/scripts/world.py	Wed May 12 16:09:46 2010 +0000
@@ -35,9 +35,9 @@
 from agents.cloud import Cloud
 from agents.beekeeper import Beekeeper
 from agents.agent import create_anonymous_agents
-from settings import Setting
+from fife.extensions.fife_settings import Setting
 
-TDS = Setting()
+TDS = Setting(app_name="rio_de_hola")
 
 class MapListener(fife.MapChangeListener):
 	def __init__(self, map):
--- a/demos/rio_de_hola/settings.py	Tue May 11 21:30:55 2010 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,133 +0,0 @@
-# -*- coding: utf-8 -*-
-
-# ####################################################################
-#  Copyright (C) 2005-2009 by the FIFE team
-#  http://www.fifengine.de
-#  This file is part of FIFE.
-#
-#  FIFE is free software; you can redistribute it and/or
-#  modify it under the terms of the GNU Lesser General Public
-#  License as published by the Free Software Foundation; either
-#  version 2.1 of the License, or (at your option) any later version.
-#
-#  This library is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#  Lesser General Public License for more details.
-#
-#  You should have received a copy of the GNU Lesser General Public
-#  License along with this library; if not, write to the
-#  Free Software Foundation, Inc.,
-#  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
-# ####################################################################
-
-import shutil
-from fife.extensions import pychan
-import os
-from fife.extensions.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()
--- a/demos/shooter/gui/mainmenu.xml	Tue May 11 21:30:55 2010 +0000
+++ b/demos/shooter/gui/mainmenu.xml	Wed May 12 16:09:46 2010 +0000
@@ -7,6 +7,7 @@
 	<VBox opaque='0' base_color="188, 0, 0" position="412,200" name="main_menu">
 		<Button name="continue" text="Continue" min_size="200,0" border_size="0"/>
 		<Button name="new_game" text="New Game" min_size="200,0" border_size="0"/>
+		<Button name="settings" text="Settings" min_size="200,0" border_size="0"/>
 		<Button name="high_scores" text="High Scores" min_size="200,0" border_size="0"/>
 		<Button name="credits" text="Credits" min_size="200,0" border_size="0"/>
 		<Button name="quit" text="Quit" min_size="200,0" border_size="0"/>
--- a/demos/shooter/run.py	Tue May 11 21:30:55 2010 +0000
+++ b/demos/shooter/run.py	Wed May 12 16:09:46 2010 +0000
@@ -39,6 +39,11 @@
 from fife.extensions.basicapplication import ApplicationBase
 from fife.extensions import pychan
 from fife.extensions.pychan import widgets
+from fife.extensions.fife_settings import Setting
+
+TDS = Setting(app_name="shooter", 
+              settings_file="./settings.xml", 
+              settings_gui_xml="")
 
 class ApplicationListener(eventlistenerbase.EventListenerBase):
 	def __init__(self, engine, world):
@@ -67,8 +72,10 @@
 		
 		#This is requred if you want to use modal dialog boxes
 		pychan.setupModalExecution(self.mainLoop,self.breakFromMainLoop)
+
+		self._setting = TDS
 		
-		self._world = world.World(self, self.engine)
+		self._world = world.World(self, self.engine, self._setting)
 		self._listener = ApplicationListener(self.engine, self._world)
 
 	def requestQuit(self):
@@ -84,23 +91,29 @@
 		settings in here to remove the complexity of loading settings from
 		an XML file which would be out of scope of this demo.
 		"""
-
+		
+		self._setting = TDS
+		
 		engineSetting = self.engine.getSettings()
-		engineSetting.setDefaultFontGlyphs(" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&amp;`'*#=[]\"")
-		engineSetting.setDefaultFontPath("fonts/FreeSans.ttf")
-		engineSetting.setDefaultFontSize(16)
-		engineSetting.setBitsPerPixel(16)
-		engineSetting.setScreenWidth(1024)
-		engineSetting.setScreenHeight(768)
-		engineSetting.setRenderBackend("OpenGL")
-		engineSetting.setFullScreen(0)
+		engineSetting.setDefaultFontGlyphs(str(self._setting.readSetting("FontGlyphs", strip=False)))
+		engineSetting.setDefaultFontPath(str(self._setting.readSetting("Font")))
+		engineSetting.setDefaultFontSize(int(self._setting.readSetting("DefaultFontSize")))
+		engineSetting.setBitsPerPixel(int(self._setting.readSetting("BitsPerPixel")))
+		engineSetting.setInitialVolume(float(self._setting.readSetting("InitialVolume")))
+		engineSetting.setSDLRemoveFakeAlpha(int(self._setting.readSetting("SDLRemoveFakeAlpha")))
+		engineSetting.setScreenWidth(int(self._setting.readSetting("ScreenWidth")))
+		engineSetting.setScreenHeight(int(self._setting.readSetting("ScreenHeight")))
+		engineSetting.setRenderBackend(str(self._setting.readSetting("RenderBackend")))
+		engineSetting.setFullScreen(int(self._setting.readSetting("FullScreen")))
 
 		try:
-			engineSetting.setWindowTitle("FIFE - Shooter")
+			engineSetting.setWindowTitle(str(self._setting.readSetting("WindowTitle")))
+			engineSetting.setWindowIcon(str(self._setting.readSetting("WindowIcon")))
 		except:
 			pass			
+
 		try:
-			engineSetting.setImageChunkingSize(256)
+			engineSetting.setImageChunkingSize(int(self._setting.readSetting("ImageChunkSize")))
 		except:
 			pass
 
@@ -109,6 +122,7 @@
 		Initialize the LogManager.
 		"""
 
+		#@todo: load this from settings
 		logmodules = ["controller",
 		              "event channel", 
 		              "audio",
@@ -132,6 +146,7 @@
 		
 		#log to both the console and log file
 		self._log = fifelog.LogManager(self.engine, 1, 1)
+		self._log.setLevelFilter(fife.LogManager.LEVEL_DEBUG)
 		if logmodules:
 			self._log.setVisibleModules(*logmodules)
 			
--- a/demos/shooter/scripts/gui/guis.py	Tue May 11 21:30:55 2010 +0000
+++ b/demos/shooter/scripts/gui/guis.py	Wed May 12 16:09:46 2010 +0000
@@ -29,8 +29,9 @@
 from xml.sax.xmlreader import AttributesNSImpl
 
 class MainMenu(object):
-	def __init__(self, world):
+	def __init__(self, world, setting):
 		self._world = world
+		self._setting = setting
 		self._widget = pychan.loadXML('gui/mainmenu.xml')
 
 		self._continue = self._widget.findChild(name="continue")
@@ -44,6 +45,7 @@
 		eventMap = {
 			'continue': self._world.continueGame,
 			'new_game': self._world.newGame,
+			'settings': self._setting.onOptionsPress,
 			'credits': self._world.showCredits,
 			'high_scores': self._world.showHighScores,
 			'quit': self._world.quit,
--- a/demos/shooter/scripts/world.py	Tue May 11 21:30:55 2010 +0000
+++ b/demos/shooter/scripts/world.py	Wed May 12 16:09:46 2010 +0000
@@ -48,11 +48,12 @@
 	   ( by inheriting from EventlistenerBase )
 
 	"""
-	def __init__(self, app, engine):
+	def __init__(self, app, engine, setting):
 		super(World, self).__init__(engine, regKeys=True)
 
 		self._applictaion = app
 		self._engine = engine
+		self._setting = setting
 		self._timemanager = engine.getTimeManager()
 		self._eventmanager = engine.getEventManager()
 		self._model = engine.getModel()
@@ -72,7 +73,7 @@
 		
 		self._soundmanager = SoundManager(self._engine)
 		
-		self._mainmenu = MainMenu(self)
+		self._mainmenu = MainMenu(self, self._setting)
 		self.showMainMenu()
 		
 		self._hudwindow = HeadsUpDisplay(self)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/demos/shooter/settings-dist.xml	Wed May 12 16:09:46 2010 +0000
@@ -0,0 +1,24 @@
+<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 - Shooter Demo </WindowTitle>
+	<WindowIcon> </WindowIcon>
+
+	<MapFile> maps/shrine.xml </MapFile>
+	<Font> fonts/FreeSans.ttf </Font>
+	<FontGlyphs> abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&amp;`'*#=[]\"</FontGlyphs>
+	<DefaultFontSize> 16 </DefaultFontSize>
+	<LogModules> controller </LogModules>
+	<PychanDebug> False </PychanDebug>
+	<LogToPrompt> 1 </LogToPrompt>
+	<LogToFile> 0 </LogToFile>
+	<UsePsyco> False </UsePsyco>
+	<ProfilingOn> False </ProfilingOn>
+	<ImageChunkSize> 256 </ImageChunkSize>
+</Settings>
--- a/engine/python/fife/extensions/fifelog.py	Tue May 11 21:30:55 2010 +0000
+++ b/engine/python/fife/extensions/fifelog.py	Wed May 12 16:09:46 2010 +0000
@@ -102,7 +102,14 @@
 	def setLevelFilter(self, fltr):
 		"""
 		Sets the minimum log level to view.
+		
 		@param fltr: The filter level
+		
+		Valid values:
+			fife.LogManager.LEVEL_DEBUG
+			fife.LogManager.LEVEL_LOG
+			fife.LogManager.LEVEL_WARN
+   			fife.LogManager.LEVEL_ERROR
 		"""
 		self.lm.setLevelFilter(fltr)