TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / GUIScripts / bg1 / GUIPR.py
blob7ef07eb1aaf4c7eaed283025bce69cdb3f78fc0b
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 # GUIPR.py - scripts to control priest spells windows from GUIPR winpack
23 ###################################################
25 import GemRB
26 import GUICommon
27 import CommonTables
28 import GUICommonWindows
29 from GUIDefines import *
30 from ie_stats import *
31 from ie_action import ACT_CAST
33 PriestWindow = None
34 PriestSpellInfoWindow = None
35 PriestSpellLevel = 0
36 PriestSpellUnmemorizeWindow = None
37 OldOptionsWindow = None
40 def OpenPriestWindow ():
41 global PriestWindow, OptionsWindow
42 global OldOptionsWindow
45 if GUICommon.CloseOtherWindow (OpenPriestWindow):
46 if PriestWindow:
47 PriestWindow.Unload ()
48 if OptionsWindow:
49 OptionsWindow.Unload ()
50 PriestWindow = None
51 GemRB.SetVar ("OtherWindow", -1)
52 GUICommon.GameWindow.SetVisible(WINDOW_VISIBLE)
53 GemRB.UnhideGUI ()
54 OptionsWindow = OldOptionsWindow
55 OldOptionsWindow = None
56 GUICommonWindows.SetSelectionChangeHandler(None)
57 return
59 GemRB.HideGUI ()
60 GUICommon.GameWindow.SetVisible(WINDOW_INVISIBLE)
62 GemRB.LoadWindowPack ("GUIPR")
63 PriestWindow = Window = GemRB.LoadWindow (2)
64 GemRB.SetVar ("OtherWindow", PriestWindow.ID)
65 OldOptionsWindow = GUICommonWindows.OptionsWindow
66 OptionsWindow = GemRB.LoadWindow (0)
67 GUICommonWindows.SetupMenuWindowControls (OptionsWindow, 0, OpenPriestWindow)
68 OptionsWindow.SetFrame ()
70 Button = Window.GetControl (1)
71 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, PriestPrevLevelPress)
73 Button = Window.GetControl (2)
74 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, PriestNextLevelPress)
76 ## #setup level buttons
77 ## for i in range (7):
78 ## Button = Window.GetControl (55 + i)
79 ## Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, RefreshPriestLevel)
80 ## Button.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
82 ## for i in range (7):
83 ## Button = Window.GetControl (55 + i)
84 ## Button.SetVarAssoc ("PriestSpellLevel", i)
86 # Setup memorized spells buttons
87 for i in range (12):
88 Button = Window.GetControl (3 + i)
89 Button.SetBorder (0,0,0,0,0,0,0,0,64,0,1)
90 Button.SetSprites ("SPELFRAM",0,0,0,0,0)
91 Button.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
92 Button.SetState (IE_GUI_BUTTON_LOCKED)
94 # Setup book spells buttons
95 for i in range (20):
96 Button = Window.GetControl (27 + i)
97 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
98 Button.SetState (IE_GUI_BUTTON_LOCKED)
100 GUICommonWindows.SetSelectionChangeHandler (UpdatePriestWindow)
101 UpdatePriestWindow ()
103 OptionsWindow.SetVisible (WINDOW_VISIBLE)
104 Window.SetVisible (WINDOW_FRONT)
105 GUICommonWindows.PortraitWindow.SetVisible (WINDOW_VISIBLE)
106 return
109 def UpdatePriestWindow ():
110 global PriestMemorizedSpellList, PriestKnownSpellList
112 PriestMemorizedSpellList = []
113 PriestKnownSpellList = []
115 Window = PriestWindow
116 pc = GemRB.GameGetSelectedPCSingle ()
117 Type = IE_SPELL_TYPE_PRIEST
118 level = PriestSpellLevel
119 max_mem_cnt = GemRB.GetMemorizableSpellsCount (pc, Type, level)
121 Label = Window.GetControl (0x10000032)
122 GemRB.SetToken ('LEVEL', str (level + 1))
123 Label.SetText (12137)
125 Name = GemRB.GetPlayerName (pc, 0)
126 Label = Window.GetControl (0x10000035)
127 Label.SetText (Name)
129 mem_cnt = GemRB.GetMemorizedSpellsCount (pc, Type, level)
130 for i in range (12):
131 Button = Window.GetControl (3 + i)
132 if i < mem_cnt:
133 ms = GemRB.GetMemorizedSpell (pc, Type, level, i)
134 Button.SetSpellIcon (ms['SpellResRef'])
135 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_NAND)
136 Button.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
137 if ms['Flags']:
138 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenPriestSpellUnmemorizeWindow)
139 else:
140 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OnPriestUnmemorizeSpell)
141 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, OpenPriestSpellInfoWindow)
142 spell = GemRB.GetSpell (ms['SpellResRef'])
143 Button.SetTooltip (spell['SpellName'])
144 PriestMemorizedSpellList.append (ms['SpellResRef'])
145 Button.SetVarAssoc ("SpellButton", i)
146 Button.EnableBorder (0, ms['Flags'] == 0)
147 else:
148 if i < max_mem_cnt:
149 Button.SetFlags (IE_GUI_BUTTON_NORMAL, OP_SET)
150 else:
151 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
152 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, None)
153 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, None)
154 Button.SetTooltip ('')
155 Button.EnableBorder (0, 0)
158 known_cnt = GemRB.GetKnownSpellsCount (pc, Type, level)
159 for i in range (20):
160 Button = Window.GetControl (27 + i)
161 if i < known_cnt:
162 ks = GemRB.GetKnownSpell (pc, Type, level, i)
163 Button.SetSpellIcon (ks['SpellResRef'])
164 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_NAND)
165 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OnPriestMemorizeSpell)
166 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, OpenPriestSpellInfoWindow)
167 spell = GemRB.GetSpell (ks['SpellResRef'])
168 Button.SetTooltip (spell['SpellName'])
169 PriestKnownSpellList.append (ks['SpellResRef'])
170 Button.SetVarAssoc ("SpellButton", 100 + i)
172 else:
173 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
174 Button.SetFlags (IE_GUI_BUTTON_PICTURE, OP_NAND)
175 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, None)
176 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, None)
177 Button.SetTooltip ('')
178 Button.EnableBorder (0, 0)
180 Class = GemRB.GetPlayerStat (pc, IE_CLASS)
181 DivineCaster = CommonTables.ClassSkills.GetValue (Class, 1)
182 if DivineCaster == "*":
183 # also check the DRUIDSPELL column
184 DivineCaster = CommonTables.ClassSkills.GetValue (Class, 0)
185 CantCast = DivineCaster == "*" or GemRB.GetPlayerStat(pc, IE_DISABLEDBUTTON)&(1<<ACT_CAST)
186 if CantCast or GemRB.GetPlayerStat (pc, IE_STATE_ID) & STATE_DEAD:
187 Window.SetVisible (WINDOW_GRAYED)
188 else:
189 Window.SetVisible (WINDOW_VISIBLE)
190 return
192 def PriestPrevLevelPress ():
193 global PriestSpellLevel
195 if PriestSpellLevel > 0:
196 PriestSpellLevel = PriestSpellLevel - 1
197 UpdatePriestWindow ()
198 return
200 def PriestNextLevelPress ():
201 global PriestSpellLevel
203 if PriestSpellLevel < 6:
204 PriestSpellLevel = PriestSpellLevel + 1
205 UpdatePriestWindow ()
206 return
208 def RefreshPriestLevel ():
209 global PriestSpellLevel
211 PriestSpellLevel = GemRB.GetVar ("PriestSpellLevel")
212 UpdatePriestWindow ()
213 return
215 def OpenPriestSpellInfoWindow ():
216 global PriestSpellInfoWindow
218 if PriestSpellInfoWindow != None:
219 if PriestSpellInfoWindow:
220 PriestSpellInfoWindow.Unload ()
221 PriestSpellInfoWindow = None
222 return
224 PriestSpellInfoWindow = Window = GemRB.LoadWindow (3)
226 #back
227 Button = Window.GetControl (5)
228 Button.SetText (15416)
229 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenPriestSpellInfoWindow)
231 index = GemRB.GetVar ("SpellButton")
232 if index < 100:
233 ResRef = PriestMemorizedSpellList[index]
234 else:
235 ResRef = PriestKnownSpellList[index - 100]
237 spell = GemRB.GetSpell (ResRef)
239 #Label = Window.GetControl (0x0fffffff)
240 #Label.SetText (spell['SpellName'])
242 Label = Window.GetControl (0x10000000)
243 Label.SetText (spell['SpellName'])
245 Button = Window.GetControl (2)
246 Button.SetSpellIcon (ResRef)
248 Text = Window.GetControl (3)
249 Text.SetText (spell['SpellDesc'])
251 Window.ShowModal (MODAL_SHADOW_GRAY)
252 return
254 def OnPriestMemorizeSpell ():
255 pc = GemRB.GameGetSelectedPCSingle ()
256 level = PriestSpellLevel
257 Type = IE_SPELL_TYPE_PRIEST
259 index = GemRB.GetVar ("SpellButton") - 100
261 if GemRB.MemorizeSpell (pc, Type, level, index):
262 UpdatePriestWindow ()
263 return
265 def OpenPriestSpellRemoveWindow ():
266 global PriestSpellUnmemorizeWindow
268 PriestSpellUnmemorizeWindow = Window = GemRB.LoadWindow (5)
270 # "Are you sure you want to ....?"
271 TextArea = Window.GetControl (3)
272 TextArea.SetText (11824)
274 # Remove
275 Button = Window.GetControl (0)
276 Button.SetText (17507)
277 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OnPriestRemoveSpell)
278 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
280 # Cancel
281 Button = Window.GetControl (1)
282 Button.SetText (13727)
283 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenPriestSpellRemoveWindow)
284 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
286 Window.ShowModal (MODAL_SHADOW_GRAY)
287 return
289 def ClosePriestSpellUnmemorizeWindow ():
290 global PriestSpellUnmemorizeWindow
292 if PriestSpellUnmemorizeWindow:
293 PriestSpellUnmemorizeWindow.Unload ()
294 PriestSpellUnmemorizeWindow = None
295 return
297 def OpenPriestSpellUnmemorizeWindow ():
298 global PriestSpellUnmemorizeWindow
300 PriestSpellUnmemorizeWindow = Window = GemRB.LoadWindow (5)
302 # "Are you sure you want to ....?"
303 TextArea = Window.GetControl (3)
304 TextArea.SetText (11824)
306 # Remove
307 Button = Window.GetControl (0)
308 Button.SetText (17507)
309 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OnPriestUnmemorizeSpell)
310 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
312 # Cancel
313 Button = Window.GetControl (1)
314 Button.SetText (13727)
315 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, ClosePriestSpellUnmemorizeWindow)
316 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
318 Window.ShowModal (MODAL_SHADOW_GRAY)
319 return
321 def OnPriestUnmemorizeSpell ():
322 if PriestSpellUnmemorizeWindow:
323 ClosePriestSpellUnmemorizeWindow ()
325 pc = GemRB.GameGetSelectedPCSingle ()
326 level = PriestSpellLevel
327 Type = IE_SPELL_TYPE_PRIEST
329 index = GemRB.GetVar ("SpellButton")
331 if GemRB.UnmemorizeSpell (pc, Type, level, index):
332 UpdatePriestWindow ()
333 return
335 def OnPriestRemoveSpell ():
336 ClosePriestSpellUnmemorizeWindow()
337 OpenPriestSpellInfoWindow()
339 pc = GemRB.GameGetSelectedPCSingle ()
340 level = PriestSpellLevel
341 Type = IE_SPELL_TYPE_PRIEST
343 index = GemRB.GetVar ("SpellButton")-100
345 #remove spell from memory
346 GemRB.RemoveSpell (pc, Type, level, index)
347 UpdatePriestWindow ()
348 return
350 ###################################################
351 # End of file GUIPR.py