changeset 143:fe7ff4808529

- added guichan slider widget to pychan gui wrapper - added new attribute class to attrs.py (FloatAttr) - added demo to pychan demo client NOTE: - demo isn't nice yet - but I'll refactor the client anyway
author chewie@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 07 Oct 2008 02:12:57 +0000
parents 816b17db2bec
children d2f1e81fbe2c
files clients/pychan_demo/gui/demoapp.xml clients/pychan_demo/pychan_test.py engine/extensions/pychan/attrs.py engine/extensions/pychan/manager.py engine/extensions/pychan/widgets.py
diffstat 5 files changed, 123 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/clients/pychan_demo/gui/demoapp.xml	Sun Oct 05 15:27:31 2008 +0000
+++ b/clients/pychan_demo/gui/demoapp.xml	Tue Oct 07 02:12:57 2008 +0000
@@ -1,4 +1,9 @@
 <Window title="This is the PyChan demo application.">
+	<HBox>
+		<Slider size="100,15" name="slider" orientation="0" scale_start="0.1" scale_end="1.5" base_color="93,161,102" />
+		<Label name="slider_desc" text="Move the slider!" />
+		<TextBox name="slider_value" text="0" base_color="0,0,0,0"/>
+	</HBox>
 	<VBox>
 		<HBox>
 			<ScrollArea size="200,500">
--- a/clients/pychan_demo/pychan_test.py	Sun Oct 05 15:27:31 2008 +0000
+++ b/clients/pychan_demo/pychan_test.py	Tue Oct 07 02:12:57 2008 +0000
@@ -48,7 +48,8 @@
 		eventMap = {
 			'creditsLink'  : self.showCredits,
 			'closeButton'  : self.quit,
-			'demoList' : self.selectExample
+			'demoList' : self.selectExample,
+			'slider': self.test_slider
 		}
 		self.gui.mapEvents(eventMap)
 
@@ -65,6 +66,9 @@
 		self.demoList.items += self.examples.keys()
 		self.gui.show()
 		
+		self.slider = self.gui.findChild(name='slider')
+		self.slider_value = self.gui.findChild(name='slider_value')
+		
 		self.currentExample = None
 		self.creditsWidget = None
 
@@ -78,6 +82,9 @@
 
 	def showCredits(self):
 		print pychan.loadXML('gui/credits.xml').execute({ 'okButton' : "Yay!" })
