Added chest interaction
[butaba-adventures.git] / utility.py
index bc760f3..f88430c 100644 (file)
@@ -70,7 +70,7 @@ def ask_question (surface, question, answers, bgscreen):
 # function displaying the contents of a container. Object must contain
 # items list
 # edgewidth - container edges to avoid drawing items in
-def display_container_contents (surface, obj, bgimage, edgewidth=0):
+def get_container_object (surface, obj, bgimage, edgewidth=0):
 
        # get the number of items
        numitems = len (obj.objects)
@@ -82,6 +82,10 @@ def display_container_contents (surface, obj, bgimage, edgewidth=0):
        objposx = surface.get_width()/2 - bgimage.get_width()/2
        objposy = surface.get_height()/2 - bgimage.get_height()/2
 
+       selitem = 0
+       selrow = 0
+       selcol = 0
+
        while 1:
                # display the background for the container
                surface.blit (bgimage, (objposx, objposy))
@@ -90,20 +94,44 @@ def display_container_contents (surface, obj, bgimage, edgewidth=0):
                i = 0
                j = 0
 
+               # display all the items in container
                for item in obj.objects:
                        surface.blit (item.image, (objposx + edgewidth+ j*48, objposy + edgewidth + i*48))
                        j += 1
                        if j >= num_cols:
                                j = 0
                                i += 1
-                               # only display as many items as will fit in the container
-                               if i >= num_rows:
-                                       break
+               # only draw selector if there is at least one item
+               if numitems > 0:
+                       pygame.draw.rect (surface, pygame.Color (255,255,255),
+                                       pygame.Rect(objposx + edgewidth + selcol*48, objposy + edgewidth + selrow*48, 48, 48), 1)
 
                pygame.display.update ()
+
+               # get events
                for event in pygame.event.get ():
                        if event.type == pygame.QUIT:
                                sys.exit (0)
                        elif event.type == pygame.KEYDOWN:
                                if event.key == pygame.K_ESCAPE:
-                                       return
+                                       return None
+                               elif event.key == pygame.K_RETURN:
+                                       if numitems > 0 and selitem >= 0 and selitem < numitems:
+                                               return obj.objects[selitem]
+                                       else:
+                                               return None
+                               elif event.key == pygame.K_UP or event.key == pygame.K_LEFT:
+                                       # go to the prev item
+                                       selitem -= 1
+                                       if selitem < 0:
+                                               selitem = numitems - 1
+                                       selrow = selitem / num_cols
+                                       selcol = selitem % num_cols
+
+                               elif event.key == pygame.K_DOWN or event.key == pygame.K_RIGHT:
+                                       # go to the next item
+                                       selitem += 1
+                                       if selitem > numitems - 1:
+                                               selitem = 0
+                                       selrow = selitem / num_cols
+                                       selcol = selitem % num_cols