factored out the EFFv2 saving into EFFImporter
[gemrb.git] / gemrb / GUIScripts / InventoryCommon.py
blob39a7da9bdcfa176c334a289f1373c1c470fa4ece
1 # -*-python-*-
2 # GemRB - Infinity Engine Emulator
3 # Copyright (C) 2010 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 import GemRB
21 import GUICommon
22 from GUIDefines import *
23 from ie_stats import *
24 from ie_slots import *
25 from ie_spells import *
27 OverSlot = None
28 UsedSlot = None
29 ItemInfoWindow = None
30 ItemAmountWindow = None
31 ItemIdentifyWindow = None
32 ItemAbilitiesWindow = None
33 ErrorWindow = None
34 StackAmount = 0
36 def OnDragItemGround ():
37 """Drops and item to the ground."""
39 pc = GemRB.GameGetSelectedPCSingle ()
40 slot = GemRB.GetVar ("GroundItemButton") + GemRB.GetVar ("TopIndex")
42 if GemRB.IsDraggingItem ()==0:
43 slot_item = GemRB.GetContainerItem (pc, slot)
44 item = GemRB.GetItem (slot_item["ItemResRef"])
45 GemRB.DragItem (pc, slot, item["ItemIcon"], 0, 1) #container
46 if GUICommon.GameIsPST():
47 GemRB.PlaySound (item["DescIcon"])
48 else:
49 GemRB.DropDraggedItem (pc, -2) #dropping on ground
51 GUIINV.UpdateInventoryWindow ()
52 return
54 def OnAutoEquip ():
55 """Auto-equips equipment if possible."""
57 if GemRB.IsDraggingItem ()!=1:
58 return
60 pc = GemRB.GameGetSelectedPCSingle ()
62 #-1 : drop stuff in equipable slots (but not inventory)
63 GemRB.DropDraggedItem (pc, -1)
65 if GemRB.IsDraggingItem ()==1:
66 GemRB.PlaySound("GAM_47") #failed equip
68 GUIINV.UpdateInventoryWindow ()
69 return
71 def OnDragItem ():
72 """Updates dragging."""
74 if GemRB.IsDraggingItem()==2:
75 return
77 #don't call when splitting items
78 if ItemAmountWindow != None:
79 return
81 pc = GemRB.GameGetSelectedPCSingle ()
82 slot = GemRB.GetVar ("ItemButton")
83 slot_item = GemRB.GetSlotItem (pc, slot)
85 if not GemRB.IsDraggingItem ():
86 item = GemRB.GetItem (slot_item["ItemResRef"])
87 GemRB.DragItem (pc, slot, item["ItemIcon"], 0, 0)
88 else:
89 SlotType = GemRB.GetSlotType (slot, pc)
90 #special monk check
91 if GemRB.GetPlayerStat (pc, IE_CLASS)==0x14:
92 #offhand slot mark
93 if SlotType["Effects"]==TYPE_OFFHAND:
94 SlotType["ResRef"]=""
95 GemRB.DisplayString (61355, 0xffffff)
97 if SlotType["ResRef"]!="":
98 if slot_item:
99 item = GemRB.GetItem (slot_item["ItemResRef"])
100 #drag items into a bag
101 if item["Function"]&4:
102 #first swap them
103 GemRB.DropDraggedItem (pc, slot)
104 #enter the store
105 GemRB.EnterStore (slot_item["ItemResRef"])
106 #if it is possible to add, then do it
107 if GemRB.IsValidStoreItem (pc, slot, 0)&SHOP_SELL:
108 GemRB.ChangeStoreItem (pc, slot, SHOP_SELL)
109 else:
110 GemRB.DisplayString( 9375, 0xfffffff)
111 #leave (save) store
112 GemRB.LeaveStore()
114 GemRB.DropDraggedItem (pc, slot)
116 GUIINV.UpdateInventoryWindow ()
117 return
119 def OnDropItemToPC ():
120 """Gives an item to another character."""
122 pc = GemRB.GetVar ("PressedPortrait")
124 #-3 : drop stuff in inventory (but not equippable slots)
125 GemRB.DropDraggedItem (pc, -3)
126 if GemRB.IsDraggingItem ()==1:
127 if GUICommon.HasTOB ():
128 GemRB.DisplayString (61794, 0xfffffff)
129 elif GUICommon.GameIsPST ():
130 GemRB.DisplayString (19257, 0xfffffff)
131 else:
132 GemRB.DisplayString (17999, 0xfffffff)
134 GUIINV.UpdateInventoryWindow ()
135 return
137 def DecreaseStackAmount ():
138 """Decreases the split size."""
140 Text = ItemAmountWindow.GetControl (6)
141 Amount = Text.QueryText ()
142 number = int ("0"+Amount)-1
143 if number<1:
144 number=1
145 Text.SetText (str (number))
146 return
148 def IncreaseStackAmount ():
149 """Increases the split size."""
151 Text = ItemAmountWindow.GetControl (6)
152 Amount = Text.QueryText ()
153 number = int ("0"+Amount)+1
154 if number>StackAmount:
155 number=StackAmount
156 Text.SetText (str (number))
157 return
159 def DragItemAmount ():
160 """Drag a split item."""
162 pc = GemRB.GameGetSelectedPCSingle ()
164 #emergency dropping
165 if GemRB.IsDraggingItem()==1:
166 GemRB.DropDraggedItem (pc, UsedSlot)
167 UpdateSlot (pc, UsedSlot-1)
169 slot_item = GemRB.GetSlotItem (pc, UsedSlot)
171 #if dropping didn't help, don't die if slot_item isn't here
172 if slot_item:
173 Text = ItemAmountWindow.GetControl (6)
174 Amount = Text.QueryText ()
175 item = GemRB.GetItem (slot_item["ItemResRef"])
176 GemRB.DragItem (pc, UsedSlot, item["ItemIcon"], int ("0"+Amount), 0)
177 OpenItemAmountWindow ()
178 return
180 def MouseEnterSlot ():
181 global OverSlot
183 pc = GemRB.GameGetSelectedPCSingle ()
184 OverSlot = GemRB.GetVar ("ItemButton")
185 if GemRB.IsDraggingItem ()==1:
186 UpdateSlot (pc, OverSlot-1)
187 return
189 def MouseLeaveSlot ():
190 global OverSlot
192 pc = GemRB.GameGetSelectedPCSingle ()
193 slot = GemRB.GetVar ("ItemButton")
194 if slot == OverSlot or not GemRB.IsDraggingItem ():
195 OverSlot = None
196 UpdateSlot (pc, slot-1)
197 return
199 def MouseEnterGround ():
200 Window = GUIINV.InventoryWindow
202 if GUICommon.GameIsPST():
203 offset = 47
204 else:
205 offset = 68
206 i = GemRB.GetVar ("GroundItemButton")
207 Button = Window.GetControl (i+offset)
209 if GemRB.IsDraggingItem ()==1:
210 Button.SetState (IE_GUI_BUTTON_SELECTED)
211 return
213 def MouseLeaveGround ():
214 Window = GUIINV.InventoryWindow
216 if GUICommon.GameIsPST():
217 offset = 47
218 else:
219 offset = 68
220 i = GemRB.GetVar ("GroundItemButton")
221 Button = Window.GetControl (i+offset)
223 if GemRB.IsDraggingItem ()==1:
224 Button.SetState (IE_GUI_BUTTON_SECOND)
225 return
227 def CloseItemInfoWindow ():
228 if ItemInfoWindow:
229 ItemInfoWindow.Unload ()
230 GUIINV.UpdateInventoryWindow ()
231 return
233 def DisplayItem (itemresref, type):
234 global ItemInfoWindow
236 item = GemRB.GetItem (itemresref)
237 ItemInfoWindow = Window = GemRB.LoadWindow (5)
239 if GUICommon.GameIsPST():
240 strrefs = [ 1403, 4256, 4255, 4251, 4252, 4254, 4279 ]
241 else:
242 strrefs = [ 11973, 14133, 11960, 19392, 17104, item["DialogName"], 17108 ]
244 # item name
245 Label = Window.GetControl (0x10000000)
246 if (type&2):
247 text = item["ItemName"]
248 else:
249 text = item["ItemNameIdentified"]
250 Label.SetText (text)
252 #item icon
253 Button = Window.GetControl (2)
254 if GUICommon.GameIsPST():
255 Button.SetFlags (IE_GUI_BUTTON_PICTURE | IE_GUI_BUTTON_NO_IMAGE, OP_SET)
256 Button.SetItemIcon (itemresref)
257 else:
258 Button.SetFlags (IE_GUI_BUTTON_PICTURE, OP_OR)
259 Button.SetItemIcon (itemresref,0)
260 Button.SetState (IE_GUI_BUTTON_LOCKED)
262 #middle button
263 Button = Window.GetControl (4)
264 Button.SetText (strrefs[0])
265 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseItemInfoWindow)
266 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
268 #textarea
269 Text = Window.GetControl (5)
270 if (type&2):
271 text = item["ItemDesc"]
272 else:
273 text = item["ItemDescIdentified"]
274 Text.SetText (text)
276 #left button
277 Button = Window.GetControl(8)
278 select = (type&1) and (item["Function"]&8)
280 if type&2:
281 Button.SetText (strrefs[1])
282 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, IdentifyItemWindow)
283 elif select and not GUICommon.GameIsPST():
284 Button.SetText (strrefs[2])
285 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbilitiesItemWindow)
286 else:
287 Button.SetState (IE_GUI_BUTTON_LOCKED)
288 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
290 #description icon
291 if GUICommon.GameIsBG2() or GUICommon.GameIsPST():
292 Button = Window.GetControl (7)
293 Button.SetFlags (IE_GUI_BUTTON_PICTURE | IE_GUI_BUTTON_CENTER_PICTURES | IE_GUI_BUTTON_NO_IMAGE, OP_OR)
294 if GUICommon.GameIsPST():
295 Button.SetItemIcon (itemresref, 1) # no DescIcon
296 else:
297 Button.SetItemIcon (itemresref, 2)
298 Button.SetState (IE_GUI_BUTTON_LOCKED)
300 #right button
301 Button = Window.GetControl(9)
302 drink = (type&1) and (item["Function"]&1)
303 read = (type&1) and (item["Function"]&2)
304 #sorcerors cannot learn spells
305 # FIXME: unhardcode
306 pc = GemRB.GameGetSelectedPCSingle ()
307 if GemRB.GetPlayerStat (pc, IE_CLASS) == 19:
308 read = 0
309 container = (type&1) and (item["Function"]&4)
310 dialog = (type&1) and (item["Dialog"]!="")
311 familiar = (type&1) and (item["Type"] == 38)
312 if drink:
313 Button.SetText (strrefs[3])
314 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, DrinkItemWindow)
315 elif read:
316 Button.SetText (strrefs[4])
317 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, ReadItemWindow)
318 elif container:
319 if GUICommon.GameIsIWD2() or GUICommon.GameIsHOW():
320 Button.SetText (24891) # Open Container
321 elif GUICommon.GameIsBG2():
322 Button.SetText (44002) # open container
323 else:
324 # a fallback, since the originals have nothing appropriate
325 # FIXME: where do mods add the new string? This is untranslatable
326 Button.SetText ("Open container")
327 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenItemWindow)
328 elif dialog:
329 Button.SetText (strrefs[5])
330 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, DialogItemWindow)
331 elif familiar:
332 Button.SetText (4373)
333 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, ReleaseFamiliar)
334 else:
335 Button.SetState (IE_GUI_BUTTON_LOCKED)
336 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
338 Label = Window.HasControl(0x1000000b)
339 if Label:
340 Label = Window.GetControl (0x1000000b)
341 if (type&2):
342 # NOT IDENTIFIED
343 Label.SetText (strrefs[6])
344 else:
345 Label.SetText ("")
347 # in pst one can cycle through all the items from the description window
348 if Window.HasControl (14):
349 #left scroll
350 Button = Window.GetControl (13)
351 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, GUIINV.LeftItemScroll)
353 #right scroll
354 Button = Window.GetControl (14)
355 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, GUIINV.RightItemScroll)
357 ItemInfoWindow.ShowModal(MODAL_SHADOW_GRAY)
358 return
360 def OpenItemInfoWindow ():
361 pc = GemRB.GameGetSelectedPCSingle ()
362 if GUICommon.GameIsPST():
363 slot, slot_item = GUIINV.ItemHash[GemRB.GetVar ('ItemButton')]
364 else:
365 slot = GemRB.GetVar ("ItemButton")
366 slot_item = GemRB.GetSlotItem (pc, slot)
367 item = GemRB.GetItem (slot_item["ItemResRef"])
369 #auto identify when lore is high enough
370 if item["LoreToID"]<=GemRB.GetPlayerStat (pc, IE_LORE):
371 GemRB.ChangeItemFlag (pc, slot, IE_INV_ITEM_IDENTIFIED, OP_OR)
372 slot_item["Flags"] |= IE_INV_ITEM_IDENTIFIED
373 GUIINV.UpdateInventoryWindow ()
375 if slot_item["Flags"] & IE_INV_ITEM_IDENTIFIED:
376 value = 1
377 else:
378 value = 3
379 DisplayItem (slot_item["ItemResRef"], value)
380 return
382 def OpenGroundItemInfoWindow ():
383 global ItemInfoWindow
385 pc = GemRB.GameGetSelectedPCSingle ()
386 slot = GemRB.GetVar("TopIndex")+GemRB.GetVar("GroundItemButton")
387 slot_item = GemRB.GetContainerItem (pc, slot)
389 #the ground items are only displayable
390 if slot_item["Flags"] & IE_INV_ITEM_IDENTIFIED:
391 value = 0
392 else:
393 value = 2
394 DisplayItem(slot_item["ItemResRef"], value)
395 return
397 def OpenItemAmountWindow ():
398 """Open the split window."""
400 global UsedSlot, OverSlot
401 global ItemAmountWindow, StackAmount
403 pc = GemRB.GameGetSelectedPCSingle ()
405 if ItemAmountWindow != None:
406 if ItemAmountWindow:
407 ItemAmountWindow.Unload ()
408 ItemAmountWindow = None
409 GemRB.SetRepeatClickFlags (GEM_RK_DISABLE, OP_OR)
410 UsedSlot = None
411 OverSlot = None
412 GUIINV.UpdateInventoryWindow()
413 return
415 UsedSlot = GemRB.GetVar ("ItemButton")
416 if GemRB.IsDraggingItem ()==1:
417 GemRB.DropDraggedItem (pc, UsedSlot)
418 #redraw slot
419 UpdateSlot (pc, UsedSlot-1)
420 # disallow splitting while holding split items (double splitting)
421 if GemRB.IsDraggingItem () == 1:
422 return
424 slot_item = GemRB.GetSlotItem (pc, UsedSlot)
426 if slot_item:
427 StackAmount = slot_item["Usages0"]
428 else:
429 StackAmount = 0
430 if StackAmount<=1:
431 UpdateSlot (pc, UsedSlot-1)
432 return
434 ResRef = slot_item['ItemResRef']
435 item = GemRB.GetItem (ResRef)
437 ItemAmountWindow = Window = GemRB.LoadWindow (4)
439 # item icon
440 Icon = Window.GetControl (0)
441 Icon.SetFlags (IE_GUI_BUTTON_PICTURE | IE_GUI_BUTTON_NO_IMAGE, OP_SET)
442 Icon.SetItemIcon (ResRef)
444 # item amount
445 Text = Window.GetControl (6)
446 # FIXME: use a proper size
447 # FIXME: fix it for all the games
448 if GUICommon.GameIsIWD2():
449 Text.SetSize (40, 40)
450 Text.SetText (str (StackAmount//2))
451 Text.SetStatus (IE_GUI_EDIT_NUMBER|IE_GUI_CONTROL_FOCUSED)
453 # Decrease
454 Button = Window.GetControl (4)
455 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, DecreaseStackAmount)
457 # Increase
458 Button = Window.GetControl (3)
459 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, IncreaseStackAmount)
461 # Done
462 Button = Window.GetControl (2)
463 Button.SetText (11973)
464 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, DragItemAmount)
465 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
467 # Cancel
468 Button = Window.GetControl (1)
469 Button.SetText (13727)
470 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenItemAmountWindow)
471 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
473 #GemRB.UnhideGUI ()
474 GemRB.SetRepeatClickFlags (GEM_RK_DISABLE, OP_NAND)
475 Window.ShowModal (MODAL_SHADOW_GRAY)
476 return
478 def UpdateSlot (pc, slot):
479 """Updates a specific slot."""
481 Window = GUIINV.InventoryWindow
482 SlotType = GemRB.GetSlotType (slot+1, pc)
483 ControlID = SlotType["ID"]
485 if not ControlID:
486 return
488 if GemRB.IsDraggingItem ()==1:
489 #get dragged item
490 drag_item = GemRB.GetSlotItem (0,0)
491 itemname = drag_item["ItemResRef"]
492 drag_item = GemRB.GetItem (itemname)
493 else:
494 itemname = ""
496 Button = Window.GetControl (ControlID)
497 slot_item = GemRB.GetSlotItem (pc, slot+1)
499 Button.SetEvent (IE_GUI_BUTTON_ON_DRAG_DROP, OnDragItem)
500 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_NAND)
501 GUICommon.UpdateInventorySlot (pc, Button, slot_item, "inventory", SlotType["Type"]&SLOT_INVENTORY == 0)
503 if slot_item:
504 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OnDragItem)
505 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, OpenItemInfoWindow)
506 Button.SetEvent (IE_GUI_BUTTON_ON_SHIFT_PRESS, OpenItemAmountWindow)
507 else:
508 if SlotType["ResRef"]=="*":
509 Button.SetBAM ("",0,0)
510 Button.SetTooltip (SlotType["Tip"])
511 itemname = ""
512 elif SlotType["ResRef"]=="":
513 Button.SetBAM ("",0,0)
514 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_OR)
515 Button.SetTooltip ("")
516 itemname = ""
517 else:
518 Button.SetBAM (SlotType["ResRef"],0,0)
519 Button.SetTooltip (SlotType["Tip"])
521 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, None)
522 Button.SetEvent (IE_GUI_BUTTON_ON_RIGHT_PRESS, None)
523 Button.SetEvent (IE_GUI_BUTTON_ON_SHIFT_PRESS, None)
524 Button.SetEvent (IE_GUI_BUTTON_ON_DOUBLE_PRESS, OpenItemAmountWindow)
526 if OverSlot == slot+1:
527 if GemRB.CanUseItemType (SlotType["Type"], itemname):
528 Button.SetState (IE_GUI_BUTTON_SELECTED)
529 else:
530 Button.SetState (IE_GUI_BUTTON_ENABLED)
531 else:
532 if (SlotType["Type"]&SLOT_INVENTORY) or not GemRB.CanUseItemType (SlotType["Type"], itemname):
533 Button.SetState (IE_GUI_BUTTON_ENABLED)
534 else:
535 Button.SetState (IE_GUI_BUTTON_SECOND)
537 if slot_item and (GemRB.GetEquippedQuickSlot (pc)==slot+1 or GemRB.GetEquippedAmmunition (pc)==slot+1):
538 Button.SetState (IE_GUI_BUTTON_THIRD)
540 return
542 def CancelColor():
543 if ColorPicker:
544 ColorPicker.Unload ()
545 GUIINV.InventoryWindow.SetVisible (WINDOW_VISIBLE)
546 return
548 def ColorDonePress():
549 """Saves the selected colors."""
551 pc = GemRB.GameGetSelectedPCSingle ()
553 if ColorPicker:
554 ColorPicker.Unload ()
556 ColorTable = GemRB.LoadTable ("clowncol")
557 PickedColor=ColorTable.GetValue (ColorIndex, GemRB.GetVar ("Selected"))
558 if ColorIndex==0:
559 GUICommon.SetColorStat (pc, IE_HAIR_COLOR, PickedColor)
560 elif ColorIndex==1:
561 GUICommon.SetColorStat (pc, IE_SKIN_COLOR, PickedColor)
562 elif ColorIndex==2:
563 GUICommon.SetColorStat (pc, IE_MAJOR_COLOR, PickedColor)
564 else:
565 GUICommon.SetColorStat (pc, IE_MINOR_COLOR, PickedColor)
566 GUIINV.UpdateInventoryWindow ()
567 return
569 def HairPress():
570 global ColorIndex, PickedColor
572 pc = GemRB.GameGetSelectedPCSingle ()
573 ColorIndex = 0
574 PickedColor = GemRB.GetPlayerStat (pc, IE_HAIR_COLOR, 1) & 0xFF
575 GetColor()
576 return
578 def SkinPress():
579 global ColorIndex, PickedColor
581 pc = GemRB.GameGetSelectedPCSingle ()
582 ColorIndex = 1
583 PickedColor = GemRB.GetPlayerStat (pc, IE_SKIN_COLOR, 1) & 0xFF
584 GetColor()
585 return
587 def MajorPress():
588 """Selects the major color."""
589 global ColorIndex, PickedColor
591 pc = GemRB.GameGetSelectedPCSingle ()
592 ColorIndex = 2
593 PickedColor = GemRB.GetPlayerStat (pc, IE_MAJOR_COLOR, 1) & 0xFF
594 GetColor()
595 return
597 def MinorPress():
598 """Selects the minor color."""
599 global ColorIndex, PickedColor
601 pc = GemRB.GameGetSelectedPCSingle ()
602 ColorIndex = 3
603 PickedColor = GemRB.GetPlayerStat (pc, IE_MINOR_COLOR, 1) & 0xFF
604 GetColor()
605 return
607 def GetColor():
608 """Opens the color selection window."""
610 global ColorPicker
612 ColorTable = GemRB.LoadTable ("clowncol")
613 GUIINV.InventoryWindow.SetVisible (WINDOW_GRAYED) #darken it
614 ColorPicker=GemRB.LoadWindow (3)
615 GemRB.SetVar ("Selected",-1)
616 if GUICommon.GameIsIWD2():
617 Button = ColorPicker.GetControl (35)
618 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CancelColor)
619 Button.SetText(13727)
621 for i in range (34):
622 Button = ColorPicker.GetControl (i)
623 Button.SetState (IE_GUI_BUTTON_DISABLED)
624 Button.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_RADIOBUTTON,OP_OR)
626 Selected = -1
627 for i in range (34):
628 MyColor = ColorTable.GetValue (ColorIndex, i)
629 if MyColor == "*":
630 break
631 Button = ColorPicker.GetControl (i)
632 Button.SetBAM ("COLGRAD", 2, 0, MyColor)
633 if PickedColor == MyColor:
634 GemRB.SetVar ("Selected",i)
635 Selected = i
636 Button.SetState (IE_GUI_BUTTON_ENABLED)
637 Button.SetVarAssoc ("Selected",i)
638 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, ColorDonePress)
639 ColorPicker.SetVisible (WINDOW_VISIBLE)
640 return
642 def ReleaseFamiliar ():
643 """Simple Use Item"""
645 pc = GemRB.GameGetSelectedPCSingle ()
646 slot = GemRB.GetVar ("ItemButton")
647 # the header is always the first, target is always self
648 GemRB.UseItem (pc, slot, 0, 5)
649 CloseItemInfoWindow ()
650 return
652 def DrinkItemWindow ():
653 """Drink the potion"""
655 pc = GemRB.GameGetSelectedPCSingle ()
656 slot = GemRB.GetVar ("ItemButton")
657 # the drink item header is always the first
658 GemRB.UseItem (pc, slot, 0)
659 CloseItemInfoWindow ()
660 return
662 def OpenErrorWindow (strref):
663 """Opens the error window and displays the string."""
665 global ErrorWindow
666 pc = GemRB.GameGetSelectedPCSingle ()
668 ErrorWindow = Window = GemRB.LoadWindow (7)
669 Button = Window.GetControl (0)
670 Button.SetText (11973)
671 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseErrorWindow)
672 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
674 TextArea = Window.GetControl (3)
675 TextArea.SetText (strref)
676 Window.ShowModal (MODAL_SHADOW_GRAY)
677 return
679 def CloseErrorWindow ():
680 if ErrorWindow:
681 ErrorWindow.Unload ()
682 GUIINV.UpdateInventoryWindow ()
683 return
685 def ReadItemWindow ():
686 global level, spell_ref
687 """Tries to learn the mage scroll."""
689 pc = GemRB.GameGetSelectedPCSingle ()
690 slot = GemRB.GetVar ("ItemButton")
691 ret = GUICommon.CannotLearnSlotSpell()
693 if ret:
694 strref = 72873
695 CloseItemInfoWindow ()
696 if GUICommon.HasTOB():
697 OpenErrorWindow (strref)
698 return
700 slot_item = GemRB.GetSlotItem (pc, slot)
701 spell_ref = GemRB.GetItem (slot_item['ItemResRef'], pc)['Spell']
702 spell = GemRB.GetSpell (spell_ref)
703 if spell:
704 # can we learn more spells of this level?
705 level = spell['SpellLevel']-1
706 spell_count = GemRB.GetKnownSpellsCount (pc, IE_SPELL_TYPE_WIZARD, level)
707 if spell_count > GemRB.GetAbilityBonus (IE_INT, 2, GemRB.GetPlayerStat (pc, IE_INT)):
708 ret = LSR_FULL
709 if GUICommon.GameIsBG2():
710 strref = 32097
711 else:
712 strref = -1
713 else:
714 GemRB.UseItem (pc, slot, 1, 5)
715 GemRB.SetTimedEvent(DelayedReadItemWindow, 1)
716 return
717 else:
718 print "WARNING: invalid spell header in item", slot_item['ItemResRef']
719 CloseItemInfoWindow ()
720 return -1
722 CloseItemInfoWindow ()
723 OpenErrorWindow (strref)
725 return ret
727 def DelayedReadItemWindow ():
728 global level, spell_ref
730 pc = GemRB.GameGetSelectedPCSingle ()
731 if GUICommon.HasSpell (pc, IE_SPELL_TYPE_WIZARD, level, spell_ref):
732 strref = 10830
733 else:
734 ret = LSR_FAILED
735 strref = 10831
736 CloseItemInfoWindow ()
737 if not GUICommon.GameIsPST():
738 OpenErrorWindow (strref)
739 return
741 def OpenItemWindow ():
742 """Displays information about the item."""
744 #close inventory
745 GemRB.SetVar ("Inventory", 1)
746 if ItemInfoWindow:
747 ItemInfoWindow.Unload ()
748 GUIINV.OpenInventoryWindow ()
749 pc = GemRB.GameGetSelectedPCSingle ()
750 slot = GemRB.GetVar ("ItemButton")
751 slot_item = GemRB.GetSlotItem (pc, slot)
752 ResRef = slot_item['ItemResRef']
753 #the store will have to reopen the inventory
754 GemRB.EnterStore (ResRef)
755 return
757 def DialogItemWindow ():
758 """Converse with an item."""
760 if ItemInfoWindow:
761 ItemInfoWindow.Unload ()
762 GUIINV.OpenInventoryWindow ()
763 pc = GemRB.GameGetSelectedPCSingle ()
764 if GUICommon.GameIsPST():
765 slot, slot_item = GUIINV.ItemHash[GemRB.GetVar ('ItemButton')]
766 else:
767 slot = GemRB.GetVar ("ItemButton")
768 slot_item = GemRB.GetSlotItem (pc, slot)
769 ResRef = slot_item['ItemResRef']
770 item = GemRB.GetItem (ResRef)
771 dialog=item["Dialog"]
772 GemRB.ExecuteString ("StartDialogOverride(\""+dialog+"\",Myself,0,0,1)", pc)
773 return
775 def IdentifyUseSpell ():
776 """Identifies the item with a memorized spell."""
778 global ItemIdentifyWindow
780 pc = GemRB.GameGetSelectedPCSingle ()
781 slot = GemRB.GetVar ("ItemButton")
782 if ItemIdentifyWindow:
783 ItemIdentifyWindow.Unload ()
784 GemRB.HasSpecialSpell (pc, 1, 1)
785 if ItemInfoWindow:
786 ItemInfoWindow.Unload ()
787 GemRB.ChangeItemFlag (pc, slot, IE_INV_ITEM_IDENTIFIED, OP_OR)
788 OpenItemInfoWindow()
789 return
791 def IdentifyUseScroll ():
792 """Identifies the item with a scroll or other item."""
794 global ItemIdentifyWindow
796 pc = GemRB.GameGetSelectedPCSingle ()
797 slot = GemRB.GetVar ("ItemButton")
798 if ItemIdentifyWindow:
799 ItemIdentifyWindow.Unload ()
800 if ItemInfoWindow:
801 ItemInfoWindow.Unload ()
802 if GemRB.HasSpecialItem (pc, 1, 1):
803 GemRB.ChangeItemFlag (pc, slot, IE_INV_ITEM_IDENTIFIED, OP_OR)
804 OpenItemInfoWindow()
805 return
807 def CloseIdentifyItemWindow ():
808 global ItemIdentifyWindow
810 if ItemIdentifyWindow:
811 ItemIdentifyWindow.Unload ()
812 ItemIdentifyWindow = None
813 return
815 def IdentifyItemWindow ():
816 global ItemIdentifyWindow
818 pc = GemRB.GameGetSelectedPCSingle ()
820 ItemIdentifyWindow = Window = GemRB.LoadWindow (9)
821 Button = Window.GetControl (0)
822 if GUICommon.GameIsPST():
823 Button.SetText (4259)
824 else:
825 Button.SetText (17105)
826 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, IdentifyUseSpell)
827 if not GemRB.HasSpecialSpell (pc, 1, 0):
828 Button.SetState (IE_GUI_BUTTON_DISABLED)
830 Button = Window.GetControl (1)
831 if GUICommon.GameIsPST():
832 Button.SetText (4260)
833 else:
834 Button.SetText (17106)
835 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, IdentifyUseScroll)
836 if not GemRB.HasSpecialItem (pc, 1, 0):
837 Button.SetState (IE_GUI_BUTTON_DISABLED)
839 Button = Window.GetControl (2)
840 if GUICommon.GameIsPST():
841 Button.SetText (4196)
842 else:
843 Button.SetText (13727)
844 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseIdentifyItemWindow)
845 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
847 TextArea = Window.GetControl (3)
848 if GUICommon.GameIsPST():
849 TextArea.SetText (4258)
850 else:
851 TextArea.SetText (19394)
852 Window.ShowModal (MODAL_SHADOW_GRAY)
853 return
855 def DoneAbilitiesItemWindow ():
856 pc = GemRB.GameGetSelectedPCSingle ()
857 slot = GemRB.GetVar ("ItemButton")
858 GemRB.SetupQuickSlot (pc, 0, slot, GemRB.GetVar ("Ability") )
859 CloseAbilitiesItemWindow ()
860 return
862 def CloseAbilitiesItemWindow ():
863 global ItemAbilitiesWindow
865 if ItemAbilitiesWindow:
866 ItemAbilitiesWindow.Unload ()
867 ItemAbilitiesWindow = None
868 return
870 def AbilitiesItemWindow ():
871 global ItemAbilitiesWindow
873 ItemAbilitiesWindow = Window = GemRB.LoadWindow (6)
875 pc = GemRB.GameGetSelectedPCSingle ()
876 slot = GemRB.GetVar ("ItemButton")
877 slot_item = GemRB.GetSlotItem (pc, slot)
878 item = GemRB.GetItem (slot_item["ItemResRef"])
879 Tips = item["Tooltips"]
881 GemRB.SetVar ("Ability", slot_item["Header"])
882 for i in range(3):
883 Button = Window.GetControl (i+1)
884 Button.SetSprites ("GUIBTBUT",i,0,1,2,0)
885 Button.SetFlags (IE_GUI_BUTTON_RADIOBUTTON, OP_OR)
886 Button.SetVarAssoc ("Ability",i)
887 Text = Window.GetControl (i+0x10000003)
888 if i<len(Tips):
889 Button.SetItemIcon (slot_item['ItemResRef'],i+6)
890 Text.SetText (Tips[i])
891 else:
892 #disable button
893 Button.SetItemIcon ("",3)
894 Text.SetText ("")
895 Button.SetState (IE_GUI_BUTTON_DISABLED)
897 TextArea = Window.GetControl (8)
898 TextArea.SetText (11322)
900 Button = Window.GetControl (7)
901 Button.SetText (11973)
902 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, DoneAbilitiesItemWindow)
903 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
905 Button = Window.GetControl (10)
906 Button.SetText (13727)
907 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseAbilitiesItemWindow)
908 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
909 Window.ShowModal (MODAL_SHADOW_GRAY)
910 return
912 import GUIINV