Changeset 223

Show
Ignore:
Timestamp:
12/25/06 16:51:01 (5 years ago)
Author:
mg
Message:

Make the menu scroll if it doesn't fit on screen.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/pyspacewar/ui.py

    r222 r223  
    792792        height = max(0, (item_height + yspacing) * len(items) - yspacing) 
    793793        HUDElement.__init__(self, width, height, xalign, yalign) 
     794        self.full_height = height 
    794795        self.font = font 
    795796        self.items = items 
     
    798799        self.ypadding = ypadding 
    799800        self.selected_item = 0 
     801        self.top = 0 
    800802        self.item_height = item_height 
    801         self.surface = pygame.Surface((self.width, self.height)) 
     803        self.surface = pygame.Surface((self.width, self.full_height)) 
    802804        self.surface.set_alpha(255 * 0.9) 
    803805        self.surface.set_colorkey((1, 1, 1)) 
    804806        self.invalidate() 
     807 
     808    def position(self, surface, margin=10): 
     809        """Calculate screen position for the widget.""" 
     810        max_height = surface.get_height() - 2 * margin 
     811        item_spacing = self.item_height + self.yspacing 
     812        self.height = self.full_height 
     813        while self.height > max_height: 
     814            self.height -= item_spacing 
     815        if self.selected_item * item_spacing < self.top: 
     816            self.top = self.selected_item * item_spacing 
     817        while (self.selected_item * item_spacing + self.item_height > 
     818               self.top + self.height): 
     819            self.top += item_spacing 
     820        return HUDElement.position(self, surface, margin) 
    805821 
    806822    def invalidate(self): 
     
    821837        """Find the item at given coordinates.""" 
    822838        ix, iy = self.position(surface) 
     839        iy -= self.top 
    823840        for idx, item in enumerate(self.items): 
    824841            if (ix <= x < ix + self.width and 
     
    856873            self._draw() 
    857874        x, y = self.position(surface) 
    858         surface.blit(self.surface, (x, y)) 
     875        surface.blit(self.surface, (x, y), 
     876                     (0, self.top, self.width, self.height)) 
    859877 
    860878