comparison demos/pychan_demo/pychan_test.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 cfc7c384603b
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # ####################################################################
5 # Copyright (C) 2005-2009 by the FIFE team
6 # http://www.fifengine.de
7 # This file is part of FIFE.
8 #
9 # FIFE is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13 #
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the
21 # Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 # ####################################################################
24
25 # This is the pychan demo client for FIFE.
26
27 import sys, os, re
28
29 from fife import fife
30 from fife.extensions import fifelog
31 from fife.extensions import basicapplication
32 from fife.extensions import pychan
33 from fife.extensions.pychan.dialogs import trace
34
35 class PyChanExample(object):
36 """
37 Example class.
38 """
39 def __init__(self,xmlFile):
40 self.xmlFile = xmlFile
41 self.widget = None
42
43 def start(self):
44 """
45 The Example Protocoll: start
46 """
47 # For simplicity the most basic examples should define
48 # a okButton and/or a closeButton. Those are mapped
49 # to the stop handler.
50 self.widget = pychan.loadXML(self.xmlFile)
51 eventMap = {
52 'closeButton':self.stop,
53 'okButton' :self.stop
54 }
55 # Since the basic example are not required to
56 # supply close and ok button, we 'ignoreMissing'
57 self.widget.mapEvents(eventMap, ignoreMissing = True)
58 self.widget.show()
59
60 #from pprint import pprint
61 #pprint(self.widget.getNamedChildren())
62
63 def stop(self):
64 """
65 The Example Protocoll: stop
66 """
67 if self.widget:
68 self.widget.hide()
69 self.widget = None
70
71 class TextSetter(object):
72 def __init__(self,text):
73 self.text = text
74 def __call__(self,widget):
75 widget.text = self.text
76
77 class DemoApplication(basicapplication.ApplicationBase):
78 def __init__(self):
79 # Let the ApplicationBase initialise FIFE
80 super(DemoApplication,self).__init__()
81
82 # Init Pychan
83 pychan.init(self.engine,debug=False)
84 pychan.loadFonts("fonts/freefont.fontdef")
85 pychan.manager.setDefaultFont("FreeSans")
86 pychan.setupModalExecution(self.mainLoop,self.breakFromMainLoop)
87
88 # Build the main GUI
89 self.gui = pychan.loadXML('gui/demoapp.xml')
90 self.gui.min_size = self.engine.getRenderBackend().getScreenWidth(),self.engine.getRenderBackend().getScreenHeight()
91
92 eventMap = {
93 'creditsLink' : self.showCredits,
94 'closeButton' : self.quit,
95 'demoList' : self.selectExample,
96 }
97 self.gui.mapEvents(eventMap)
98
99 # A simple hover-effect for the credits label
100 credits = self.gui.findChild(name="creditsLink")
101 # setEnterCallback is deprecated - we use it here to test it.
102 credits.setEnterCallback(TextSetter(u"CREDITS"))
103 # Note that we can't simply write:
104 # credits.capture(credits._setText(u"Credits"), event_name="mouseExited")
105 # that's because that would call credits._setText _NOW_ and we want to call
106 # it later.
107 credits.capture(lambda : credits._setText(u"Credits"), event_name="mouseExited")
108
109 # Our list of examples
110 # We keep a dictionary of these and fill
111 # the ListBox on the left with its names.
112 from dynamic import DynamicExample
113 from styling import StylingExample
114 from sliders import SliderExample
115 self.examples = {
116 'Absolute Positioning' : PyChanExample('gui/absolute.xml'),
117 'All Widgets' : PyChanExample('gui/all_widgets.xml'),
118 'Basic Styling' : StylingExample(),
119 'Dynamic Widgets' : DynamicExample(),
120 'Sliders' : SliderExample(),
121 'ScrollArea' : PyChanExample('gui/scrollarea.xml'),
122 }
123 self.demoList = self.gui.findChild(name='demoList')
124 self.demoList.items = sorted(self.examples.keys())
125
126 # Finally show the main GUI
127 self.gui.show()
128
129 self.currentExample = None
130 self.creditsWidget = None
131
132 # We use the trace decorator which can help debugging the examples.
133 # mostly it's for show though :-)
134 @trace
135 def selectExample(self):
136 """
137 Callback handler for clicking on the example list.
138 """
139 if self.demoList.selected_item is None:
140 return
141 #print "selected",self.demoList.selected_item
142 if self.currentExample:
143 self.currentExample.stop()
144 self.currentExample = self.examples[self.demoList.selected_item]
145 self.gui.findChild(name="xmlSource").text = unicode(open(self.currentExample.xmlFile).read(), 'utf8')
146 self.currentExample.start()
147
148 def showCredits(self):
149 """
150 Callback handler from the credits link/label.
151 """
152 # We use PyChan's synchronous execution feature here.
153 pychan.loadXML('gui/credits.xml').execute({ 'okButton' : "Yay!" })
154
155 class TestXMLApplication(basicapplication.ApplicationBase):
156 """
157 Test Application. Run the pychan_test.py file
158 with the XML file you want to load as argument.
159 """
160 def __init__(self,xmlfile):
161 super(TestXMLApplication,self).__init__()
162 pychan.init(self.engine,debug=True)
163 self.start()
164 @trace
165 def start(self):
166 self.widget = pychan.loadXML(xmlfile)
167 self.widget.show()
168
169 if __name__ == '__main__':
170 import sys
171 if len(sys.argv) == 2:
172 app = TestXMLApplication(sys.argv[1])
173 else:
174 app = DemoApplication()
175 app.run()