TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / GUIScripts / bg2 / GUICG22.py
blobd0e4920b8874a96a8521b233939407e317182bb2
1 # -*-python-*-
2 # GemRB - Infinity Engine Emulator
3 # Copyright (C) 2003-2007 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.
19 #character generation, class kit (GUICG22)
21 import GemRB
22 import CommonTables
23 from ie_stats import *
24 from GUIDefines import *
26 KitWindow = 0
27 TextAreaControl = 0
28 DoneButton = 0
29 SchoolList = 0
30 ClassID = 0
31 TopIndex = 0
32 RowCount = 10
33 KitTable = 0
34 Init = 0
35 MyChar = 0
36 KitSelected = 0 #store clicked kit on redraw as number within RowCount
37 EnhanceGUI = GemRB.GetVar("GUIEnhancements") #extra kit button and scroll bar toggle
39 def OnLoad():
40 global KitWindow, TextAreaControl, DoneButton
41 global SchoolList, ClassID
42 global RowCount, TopIndex, KitTable, Init, MyChar
44 GemRB.LoadWindowPack("GUICG", 640, 480)
45 MyChar = GemRB.GetVar ("Slot")
46 Race = GemRB.GetPlayerStat (MyChar, IE_RACE)
47 RaceName = CommonTables.Races.GetRowName(CommonTables.Races.FindValue (3, Race) )
49 ClassID = GemRB.GetPlayerStat (MyChar, IE_CLASS)
50 ClassName = CommonTables.Classes.GetRowName (GemRB.GetPlayerStat (MyChar, IE_HITPOINTS)) # barbarian hack
52 KitTable = GemRB.LoadTable("kittable")
53 KitTableName = KitTable.GetValue(ClassName, RaceName)
54 KitTable = GemRB.LoadTable(KitTableName,1)
56 SchoolList = GemRB.LoadTable("magesch")
58 #there is a specialist mage window, but it is easier to use
59 #the class kit window for both
60 KitWindow = GemRB.LoadWindow(22)
61 if ClassID == 1:
62 Label = KitWindow.GetControl(0xfffffff)
63 Label.SetText(595)
65 for i in range(10):
66 if i<4:
67 Button = KitWindow.GetControl(i+1)
68 else:
69 Button = KitWindow.GetControl(i+5)
70 Button.SetState(IE_GUI_BUTTON_DISABLED)
71 Button.SetFlags(IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
73 if not KitTable: # sorcerer or monk
74 RowCount = 1
75 else:
76 if ClassID == 1: # mages
77 RowCount = SchoolList.GetRowCount()
78 else:
79 RowCount = KitTable.GetRowCount()
81 TopIndex = 0
82 GemRB.SetVar("TopIndex", 0)
83 if EnhanceGUI:
84 tmpRowCount = RowCount
85 if RowCount>10: #create 11 kit button
86 KitWindow.CreateButton (15, 18, 250, 271, 20)
87 extrakit = KitWindow.GetControl(15)
88 extrakit.SetState(IE_GUI_BUTTON_DISABLED)
89 extrakit.SetFlags(IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
90 extrakit.SetSprites("GUICGBC",0, 0,1,2,3)
91 RowCount = 11
92 if tmpRowCount>11: #create scroll bar
93 KitWindow.CreateScrollBar(1000, 290, 50, 16, 220)
94 ScrollBar = KitWindow.GetControl (1000)
95 ScrollBar.SetSprites("GUISCRCW", 0, 0,1,2,3,5,4)
96 ScrollBar.SetVarAssoc("TopIndex",tmpRowCount-10)
97 ScrollBar.SetEvent(IE_GUI_SCROLLBAR_ON_CHANGE, RedrawKits)
98 ScrollBar.SetDefaultScrollBar()
99 elif not EnhanceGUI and RowCount>10:
100 RowCount = 10
102 for i in range(RowCount):
103 if i<4:
104 Button = KitWindow.GetControl(i+1)
105 else:
106 Button = KitWindow.GetControl(i+5)
107 Button.SetVarAssoc("ButtonPressed", i)
108 Button.SetEvent(IE_GUI_BUTTON_ON_PRESS, KitPress)
110 BackButton = KitWindow.GetControl(8)
111 BackButton.SetText(15416)
112 BackButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
113 DoneButton = KitWindow.GetControl(7)
114 DoneButton.SetText(11973)
115 DoneButton.SetFlags(IE_GUI_BUTTON_DEFAULT,OP_OR)
117 TextAreaControl = KitWindow.GetControl(5)
118 TextAreaControl.SetText(17247)
120 DoneButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, NextPress)
121 BackButton.SetEvent(IE_GUI_BUTTON_ON_PRESS, BackPress)
122 Init = 1
123 RedrawKits()
124 KitPress()
125 KitWindow.SetVisible(WINDOW_VISIBLE)
126 return
128 def RedrawKits():
129 global TopIndex, Init, KitSelected
131 TopIndex=GemRB.GetVar("TopIndex")
132 EnabledButtons = []
133 for i in range(RowCount):
134 if i<4:
135 Button = KitWindow.GetControl(i+1)
136 else:
137 Button = KitWindow.GetControl(i+5)
138 Button.SetState(IE_GUI_BUTTON_DISABLED)
139 if not KitTable:
140 if ClassID == 1:
141 # TODO: check if this is ever reached
142 Kit = GemRB.GetVar("MAGESCHOOL")
143 KitName = SchoolList.GetValue(i+TopIndex, 0)
144 Kit = SchoolList.GetValue (Kit, 3)
145 else:
146 Kit = 0
147 KitName = CommonTables.Classes.GetValue(GemRB.GetVar("Class")-1, 0)
148 else:
149 Kit = KitTable.GetValue (i+TopIndex,0)
150 if ClassID == 1:
151 KitName = SchoolList.GetValue (i+TopIndex, 0)
152 if Kit == 0:
153 KitName = SchoolList.GetValue (0, 0)
154 Button.SetState(IE_GUI_BUTTON_ENABLED)
155 if Init: #preselection of mage plain kit
156 Button.SetState(IE_GUI_BUTTON_SELECTED)
157 KitSelected = i+TopIndex
158 Init=0
159 if Kit != "*":
160 EnabledButtons.append(Kit-21)
161 else:
162 if Kit:
163 KitName = CommonTables.KitList.GetValue(Kit, 1)
164 else:
165 KitName = CommonTables.Classes.GetValue(GemRB.GetVar("Class")-1, 0)
166 Button.SetText(KitName)
167 if not EnabledButtons or i+TopIndex in EnabledButtons:
168 Button.SetState(IE_GUI_BUTTON_ENABLED)
169 if Init and i+TopIndex>0:
170 Button.SetState(IE_GUI_BUTTON_SELECTED)
171 KitSelected = i+TopIndex
172 Init=0
173 if Kit == "*":
174 continue
175 if Init and i+TopIndex==0:
176 if EnabledButtons:
177 GemRB.SetVar("ButtonPressed", EnabledButtons[0]) #but leave Init==1
178 else:
179 GemRB.SetVar("ButtonPressed", 0)
180 Button.SetState(IE_GUI_BUTTON_SELECTED)
181 KitSelected = i+TopIndex
182 Init=0
183 if not Init and i+TopIndex == KitSelected: #remark selection state on redraw
184 Button.SetState(IE_GUI_BUTTON_SELECTED)
185 return
187 def KitPress():
188 global KitSelected
190 ButtonPressed=GemRB.GetVar("ButtonPressed")
191 KitSelected = ButtonPressed + TopIndex
192 if not KitTable:
193 if ClassID == 1:
194 # TODO: this seems to be never reached
195 Kit = GemRB.GetVar("MAGESCHOOL")
196 Kit = SchoolList.GetValue (Kit, 3)
197 else:
198 Kit = 0
199 else:
200 Kit = KitTable.GetValue (ButtonPressed+TopIndex, 0)
201 if ClassID == 1:
202 if ButtonPressed + TopIndex == 0:
203 Kit = 0
204 else:
205 Kit = ButtonPressed + TopIndex + 21
207 if ClassID == 1 and Kit != 0:
208 GemRB.SetVar("MAGESCHOOL", Kit-21) # hack: -21 to make the generalist 0
209 else:
210 GemRB.SetVar("MAGESCHOOL", 0) # so bards don't get schools
212 if Kit == 0:
213 KitDescription = CommonTables.Classes.GetValue(GemRB.GetVar("Class")-1, 1)
214 else:
215 KitDescription = CommonTables.KitList.GetValue(Kit, 3)
217 TextAreaControl.SetText(KitDescription)
218 DoneButton.SetState(IE_GUI_BUTTON_ENABLED)
220 GemRB.SetVar("Class Kit", Kit)
222 return
224 def BackPress():
225 GemRB.SetVar("Class Kit", 0) # reverting the value so we are idempotent
226 GemRB.SetVar("MAGESCHOOL", 0)
227 if KitWindow:
228 KitWindow.Unload()
229 GemRB.SetNextScript("GUICG2")
230 return
232 def NextPress():
233 if KitWindow:
234 KitWindow.Unload()
236 #make gnomes always kitted
237 KitIndex = GemRB.GetVar ("Class Kit")
238 MageSchool = GemRB.GetVar ("MAGESCHOOL")
239 if MageSchool and not KitIndex:
240 SchoolTable = GemRB.LoadTable ("magesch")
241 KitIndex = CommonTables.KitList.FindValue (6, SchoolTable.GetValue (MageSchool, 3) )
243 #save the kit
244 KitValue = (0x4000 + KitIndex)
245 KitName = CommonTables.KitList.GetValue (KitIndex, 0)
246 GemRB.SetPlayerStat (MyChar, IE_KIT, KitValue)
248 GemRB.SetNextScript("CharGen4") #abilities
249 return