factored out the EFFv2 saving into EFFImporter
[gemrb.git] / gemrb / GUIScripts / bg1 / GUIINV.py
blob11f549cfa1a952c757a35c0b01ae3ce9d8b14141
1 #-*-python-*-
2 #GemRB - Infinity Engine Emulator
3 #Copyright (C) 2003-2004 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.
20 #GUIINV.py - scripts to control inventory windows from GUIINV winpack
22 ###################################################
24 import GemRB
25 import GUICommon
26 import CommonTables
27 import InventoryCommon
28 from GUIDefines import *
29 from ie_stats import *
30 from ie_slots import *
32 InventoryWindow = None
33 PortraitWindow = None
34 OptionsWindow = None
35 OldPortraitWindow = None
36 OldOptionsWindow = None
38 def OpenInventoryWindowClick ():
39 tmp = GemRB.GetVar ("PressedPortrait")
40 GemRB.GameSelectPC (tmp, True, SELECT_REPLACE)
41 OpenInventoryWindow ()
42 return
44 def OpenInventoryWindow ():
45 """Opens the inventory window."""
47 import GUICommonWindows
49 global InventoryWindow, OptionsWindow, PortraitWindow
50 global OldPortraitWindow, OldOptionsWindow
52 if GUICommon.CloseOtherWindow (OpenInventoryWindow):
53 if GemRB.IsDraggingItem () == 1:
54 pc = GemRB.GameGetSelectedPCSingle ()
55 #store the item in the inventory before window is closed
56 GemRB.DropDraggedItem (pc, -3)
57 #dropping on ground if cannot store in inventory
58 if GemRB.IsDraggingItem () == 1:
59 GemRB.DropDraggedItem (pc, -2)
61 if InventoryWindow:
62 InventoryWindow.Unload ()
63 if OptionsWindow:
64 OptionsWindow.Unload ()
65 if PortraitWindow:
66 PortraitWindow.Unload ()
68 InventoryWindow = None
69 GemRB.SetVar ("OtherWindow", -1)
70 GUICommon.GameWindow.SetVisible(WINDOW_VISIBLE)
71 GemRB.UnhideGUI ()
72 GUICommonWindows.PortraitWindow = OldPortraitWindow
73 OldPortraitWindow = None
74 GUICommonWindows.OptionsWindow = OldOptionsWindow
75 OldOptionsWindow = None
76 GUICommonWindows.SetSelectionChangeHandler (None)
77 return
79 GemRB.HideGUI ()
80 GUICommon.GameWindow.SetVisible(WINDOW_INVISIBLE)
82 GemRB.LoadWindowPack ("GUIINV", 640, 480)
83 InventoryWindow = Window = GemRB.LoadWindow (2)
84 GemRB.SetVar ("OtherWindow", InventoryWindow.ID)
85 GemRB.SetVar ("MessageLabel", Window.GetControl (0x1000003f).ID )
86 OldOptionsWindow = GUICommonWindows.OptionsWindow
87 OptionsWindow = GemRB.LoadWindow (0)
88 GUICommonWindows.SetupMenuWindowControls (OptionsWindow, 0, OpenInventoryWindow)
89 #saving the original portrait window
90 OldPortraitWindow = GUICommonWindows.PortraitWindow
91 PortraitWindow = GUICommonWindows.OpenPortraitWindow (0)
93 #ground items scrollbar
94 ScrollBar = Window.GetControl (66)
95 ScrollBar.SetEvent (IE_GUI_SCROLLBAR_ON_CHANGE, RefreshInventoryWindow)
97 #Ground Item
98 for i in range (5):
99 Button = Window.GetControl (i+68)
100 Button.SetEvent (IE_GUI_MOUSE_ENTER_BUTTON, InventoryCommon.MouseEnterGround)
101 Button.SetEvent (IE_GUI_MOUSE_LEAVE_BUTTON, InventoryCommon.MouseLeaveGround)
102 Button.SetVarAssoc ("GroundItemButton", i)
103 Button.SetSprites ("STONSLOT",0,0,2,4,3)
104 Button.SetFont ("NUMBER")
105 Button.SetBorder (0,0,0,0,0,128,128,255,64,0,1)
106 Button.SetBorder (1,2,2,2,2,32,32,255,0,0,0)
107 Button.SetBorder (2,0,0,0,0,255,128,128,64,0,1)
108 Button.SetFlags (IE_GUI_BUTTON_ALIGN_RIGHT | IE_GUI_BUTTON_ALIGN_TOP | IE_GUI_BUTTON_PICTURE, OP_OR)
110 #major & minor clothing color
111 Button = Window.GetControl (62)
112 Button.SetFlags (IE_GUI_BUTTON_PICTURE,OP_OR)
113 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, InventoryCommon.MajorPress)
114 Button.SetTooltip (12007)
116 Button = Window.GetControl (63)
117 Button.SetFlags (IE_GUI_BUTTON_PICTURE,OP_OR)
118 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, InventoryCommon.MinorPress)
119 Button.SetTooltip (12008)
121 #portrait
122 Button = Window.GetControl (50)
123 Button.SetState (IE_GUI_BUTTON_LOCKED)
124 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE | IE_GUI_BUTTON_PICTURE, OP_SET)
125 Button.SetEvent (IE_GUI_BUTTON_ON_DRAG_DROP, InventoryCommon.OnAutoEquip)
127 #encumbrance
128 Label = Window.CreateLabel (0x10000043, 14,375,60,20,"NUMBER","0:",IE_FONT_ALIGN_LEFT|IE_FONT_ALIGN_TOP)
129 Label = Window.CreateLabel (0x10000044, 8,445,60,20,"NUMBER","0:",IE_FONT_ALIGN_RIGHT|IE_FONT_ALIGN_TOP)
131 #armor class
132 Label = Window.GetControl (0x10000038)
133 Label.SetTooltip (17183)
135 #hp current
136 Label = Window.GetControl (0x10000039)
137 Label.SetTooltip (17184)
139 #hp max
140 Label = Window.GetControl (0x1000003a)
141 Label.SetTooltip (17378)
143 #info label, game paused, etc
144 Label = Window.GetControl (0x1000003f)
145 Label.SetText ("")
147 SlotCount = GemRB.GetSlotType (-1)["Count"]
148 for slot in range (SlotCount):
149 SlotType = GemRB.GetSlotType (slot+1)
150 if SlotType["ID"]:
151 Button = Window.GetControl (SlotType["ID"])
152 Button.SetEvent (IE_GUI_MOUSE_ENTER_BUTTON, InventoryCommon.MouseEnterSlot)
153 Button.SetEvent (IE_GUI_MOUSE_LEAVE_BUTTON, InventoryCommon.MouseLeaveSlot)
154 Button.SetVarAssoc ("ItemButton", slot+1)
155 #keeping 1 in the original place, because it is how
156 #the gui resource has it, but setting the other cycles
157 Button.SetSprites ("STONSLOT",0,0,1,2,3)
158 Button.SetFont ("NUMBER")
159 Button.SetBorder (0,0,0,0,0,128,128,255,64,0,1)
160 Button.SetBorder (1,2,2,2,2,32,32,255,0,0,0)
161 Button.SetBorder (2,0,0,0,0,255,128,128,64,0,1)
162 Button.SetFlags (IE_GUI_BUTTON_ALIGN_RIGHT | IE_GUI_BUTTON_ALIGN_TOP | IE_GUI_BUTTON_PICTURE, OP_OR)
164 GemRB.SetVar ("TopIndex", 0)
165 GUICommonWindows.SetSelectionChangeHandler (UpdateInventoryWindow)
166 UpdateInventoryWindow ()
167 OptionsWindow.SetVisible (WINDOW_VISIBLE)
168 Window.SetVisible (WINDOW_FRONT)
169 PortraitWindow.SetVisible (WINDOW_VISIBLE)
171 # force unpause the game
172 GemRB.GamePause(0, 0)
173 return
175 #complete update
176 def UpdateInventoryWindow ():
177 Window = InventoryWindow
179 pc = GemRB.GameGetSelectedPCSingle ()
180 Container = GemRB.GetContainer (pc, 1)
181 ScrollBar = Window.GetControl (66)
182 Count = Container['ItemCount']
183 if Count<1:
184 Count=1
185 ScrollBar.SetVarAssoc ("TopIndex", Count)
186 RefreshInventoryWindow ()
187 #populate inventory slot controls
188 SlotCount = GemRB.GetSlotType (-1)["Count"]
189 for i in range (SlotCount):
190 InventoryCommon.UpdateSlot (pc, i)
191 return
193 #partial update without altering TopIndex
194 def RefreshInventoryWindow ():
195 Window = InventoryWindow
197 pc = GemRB.GameGetSelectedPCSingle ()
199 #name
200 Label = Window.GetControl (0x10000032)
201 Label.SetText (GemRB.GetPlayerName (pc, 0))
203 #portrait
204 Button = Window.GetControl (50)
205 Color1 = GemRB.GetPlayerStat (pc, IE_METAL_COLOR)
206 Color2 = GemRB.GetPlayerStat (pc, IE_MINOR_COLOR)
207 Color3 = GemRB.GetPlayerStat (pc, IE_MAJOR_COLOR)
208 Color4 = GemRB.GetPlayerStat (pc, IE_SKIN_COLOR)
209 Color5 = GemRB.GetPlayerStat (pc, IE_LEATHER_COLOR)
210 Color6 = GemRB.GetPlayerStat (pc, IE_ARMOR_COLOR)
211 Color7 = GemRB.GetPlayerStat (pc, IE_HAIR_COLOR)
212 Button.SetPLT (GUICommon.GetActorPaperDoll (pc),
213 Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 0)
215 anim_id = GemRB.GetPlayerStat (pc, IE_ANIMATION_ID)
216 row = "0x%04X" %anim_id
217 size = CommonTables.Pdolls.GetValue (row, "SIZE")
219 #Weapon
220 slot_item = GemRB.GetSlotItem (pc, GemRB.GetEquippedQuickSlot (pc) )
221 if slot_item:
222 item = GemRB.GetItem (slot_item["ItemResRef"])
223 if (item['AnimationType'] != ''):
224 Button.SetPLT("WP" + size + item['AnimationType'] + "INV", Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 1)
226 #Shield
227 slot_item = GemRB.GetSlotItem (pc, 3)
228 if slot_item:
229 itemname = slot_item["ItemResRef"]
230 item = GemRB.GetItem (itemname)
231 if (item['AnimationType'] != ''):
232 if (GemRB.CanUseItemType (SLOT_WEAPON, itemname)):
233 #off-hand weapon
234 Button.SetPLT("WP" + size + item['AnimationType'] + "OIN", Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 2)
235 else:
236 #shield
237 Button.SetPLT("WP" + size + item['AnimationType'] + "INV", Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 2)
239 #Helmet
240 slot_item = GemRB.GetSlotItem (pc, 1)
241 if slot_item:
242 item = GemRB.GetItem (slot_item["ItemResRef"])
243 if (item['AnimationType'] != ''):
244 Button.SetPLT("WP" + size + item['AnimationType'] + "INV", Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 3)
246 #encumbrance
247 GUICommon.SetEncumbranceLabels ( Window, 0x10000043, 0x10000044, pc)
249 #armor class
250 ac = GemRB.GetPlayerStat (pc, IE_ARMORCLASS)
251 ac += GemRB.GetAbilityBonus (IE_DEX, 2, GemRB.GetPlayerStat (pc, IE_DEX) )
252 Label = Window.GetControl (0x10000038)
253 Label.SetText (str (ac))
255 #hp current
256 hp = GemRB.GetPlayerStat (pc, IE_HITPOINTS)
257 Label = Window.GetControl (0x10000039)
258 Label.SetText (str (hp))
260 #hp max
261 hpmax = GemRB.GetPlayerStat (pc, IE_MAXHITPOINTS)
262 Label = Window.GetControl (0x1000003a)
263 Label.SetText (str (hpmax))
265 #party gold
266 Label = Window.GetControl (0x10000040)
267 Label.SetText (str (GemRB.GameGetPartyGold ()))
269 #class
270 ClassTitle = GUICommon.GetActorClassTitle (pc)
271 Label = Window.GetControl (0x10000042)
272 Label.SetText (ClassTitle)
274 Button = Window.GetControl (62)
275 Color = GemRB.GetPlayerStat (pc, IE_MAJOR_COLOR, 1) & 0xFF
276 Button.SetBAM ("COLGRAD", 0, 0, Color)
278 Button = Window.GetControl (63)
279 Color = GemRB.GetPlayerStat (pc, IE_MINOR_COLOR, 1) & 0xFF
280 Button.SetBAM ("COLGRAD", 0, 0, Color)
282 #update ground inventory slots
283 Container = GemRB.GetContainer (pc, 1)
284 TopIndex = GemRB.GetVar ("TopIndex")
285 for i in range (5):
286 Button = Window.GetControl (i+68)
287 if GemRB.IsDraggingItem ()==1:
288 Button.SetState (IE_GUI_BUTTON_SECOND)
289 else:
290 Button.SetState (IE_GUI_BUTTON_ENABLED)
291 Button.SetEvent (IE_GUI_BUTTON_ON_DRAG_DROP, InventoryCommon.OnDragItemGround)
292 Slot = GemRB.GetContainerItem (pc, i+TopIndex)
294 if Slot == None:
295 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, None)
296 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, None)
297 Button.SetEvent (IE_GUI_BUTTON_ON_SHIFT_PRESS, None)
298 else:
299 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, InventoryCommon.OnDragItemGround)
300 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, InventoryCommon.OpenGroundItemInfoWindow)
301 Button.SetEvent (IE_GUI_BUTTON_ON_SHIFT_PRESS, None) #TODO: implement OpenGroundItemAmountWindow
303 GUICommon.UpdateInventorySlot (pc, Button, Slot, "ground")
305 #making window visible/shaded depending on the pc's state
306 held = GemRB.GetPlayerStat (pc, IE_HELD) + GemRB.GetPlayerStat (pc, IE_CASTERHOLD)
307 if held == 0 and GemRB.GetPlayerStat (pc, IE_STATE_ID) & STATE_DEAD == 0:
308 Window.SetVisible (WINDOW_VISIBLE)
309 else:
310 Window.SetVisible (WINDOW_GRAYED)
311 return
313 ###################################################
314 #End of file GUIINV.py