TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / GUIScripts / bg2 / GUIWORLD.py
blob065d6a99083da6866f2a85b785446bbf472bc995
1 # -*-python-*-
2 # GemRB - Infinity Engine Emulator
3 # Copyright (C) 2003 The GemRB Project
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 # GUIW.py - scripts to control some windows from the GUIWORLD winpack
22 # except of Actions, Portrait, Options and Dialog windows
23 #################################################################
25 import GemRB
26 from GUIDefines import *
27 import GUICommon
28 import GUICommonWindows
29 import GUIClasses
30 from ie_stats import *
32 FRAME_PC_SELECTED = 0
33 FRAME_PC_TARGET = 1
35 ContinueWindow = None
36 ReformPartyWindow = None
37 OldActionsWindow = None
38 OldMessageWindow = None
40 removable_pcs = []
42 def CloseContinueWindow ():
43 if ContinueWindow:
44 # don't close the actual window now to avoid flickering: we might still want it open
45 GemRB.SetVar ("DialogChoose", GemRB.GetVar ("DialogOption"))
47 def NextDialogState ():
48 global ContinueWindow, OldActionsWindow
50 if ContinueWindow == None:
51 return
53 hideflag = GemRB.HideGUI ()
55 if ContinueWindow:
56 ContinueWindow.Unload ()
57 GemRB.SetVar ("ActionsWindow", OldActionsWindow.ID)
58 ContinueWindow = None
59 OldActionsWindow = None
60 if hideflag:
61 GemRB.UnhideGUI ()
64 def OpenEndMessageWindow ():
65 global ContinueWindow, OldActionsWindow
67 hideflag = GemRB.HideGUI ()
69 if not ContinueWindow:
70 # try to force-close anything which is open
71 GUICommon.CloseOtherWindow(None)
73 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
74 ContinueWindow = Window = GemRB.LoadWindow (9)
75 OldActionsWindow = GUIClasses.GWindow( GemRB.GetVar ("ActionsWindow") )
76 GemRB.SetVar ("ActionsWindow", Window.ID)
78 #end dialog
79 Button = ContinueWindow.GetControl (0)
80 Button.SetText (9371)
81 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseContinueWindow)
82 if GUICommonWindows.PortraitWindow:
83 GUICommonWindows.UpdatePortraitWindow ()
84 if hideflag:
85 GemRB.UnhideGUI ()
88 def OpenContinueMessageWindow ():
89 global ContinueWindow, OldActionsWindow
91 hideflag = GemRB.HideGUI ()
93 if not ContinueWindow:
94 # try to force-close anything which is open
95 GUICommon.CloseOtherWindow(None)
97 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
98 ContinueWindow = Window = GemRB.LoadWindow (9)
99 OldActionsWindow = GUIClasses.GWindow( GemRB.GetVar ("ActionsWindow") )
100 GemRB.SetVar ("ActionsWindow", Window.ID)
102 #continue
103 Button = ContinueWindow.GetControl (0)
104 Button.SetText (9372)
105 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseContinueWindow)
106 if hideflag:
107 GemRB.UnhideGUI ()
109 def UpdateReformWindow ():
110 Window = ReformPartyWindow
112 select = GemRB.GetVar ("Selected")
114 need_to_drop = GemRB.GetPartySize ()-PARTY_SIZE
115 if need_to_drop<0:
116 need_to_drop = 0
118 #excess player number
119 Label = Window.GetControl (0x1000000f)
120 Label.SetText (str(need_to_drop) )
122 #done
123 Button = Window.GetControl (8)
124 if need_to_drop:
125 Button.SetState (IE_GUI_BUTTON_DISABLED)
126 else:
127 Button.SetState (IE_GUI_BUTTON_ENABLED)
129 #remove
130 Button = Window.GetControl (15)
131 if select:
132 Button.SetState (IE_GUI_BUTTON_ENABLED)
133 else:
134 Button.SetState (IE_GUI_BUTTON_DISABLED)
136 for i in range (PARTY_SIZE+1):
137 Button = Window.GetControl (i)
138 if i+1 not in removable_pcs:
139 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
140 Button.SetState (IE_GUI_BUTTON_LOCKED)
141 continue
143 for i in removable_pcs:
144 Button = Window.GetControl (removable_pcs.index(i))
145 Button.EnableBorder (FRAME_PC_SELECTED, select == i )
146 pic = GemRB.GetPlayerPortrait (i, 1)
147 if not pic:
148 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
149 Button.SetState (IE_GUI_BUTTON_LOCKED)
150 continue
151 Button.SetState (IE_GUI_BUTTON_ENABLED)
152 Button.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_ALIGN_BOTTOM|IE_GUI_BUTTON_ALIGN_LEFT, OP_SET)
153 Button.SetPicture (pic, "NOPORTSM")
154 GUICommonWindows.UpdatePortraitWindow ()
155 return
157 def RemovePlayer ():
158 global ReformPartyWindow
160 hideflag = GemRB.HideGUI ()
162 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
163 if ReformPartyWindow:
164 ReformPartyWindow.Unload ()
165 ReformPartyWindow = Window = GemRB.LoadWindow (25)
166 GemRB.SetVar ("OtherWindow", Window.ID)
168 #are you sure
169 Label = Window.GetControl (0x0fffffff)
170 Label.SetText (17518)
172 #confirm
173 Button = Window.GetControl (1)
174 Button.SetText (17507)
175 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, RemovePlayerConfirm)
176 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
178 #cancel
179 Button = Window.GetControl (2)
180 Button.SetText (13727)
181 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, RemovePlayerCancel)
182 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
184 GemRB.SetVar ("OtherWindow", Window.ID)
185 GemRB.SetVar ("ActionsWindow", -1)
186 if hideflag:
187 GemRB.UnhideGUI ()
188 Window.ShowModal (MODAL_SHADOW_GRAY)
189 return
191 def RemovePlayerConfirm ():
192 slot = GemRB.GetVar ("Selected")
193 GemRB.LeaveParty (slot, 2)
194 OpenReformPartyWindow ()
195 return
197 def RemovePlayerCancel ():
198 #Once for getting rid of the confirmation window
199 OpenReformPartyWindow ()
200 #and once for reopening the reform party window
201 OpenReformPartyWindow ()
202 return
204 def OpenReformPartyWindow ():
205 global ReformPartyWindow, OldActionsWindow, OldMessageWindow
206 global removable_pcs
208 GemRB.SetVar ("Selected", 0)
209 hideflag = GemRB.HideGUI ()
211 if ReformPartyWindow:
212 if ReformPartyWindow:
213 ReformPartyWindow.Unload ()
215 GemRB.SetVar ("ActionsWindow", OldActionsWindow.ID)
216 GemRB.SetVar ("MessageWindow", OldMessageWindow.ID)
217 GemRB.SetVar ("OtherWindow", -1)
219 OldActionsWindow = None
220 OldMessageWindow = None
221 ReformPartyWindow = None
222 if hideflag:
223 GemRB.UnhideGUI ()
224 #re-enabling party size control
225 GemRB.GameSetPartySize (PARTY_SIZE)
226 GUICommonWindows.UpdatePortraitWindow()
227 return
229 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
230 ReformPartyWindow = Window = GemRB.LoadWindow (24)
231 GemRB.SetVar ("OtherWindow", Window.ID)
233 # skip exportable party members (usually only the protagonist)
234 removable_pcs = []
235 for i in range (1, GemRB.GetPartySize()+1):
236 if not GemRB.GetPlayerStat (i, IE_MC_FLAGS)&MC_EXPORTABLE:
237 removable_pcs.append(i)
239 #PC portraits
240 for j in range (PARTY_SIZE+1):
241 Button = Window.GetControl (j)
242 Button.SetState (IE_GUI_BUTTON_LOCKED)
243 Button.SetFlags (IE_GUI_BUTTON_RADIOBUTTON|IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE,OP_SET)
244 Button.SetBorder (FRAME_PC_SELECTED, 1, 1, 2, 2, 0, 255, 0, 255)
245 if j < len(removable_pcs):
246 Button.SetVarAssoc ("Selected", removable_pcs[j])
247 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, UpdateReformWindow)
249 # Remove
250 Button = Window.GetControl (15)
251 Button.SetText (17507)
252 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, RemovePlayer)
254 # Done
255 Button = Window.GetControl (8)
256 Button.SetText (11973)
257 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenReformPartyWindow)
259 OldActionsWindow = GUIClasses.GWindow( GemRB.GetVar ("ActionsWindow") )
260 OldMessageWindow = GUIClasses.GWindow( GemRB.GetVar ("MessageWindow") )
261 GemRB.SetVar ("ActionsWindow", -1)
262 GemRB.SetVar ("MessageWindow", -1)
264 # if nobody can be removed, just close the window
265 if not removable_pcs:
266 OpenReformPartyWindow ()
267 if hideflag:
268 GemRB.UnhideGUI ()
269 return
271 UpdateReformWindow ()
272 if hideflag:
273 GemRB.UnhideGUI ()
274 Window.ShowModal (MODAL_SHADOW_GRAY)
275 return
277 def DeathWindow ():
278 GemRB.HideGUI ()
279 GemRB.SetTimedEvent (DeathWindowEnd, 10)
280 return
282 def DeathWindowEnd ():
283 #playing death movie before continuing
284 GemRB.PlayMovie ("deathand",1)
285 GemRB.GamePause (1,1)
287 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
288 Window = GemRB.LoadWindow (17)
290 #reason for death
291 Label = Window.GetControl (0x0fffffff)
292 Label.SetText (16498)
294 #load
295 Button = Window.GetControl (1)
296 Button.SetText (15590)
297 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, LoadPress)
299 #quit
300 Button = Window.GetControl (2)
301 Button.SetText (15417)
302 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, QuitPress)
304 GemRB.HideGUI ()
305 GemRB.SetVar ("MessageWindow", -1)
306 GemRB.UnhideGUI ()
307 Window.ShowModal (MODAL_SHADOW_GRAY)
308 return
310 def QuitPress():
311 GemRB.QuitGame ()
312 GemRB.SetNextScript ("Start")
313 return
315 def LoadPress():
316 GemRB.QuitGame ()
317 GemRB.SetNextScript ("GUILOAD")
318 return