comparison gui/inventorygui.py @ 147:bb29d81d7ce6

Removed the _InventoryGUI class. That was the class for the Old Inventory screen.
author KarstenBock@gmx.net
date Sun, 09 Oct 2011 20:54:03 +0200
parents abfeac863dd6
children 2241722311bf
comparison
equal deleted inserted replaced
146:abfeac863dd6 147:bb29d81d7ce6
386 self.updateImages() 386 self.updateImages()
387 self.gui.show() 387 self.gui.show()
388 388
389 def closeInventory(self): 389 def closeInventory(self):
390 self.gui.hide() 390 self.gui.hide()
391 391
392
393 class _InventoryGUI(ContainerGUIBase):
394 """Inventory GUI class"""
395 def __init__(self, controller, inventory, callbacks):
396 """Initialise the instance.
397 @param controller: Current Controller
398 @type controller: Class derived from ControllerBase
399 @type inventory: Inventory
400 @param inventory: An inventory object to be displayed and manipulated
401 @type callbacks: dict
402 @param callbacks: a dict of callbacks
403 refreshReadyImages:
404 Function that will make the ready slots on the HUD
405 reflect those within the inventory
406 toggleInventoryButton:
407 Function that will toggle the state of the inventory button
408 @return: None"""
409 super(InventoryGUI, self).__init__(controller, "gui/inventory.xml")
410 self.engine = controller.engine
411 self.readyCallback = callbacks['refreshReadyImages']
412 self.toggleInventoryButtonCallback = callbacks['toggleInventoryButton']
413 self.original_cursor_id = self.engine.getCursor().getId()
414
415 self.inventory_shown = False
416 events_to_map = {}
417 self.inventory_storage = inventory
418
419 # Buttons of inventory arranged by slots
420
421 self.slot_buttons = {'head': ('Head',), 'chest': ('Body',),
422 'left_arm': ('LeftHand',),
423 'right_arm': ('RightHand',),
424 'hips' : ('Belt',), 'left_leg': ('LeftFoot',),
425 'right_leg': ('RightFoot',),
426 'left_hand': ('LeftHeld',),
427 'right_hand': ('RightHeld',),
428 'backpack': ('A1', 'A2', 'A3', 'A4', 'A5',
429 'B1', 'B2', 'B3', 'B4', 'B5',
430 'C1', 'C2', 'C3', 'C4', 'C5',
431 'D1', 'D2', 'D3', 'D4', 'D5'),
432 'ready': ('Ready1', 'Ready2', 'Ready3', 'Ready4')
433 }
434 # the images that should be used for the buttons when they are "empty"
435 self.slot_empty_images = {'head':'gui/inv_images/inv_head.png',
436 'chest':'gui/inv_images/inv_torso.png',
437 'left_arm':'gui/inv_images/inv_lhand.png',
438 'right_arm':'gui/inv_images/inv_rhand.png',
439 'hips':'gui/inv_images/inv_belt.png',
440 'left_leg':'gui/inv_images/inv_lfoot.png',
441 'right_leg':'gui/inv_images/inv_rfoot.png',
442 'left_hand':'gui/inv_images/inv_litem.png',
443 'right_hand':'gui/inv_images/inv_ritem.png',
444 'backpack':'gui/inv_images/inv_backpack.png',
445 'ready':'gui/inv_images/inv_belt_pouches.png',
446 }
447 self.updateInventoryButtons()
448
449 for slot in self.slot_buttons:
450 for _, button in enumerate(self.slot_buttons[slot]):
451 events_to_map[button] = cbwa(self.dragDrop, button)
452 events_to_map[button + "/mouseReleased"] = \
453 self.showContextMenu
454 events_to_map['close_button'] = self.closeInventoryAndToggle
455 self.gui.mapEvents(events_to_map)
456 # TODO: Why the commented out code?
457 # self.resetMouseCursor()
458
459 def updateImages(self):
460 self.updateInventoryButtons()
461
462 def updateInventoryButtons (self):
463 for slot in self.slot_buttons:
464 for index, button in enumerate(self.slot_buttons[slot]):
465 widget = self.gui.findChild(name=button)
466 widget.slot = slot
467 widget.index = index
468 widget.item = self.inventory_storage.getItemsInSlot(widget.slot,
469 widget.index)
470 self.updateImage(widget)
471
472 def updateImage(self, button):
473 if (button.item == None):
474 image = self.slot_empty_images[button.slot]
475 else:
476 image = button.item.getInventoryThumbnail()
477 button.up_image = image
478 button.down_image = image
479 button.hover_image = image
480
481 def closeInventory(self):
482 """Close the inventory.
483 @return: None"""
484 self.gui.hide()
485
486 def closeInventoryAndToggle(self):
487 """Close the inventory screen.
488 @return: None"""
489 self.closeInventory()
490 self.toggleInventoryButtonCallback()
491 self.inventory_shown = False
492
493 def toggleInventory(self, toggleImage=True):
494 """Pause the game and enter the inventory screen, or close the
495 inventory screen and resume the game.
496 @type toggleImage: bool
497 @param toggleImage:
498 Call toggleInventoryCallback if True. Toggling via a
499 keypress requires that we toggle the Hud inventory image
500 explicitly. Clicking on the Hud inventory button toggles the
501 image implicitly, so we don't change it.
502 @return: None"""
503 if not self.inventory_shown:
504 self.showInventory()
505 self.inventory_shown = True
506 else:
507 self.closeInventory()
508 self.inventory_shown = False
509
510 if toggleImage:
511 self.toggleInventoryButtonCallback()
512
513 def showInventory(self):
514 """Show the inventory.
515 @return: None"""
516 self.updateInventoryButtons()
517 self.gui.show()
518
519 def dragObject(self, obj):
520 """Drag the selected object.
521 @type obj: string
522 @param obj: The name of the object within
523 the dictionary 'self.buttons'
524 @return: None"""
525 # get the widget from the inventory with the name obj
526 drag_widget = self.gui.findChild(name = obj)
527 drag_item = drag_widget.item
528 # only drag if the widget is not empty
529 if (drag_item != None):
530 # get the item that the widget is 'storing'
531 data_drag.dragged_item = drag_widget.item
532 # get the up and down images of the widget
533 up_image = drag_widget.up_image
534 down_image = drag_widget.down_image
535 # set the mouse cursor to be the widget's image
536 self.controller.setMouseCursor(up_image.source,down_image.source)
537 data_drag.dragged_image = up_image.source
538 data_drag.dragging = True
539 data_drag.dragged_widget = drag_widget
540 data_drag.source_container = self.inventory_storage
541
542 self.inventory_storage.takeItem(drag_widget.item)
543 # after dragging the 'item', set the widgets' images
544 # so that it has it's default 'empty' images
545 drag_widget.item = None
546 self.updateImage(drag_widget)
547
548
549 def dropObject(self, obj):
550 """Drops the object being dropped
551 @type obj: string
552 @param obj: The name of the object within
553 the dictionary 'self.buttons'
554 @return: None"""
555 drop_widget = self.gui.findChild(name = obj)
556 drop_slot, drop_index = drop_widget.slot, drop_widget.index
557 replace_item = None
558 try :
559 if data_drag.dragging:
560 inventory = self.inventory_storage
561 drag_item = data_drag.dragged_item
562 #this will get the replacement item and data for drag_drop if
563 ## there is an item All ready occupying the slot
564 if not inventory.isSlotEmpty(drop_slot, drop_index):
565 #get the item and then remove it from the inventory
566 replace_item = inventory.getItemsInSlot \
567 (drop_slot, drop_index)
568 self.dragObject(obj)
569 self.inventory_storage.moveItemToSlot(drag_item,
570 drop_slot,
571 drop_index)
572
573 if drop_widget.slot == 'ready':
574 self.readyCallback()
575
576 if replace_item == None:
577 self.controller.resetMouseCursor()
578 data_drag.dragging = False
579 except Container.TooBig :
580 logger.warning("%s too big to fit "
581 "into %s" % (data_drag.dragged_item,
582 drop_widget.slot))
583 except (Container.SlotBusy, Container.ItemSelf):
584 pass
585 self.updateInventoryButtons()
586
587 def createMenuItems(self, item, actions):
588 """Creates context menu items for the InventoryGUI"""
589 menu_actions = super(InventoryGUI, self).createMenuItems(item, actions)
590 param_dict = {}
591 param_dict["controller"] = self.controller
592 param_dict["commands"] = {}
593 param_dict["item"] = item
594 param_dict["container_gui"] = self
595 menu_actions.append(["Drop",
596 "Drop",
597 self.executeMenuItem,
598 ACTIONS["DropFromInventory"](**param_dict)])
599 return menu_actions
600
601 def getImage(self, name):
602 """Return a current image from the inventory
603 @type name: string
604 @param name: name of image to get
605 @return: None"""
606 return self.gui.findChild(name = name)