def _handleenter(self): #Set the state to "activated" self._state = 1 #If the inventory is visible, and the show and hide alphas are different, we need to redraw the inventory if self._visible and (self._hidealpha != self._showalpha): #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Set the cursor to be an arrow globals.curcursor = globals.cursors['arrow'] #Update the screen globals.updateuhasurface(dirtyrect)
def _itemupdate(self, item): #Only update if the item is in the players inventory if item in self._itemlist: #If initialized, reload the items image if self._initialized: self._itemimages[self._itemlist.index(item)] = [item._draw(),None] #Redraw the inventory if needed if self._visible: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect)
def _handleevent(self, event): if event.type == 'init': #If init, initialize the area self._handleinit() elif event.type == 'mouseenter': #If enter, do entrance function self._handleenter() elif event.type == 'mouseexit': #If exit, do exit function self._handleexit() elif event.type == pygame.MOUSEMOTION: #If mouse has moved, find the currently highlighted item self._handlemove(event) #Return 1 so this event is not passed to other objects return 1 elif event.type == pygame.MOUSEBUTTONDOWN: #If a click, show the current item if one is selected (or scroll if needed) if self._currentitem != None: if self._currentitem != 'back' and self._currentitem != 'fwd': #It's not a scroll arrow, so call it's click function self._itemlist[self._currentitem]._click() elif self._currentitem == 'back': #it's the back arrow, so decrement the starting item if self._startitem != 0: self._startitem -= 1 else: #Its fwd scroll so increment the starting item if self._enditem != len(self._itemimages): self._startitem +=1 if self._visible: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect) #Return 1 so this event is not passed to other objects return 1
def remove(self, item): #Only remove it if it is in the inventory if item in self._itemlist: #Delete this items image del self._itemimages[self._itemlist.index(item)] #Remove the item from the inventory self._itemlist.remove(item) #Redraw the inventory if needed if self._visible: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect)
def _handleexit(self): #Set the state to "deactivated" self._state = 0 #If the inventory is visible and the show and hide alphas are different or there is a highlighted item if self._visible and (self._hidealpha != self._showalpha or self._currentitem != None): #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Clear the current item self._currentitem = None #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrects = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect) #Clear the current item self._currentitem = None
def add(self, item): #Only add it if it's not already there if item not in self._itemlist: #Add it to the inventory self._itemlist.append(item) #If the inventory bar has been initialized, load the item if self._initialized: self._itemimages.append([item._draw(),None]) #Redraw the inventory if needed if self._visible: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect)
def update(self, rect = None, bgfile = None, bgdatfile = None, bgencryption = None, hidealpha = None, showalpha = None, mouseoveralpha = None, visible = None, enabled = None): #This will hold the area of the screen effected by this update dirtyrect = None if self._visible: #Hide all areas above this one, and restore the background dirtyrect = userarea._redrawfrom(self) globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Update attributes if rect != None: self._rect = pygame.Rect(rect) if bgfile != None: self._bgfile = bgfile if bgdatfile != None: self._bgdatfile = bgdatfile if bgencryption != None: self._bgencryption = bgencryption if hidealpha != None: self._hidealpha = hidealpha if showalpha != None: self._showalpha = showalpha if mouseoveralpha != None: self._mouseoveralpha = mouseoveralpha if visible != None: self._visible = visible if enabled != None: self._enabled = enabled #If any of the following changed, reload the background file if bgfile != None or bgdatfile != None or bgencryption != None: self._bgfilesurface = None if self._visible: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self, 0, dirtyrect) #Draw area self._draw() if dirtyrect != None: #Restore all areas above this one (if needed) dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) globals.updateuhasurface(dirtyrect)
def _handlemove(self, event): #Only check movement if visible if self._visible: #If there are any items in the inventory if len(self._itemlist): #Adjust the event position to be relative to the topleft of the inventory bar pos = (event.pos[0] - self._rect.left, event.pos[1] - self._rect.top) #If the back scroll arrow is displayed if self._backrect != None: #Check if the mouse is over the back scroll arrow if self._backrect.collidepoint(pos): #If so, set the back scroll arrow to be the highlighted item if it is not already if self._currentitem != 'back': self._currentitem = 'back' #If the item needs to be highlighted if self._mouseoveralpha != self._showalpha: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect) #Found the current item, so return return #If the forward scroll arrow is displayed if self._fwdrect != None: #Check if the mouse is over the forward scroll arrow if self._fwdrect.collidepoint(pos): #If so, set the forward scroll arrow to be the highlighted item if it is not already if self._currentitem != 'fwd': self._currentitem = 'fwd' #If the item needs to be highlighted if self._mouseoveralpha != self._showalpha: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect) #Found the current item, so return return #If it wasn't the back or fwd arrows, loop through items to find the current one for item in range(self._startitem, self._enditem + 1): #This is to prevent an "out of bounds" error caused by the +1 above if item < len(self._itemimages): #Check if the mouse is over this item if self._itemimages[item][1].collidepoint(pos): #If so, set this item to be the highlighted item if it is not already if self._currentitem != item: self._currentitem = item #If the item needs to be highlighted if self._mouseoveralpha != self._showalpha: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect) #Found the current item, so return return #If we got here the mouse is not over any items, so set the current item to None, and redraw the inventory self._currentitem = None #An item may need to be un-highlighted if self._mouseoveralpha != self._showalpha: #Hide all areas above this one dirtyrect = userarea._redrawfrom(self) #Restore the background globals.uhabuffer.fill((0,0,0,0), self._rect) globals.uhabuffer.blit(self._bgsurface, self._rect) #Draw the updated inventory self._draw() #Redraw all areas above this one dirtyrect = userarea._redrawfrom(self, 1, dirtyrect) #Update the screen globals.updateuhasurface(dirtyrect)