def register_dynamic(event_namespace, handler_string, args=None, kwargs=None, priority=0, scene=None): """ Similar to :func:`spyral.event.register` function, except that instead of passing in a function, you pass in the name of a property of this scene that holds a function. Example:: class MyScene(Scene): def __init__(self): ... self.register_dynamic("orc.dies", "future_function") ... :param str event_namespace: The namespace of the event, e.g. ``"input.mouse.left.click"`` or ``"pong.score"``. :param str handler: The name of an attribute on this scene that will hold a function. The first argument to the function will be the event. :param args: any additional arguments that need to be passed in to the handler. :type args: sequence :param kwargs: any additional keyword arguments that need to be passed into the handler. :type kwargs: dict :param int priority: the higher the `priority`, the sooner this handler will be called in reaction to the event, relative to the other event handlers registered. :param scene: The scene to register this event on; if it is ``None``, then it will be attached to the currently running scene. :type scene: :class:`Scene <spyral.Scene>` or ``None`` """ if scene is None: scene = spyral._get_executing_scene() scene._reg_internal(event_namespace, (handler_string,), args, kwargs, priority, True)
def _set_background(self, image): surface = image._surf scene = spyral._get_executing_scene() if surface.get_size() != self.size: raise spyral.BackgroundSizeError("Background size must match the scene's size.") self._background = pygame.transform.smoothscale(surface, self._surface.get_size()) self._clear_this_frame.append(self._background.get_rect())
def register(event_namespace, handler, args=None, kwargs=None, priority=0, scene=None): """ Registers an event `handler` to a namespace. Whenever an event in that `event_namespace` is fired, the event `handler` will execute with that event. :param event_namespace: the namespace of the event, e.g. ``"input.mouse.left.click"`` or ``"pong.score"``. :type event_namespace: str :param handler: A function that will handle the event. The first argument to the function will be the event. :type handler: function :param args: any additional arguments that need to be passed in to the handler. :type args: sequence :param kwargs: any additional keyword arguments that need to be passed into the handler. :type kwargs: dict :param int priority: the higher the `priority`, the sooner this handler will be called in reaction to the event, relative to the other event handlers registered. :param scene: The scene to register this event on; if it is ``None``, then it will be attached to the currently running scene. :type scene: :class:`Scene <spyral.Scene>` or ``None`` """ if scene is None: scene = spyral._get_executing_scene() scene._reg_internal(event_namespace, (WeakMethod(handler),), args, kwargs, priority, False)
def register_multiple_dynamic(event_namespace, handler_strings, args=None, kwargs=None, priority=0, scene=None): """ Similar to :func:`spyral.Scene.register` function, except a sequence of strings representing handlers can be given instead of just one. :param event_namespace: the namespace of the event, e.g. ``"input.mouse.left.click"`` or ``"pong.score"``. :type event_namespace: string :param handler: A list of names of an attribute on this scene that will hold a function. The first argument to the function will be the event. :type handler: list of strings :param args: any additional arguments that need to be passed in to the handler. :type args: sequence :param kwargs: any additional keyword arguments that need to be passed into the handler. :type kwargs: dict :param int priority: the higher the `priority`, the sooner this handler will be called in reaction to the event, relative to the other event handlers registered. :param scene: The scene to register this event on; if it is ``None``, then it will be attached to the currently running scene. :type scene: :class:`Scene <spyral.Scene>` or ``None`` """ if scene is None: scene = spyral._get_executing_scene() scene._reg_internal(event_namespace, handler_strings, args, kwargs, priority, True)
def register(event_namespace, handler, args=None, kwargs=None, priority=0, scene=None): """ Registers an event `handler` to a namespace. Whenever an event in that `event_namespace` is fired, the event `handler` will execute with that event. :param event_namespace: the namespace of the event, e.g. ``"input.mouse.left.click"`` or ``"pong.score"``. :type event_namespace: str :param handler: A function that will handle the event. The first argument to the function will be the event. :type handler: function :param args: any additional arguments that need to be passed in to the handler. :type args: sequence :param kwargs: any additional keyword arguments that need to be passed into the handler. :type kwargs: dict :param int priority: the higher the `priority`, the sooner this handler will be called in reaction to the event, relative to the other event handlers registered. :param scene: The scene to register this event on; if it is ``None``, then it will be attached to the currently running scene. :type scene: :class:`Scene <spyral.Scene>` or ``None`` """ if scene is None: scene = spyral._get_executing_scene() scene._reg_internal(event_namespace, (WeakMethod(handler), ), args, kwargs, priority, False)
def _set_background(self, image): self._background_image = image self._background_version = image._version surface = image._surf scene = spyral._get_executing_scene() if surface.get_size() != self.size: raise spyral.BackgroundSizeError("Background size must match " "the scene's size.") size = self._surface.get_size() self._background = pygame.transform.smoothscale(surface, size) self._clear_this_frame.append(self._background.get_rect())
def queue(type, event = None, _scene = None): """ Queues a new event in the system. You must specify the *type* of the string, e.g. "system.quit", "ball.collides.paddle.", or "input.mouse.up". You can create an *Event* object to pass into the *event* parameter, which can store properties of the event:: collision_event = Event(ball=ball, paddle=paddle) spyral.event.queue("ball.collides.paddle", collision_event) """ if _scene is None: _scene = spyral._get_executing_scene() _scene._queue_event(type, event)
def clear_namespace(namespace, scene=None): """ Clears all handlers from namespaces that are at least as specific as the provided `namespace`. :param str namespace: The complete namespace. :param scene: The scene to clear the namespace of; if it is ``None``, then it will be attached to the currently running scene. :type scene: :class:`Scene <spyral.Scene>` or ``None`` """ if scene is None: scene = spyral._get_executing_scene() scene._clear_namespace(namespace)
def unregister(event_namespace, handler, scene=None): """ Unregisters a registered handler for that namespace. Dynamic handler strings are supported as well. :param str event_namespace: An event namespace :param handler: The handler to unregister. :type handler: a function or string. :param scene: The scene to unregister the event; if it is ``None``, then it will be attached to the currently running scene. :type scene: :class:`Scene <spyral.Scene>` or ``None`` """ if scene is None: scene = spyral._get_executing_scene() scene._unregister(event_namespace, handler)
def handle(event_name, event=None, scene=None): """ Instructs spyral to execute the handlers for this event right now. When you have a custom event, this is the function you call to have the event occur. :param str event_name: The type of event (e.g., ``"system.quit"``, ``"input.mouse.up"``, or ``"pong.score"``. :param event: An Event object that holds properties for the event. :type event: :class:`Event <spyral.event.Event>` :param scene: The scene to queue this event on; if ``None`` is given, the currently executing scene will be used. :type scene: :class:`Scene <spyral.Scene>` or ``None``. """ if scene is None: scene = spyral._get_executing_scene() scene._handle_event(event_name, event)
def queue(event_name, event=None, scene=None): """ Queues a new event in the system, meaning that it will be run at the next available opportunity. :param str event_name: The type of event (e.g., ``"system.quit"``, ``"input.mouse.up"``, or ``"pong.score"``. :param event: An Event object that holds properties for the event. :type event: :class:`Event <spyral.event.Event>` :param scene: The scene to queue this event on; if `None` is given, the currently executing scene will be used. :type scene: :class:`Scene <spyral.Scene>` or `None`. """ if scene is None: scene = spyral._get_executing_scene() scene._queue_event(event_name, event)
def register_dynamic(event_namespace, handler_string, args=None, kwargs=None, priority=0, scene=None): """ Similar to :func:`spyral.event.register` function, except that instead of passing in a function, you pass in the name of a property of this scene that holds a function. Example:: class MyScene(Scene): def __init__(self): ... self.register_dynamic("orc.dies", "future_function") ... :param str event_namespace: The namespace of the event, e.g. ``"input.mouse.left.click"`` or ``"pong.score"``. :param str handler: The name of an attribute on this scene that will hold a function. The first argument to the function will be the event. :param args: any additional arguments that need to be passed in to the handler. :type args: sequence :param kwargs: any additional keyword arguments that need to be passed into the handler. :type kwargs: dict :param int priority: the higher the `priority`, the sooner this handler will be called in reaction to the event, relative to the other event handlers registered. :param scene: The scene to register this event on; if it is ``None``, then it will be attached to the currently running scene. :type scene: :class:`Scene <spyral.Scene>` or ``None`` """ if scene is None: scene = spyral._get_executing_scene() scene._reg_internal(event_namespace, (handler_string, ), args, kwargs, priority, True)
def handle(type, event = None, _scene = None): if _scene is None: _scene = spyral._get_executing_scene() _scene._handle_event(type, event)
def __init__(self): self._greenlet = greenlet.greenlet(self.main) scene = spyral._get_executing_scene() scene._register_actor(self, self._greenlet)