factored out the EFFv2 saving into EFFImporter
[gemrb.git] / gemrb / GUIScripts / bg1 / CharGenCommon.py
blob9c6aeed17c1924a68cbbe9bf65550844d68217e5
1 # GemRB - Infinity Engine Emulator
2 # Copyright (C) 2003 The GemRB Project
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 # common character generation display code
20 import GemRB
21 import GUICommon
22 #import GUIClasses
23 from ie_stats import *
24 from GUIDefines import *
26 class CharGen:
27 def __init__(self,stages,startText,importFn,resetButton=False):
28 """Sets up a character generation system.
29 slot: index of slot for which to generate
30 stages: List stages
31 startText: text to set info field in stage 0
32 importFn: function ran when import is pressed
33 resetButton: whether or not to show a reset button
35 where each stage is defined by either an intermediate screen:
36 name: name
37 control: id of the controle
38 text: id of button text or string to use
39 or a request stage
40 name: name
41 script | setFn: script name to proceed or function to request and set data for this stage
42 commentFn(area): function to append information to the text area
43 unsetFn: function to remove data for this stage
44 guard return wether or not to activate this stage
45 """
46 self.stages = stages
47 self.imp = importFn
48 self.showReset = resetButton
49 self.step = 0
50 self.window = None
51 self.startText=startText
53 def displayOverview(self):
54 """
55 Sets up the primary character generation window.
56 show all comments of previous stages
57 """
58 if(self.window):
59 CharGenWindow = self.window
60 else:
61 GemRB.LoadWindowPack ("GUICG", 640, 480)
62 CharGenWindow = GemRB.LoadWindow (0)
63 CharGenWindow.SetFrame ()
65 step = self.step
67 #set portrait
68 PortraitButton = CharGenWindow.GetControl (12)
69 PortraitButton.SetFlags(IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_NO_IMAGE,OP_SET)
70 PortraitName = GemRB.GetToken ("LargePortrait")
71 PortraitButton.SetPicture (PortraitName, "NOPORTMD")
72 PortraitButton.SetState(IE_GUI_BUTTON_LOCKED)
74 #set stage buttons
75 i = 0
76 for stage in self.stages:
77 if(len(stage) == 3): #short stage
78 (name,control,text) = stage
79 button = CharGenWindow.GetControl(control)
80 button.SetText(text);
81 if i == step:
82 button.SetState(IE_GUI_BUTTON_ENABLED)
83 button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
84 button.SetEvent (IE_GUI_BUTTON_ON_PRESS, NextPress)
85 else:
86 button.SetState(IE_GUI_BUTTON_DISABLED)
87 i = i + 1
89 #set back button
90 BackButton = CharGenWindow.GetControl (11)
91 #BackButton.SetText (15416)
92 if(self.step != 0):
93 BackButton.SetState (IE_GUI_BUTTON_ENABLED)
94 else:
95 BackButton.SetState(IE_GUI_BUTTON_DISABLED)
96 BackButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, BackPress)
97 BackButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
99 AcceptButton = CharGenWindow.GetControl (8)
100 playmode = GemRB.GetVar ("PlayMode")
101 if playmode>=0:
102 AcceptButton.SetText (11962)
103 else:
104 AcceptButton.SetText (13956)
106 #set scrollbar
107 ScrollBar = CharGenWindow.GetControl (10)
108 ScrollBar.SetDefaultScrollBar()
110 #set import
111 ImportButton = CharGenWindow.GetControl (13)
112 ImportButton.SetText (13955)
113 ImportButton.SetState (IE_GUI_BUTTON_ENABLED)
114 ImportButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ImportPress)
116 #set cancel and start over
117 CancelButton = CharGenWindow.GetControl (15)
118 if step == 0 or not self.showReset:
119 CancelButton.SetText (13727) # Cancel
120 else:
121 CancelButton.SetText (8159) # Start over
122 CancelButton.SetState (IE_GUI_BUTTON_ENABLED)
123 CancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, CancelPress)
125 #set and fill overview
126 TextAreaControl = CharGenWindow.GetControl (9)
127 if(self.step == 0):
128 TextAreaControl.SetText(self.startText)
129 else:
130 TextAreaControl.SetText("")
131 for part in range(step):
132 if(len(self.stages[part]) == 5):
133 (name,setFn,commentFn,unsetFn,guard) = self.stages[part]
134 if(commentFn != None):
135 commentFn(TextAreaControl)
137 #show
138 CharGenWindow.SetVisible(WINDOW_VISIBLE)
139 CharGenWindow.Invalidate()
140 self.window = CharGenWindow
142 def unset(self,stage):
143 #print "unset" , stage
144 if(len(self.stages[stage]) == 5):
145 (name,setFn,commentFn,unsetFn,guard) = self.stages[stage]
146 if(unsetFn != None):
147 unsetFn()
148 #set next script to for step, return false if the current step should be skipped
149 def setScript(self):
150 #print 'set',self.step
151 if(len(self.stages[self.step]) == 5): #long record: script of function
152 (name,setFn,commentFn,unsetFn,guardFn) = self.stages[self.step]
153 if(guardFn and not guardFn()):
154 return False
155 if(hasattr(setFn, "__call__")):
156 setFn()
157 else:
158 GemRB.SetNextScript(setFn)
159 else: #short record: overview
160 GemRB.SetNextScript ("CharGen")
161 return True
163 def cancel(self):
164 """Revert back to the first step; unset all actions."""
165 #if self.window:
166 # self.window.Unload()
167 #reset
168 for i in range(self.step,-1,-1):
169 self.unset(i)
171 # if required back to main screen, otherwise reloop
172 if self.step == 0 or not self.showReset:
173 self.window.Unload()
174 self.window = None
175 GemRB.SetNextScript ("Start")
176 else:
177 GemRB.SetNextScript ("CharGen")
179 self.step = 0
182 def imprt(self):
183 """Opens the character import window."""
184 self.imp()
186 def back(self):
187 """Moves to the previous step. Unsets last"""
188 GUICommon.CloseOtherWindow(None)
189 self.unset(self.step)
190 if len(self.stages[self.step]) == 3:
191 #short, return to other short
192 self.step = self.step - 1
193 self.unset(self.step)
194 while len(self.stages[self.step]) != 3:
195 self.step = self.step - 1
196 self.unset(self.step)
197 else:
198 self.step = self.step - 1
199 self.unset(self.step)
201 while(not self.setScript()):
202 self.step = self.step - 1
203 self.unset(self.step)
206 def next(self):
207 """Calls the next setter."""
208 GUICommon.CloseOtherWindow(None)
209 self.step = self.step + 1
210 while(not self.setScript()):
211 self.step = self.step + 1
213 def close(self):
214 if(self.window):
215 self.window.Unload()
217 def jumpTo(self,to):
218 if type(to) == str:
219 done = False
220 for i in range(len(self.stages)):
221 if(to == self.stages[i][0]):
222 self.step = i
223 done = True
224 if(not done):
225 raise ValueError("stage name not found: "+str(to))
226 elif type(to) == int:
227 if(to<0):
228 to = len(self.stages)+to
229 if(to<0 or to>=len(self.stages)):
230 raise ValueError("stage index not found: "+str(to))
231 self.step = to
233 return
234 else:
235 raise ValueError("bad arg type: "+str(type(to)) + " " + str(to))
236 GUICommon.CloseOtherWindow(None)
237 while(not self.setScript()):
238 self.step = self.step + 1
240 CharGenMaster = None
242 def CancelPress():
243 """Revert back to the first step; if there, free the actor."""
244 global CharGenMaster
245 CharGenMaster.cancel()
247 def ImportPress():
248 """Opens the character import window."""
249 global CharGenMaster
250 CharGenMaster.imprt()
252 def BackPress():
253 """Moves to the previous step."""
254 global CharGenMaster
255 print "back"
256 GemRB.SetRepeatClickFlags(GEM_RK_DISABLE, OP_OR)
257 CharGenMaster.back()
259 def NextPress():
260 """Moves to the next step."""
261 global CharGenMaster
262 CharGenMaster.next()
264 def back():
265 """Moves to the previous step."""
266 global CharGenMaster
267 CharGenMaster.back()
269 def next():
270 """Moves to the next step."""
271 global CharGenMaster
272 CharGenMaster.next()
274 def close():
275 """Terminates CharGen."""
276 global CharGenMaster
277 CharGenMaster.close()
278 CharGenMaster = None
280 def jumpTo(stage):
281 global CharGenMaster
282 CharGenMaster.jumpTo(stage)