class Model(Active): def __init__(self, dispatcher): super(Model, self).__init__(dispatcher) self._components = [] self._signalManager = Dispatcher() self._name = '' def setName(self, name): self._name = name def __str__(self): return '[' + self._name + ']' def addComponent(self, factory): for dep in factory.getDependencies(): if not self.hasComponent(dep.getComponentClass()): self.addComponent(dep) component = factory.create(self) self._components.append(component) def hasComponent(self, componentClass): for component in self._components: if component.__class__ == componentClass: return True return False def _addExtension(self, signalName, extension): self._signalManager.addListener(signalName, extension) def _signal(self, signal): self._signalManager.dispatch(signal) def saveEvent(self, eventType, event): eventAttr = '_' + eventType + '_' + event.getClassName() setattr(self, eventAttr, event) ''' Search amongst components for a method named "name", and execute it. @param string name Name of called method. @throws AttributeError if name not found. ''' def __getattr__(self, name): for component in self._components: if hasattr(component, name): method = getattr(component, name) return lambda *args: \ method() if () == args else method(*args) raise AttributeError("'" + self.__class__.__name__ + "': object has no attribute '" + name + "'")
def __init__(self, dispatcher): super(Model, self).__init__(dispatcher) self._components = [] self._signalManager = Dispatcher() self._name = ''
# GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this library. If not, see <http://www.gnu.org/licenses/> import display from event import Dispatcher import pygame import logging logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("test") resolution = (400, 400) d = Dispatcher() def printEvent(e): print e def paintRect(e): print e pygame.display.get_surface().fill((100, 100, 150), pygame.Rect(e.pos, (10, 10))) d.addHandlers({pygame.MOUSEBUTTONDOWN:paintRect, pygame.MOUSEBUTTONUP:printEvent, pygame.VIDEORESIZE:lambda e: display.create(e.size)}) display.setFlags(resizable=True) display.setCaption("pygame-platform test")
logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("test") resolution = (400, 400) root = PygameRoot(resolution) def printCommand(): print "button clicked" button = CommandButton(printCommand, root, (10, 10), (50, 50)) switch = Switch([(255, 0, 0), (255, 255, 0), (0, 255, 0), (0, 255, 255), (0, 0, 255), (255, 0, 255), (255, 255, 255)], root, (40, 40), (80, 80)) d = Dispatcher() d.addHandlers({pygame.MOUSEBUTTONDOWN:root.mouse.onButtonDown, pygame.MOUSEBUTTONUP:root.mouse.onButtonUp, pygame.MOUSEMOTION:root.mouse.onMove, pygame.VIDEORESIZE:lambda e:root.resize(e.size)}) def draw(): # should fill display with a clearing color first each frame drawRect((0,0,0), root.region.pos, root.region.size) for element in root: # should eventually use sorted rendering element.presentation.draw() drawRect((128, 128, 128), root.cursor.pos, (10, 10)) pygame.display.flip() try: d.start(draw)