Panel.py :  » Game-2D-3D » Pyzzle » pyzzle-0.9 » pyzzle » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » Pyzzle 
Pyzzle » pyzzle 0.9 » pyzzle » Panel.py

from pygame.sprite import *
from pygame.rect import Rect
import pyzzle, media
import os

class Panel(Sprite):
    """A pygame Sprite that borrows some methods of the pygame Group class.
    This allows Panels to be nested. 
    
    All nested sprites must have the rect attribute."""
    
    cursorDefault='default.png'
    
    def __init__(self):
        self.cursor=Panel.cursorDefault
        
        Sprite.__init__(self)
        self._rect=Rect(0,0,0,0)
        self.sprites=LayeredUpdates()
        
        self.onEnter=lambda x:None
        """The function that is called when the player enters the slide."""
        self.onExit =lambda x:None
        """The function that is called when the player exits the slide."""
    def __iter__(self):
        return self.sprites.__iter__()
    def __contains__(self, sprite):
        return self.sprites.__contains__(sprite)
    
    def _getRect(self):
        return self._rect
    def _setRect(self,rect):
        self._rect=rect
    rect=property(_getRect, _setRect)
    
    def add(self, sprite):
        """Adds the sprite to the Panel. 
        @see: Group.add()
        @note: While this method overrides Sprite.add(),
            it does not perform the same behavior! 
        """
        self.sprites.add(sprite)
        if hasattr(sprite, '_layer'):
            self.sprites.change_layer(sprite, sprite._layer)
    def remove(self, sprite):
        """Removes the sprite from the Panel. 
        @see: Group.add()"""
        self.sprites.remove(sprite)
    def empty(self):
        """Empties all nested sprites from the Panel. 
        @see: Group.empty()"""
        self.sprites.empty()
    def draw(self, screen):
        """Draws nested sprites to the screen. 
        An image attribute or draw(screen) function is 
        needed for nested sprites to render.
        @type screen: Surface
        @param screen: The surface to draw on.
        @see: Group.draw()"""
        for sprite in self.sprites:
            sprite._getRect()
            if hasattr(sprite, 'draw'):
                sprite.draw(screen)
            elif hasattr(sprite, 'image'):
                screen.blit(sprite.image, sprite.rect)
    def highlight(self):
        """Called when the cursor hovers over the Panel.
        Calls the highlight() method of all nested sprites, where present.
        @rtype: string
        @return: The name of the cursor file that must be displayed
        """
        highlighted=None
        for sprite in self.sprites:
            if sprite.rect.collidepoint(pyzzle.cursor.rect.center):
                highlighted=sprite
        if hasattr(highlighted, 'highlight'):
            return highlighted.highlight()
        else:
            return self.cursor
    def click(self, **param):
        """Called when the user clicks the Panel.
        Calls the click() method of the topmost nested sprite under the cursor.
        If no click() method is found, nothing happens."""
        highlighted=None
        for sprite in self.sprites:
            if sprite.rect.collidepoint(pyzzle.cursor.rect.center):
                highlighted=sprite
        if hasattr(highlighted, 'click'):
            highlighted.click(**param)
    def enter(self, oldslide=None, delay=.1):
        """Called when the Panel is presented to the user.
        Calls the enter() method of all nested sprites, where present.
        @type oldslide: Panel
        @param oldslide: The Panel that was previously presented to the user,
            to be replaced by self
        @param delay: The time it should take for oldslide to transition to self
        """
        for sprite in self.sprites:
            if hasattr(sprite, 'enter'):
                sprite.enter(oldslide=None, delay=.1)
    def exit(self, newslide=None, delay=.1):
        """Called when the Panel is removed from the user.
        Calls the exit() method of all nested sprites, where present.
        @type newslide: Panel
        @param newslide: The Panel that self will be replaced with.
        @param delay: The time it should take for self to transition to newslide.
        """
        for sprite in self.sprites:
            if hasattr(sprite, 'exit'):
                sprite.exit(newslide=None, delay=.1)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.