GUIScript: Rename SetTimedEvent to SetTimedEventByName.
[gemrb.git] / gemrb / GUIScripts / bg2 / GUIWORLD.py
blob3978e0376eb7ff53642487f0c1c826fdeb4d40f5
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 GUICommonWindows
28 import GUICommon
29 from GUIClasses import GWindow
31 FRAME_PC_SELECTED = 0
32 FRAME_PC_TARGET = 1
34 ContainerWindow = None
35 ContinueWindow = None
36 ReformPartyWindow = None
37 OldActionsWindow = None
38 OldMessageWindow = None
39 Container = None
41 def CloseContinueWindow ():
42 if ContinueWindow:
43 # don't close the actual window now to avoid flickering: we might still want it open
44 GemRB.SetVar ("DialogChoose", GemRB.GetVar ("DialogOption"))
46 def NextDialogState ():
47 global ContinueWindow, OldActionsWindow
49 if ContinueWindow == None:
50 return
52 hideflag = GemRB.HideGUI ()
54 if ContinueWindow:
55 ContinueWindow.Unload ()
56 GemRB.SetVar ("ActionsWindow", OldActionsWindow.ID)
57 ContinueWindow = None
58 OldActionsWindow = None
59 if hideflag:
60 GemRB.UnhideGUI ()
63 def OpenEndMessageWindow ():
64 global ContinueWindow, OldActionsWindow
66 hideflag = GemRB.HideGUI ()
68 if not ContinueWindow:
69 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
70 ContinueWindow = Window = GemRB.LoadWindow (9)
71 OldActionsWindow = GWindow( GemRB.GetVar ("ActionsWindow") )
72 GemRB.SetVar ("ActionsWindow", Window.ID)
74 #end dialog
75 Button = ContinueWindow.GetControl (0)
76 Button.SetText (9371)
77 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "CloseContinueWindow")
78 if GUICommonWindows.PortraitWindow:
79 GUICommonWindows.UpdatePortraitWindow ()
80 if hideflag:
81 GemRB.UnhideGUI ()
84 def OpenContinueMessageWindow ():
85 global ContinueWindow, OldActionsWindow
87 hideflag = GemRB.HideGUI ()
89 if not ContinueWindow:
90 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
91 ContinueWindow = Window = GemRB.LoadWindow (9)
92 OldActionsWindow = GWindow( GemRB.GetVar ("ActionsWindow") )
93 GemRB.SetVar ("ActionsWindow", Window.ID)
95 #continue
96 Button = ContinueWindow.GetControl (0)
97 Button.SetText (9372)
98 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "CloseContinueWindow")
99 if hideflag:
100 GemRB.UnhideGUI ()
103 def CloseContainerWindow ():
104 global OldActionsWindow, OldMessageWindow, ContainerWindow
106 if ContainerWindow == None:
107 return
109 hideflag = GemRB.HideGUI ()
111 if ContainerWindow:
112 ContainerWindow.Unload ()
113 ContainerWindow = None
114 GemRB.SetVar ("ActionsWindow", OldActionsWindow.ID)
115 GemRB.SetVar ("MessageWindow", OldMessageWindow.ID)
116 Table = GemRB.LoadTable ("containr")
117 row = Container['Type']
118 tmp = Table.GetValue (row, 2)
119 #play closing sound if applicable
120 if tmp!='*':
121 GemRB.PlaySound (tmp)
123 #it is enough to close here
125 if hideflag:
126 GemRB.UnhideGUI ()
129 def UpdateContainerWindow ():
130 global Container
132 Window = ContainerWindow
134 pc = GemRB.GameGetFirstSelectedPC ()
135 SetEncumbranceLabels( Window, 0x10000043, 0x10000044, pc)
137 party_gold = GemRB.GameGetPartyGold ()
138 Text = Window.GetControl (0x10000036)
139 Text.SetText (str (party_gold))
141 Container = GemRB.GetContainer(0) #will use first selected pc anyway
142 LeftCount = Container['ItemCount']
143 ScrollBar = Window.GetControl (52)
144 Count = LeftCount/3
145 if Count<1:
146 Count=1
147 ScrollBar.SetVarAssoc ("LeftTopIndex", Count)
149 inventory_slots = GemRB.GetSlots (pc, 0x8000)
150 RightCount = len(inventory_slots)
151 ScrollBar = Window.GetControl (53)
152 Count = RightCount/2
153 if Count<1:
154 Count=1
155 ScrollBar.SetVarAssoc ("RightTopIndex", Count)
157 RedrawContainerWindow ()
160 def RedrawContainerWindow ():
161 Window = ContainerWindow
163 LeftTopIndex = GemRB.GetVar ("LeftTopIndex") * 3
164 LeftIndex = GemRB.GetVar ("LeftIndex")
165 RightTopIndex = GemRB.GetVar ("RightTopIndex") * 2
166 RightIndex = GemRB.GetVar ("RightIndex")
167 LeftCount = Container['ItemCount']
168 pc = GemRB.GameGetFirstSelectedPC ()
169 inventory_slots = GemRB.GetSlots (pc, 0x8000)
170 RightCount = len(inventory_slots)
172 for i in range (6):
173 #this is an autoselected container, but we could use PC too
174 Slot = GemRB.GetContainerItem (0, i+LeftTopIndex)
175 Button = Window.GetControl (i)
176 if Slot != None:
177 Button.SetVarAssoc ("LeftIndex", LeftTopIndex+i)
178 else:
179 Button.SetVarAssoc ("LeftIndex", -1)
180 GUICommon.UpdateInventorySlot (pc, Button, Slot, "container")
182 for i in range (4):
183 if i+RightTopIndex<RightCount:
184 Slot = GemRB.GetSlotItem (pc, inventory_slots[i+RightTopIndex])
185 else:
186 Slot = None
187 Button = Window.GetControl (i+10)
188 if Slot!=None:
189 Button.SetVarAssoc ("RightIndex", RightTopIndex+i)
190 else:
191 Button.SetVarAssoc ("RightIndex", -1)
192 GUICommon.UpdateInventorySlot (pc, Button, Slot, "inventory")
195 def OpenContainerWindow ():
196 global OldActionsWindow, OldMessageWindow
197 global ContainerWindow, Container
199 if ContainerWindow:
200 return
202 hideflag = GemRB.HideGUI ()
204 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
205 ContainerWindow = Window = GemRB.LoadWindow (8)
206 OldActionsWindow = GWindow( GemRB.GetVar ("ActionsWindow") )
207 OldMessageWindow = GWindow( GemRB.GetVar ("MessageWindow") )
208 GemRB.SetVar ("ActionsWindow", Window.ID)
209 GemRB.SetVar ("MessageWindow", -1)
211 Container = GemRB.GetContainer(0)
213 # Gears (time) when options pane is down
214 Button = Window.GetControl (62)
215 Label = Button.CreateLabelOnButton (0x1000003e, "NORMAL", 0)
217 Label.SetAnimation ("CPEN")
218 Button.SetAnimation ("CGEAR")
219 Button.SetBAM ("CDIAL", 0, 0)
220 Button.SetState (IE_GUI_BUTTON_ENABLED)
221 Button.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_ANIMATED|IE_GUI_BUTTON_NORMAL, OP_SET)
222 Button.SetEventByName(IE_GUI_BUTTON_ON_PRESS, "GearsClicked")
223 GUICommon.SetGamedaysAndHourToken()
224 Button.SetTooltip(16041)
226 # 0 - 5 - Ground Item
227 # 10 - 13 - Personal Item
228 # 50 hand
229 # 52, 53 scroller ground, scroller personal
230 # 54 - encumbrance
232 for i in range (6):
233 Button = Window.GetControl (i)
234 Button.SetVarAssoc ("LeftIndex", i)
235 #Button.SetFlags (IE_GUI_BUTTON_CHECKBOX, OP_OR)
236 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "TakeItemContainer")
238 for i in range (4):
239 Button = Window.GetControl (i+10)
240 Button.SetVarAssoc ("RightIndex", i)
241 #Button.SetFlags (IE_GUI_BUTTON_CHECKBOX, OP_OR)
242 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "DropItemContainer")
244 # left scrollbar
245 ScrollBar = Window.GetControl (52)
246 ScrollBar.SetEventByName (IE_GUI_SCROLLBAR_ON_CHANGE, "RedrawContainerWindow")
248 # right scrollbar
249 ScrollBar = Window.GetControl (53)
250 ScrollBar.SetEventByName (IE_GUI_SCROLLBAR_ON_CHANGE, "RedrawContainerWindow")
252 Label = Window.CreateLabel (0x10000043, 323,14,60,15,"NUMBER","0:",IE_FONT_ALIGN_LEFT|IE_FONT_ALIGN_TOP)
253 Label = Window.CreateLabel (0x10000044, 323,20,80,15,"NUMBER","0:",IE_FONT_ALIGN_RIGHT|IE_FONT_ALIGN_TOP)
255 Button = Window.GetControl (50)
256 Button.SetState (IE_GUI_BUTTON_LOCKED)
257 Table = GemRB.LoadTable ("containr")
258 row = Container['Type']
259 tmp = Table.GetValue (row, 0)
260 if tmp!='*':
261 GemRB.PlaySound (tmp)
262 tmp = Table.GetValue (row, 1)
263 if tmp!='*':
264 Button.SetSprites (tmp, 0, 0, 0, 0, 0 )
266 # Done
267 Button = Window.GetControl (51)
268 Button.SetText (1403)
269 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "LeaveContainer")
271 GemRB.SetVar ("LeftTopIndex", 0)
272 GemRB.SetVar ("RightTopIndex", 0)
273 UpdateContainerWindow ()
274 if hideflag:
275 GemRB.UnhideGUI ()
278 #doing this way it will inform the core system too, which in turn will call
279 #CloseContainerWindow ()
280 def LeaveContainer ():
281 GemRB.LeaveContainer()
283 def DropItemContainer ():
284 RightIndex = GemRB.GetVar ("RightIndex")
285 if RightIndex<0:
286 return
288 #we need to get the right slot number
289 pc = GemRB.GameGetFirstSelectedPC ()
290 inventory_slots = GemRB.GetSlots (pc, 0x8000)
291 if RightIndex >= len(inventory_slots):
292 return
293 GemRB.ChangeContainerItem (0, inventory_slots[RightIndex], 0)
294 UpdateContainerWindow ()
297 def TakeItemContainer ():
298 LeftIndex = GemRB.GetVar ("LeftIndex")
299 if LeftIndex<0:
300 return
302 if LeftIndex >= Container['ItemCount']:
303 return
304 GemRB.ChangeContainerItem (0, LeftIndex, 1)
305 UpdateContainerWindow ()
308 def UpdateReformWindow ():
309 Window = ReformPartyWindow
311 select = GemRB.GetVar ("Selected")
313 need_to_drop = GemRB.GetPartySize ()-PARTY_SIZE
314 if need_to_drop<0:
315 need_to_drop = 0
317 #excess player number
318 Label = Window.GetControl (0x1000000f)
319 Label.SetText (str(need_to_drop) )
321 #done
322 Button = Window.GetControl (8)
323 if need_to_drop:
324 Button.SetState (IE_GUI_BUTTON_DISABLED)
325 else:
326 Button.SetState (IE_GUI_BUTTON_ENABLED)
328 #remove
329 Button = Window.GetControl (15)
330 if select:
331 Button.SetState (IE_GUI_BUTTON_ENABLED)
332 else:
333 Button.SetState (IE_GUI_BUTTON_DISABLED)
335 for i in range (PARTY_SIZE+1):
336 Button = Window.GetControl (i)
337 Button.EnableBorder (FRAME_PC_SELECTED, select == i+2 )
338 #+2 because protagonist is skipped
339 pic = GemRB.GetPlayerPortrait (i+2,1)
340 if not pic:
341 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
342 Button.SetState (IE_GUI_BUTTON_LOCKED)
343 continue
345 Button.SetState (IE_GUI_BUTTON_ENABLED)
346 Button.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_ALIGN_BOTTOM|IE_GUI_BUTTON_ALIGN_LEFT, OP_SET)
347 Button.SetPicture (pic, "NOPORTSM")
348 GUICommonWindows.UpdatePortraitWindow ()
349 return
351 def RemovePlayer ():
352 global ReformPartyWindow
354 hideflag = GemRB.HideGUI ()
356 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
357 if ReformPartyWindow:
358 ReformPartyWindow.Unload ()
359 ReformPartyWindow = Window = GemRB.LoadWindow (25)
360 GemRB.SetVar ("OtherWindow", Window.ID)
362 #are you sure
363 Label = Window.GetControl (0x0fffffff)
364 Label.SetText (17518)
366 #confirm
367 Button = Window.GetControl (1)
368 Button.SetText (17507)
369 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "RemovePlayerConfirm")
370 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
372 #cancel
373 Button = Window.GetControl (2)
374 Button.SetText (13727)
375 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "RemovePlayerCancel")
376 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
378 GemRB.SetVar ("OtherWindow", Window.ID)
379 GemRB.SetVar ("ActionsWindow", -1)
380 if hideflag:
381 GemRB.UnhideGUI ()
382 Window.ShowModal (MODAL_SHADOW_GRAY)
383 return
385 def RemovePlayerConfirm ():
386 slot = GemRB.GetVar ("Selected")
387 GemRB.LeaveParty (slot, 2)
388 OpenReformPartyWindow ()
389 return
391 def RemovePlayerCancel ():
392 #Once for getting rid of the confirmation window
393 OpenReformPartyWindow ()
394 #and once for reopening the reform party window
395 OpenReformPartyWindow ()
396 return
398 def OpenReformPartyWindow ():
399 global ReformPartyWindow, OldActionsWindow, OldMessageWindow
401 GemRB.SetVar ("Selected", 0)
402 hideflag = GemRB.HideGUI ()
404 if ReformPartyWindow:
405 if ReformPartyWindow:
406 ReformPartyWindow.Unload ()
408 GemRB.SetVar ("ActionsWindow", OldActionsWindow.ID)
409 GemRB.SetVar ("MessageWindow", OldMessageWindow.ID)
410 GemRB.SetVar ("OtherWindow", -1)
412 OldActionsWindow = None
413 OldMessageWindow = None
414 ReformPartyWindow = None
415 if hideflag:
416 GemRB.UnhideGUI ()
417 #re-enabling party size control
418 GemRB.GameSetPartySize (PARTY_SIZE)
419 GUICommonWindows.UpdatePortraitWindow()
420 return
422 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
423 ReformPartyWindow = Window = GemRB.LoadWindow (24)
424 GemRB.SetVar ("OtherWindow", Window.ID)
426 #PC portraits
427 for j in range (PARTY_SIZE+1):
428 Button = Window.GetControl (j)
429 Button.SetState (IE_GUI_BUTTON_LOCKED)
430 Button.SetFlags (IE_GUI_BUTTON_RADIOBUTTON|IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE,OP_SET)
431 Button.SetBorder (FRAME_PC_SELECTED, 1, 1, 2, 2, 0, 255, 0, 255)
432 #protagonist is skipped
433 index = j + 2
434 Button.SetVarAssoc ("Selected", index)
435 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "UpdateReformWindow")
437 # Remove
438 Button = Window.GetControl (15)
439 Button.SetText (17507)
440 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "RemovePlayer")
442 # Done
443 Button = Window.GetControl (8)
444 Button.SetText (11973)
445 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OpenReformPartyWindow")
447 OldActionsWindow = GWindow( GemRB.GetVar ("ActionsWindow") )
448 OldMessageWindow = GWindow( GemRB.GetVar ("MessageWindow") )
449 GemRB.SetVar ("ActionsWindow", -1)
450 GemRB.SetVar ("MessageWindow", -1)
451 UpdateReformWindow ()
452 if hideflag:
453 GemRB.UnhideGUI ()
454 Window.ShowModal (MODAL_SHADOW_GRAY)
455 return
457 def DeathWindow ():
458 GemRB.HideGUI ()
459 GemRB.SetTimedEventByName ("DeathWindowEnd", 10)
460 return
462 def DeathWindowEnd ():
463 #playing death movie before continuing
464 GemRB.PlayMovie ("deathand",1)
465 GemRB.GamePause (1,1)
467 GemRB.LoadWindowPack (GUICommon.GetWindowPack())
468 Window = GemRB.LoadWindow (17)
470 #reason for death
471 Label = Window.GetControl (0x0fffffff)
472 Label.SetText (16498)
474 #load
475 Button = Window.GetControl (1)
476 Button.SetText (15590)
477 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "LoadPress")
479 #quit
480 Button = Window.GetControl (2)
481 Button.SetText (15417)
482 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "QuitPress")
484 GemRB.HideGUI ()
485 GemRB.SetVar ("MessageWindow", -1)
486 GemRB.UnhideGUI ()
487 Window.ShowModal (MODAL_SHADOW_GRAY)
488 return
490 def QuitPress():
491 GemRB.QuitGame ()
492 GemRB.SetNextScript ("Start")
493 return
495 def LoadPress():
496 GemRB.QuitGame ()
497 GemRB.SetNextScript ("GUILOAD")
498 return