GUIScript: Use None instead if "", for null event handler.
[gemrb.git] / gemrb / GUIScripts / iwd / GUIINV.py
blob994a9a8f4e0d3f45ec72ca33d84766ba188bf717
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 #GUIINV.py - scripts to control inventory windows from GUIINV winpack
23 ###################################################
25 import GemRB
26 import GUICommonWindows
27 from GUIDefines import *
28 from ie_stats import *
29 from ie_slots import *
30 from ie_spells import *
31 from GUICommon import CloseOtherWindow, SetColorStat, SetEncumbranceLabels, CannotLearnSlotSpell
32 from GUICommon import GameWindow
33 from GUICommonWindows import *
35 InventoryWindow = None
36 ItemInfoWindow = None
37 ItemAmountWindow = None
38 StackAmount = 0
39 ItemIdentifyWindow = None
40 PortraitWindow = None
41 OptionsWindow = None
42 ErrorWindow = None
43 OldPortraitWindow = None
44 OldOptionsWindow = None
45 OverSlot = None
47 def OpenInventoryWindowClick ():
48 tmp = GemRB.GetVar ("PressedPortrait")+1
49 GemRB.GameSelectPC (tmp, True, SELECT_REPLACE)
50 OpenInventoryWindow ()
51 return
53 def OpenInventoryWindow ():
54 global InventoryWindow, OptionsWindow, PortraitWindow
55 global OldPortraitWindow, OldOptionsWindow
57 if CloseOtherWindow (OpenInventoryWindow):
58 if GemRB.IsDraggingItem ()==1:
59 pc = GemRB.GameGetSelectedPCSingle ()
60 #store the item in the inventory before window is closed
61 GemRB.DropDraggedItem (pc, -3)
62 #dropping on ground if cannot store in inventory
63 if GemRB.IsDraggingItem ()==1:
64 GemRB.DropDraggedItem (pc, -2)
66 if InventoryWindow:
67 InventoryWindow.Unload ()
68 if OptionsWindow:
69 OptionsWindow.Unload ()
70 if PortraitWindow:
71 PortraitWindow.Unload ()
73 InventoryWindow = None
74 GemRB.SetVar ("OtherWindow", -1)
75 GameWindow.SetVisible(WINDOW_VISIBLE)
76 GemRB.UnhideGUI ()
77 GUICommonWindows.PortraitWindow = OldPortraitWindow
78 OldPortraitWindow = None
79 GUICommonWindows.OptionsWindow = OldOptionsWindow
80 OldOptionsWindow = None
81 SetSelectionChangeHandler (None)
82 return
84 GemRB.HideGUI ()
85 GameWindow.SetVisible(WINDOW_INVISIBLE)
87 GemRB.LoadWindowPack ("GUIINV", 640, 480)
88 InventoryWindow = Window = GemRB.LoadWindow (2)
89 GemRB.SetVar ("OtherWindow", InventoryWindow.ID)
90 GemRB.SetVar ("MessageLabel", Window.GetControl (0x1000003f).ID )
91 OldOptionsWindow = GUICommonWindows.OptionsWindow
92 OptionsWindow = GemRB.LoadWindow (0)
93 SetupMenuWindowControls (OptionsWindow, 0, "OpenInventoryWindow")
94 OptionsWindow.SetFrame ()
95 #saving the original portrait window
96 OldPortraitWindow = GUICommonWindows.PortraitWindow
97 PortraitWindow = OpenPortraitWindow (0)
99 #ground items scrollbar
100 ScrollBar = Window.GetControl (66)
101 ScrollBar.SetEventByName (IE_GUI_SCROLLBAR_ON_CHANGE, "RefreshInventoryWindow")
103 #Ground Item
104 for i in range (5):
105 Button = Window.GetControl (i+68)
106 Button.SetEventByName (IE_GUI_MOUSE_ENTER_BUTTON, "MouseEnterGround")
107 Button.SetEventByName (IE_GUI_MOUSE_LEAVE_BUTTON, "MouseLeaveGround")
108 Button.SetVarAssoc ("GroundItemButton", i)
109 Button.SetSprites ("STONSLOT",0,0,1,2,3)
111 #major & minor clothing color
112 Button = Window.GetControl (62)
113 Button.SetFlags (IE_GUI_BUTTON_PICTURE,OP_OR)
114 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS,"MajorPress")
115 Button.SetTooltip (12007)
117 Button = Window.GetControl (63)
118 Button.SetFlags (IE_GUI_BUTTON_PICTURE,OP_OR)
119 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS,"MinorPress")
120 Button.SetTooltip (12008)
122 #portrait
123 Button = Window.GetControl (50)
124 Button.SetState (IE_GUI_BUTTON_LOCKED)
125 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE | IE_GUI_BUTTON_PICTURE, OP_SET)
126 Button.SetEventByName (IE_GUI_BUTTON_ON_DRAG_DROP, "OnAutoEquip")
128 #encumbrance
129 Label = Window.CreateLabel (0x10000043, 5,385,60,15,"NUMBER","0:",IE_FONT_ALIGN_LEFT|IE_FONT_ALIGN_TOP)
130 Label = Window.CreateLabel (0x10000044, 5,455,80,15,"NUMBER","0:",IE_FONT_ALIGN_RIGHT|IE_FONT_ALIGN_TOP)
132 #armor class
133 Label = Window.GetControl (0x10000038)
134 Label.SetTooltip (17183)
136 #hp current
137 Label = Window.GetControl (0x10000039)
138 Label.SetTooltip (17184)
140 #hp max
141 Label = Window.GetControl (0x1000003a)
142 Label.SetTooltip (17378)
144 #info label, game paused, etc
145 Label = Window.GetControl (0x1000003f)
146 Label.SetText ("")
148 SlotCount = GemRB.GetSlotType (-1)["Count"]
149 for slot in range (SlotCount):
150 SlotType = GemRB.GetSlotType (slot+1)
151 if SlotType["ID"]:
152 Button = Window.GetControl (SlotType["ID"])
153 Button.SetEventByName (IE_GUI_MOUSE_ENTER_BUTTON, "MouseEnterSlot")
154 Button.SetEventByName (IE_GUI_MOUSE_LEAVE_BUTTON, "MouseLeaveSlot")
155 Button.SetVarAssoc ("ItemButton", slot+1)
156 Button.SetSprites ("STONSLOT",0,0,1,2,3)
158 GemRB.SetVar ("TopIndex", 0)
159 SetSelectionChangeHandler (UpdateInventoryWindow)
160 UpdateInventoryWindow ()
161 OptionsWindow.SetVisible (WINDOW_VISIBLE)
162 Window.SetVisible (WINDOW_FRONT)
163 PortraitWindow.SetVisible (WINDOW_VISIBLE)
164 return
166 def ColorDonePress ():
167 pc = GemRB.GameGetSelectedPCSingle ()
169 if ColorPicker:
170 ColorPicker.Unload ()
171 InventoryWindow.SetVisible (WINDOW_VISIBLE)
172 ColorTable = GemRB.LoadTable ("clowncol")
173 PickedColor=ColorTable.GetValue (ColorIndex, GemRB.GetVar ("Selected"))
174 if ColorIndex==2:
175 SetColorStat (pc, IE_MAJOR_COLOR, PickedColor)
176 else:
177 SetColorStat (pc, IE_MINOR_COLOR, PickedColor)
178 UpdateInventoryWindow ()
179 return
181 def MajorPress ():
182 global ColorIndex, PickedColor
184 pc = GemRB.GameGetSelectedPCSingle ()
185 ColorIndex = 2
186 PickedColor = GemRB.GetPlayerStat (pc, IE_MAJOR_COLOR, 1) & 0xFF
187 GetColor ()
188 return
190 def MinorPress ():
191 global ColorIndex, PickedColor
193 pc = GemRB.GameGetSelectedPCSingle ()
194 ColorIndex = 3
195 PickedColor = GemRB.GetPlayerStat (pc, IE_MINOR_COLOR, 1) & 0xFF
196 GetColor ()
197 return
199 def GetColor ():
200 global ColorPicker
202 ColorTable = GemRB.LoadTable ("clowncol")
203 InventoryWindow.SetVisible (WINDOW_GRAYED) #darken it
204 ColorPicker=GemRB.LoadWindow (3)
205 GemRB.SetVar ("Selected",-1)
206 for i in range (34):
207 Button = ColorPicker.GetControl (i)
208 Button.SetState (IE_GUI_BUTTON_DISABLED)
209 Button.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_RADIOBUTTON,OP_OR)
211 Selected = -1
212 for i in range (34):
213 MyColor = ColorTable.GetValue (ColorIndex, i)
214 if MyColor == "*":
215 break
216 Button = ColorPicker.GetControl (i)
217 Button.SetBAM ("COLGRAD", 2, 0, MyColor)
218 if PickedColor == MyColor:
219 GemRB.SetVar ("Selected",i)
220 Selected = i
221 Button.SetState (IE_GUI_BUTTON_ENABLED)
222 Button.SetVarAssoc ("Selected",i)
223 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "ColorDonePress")
224 ColorPicker.SetVisible (WINDOW_VISIBLE)
225 return
227 #complete update
228 def UpdateInventoryWindow ():
229 Window = InventoryWindow
231 pc = GemRB.GameGetSelectedPCSingle ()
232 Container = GemRB.GetContainer (pc, 1)
233 ScrollBar = Window.GetControl (66)
234 Count = Container['ItemCount']
235 if Count<1:
236 Count=1
237 ScrollBar.SetVarAssoc ("TopIndex", Count)
238 RefreshInventoryWindow ()
239 #populate inventory slot controls
240 SlotCount = GemRB.GetSlotType (-1)["Count"]
241 for i in range (SlotCount):
242 UpdateSlot (pc, i)
243 return
245 #partial update without altering TopIndex
246 def RefreshInventoryWindow ():
247 Window = InventoryWindow
249 pc = GemRB.GameGetSelectedPCSingle ()
251 #name
252 Label = Window.GetControl (0x10000032)
253 Label.SetText (GemRB.GetPlayerName (pc, 0))
255 #portrait
256 Button = Window.GetControl (50)
257 Color1 = GemRB.GetPlayerStat (pc, IE_METAL_COLOR)
258 Color2 = GemRB.GetPlayerStat (pc, IE_MINOR_COLOR)
259 Color3 = GemRB.GetPlayerStat (pc, IE_MAJOR_COLOR)
260 Color4 = GemRB.GetPlayerStat (pc, IE_SKIN_COLOR)
261 Color5 = GemRB.GetPlayerStat (pc, IE_LEATHER_COLOR)
262 Color6 = GemRB.GetPlayerStat (pc, IE_ARMOR_COLOR)
263 Color7 = GemRB.GetPlayerStat (pc, IE_HAIR_COLOR)
264 Button.SetPLT (GetActorPaperDoll (pc),
265 Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 0)
267 anim_id = GemRB.GetPlayerStat (pc, IE_ANIMATION_ID)
268 row = "0x%04X" %anim_id
269 size = AppearanceAvatarTable.GetValue (row, "SIZE")
271 #Weapon
272 slot_item = GemRB.GetSlotItem (pc, GemRB.GetEquippedQuickSlot (pc) )
273 if slot_item:
274 item = GemRB.GetItem (slot_item["ItemResRef"])
275 if (item['AnimationType'] != ''):
276 Button.SetPLT("WP" + size + item['AnimationType'] + "INV", Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 1)
278 #Shield
279 slot_item = GemRB.GetSlotItem (pc, 3)
280 if slot_item:
281 itemname = slot_item["ItemResRef"]
282 item = GemRB.GetItem (itemname)
283 if (item['AnimationType'] != ''):
284 if (GemRB.CanUseItemType (SLOT_WEAPON, itemname)):
285 #off-hand weapon
286 Button.SetPLT("WP" + size + item['AnimationType'] + "OIN", Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 2)
287 else:
288 #shield
289 Button.SetPLT("WP" + size + item['AnimationType'] + "INV", Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 2)
291 #Helmet
292 slot_item = GemRB.GetSlotItem (pc, 1)
293 if slot_item:
294 item = GemRB.GetItem (slot_item["ItemResRef"])
295 if (item['AnimationType'] != ''):
296 Button.SetPLT("WP" + size + item['AnimationType'] + "INV", Color1, Color2, Color3, Color4, Color5, Color6, Color7, 0, 3)
298 #encumbrance
299 SetEncumbranceLabels ( Window, 0x10000043, 0x10000044, pc)
301 #armor class
302 Label = Window.GetControl (0x10000038)
303 ac = GemRB.GetPlayerStat (pc, IE_ARMORCLASS)
304 ac += GemRB.GetAbilityBonus (IE_DEX, 2, GemRB.GetPlayerStat (pc, IE_DEX) )
305 Label.SetText (str (ac))
306 Label.SetTooltip (10339)
308 #hp current
309 hp = GemRB.GetPlayerStat (pc, IE_HITPOINTS)
310 Label = Window.GetControl (0x10000039)
311 Label.SetText (str (hp))
312 Label.SetTooltip (17184)
314 #hp max
315 hpmax = GemRB.GetPlayerStat (pc, IE_MAXHITPOINTS)
316 Label = Window.GetControl (0x1000003a)
317 Label.SetText (str (hpmax))
318 Label.SetTooltip (17378)
320 #party gold
321 Label = Window.GetControl (0x10000040)
322 Label.SetText (str (GemRB.GameGetPartyGold ()))
324 #class
325 ClassTitle = GetActorClassTitle (pc)
326 Label = Window.GetControl (0x10000042)
327 Label.SetText (ClassTitle)
329 Button = Window.GetControl (62)
330 Color = GemRB.GetPlayerStat (pc, IE_MAJOR_COLOR, 1) & 0xFF
331 Button.SetBAM ("COLGRAD", 1, 0, Color)
333 Button = Window.GetControl (63)
334 Color = GemRB.GetPlayerStat (pc, IE_MINOR_COLOR, 1) & 0xFF
335 Button.SetBAM ("COLGRAD", 1, 0, Color)
337 #update ground inventory slots
338 Container = GemRB.GetContainer (pc, 1)
339 TopIndex = GemRB.GetVar ("TopIndex")
340 for i in range (5):
341 Button = Window.GetControl (i+68)
342 if GemRB.IsDraggingItem ()==1:
343 Button.SetState (IE_GUI_BUTTON_SECOND)
344 else:
345 Button.SetState (IE_GUI_BUTTON_ENABLED)
346 Button.SetEventByName (IE_GUI_BUTTON_ON_DRAG_DROP, "OnDragItemGround")
347 Slot = GemRB.GetContainerItem (pc, i+TopIndex)
349 if Slot == None:
350 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, None)
351 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, None)
352 Button.SetEvent (IE_GUI_BUTTON_ON_SHIFT_PRESS, None)
353 else:
354 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OnDragItemGround")
355 Button.SetEventByName (IE_GUI_BUTTON_ON_RIGHT_PRESS, "OpenGroundItemInfoWindow")
356 Button.SetEventByName (IE_GUI_BUTTON_ON_SHIFT_PRESS, "OpenGroundItemAmountWindow")
358 UpdateInventorySlot (pc, Button, Slot, "ground")
360 #making window visible/shaded depending on the pc's state
361 Window.SetVisible (WINDOW_VISIBLE)
362 return
364 def UpdateSlot (pc, slot):
365 """Updates a specific slot."""
367 Window = InventoryWindow
368 SlotType = GemRB.GetSlotType (slot+1, pc)
369 ControlID = SlotType["ID"]
371 if not ControlID:
372 return
374 if GemRB.IsDraggingItem ()==1:
375 #get dragged item
376 drag_item = GemRB.GetSlotItem (0,0)
377 itemname = drag_item["ItemResRef"]
378 drag_item = GemRB.GetItem (itemname)
379 else:
380 itemname = ""
382 Button = Window.GetControl (ControlID)
383 slot_item = GemRB.GetSlotItem (pc, slot+1)
385 Button.SetEventByName (IE_GUI_BUTTON_ON_DRAG_DROP, "OnDragItem")
386 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_NAND)
387 UpdateInventorySlot (pc, Button, slot_item, "inventory")
389 if slot_item:
390 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OnDragItem")
391 Button.SetEventByName (IE_GUI_BUTTON_ON_RIGHT_PRESS, "OpenItemInfoWindow")
392 Button.SetEventByName (IE_GUI_BUTTON_ON_SHIFT_PRESS, "OpenItemAmountWindow")
393 Button.SetEventByName (IE_GUI_BUTTON_ON_DOUBLE_PRESS, "OpenItemAmountWindow")
394 else:
395 if SlotType["ResRef"]=="*":
396 Button.SetBAM ("",0,0)
397 Button.SetTooltip (SlotType["Tip"])
398 itemname = ""
399 elif SlotType["ResRef"]=="":
400 Button.SetBAM ("",0,0)
401 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
402 Button.SetTooltip ("")
403 itemname = ""
404 else:
405 Button.SetBAM (SlotType["ResRef"],0,0)
406 Button.SetTooltip (SlotType["Tip"])
408 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, None)
409 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, None)
410 Button.SetEvent (IE_GUI_BUTTON_ON_SHIFT_PRESS, None)
411 Button.SetEvent (IE_GUI_BUTTON_ON_DOUBLE_PRESS, None)
413 if OverSlot == slot+1:
414 if GemRB.CanUseItemType (SlotType["Type"], itemname):
415 Button.SetState (IE_GUI_BUTTON_SELECTED)
416 else:
417 Button.SetState (IE_GUI_BUTTON_ENABLED)
418 else:
419 if (SlotType["Type"]&SLOT_INVENTORY) or not GemRB.CanUseItemType (SlotType["Type"], itemname):
420 Button.SetState (IE_GUI_BUTTON_ENABLED)
421 else:
422 Button.SetState (IE_GUI_BUTTON_SECOND)
424 if slot_item and (GemRB.GetEquippedQuickSlot (pc)==slot+1 or GemRB.GetEquippedAmmunition (pc)==slot+1):
425 Button.SetState (IE_GUI_BUTTON_THIRD)
427 return
429 def OnDragItemGround ():
430 pc = GemRB.GameGetSelectedPCSingle ()
431 slot = GemRB.GetVar ("GroundItemButton") + GemRB.GetVar ("TopIndex")
433 if GemRB.IsDraggingItem ()==0:
434 slot_item = GemRB.GetContainerItem (pc, slot)
435 item = GemRB.GetItem (slot_item["ItemResRef"])
436 GemRB.DragItem (pc, slot, item["ItemIcon"], 0, 1) #container
437 else:
438 GemRB.DropDraggedItem (pc, -2) #dropping on ground
440 UpdateInventoryWindow ()
441 return
443 def OnAutoEquip ():
444 if not GemRB.IsDraggingItem ()!=1:
445 return
447 pc = GemRB.GameGetSelectedPCSingle ()
449 #-1 : drop stuff in equipable slots (but not inventory)
450 GemRB.DropDraggedItem (pc, -1)
452 if GemRB.IsDraggingItem ()==1:
453 GemRB.PlaySound ("GAM_47") #failed equip
455 UpdateInventoryWindow ()
456 return
458 def OnDragItem ():
459 """Updates dragging."""
461 if GemRB.IsDraggingItem()==2:
462 return
464 pc = GemRB.GameGetSelectedPCSingle ()
465 slot = GemRB.GetVar ("ItemButton")
467 if not GemRB.IsDraggingItem ():
468 slot_item = GemRB.GetSlotItem (pc, slot)
469 item = GemRB.GetItem (slot_item["ItemResRef"])
470 GemRB.DragItem (pc, slot, item["ItemIcon"], 0, 0)
471 else:
472 SlotType = GemRB.GetSlotType (slot, pc)
473 if SlotType["ResRef"]!="":
474 GemRB.DropDraggedItem (pc, slot)
476 UpdateInventoryWindow ()
477 return
479 def OnDropItemToPC ():
480 pc = GemRB.GetVar ("PressedPortrait") + 1
482 #-3 : drop stuff in inventory (but not equippable slots)
483 GemRB.DropDraggedItem (pc, -3)
484 if GemRB.IsDraggingItem ()==1:
485 GemRB.DisplayString (17999,0xffffff)
486 UpdateInventoryWindow ()
487 return
489 def DecreaseStackAmount ():
490 Text = ItemAmountWindow.GetControl (6)
491 Amount = Text.QueryText ()
492 number = int ("0"+Amount)-1
493 if number<1:
494 number=1
495 Text.SetText (str (number))
496 return
498 def IncreaseStackAmount ():
499 Text = ItemAmountWindow.GetControl (6)
500 Amount = Text.QueryText ()
501 number = int ("0"+Amount)+1
502 if number>StackAmount:
503 number=StackAmount
504 Text.SetText (str (number))
505 return
507 def DragItemAmount ():
508 pc = GemRB.GameGetSelectedPCSingle ()
509 slot = GemRB.GetVar ("ItemButton")
510 slot_item = GemRB.GetSlotItem (pc, slot)
511 Text = ItemAmountWindow.GetControl (6)
512 Amount = Text.QueryText ()
513 item = GemRB.GetItem (slot_item["ItemResRef"])
514 GemRB.DragItem (pc, slot, item["ItemIcon"], int ("0"+Amount), 0)
515 OpenItemAmountWindow ()
516 return
518 def OpenItemAmountWindow ():
519 """Open the split window."""
521 global ItemAmountWindow, StackAmount
523 if ItemAmountWindow != None:
524 if ItemAmountWindow:
525 ItemAmountWindow.Unload ()
526 ItemAmountWindow = None
528 GemRB.SetRepeatClickFlags (GEM_RK_DISABLE, OP_OR)
529 return
531 pc = GemRB.GameGetSelectedPCSingle ()
532 slot = GemRB.GetVar ("ItemButton")
534 if GemRB.IsDraggingItem ()==1:
535 GemRB.DropDraggedItem(pc, slot)
536 # disallow splitting while holding split items (double splitting)
537 if GemRB.IsDraggingItem () == 1:
538 UpdateSlot (pc, slot-1)
539 return
540 else:
541 return
543 GemRB.SetRepeatClickFlags (GEM_RK_DISABLE, OP_NAND)
545 slot_item = GemRB.GetSlotItem (pc, slot)
546 ResRef = slot_item['ItemResRef']
547 item = GemRB.GetItem (ResRef)
549 if slot_item:
550 StackAmount = slot_item["Usages0"]
551 else:
552 StackAmount = 0
553 if StackAmount<=1:
554 #OpenItemAmountWindow ()
555 UpdateSlot (pc, slot-1)
556 return
558 ItemAmountWindow = Window = GemRB.LoadWindow (4)
559 # item icon
560 Icon = Window.GetControl (0)
561 Icon.SetFlags (IE_GUI_BUTTON_PICTURE | IE_GUI_BUTTON_NO_IMAGE, OP_SET)
562 Icon.SetItemIcon (ResRef)
564 # item amount
565 Text = Window.GetControl (6)
566 Text.SetText (str (StackAmount//2) )
567 Text.SetStatus (IE_GUI_EDIT_NUMBER|IE_GUI_CONTROL_FOCUSED)
569 # Decrease
570 Button = Window.GetControl (4)
571 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS,"DecreaseStackAmount")
573 # Increase
574 Button = Window.GetControl (3)
575 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS,"IncreaseStackAmount")
577 # Done
578 Button = Window.GetControl (2)
579 Button.SetText (11973)
580 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "DragItemAmount")
581 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
583 # Cancel
584 Button = Window.GetControl (1)
585 Button.SetText (13727)
586 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OpenItemAmountWindow")
587 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
589 # 0 bmp
590 # 1,2 done/cancel?
591 # 3 +
592 # 4 -
593 # 6 text
595 Window.ShowModal (MODAL_SHADOW_GRAY)
596 return
598 def DrinkItemWindow ():
599 pc = GemRB.GameGetSelectedPCSingle ()
600 slot = GemRB.GetVar ("ItemButton")
601 # the drink item header is always the first
602 GemRB.UseItem (pc, slot, 0)
603 CloseItemInfoWindow ()
604 return
606 def OpenErrorWindow (strref):
607 """Opens the error window and displays the string."""
609 global ErrorWindow
610 pc = GemRB.GameGetSelectedPCSingle ()
612 ErrorWindow = Window = GemRB.LoadWindow (7)
613 Button = Window.GetControl (0)
614 Button.SetText (11973)
615 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "CloseErrorWindow")
616 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
618 TextArea = Window.GetControl (3)
619 TextArea.SetText (strref)
620 Window.ShowModal (MODAL_SHADOW_GRAY)
621 return
623 def CloseErrorWindow ():
624 if ErrorWindow:
625 ErrorWindow.Unload ()
626 UpdateInventoryWindow ()
627 return
629 def ReadItemWindow ():
630 """Tries to learn the mage scroll."""
632 pc = GemRB.GameGetSelectedPCSingle ()
633 slot = GemRB.GetVar ("ItemButton")
634 ret = 0
636 slot_item = GemRB.GetSlotItem (pc, slot)
637 spell_ref = GemRB.GetItem (slot_item['ItemResRef'], pc)['Spell']
638 spell = GemRB.GetSpell (spell_ref)
639 if spell:
640 # can we learn more spells of this level?
641 spell_count = GemRB.GetKnownSpellsCount (pc, IE_SPELL_TYPE_WIZARD, spell['SpellLevel']-1)
642 if spell_count > GemRB.GetAbilityBonus (IE_INT, 2, GemRB.GetPlayerStat (pc, IE_INT)):
643 ret = LSR_FULL
644 strref = 32097
645 else:
646 if GemRB.LearnSpell (pc, spell_ref, LS_STATS|LS_ADDXP):
647 ret = LSR_FAILED
648 strref = 10831 # failure
649 else:
650 strref = 10830 # success
651 GemRB.RemoveItem (pc, slot)
652 else:
653 print "WARNING: invalid spell header in item", slot_item['ItemResRef']
654 CloseItemInfoWindow ()
655 return -1
657 CloseItemInfoWindow ()
658 OpenErrorWindow (strref)
660 return ret
662 def OpenItemWindow ():
663 #close inventory
664 GemRB.SetVar ("Inventory", 1)
665 if ItemInfoWindow:
666 ItemInfoWindow.Unload ()
667 OpenInventoryWindow ()
668 pc = GemRB.GameGetSelectedPCSingle ()
669 slot = GemRB.GetVar ("ItemButton")
670 slot_item = GemRB.GetSlotItem (pc, slot)
671 ResRef = slot_item['ItemResRef']
672 #the store will have to reopen the inventory
673 GemRB.EnterStore (ResRef)
674 return
676 def DialogItemWindow ():
677 if ItemInfoWindow:
678 ItemInfoWindow.Unload ()
679 OpenInventoryWindow ()
680 pc = GemRB.GameGetSelectedPCSingle ()
681 slot = GemRB.GetVar ("ItemButton")
682 slot_item = GemRB.GetSlotItem (pc, slot)
683 ResRef = slot_item['ItemResRef']
684 item = GemRB.GetItem (ResRef)
685 dialog=item["Dialog"]
686 GemRB.ExecuteString ("StartDialog(\""+dialog+"\",Myself)", pc)
687 return
689 def IdentifyUseSpell ():
690 global ItemIdentifyWindow
692 pc = GemRB.GameGetSelectedPCSingle ()
693 slot = GemRB.GetVar ("ItemButton")
694 if ItemIdentifyWindow:
695 ItemIdentifyWindow.Unload ()
696 GemRB.HasSpecialSpell (pc, 1, 1)
697 if ItemInfoWindow:
698 ItemInfoWindow.Unload ()
699 GemRB.ChangeItemFlag (pc, slot, IE_INV_ITEM_IDENTIFIED, OP_OR)
700 OpenItemInfoWindow ()
701 return
703 def IdentifyUseScroll ():
704 global ItemIdentifyWindow
706 pc = GemRB.GameGetSelectedPCSingle ()
707 slot = GemRB.GetVar ("ItemButton")
708 if ItemIdentifyWindow:
709 ItemIdentifyWindow.Unload ()
710 if ItemInfoWindow:
711 ItemInfoWindow.Unload ()
712 if GemRB.HasSpecialItem (pc, 1, 1):
713 GemRB.ChangeItemFlag (pc, slot, IE_INV_ITEM_IDENTIFIED, OP_OR)
714 OpenItemInfoWindow ()
715 return
717 def CloseIdentifyItemWindow ():
718 if ItemIdentifyWindow:
719 ItemIdentifyWindow.Unload ()
720 return
722 def IdentifyItemWindow ():
723 global ItemIdentifyWindow
725 pc = GemRB.GameGetSelectedPCSingle ()
727 ItemIdentifyWindow = Window = GemRB.LoadWindow (9)
728 Button = Window.GetControl (0)
729 Button.SetText (17105)
730 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "IdentifyUseSpell")
731 if not GemRB.HasSpecialSpell (pc, 1, 0):
732 Button.SetState (IE_GUI_BUTTON_DISABLED)
734 Button = Window.GetControl (1)
735 Button.SetText (17106)
736 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "IdentifyUseScroll")
737 if not GemRB.HasSpecialItem (pc, 1, 0):
738 Button.SetState (IE_GUI_BUTTON_DISABLED)
740 Button = Window.GetControl (2)
741 Button.SetText (13727)
742 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "CloseIdentifyItemWindow")
743 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
745 TextArea = Window.GetControl (3)
746 TextArea.SetText (19394)
747 Window.ShowModal (MODAL_SHADOW_GRAY)
748 return
750 def CloseItemInfoWindow ():
751 if ItemInfoWindow:
752 ItemInfoWindow.Unload ()
753 UpdateInventoryWindow ()
754 return
756 def DisplayItem (itemresref, type):
757 global ItemInfoWindow
759 item = GemRB.GetItem (itemresref)
760 ItemInfoWindow = Window = GemRB.LoadWindow (5)
762 #fake label
763 Label = Window.GetControl (0x10000000)
764 Label.SetText ("")
766 #item icon
767 Button = Window.GetControl (2)
768 Button.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
769 Button.SetItemIcon (itemresref,0)
770 Button.SetState (IE_GUI_BUTTON_LOCKED)
772 #middle button
773 Button = Window.GetControl (4)
774 Button.SetText (11973)
775 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "CloseItemInfoWindow")
776 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
778 #textarea
779 Text = Window.GetControl (5)
780 if (type&2):
781 text = item["ItemDesc"]
782 else:
783 text = item["ItemDescIdentified"]
784 Text.SetText (text)
786 #left button
787 Button = Window.GetControl (8)
789 if type&2:
790 Button.SetText (14133)
791 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "IdentifyItemWindow")
792 else:
793 Button.SetState (IE_GUI_BUTTON_LOCKED)
794 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
796 #description icon
797 #Button = Window.GetControl (7)
798 #Button.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
799 #Button.SetItemIcon (itemresref,2)
801 #right button
802 Button = Window.GetControl (9)
803 drink = (type&1) and (item["Function"]&1)
804 read = (type&1) and (item["Function"]&2)
805 container = (type&1) and (item["Function"]&4)
806 dialog = (type&1) and (item["Dialog"]!="")
807 if drink:
808 Button.SetText (19392)
809 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "DrinkItemWindow")
810 elif read and not CannotLearnSlotSpell ():
811 Button.SetText (17104)
812 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "ReadItemWindow")
813 elif container:
814 Button.SetText (24891)
815 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OpenItemWindow")
816 elif dialog:
817 Button.SetText (item["DialogName"])
818 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "DialogItemWindow")
819 else:
820 Button.SetState (IE_GUI_BUTTON_LOCKED)
821 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
823 Text = Window.GetControl (0x1000000b)
824 if (type&2):
825 text = item["ItemName"]
826 else:
827 text = item["ItemNameIdentified"]
828 Text.SetText (text)
830 ItemInfoWindow.ShowModal (MODAL_SHADOW_GRAY)
831 return
833 def OpenItemInfoWindow ():
834 pc = GemRB.GameGetSelectedPCSingle ()
835 slot = GemRB.GetVar ("ItemButton")
836 slot_item = GemRB.GetSlotItem (pc, slot)
837 item = GemRB.GetItem (slot_item["ItemResRef"])
839 #auto identify when lore is high enough
840 if item["LoreToID"]<=GemRB.GetPlayerStat (pc, IE_LORE):
841 GemRB.ChangeItemFlag (pc, slot, IE_INV_ITEM_IDENTIFIED, OP_OR)
842 slot_item["Flags"] |= IE_INV_ITEM_IDENTIFIED
843 UpdateInventoryWindow ()
845 if slot_item["Flags"] & IE_INV_ITEM_IDENTIFIED:
846 value = 1
847 else:
848 value = 3
849 DisplayItem (slot_item["ItemResRef"], value)
850 return
852 def OpenGroundItemInfoWindow ():
853 global ItemInfoWindow
855 pc = GemRB.GameGetSelectedPCSingle ()
856 slot = GemRB.GetVar ("TopIndex") + GemRB.GetVar ("GroundItemButton")
857 slot_item = GemRB.GetContainerItem (pc, slot)
859 #the ground items are only displayable
860 if slot_item["Flags"] & IE_INV_ITEM_IDENTIFIED:
861 value = 0
862 else:
863 value = 2
864 DisplayItem (slot_item["ItemResRef"], value)
865 return
867 def MouseEnterSlot ():
868 global OverSlot
870 pc = GemRB.GameGetSelectedPCSingle ()
871 OverSlot = GemRB.GetVar ("ItemButton")
872 if GemRB.IsDraggingItem ()==1:
873 UpdateSlot (pc, OverSlot-1)
874 return
876 def MouseLeaveSlot ():
877 global OverSlot
879 pc = GemRB.GameGetSelectedPCSingle ()
880 slot = GemRB.GetVar ("ItemButton")
881 if slot == OverSlot or not GemRB.IsDraggingItem ():
882 OverSlot = None
883 UpdateSlot (pc, slot-1)
884 return
886 def MouseEnterGround ():
887 Window = InventoryWindow
888 i = GemRB.GetVar ("GroundItemButton")
889 Button = Window.GetControl (i+68)
890 if GemRB.IsDraggingItem ()==1:
891 Button.SetState (IE_GUI_BUTTON_SELECTED)
892 return
894 def MouseLeaveGround ():
895 Window = InventoryWindow
896 i = GemRB.GetVar ("GroundItemButton")
897 Button = Window.GetControl (i+68)
898 if GemRB.IsDraggingItem ()==1:
899 Button.SetState (IE_GUI_BUTTON_SECOND)
900 return
902 ###################################################
903 #End of file GUIINV.py