Mercurial > fife-parpg
comparison demos/pychan_demo/pychan_demo.py @ 613:8c9cdcc9bc4f
Renaming pychan_test.py to pychan_demo.py to make it a bit more obvious which file to execute.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 24 Sep 2010 15:19:58 +0000 |
parents | demos/pychan_demo/pychan_test.py@427150724fe1 |
children |
comparison
equal
deleted
inserted
replaced
612:867aad1c01cd | 613:8c9cdcc9bc4f |
---|---|
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 fife_path = os.path.join('..','..','engine','python') | |
30 if os.path.isdir(fife_path) and fife_path not in sys.path: | |
31 sys.path.insert(0,fife_path) | |
32 | |
33 from fife import fife | |
34 print "Using the FIFE python module found here: ", os.path.dirname(fife.__file__) | |
35 | |
36 from fife.extensions import fifelog | |
37 from fife.extensions import basicapplication | |
38 from fife.extensions import pychan | |
39 from fife.extensions.pychan.dialogs import trace | |
40 | |
41 class PyChanExample(object): | |
42 """ | |
43 Example class. | |
44 """ | |
45 def __init__(self,xmlFile): | |
46 self.xmlFile = xmlFile | |
47 self.widget = None | |
48 | |
49 def start(self): | |
50 """ | |
51 The Example Protocoll: start | |
52 """ | |
53 # For simplicity the most basic examples should define | |
54 # a okButton and/or a closeButton. Those are mapped | |
55 # to the stop handler. | |
56 self.widget = pychan.loadXML(self.xmlFile) | |
57 eventMap = { | |
58 'closeButton':self.stop, | |
59 'okButton' :self.stop | |
60 } | |
61 # Since the basic example are not required to | |
62 # supply close and ok button, we 'ignoreMissing' | |
63 self.widget.mapEvents(eventMap, ignoreMissing = True) | |
64 self.widget.show() | |
65 | |
66 #from pprint import pprint | |
67 #pprint(self.widget.getNamedChildren()) | |
68 | |
69 def stop(self): | |
70 """ | |
71 The Example Protocoll: stop | |
72 """ | |
73 if self.widget: | |
74 self.widget.hide() | |
75 self.widget = None | |
76 | |
77 class TextSetter(object): | |
78 def __init__(self,text): | |
79 self.text = text | |
80 def __call__(self,widget): | |
81 widget.text = self.text | |
82 | |
83 class DemoApplication(basicapplication.ApplicationBase): | |
84 def __init__(self): | |
85 # Let the ApplicationBase initialise FIFE | |
86 super(DemoApplication,self).__init__() | |
87 | |
88 # Init Pychan | |
89 pychan.loadFonts("fonts/freefont.fontdef") | |
90 pychan.manager.setDefaultFont("FreeSans") | |
91 pychan.setupModalExecution(self.mainLoop,self.breakFromMainLoop) | |
92 | |
93 # Build the main GUI | |
94 self.gui = pychan.loadXML('gui/demoapp.xml') | |
95 self.gui.min_size = self.engine.getRenderBackend().getScreenWidth(),self.engine.getRenderBackend().getScreenHeight() | |
96 | |
97 eventMap = { | |
98 'creditsLink' : self.showCredits, | |
99 'closeButton' : self.quit, | |
100 'demoList' : self.selectExample, | |
101 } | |
102 self.gui.mapEvents(eventMap) | |
103 | |
104 # A simple hover-effect for the credits label | |
105 credits = self.gui.findChild(name="creditsLink") | |
106 # setEnterCallback is deprecated - we use it here to test it. | |
107 credits.setEnterCallback(TextSetter(u"CREDITS")) | |
108 # Note that we can't simply write: | |
109 # credits.capture(credits._setText(u"Credits"), event_name="mouseExited") | |
110 # that's because that would call credits._setText _NOW_ and we want to call | |
111 # it later. | |
112 credits.capture(lambda : credits._setText(u"Credits"), event_name="mouseExited") | |
113 | |
114 | |
115 # import example modules | |
116 from dynamic import DynamicExample | |
117 from styling import StylingExample | |
118 from sliders import SliderExample | |
119 from colortester import ColorExample | |
120 from poc_gui_animation import PocAnimations | |
121 | |
122 # Our list of examples | |
123 # We keep a dictionary of these and fill | |
124 # the ListBox on the left with its names. | |
125 self.examples = { | |
126 'Absolute Positioning' : PyChanExample('gui/absolute.xml'), | |
127 'All Widgets' : PyChanExample('gui/all_widgets.xml'), | |
128 'Basic Styling' : StylingExample(), | |
129 'Dynamic Widgets' : DynamicExample(), | |
130 'Sliders' : SliderExample(), | |
131 'ScrollArea' : PyChanExample('gui/scrollarea.xml'), | |
132 'Colortester': ColorExample(), | |
133 'GuiAnimations' : PocAnimations(), | |
134 } | |
135 self.demoList = self.gui.findChild(name='demoList') | |
136 self.demoList.items = sorted(self.examples.keys()) | |
137 | |
138 # Finally show the main GUI | |
139 self.gui.show() | |
140 | |
141 self.currentExample = None | |
142 self.creditsWidget = None | |
143 | |
144 # We use the trace decorator which can help debugging the examples. | |
145 # mostly it's for show though :-) | |
146 @trace | |
147 def selectExample(self): | |
148 """ | |
149 Callback handler for clicking on the example list. | |
150 """ | |
151 if self.demoList.selected_item is None: | |
152 return | |
153 #print "selected",self.demoList.selected_item | |
154 if self.currentExample: | |
155 self.currentExample.stop() | |
156 self.currentExample = self.examples[self.demoList.selected_item] | |
157 self.gui.findChild(name="xmlSource").text = unicode(open(self.currentExample.xmlFile).read(), 'utf8') | |
158 self.currentExample.start() | |
159 | |
160 def showCredits(self): | |
161 """ | |
162 Callback handler from the credits link/label. | |
163 """ | |
164 # We use PyChan's synchronous execution feature here. | |
165 pychan.loadXML('gui/credits.xml').execute({ 'okButton' : "Yay!" }) | |
166 | |
167 class TestXMLApplication(basicapplication.ApplicationBase): | |
168 """ | |
169 Test Application. Run the pychan_test.py file | |
170 with the XML file you want to load as argument. | |
171 """ | |
172 def __init__(self,xmlfile): | |
173 super(TestXMLApplication,self).__init__() | |
174 pychan.init(self.engine,debug=True) | |
175 self.start() | |
176 @trace | |
177 def start(self): | |
178 self.widget = pychan.loadXML(xmlfile) | |
179 self.widget.show() | |
180 | |
181 if __name__ == '__main__': | |
182 import sys | |
183 if len(sys.argv) == 2: | |
184 app = TestXMLApplication(sys.argv[1]) | |
185 else: | |
186 app = DemoApplication() | |
187 app.run() |