GUIScript: Use None instead if "", for null event handler.
[gemrb.git] / gemrb / GUIScripts / pst / GUIWORLD.py
blob8fd12e69dc429497a9fe7d834f60eb01e79067b6
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 # GUIWORLD.py - scripts to control some windows from GUIWORLD winpack
22 # except of Actions, Portrait, Options and Dialog windows
24 ###################################################
26 import GemRB
27 from GUIDefines import *
28 from GUICommon import CloseOtherWindow, SetEncumbranceLabels, GetWindowPack
29 from GUICommonWindows import EnableAnimatedWindows, DisableAnimatedWindows, \
30 SetItemButton, OpenWaitForDiscWindow
31 from GUIClasses import GWindow
33 ContainerWindow = None
34 FormationWindow = None
35 ReformPartyWindow = None
37 Container = None
39 def CloseContinueWindow ():
40 GemRB.SetVar ("DialogChoose", GemRB.GetVar ("DialogOption"))
41 Window = GWindow(GemRB.GetVar ("MessageWindow"))
42 Button = Window.GetControl (0)
43 Button.SetText(28082)
44 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OnDecreaseSize")
46 def NextDialogState ():
47 pass
49 def OpenEndMessageWindow ():
50 Window = GWindow(GemRB.GetVar ("MessageWindow"))
51 Button = Window.GetControl (0)
52 Button.SetText (34602)
53 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "CloseContinueWindow")
56 def OpenContinueMessageWindow ():
57 Window = GWindow(GemRB.GetVar ("MessageWindow"))
58 Button = Window.GetControl (0)
59 Button.SetText (34603)
60 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "CloseContinueWindow")
63 def OpenContainerWindow ():
64 global ContainerWindow, Container
66 if ContainerWindow:
67 return
69 hideflag = GemRB.HideGUI ()
71 GemRB.LoadWindowPack (GetWindowPack())
72 ContainerWindow = Window = GemRB.LoadWindow (8)
73 DisableAnimatedWindows ()
74 GemRB.SetVar ("ActionsWindow", Window.ID)
76 Container = GemRB.GetContainer(0)
78 # 0 - 5 - Ground Item
79 # 10 - 13 - Personal Item
80 # 50 hand
81 # 52, 53 scroller ground, scroller personal
82 # 54 - encumbrance
83 # 0x10000036 - label gold
85 for i in range (6):
86 Button = Window.GetControl (i)
87 Button.SetFlags (IE_GUI_BUTTON_ALIGN_RIGHT | IE_GUI_BUTTON_ALIGN_BOTTOM, OP_OR)
88 #Button.SetFont ('NUMBER')
90 for i in range (4):
91 Button = Window.GetControl (10 + i)
92 Button.SetFlags (IE_GUI_BUTTON_ALIGN_RIGHT | IE_GUI_BUTTON_ALIGN_BOTTOM, OP_OR)
93 #Button.SetFont ('NUMBER')
96 Button = Window.GetControl (50)
97 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
99 Button = Window.GetControl (54)
100 Button.SetFont ("NUMBER")
101 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
103 party_gold = GemRB.GameGetPartyGold ()
104 Text = Window.GetControl (0x10000036)
105 Text.SetText (str (party_gold))
108 Count = 1
110 # Ground items scrollbar
111 ScrollBar = Window.GetControl (52)
112 ScrollBar.SetEventByName(IE_GUI_SCROLLBAR_ON_CHANGE, "RedrawContainerWindow")
113 GemRB.SetVar ("LeftTopIndex", 0)
114 ScrollBar.SetVarAssoc ("LeftTopIndex", Count)
116 # Personal items scrollbar
117 ScrollBar = Window.GetControl (53)
118 ScrollBar.SetEventByName(IE_GUI_SCROLLBAR_ON_CHANGE, "RedrawContainerWindow")
119 GemRB.SetVar ("RightTopIndex", 0)
120 ScrollBar.SetVarAssoc ("RightTopIndex", Count)
123 # Done
124 Button = Window.GetControl (51)
125 Button.SetText (1403)
126 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "LeaveContainer")
128 UpdateContainerWindow ()
130 if hideflag:
131 GemRB.UnhideGUI ()
134 def UpdateContainerWindow ():
135 global Container
137 Window = ContainerWindow
139 pc = GemRB.GameGetFirstSelectedPC ()
140 SetEncumbranceLabels (Window, 54, None, pc)
142 party_gold = GemRB.GameGetPartyGold ()
143 Text = Window.GetControl (0x10000036)
144 Text.SetText (str (party_gold))
146 Container = GemRB.GetContainer (0) #will use first selected pc anyway
147 LeftCount = Container['ItemCount']
148 ScrollBar = Window.GetControl (52)
149 Count = LeftCount / 3
150 if Count < 1:
151 Count = 1
152 ScrollBar.SetVarAssoc ("LeftTopIndex", Count)
154 inventory_slots = GemRB.GetSlots (pc, 0x8000)
155 RightCount = len (inventory_slots)
156 ScrollBar = Window.GetControl (53)
157 Count = RightCount / 2
158 if Count < 1:
159 Count = 1
160 ScrollBar.SetVarAssoc ("RightTopIndex", Count)
162 RedrawContainerWindow ()
165 def RedrawContainerWindow ():
166 Window = ContainerWindow
168 LeftTopIndex = GemRB.GetVar ("LeftTopIndex") * 3
169 LeftIndex = GemRB.GetVar ("LeftIndex")
170 RightTopIndex = GemRB.GetVar ("RightTopIndex") * 2
171 RightIndex = GemRB.GetVar ("RightIndex")
172 LeftCount = Container['ItemCount']
173 pc = GemRB.GameGetFirstSelectedPC ()
174 inventory_slots = GemRB.GetSlots (pc, 0x8000)
175 RightCount = len (inventory_slots)
177 for i in range (6):
178 #this is an autoselected container, but we could use PC too
179 Slot = GemRB.GetContainerItem (0, i + LeftTopIndex)
180 Button = Window.GetControl (i)
183 if Slot != None:
184 SetItemButton (Window, Button, Slot, 'TakeItemContainer', '')
185 Button.SetVarAssoc ("LeftIndex", LeftTopIndex+i)
186 else:
187 SetItemButton (Window, Button, Slot, '', '')
188 Button.SetVarAssoc ("LeftIndex", -1)
191 for i in range(4):
192 if i + RightTopIndex < RightCount:
193 Slot = GemRB.GetSlotItem (pc, inventory_slots[i+RightTopIndex])
194 else:
195 Slot = None
196 Button = Window.GetControl (i + 10)
198 SetItemButton (Window, Button, Slot, '', '')
200 if Slot != None:
201 SetItemButton (Window, Button, Slot, 'DropItemContainer', '')
202 Button.SetVarAssoc ("RightIndex", RightTopIndex+i)
203 else:
204 SetItemButton (Window, Button, Slot, '', '')
205 Button.SetVarAssoc ("RightIndex", -1)
209 def CloseContainerWindow ():
210 global ContainerWindow
212 if ContainerWindow == None:
213 return
215 hideflag = GemRB.HideGUI ()
217 if ContainerWindow:
218 ContainerWindow.Unload ()
219 ContainerWindow = None
220 EnableAnimatedWindows ()
222 if hideflag:
223 GemRB.UnhideGUI ()
226 #doing this way it will inform the core system too, which in turn will call
227 #CloseContainerWindow ()
228 def LeaveContainer ():
229 GemRB.LeaveContainer()
231 def DropItemContainer ():
232 RightIndex = GemRB.GetVar ("RightIndex")
233 if RightIndex < 0:
234 return
236 #we need to get the right slot number
237 pc = GemRB.GameGetFirstSelectedPC ()
238 inventory_slots = GemRB.GetSlots (pc, 0x8000)
239 if RightIndex >= len (inventory_slots):
240 return
242 GemRB.ChangeContainerItem (0, inventory_slots[RightIndex], 0)
243 UpdateContainerWindow ()
246 def TakeItemContainer ():
247 LeftIndex = GemRB.GetVar ("LeftIndex")
248 if LeftIndex < 0:
249 return
251 if LeftIndex >= Container['ItemCount']:
252 return
254 GemRB.ChangeContainerItem (0, LeftIndex, 1)
255 UpdateContainerWindow ()
258 def OpenReformPartyWindow ():
259 global ReformPartyWindow
261 if CloseOtherWindow(OpenReformPartyWindow):
262 GemRB.HideGUI ()
263 if ReformPartyWindow:
264 ReformPartyWindow.Unload ()
265 ReformPartyWindow = None
267 GemRB.SetVar ("OtherWindow", -1)
268 EnableAnimatedWindows ()
269 GemRB.LoadWindowPack ("GUIREC")
270 GemRB.UnhideGUI ()
271 return
273 GemRB.HideGUI ()
274 GemRB.LoadWindowPack (GetWindowPack())
275 ReformPartyWindow = Window = GemRB.LoadWindow (24)
276 GemRB.SetVar ("OtherWindow", Window.ID)
277 DisableAnimatedWindows ()
279 # Remove
280 Button = Window.GetControl (15)
281 Button.SetText (42514)
282 Button.SetState (IE_GUI_BUTTON_DISABLED)
284 # Done
285 Button = Window.GetControl (8)
286 Button.SetText (1403)
287 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OpenReformPartyWindow")
289 GemRB.UnhideGUI ()
292 last_formation = None
294 def OpenFormationWindow ():
295 global FormationWindow
297 if CloseOtherWindow(OpenFormationWindow):
298 GemRB.HideGUI ()
299 if FormationWindow:
300 FormationWindow.Unload ()
301 FormationWindow = None
303 GemRB.GameSetFormation (last_formation, 0)
304 EnableAnimatedWindows ()
305 GemRB.SetVar ("OtherWindow", -1)
306 GemRB.UnhideGUI ()
307 return
309 GemRB.HideGUI ()
310 GemRB.LoadWindowPack (GetWindowPack())
311 FormationWindow = Window = GemRB.LoadWindow (27)
312 GemRB.SetVar ("OtherWindow", Window.ID)
313 DisableAnimatedWindows ()
315 # Done
316 Button = Window.GetControl (13)
317 Button.SetText (1403)
318 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OpenFormationWindow")
320 tooltips = (
321 44957, # Follow
322 44958, # T
323 44959, # Gather
324 44960, # 4 and 2
325 44961, # 3 by 2
326 44962, # Protect
327 48152, # 2 by 3
328 44964, # Rank
329 44965, # V
330 44966, # Wedge
331 44967, # S
332 44968, # Line
333 44969, # None
336 for i in range (13):
337 Button = Window.GetControl (i)
338 Button.SetVarAssoc ("SelectedFormation", i)
339 Button.SetTooltip (tooltips[i])
340 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "SelectFormation")
342 GemRB.SetVar ("SelectedFormation", GemRB.GameGetFormation (0))
343 SelectFormation ()
345 GemRB.UnhideGUI ()
347 def SelectFormation ():
348 global last_formation
349 Window = FormationWindow
351 formation = GemRB.GetVar ("SelectedFormation")
352 print "FORMATION:", formation
353 if last_formation != None and last_formation != formation:
354 Button = Window.GetControl (last_formation)
355 Button.SetState (IE_GUI_BUTTON_UNPRESSED)
357 Button = Window.GetControl (formation)
358 Button.SetState (IE_GUI_BUTTON_SELECTED)
360 last_formation = formation