+		
+	def test_slider(self):
+		self.slider_value._setText( str(self.slider.getValue()) )
 
 class TestXMLApplication(basicapplication.ApplicationBase):
 	"""
--- a/engine/extensions/pychan/attrs.py	Sun Oct 05 15:27:31 2008 +0000
+++ b/engine/extensions/pychan/attrs.py	Tue Oct 07 02:12:57 2008 +0000
@@ -85,3 +85,10 @@
 			return value
 		except:
 			raise ParserError("Expected a 0 or 1.")
+			
+class FloatAttr(Attr):
+	def parse(self, value):
+		try:
+			return float(value)
+		except:
+			raise ParseError("Expected a float.")
--- a/engine/extensions/pychan/manager.py	Sun Oct 05 15:27:31 2008 +0000
+++ b/engine/extensions/pychan/manager.py	Tue Oct 07 02:12:57 2008 +0000
@@ -172,5 +172,8 @@
 	},
 	'Icon' : {
 		'border_size' : 0
+	},
+	'Slider' : {
+		'border_size' : 0
 	}
 }
--- a/engine/extensions/pychan/widgets.py	Sun Oct 05 15:27:31 2008 +0000
+++ b/engine/extensions/pychan/widgets.py	Tue Oct 07 02:12:57 2008 +0000
@@ -10,7 +10,7 @@
 import fife, pythonize
 import tools
 from exceptions import *
-from attrs import Attr,PointAttr,ColorAttr,BoolAttr,IntAttr
+from attrs import Attr,PointAttr,ColorAttr,BoolAttr,IntAttr,FloatAttr
 
 def get_manager():
     import pychan
@@ -1581,6 +1581,103 @@
 	def __repr__(self):
 		return "<Spacer(parent.name='%s') at %x>" % (getattr(self._parent,'name','None'),id(self))
 
+class Slider(Widget):
+	""" A slider widget
+	
+	Use a callback to read out the slider value every time the marker
+	is moved.
+
+	New Attributes
+	==============
+
+	  - orientation: 1 = horizontal, 0=vertical
+	  - scale_start: float: default 0.0
+	  - scale_end: float: default 1.0
+	
+	FIXME:
+		- set new attributes for marker & step length, value
+		- update docstrings		
+	"""
+	
+	HORIZONTAL = fife.Slider.HORIZONTAL
+	VERTICAL = fife.Slider.VERTICAL
+	
+	ATTRIBUTES = Widget.ATTRIBUTES + [IntAttr('orientation'), FloatAttr('scale_start'), FloatAttr('scale_end')]
+	
+	def __init__(self, scaleStart=0.0, scaleEnd=1.0, orientation=HORIZONTAL, **kwargs):
+		self.real_widget = fife.Slider(scaleStart, scaleEnd)
+		self.orientation = orientation
+		self.setOrientation(self.orientation)
+		super(Slider, self).__init__(**kwargs)
+	
+	def _setScale(self, start, end):
+		"""setScale(self, double scaleStart, double scaleEnd)"""
+		if type(start) != float:
+			raise RuntimeError("Slider expects float for start scale")
+		if type(end) != float:
+			raise RuntimeError("Slider expects float for end scale")		
+		self.real_widget.setScale(start, end)
+	
+	def getScaleStart(self):
+		"""getScaleStart(self) -> double"""
+		return self.real_widget.getScaleStart()
+		
+	def setScaleStart(self, start):
+		"""setScaleStart(self, double scaleStart)"""
+		if type(start) != float:
+			raise RuntimeError("Slider expects float for start scale")
+		self.real_widget.setScaleStart(start)		
+	scale_start = property(getScaleStart, setScaleStart)
+
+	def getScaleEnd(self):
+		"""getScaleEnd(self) -> double"""
+		return self.real_widget.getScaleEnd()
+
+	def setScaleEnd(self, end):
+		"""setScaleEnd(self, double scaleEnd)"""
+		if type(end) != float:
+			raise RuntimeError("Slider expects float for end scale")
+		self.real_widget.setScaleEnd(end)
+	scale_end = property(getScaleEnd, setScaleEnd)
+
+	def getValue(self):
+		"""getValue(self) -> double"""
+		return self.real_widget.getValue()
+
+	def setValue(self, value):
+		"""setValue(self, double value)"""
+		if type(value) != float:
+			raise RuntimeError("Slider only accepts float values")
+		self.real_widget.setValue(value)
+
+	def setMarkerLength(self, length):
+		"""setMarkerLength(self, int length)"""
+		if type(length) != int:
+			raise RuntimeError("Slider only accepts int for Marker lenght")
+		self.real_widget.setMarkerLength(length)
+
+	def getMarkerLength(self):
+		"""getMarkerLength(self) -> int"""
+		return self.real_widget.getMarkerLength()
+
+	def setOrientation(self, orientation):
+		"""setOrientation(self, Orientation orientation)"""
+		self.real_widget.setOrientation(orientation)
+		
+	def getOrientation(self):
+		"""getOrientation(self) -> int"""
+		return self.real_widget.getOrientation()
+	orientation = property(getOrientation, setOrientation)
+
+	def setStepLength(self, length):
+		"""setStepLength(self, double length)"""
+		if type(length) != float:
+			raise RuntimeError("Slider only accepts floats for step length")
+		self.real_widget.setStepLength(length)
+
+	def getStepLength(self):
+		"""getStepLength(self) -> double"""
+		return self.real_widget.getStepLength()
 
 # Global Widget Class registry
 
@@ -1607,7 +1704,8 @@
 	"TextField" : TextField,
 	"TextBox" : TextBox,
 	"ListBox" : ListBox,
-	"DropDown" : DropDown
+	"DropDown" : DropDown,
+	"Slider" : Slider
 }
 
 def registerWidget(cls):