Example #1
0
    def __init__(self, parent):
        _all_sprites.append(_wref(self))
        self._age = 0
        self._static = False
        self._image = None
        self._image_version = None
        self._layer = None
        self._computed_layer = 1
        self._make_static = False
        self._pos = spyral.Vec2D(0, 0)
        self._blend_flags = 0
        self._visible = True
        self._anchor = 'topleft'
        self._offset = spyral.Vec2D(0, 0)
        self._scale = spyral.Vec2D(1.0, 1.0)
        self._scaled_image = None
        self._parent = _wref(parent)
        self._scene = _wref(parent.scene)
        self._angle = 0
        self._crop = None
        self._transform_image = None
        self._transform_offset = spyral.Vec2D(0, 0)
        self._flip_x = False
        self._flip_y = False
        self._animations = []
        self._progress = {}
        self._mask = None

        parent._add_child(self)

        self._scene()._register_sprite(self)
        self._scene()._apply_style(self)
        spyral.event.register('director.render', self._draw,
                              scene=self._scene())
Example #2
0
 def __init__(self, scene):
     self.layers = {_wref(scene) : []}
     self.child_views = {_wref(scene) : []}
     self.layer_location = {_wref(scene) : [0]}
     self.scene = _wref(scene)
     self.tree_height = {_wref(scene) : 1}
     self._precompute_positions()
     self.maximum_height = 1
    def __init__(self, view):
        _all_sprites.append(_wref(self))
        self._age = 0
        self._static = False
        self._image = None
        self._image_version = None
        self._layer = '__default__'
        self._make_static = False
        self._pos = spyral.Vec2D(0, 0)
        self._blend_flags = 0
        self._visible = True
        self._anchor = 'topleft'
        self._offset = spyral.Vec2D(0, 0)
        self._scale = spyral.Vec2D(1.0, 1.0)
        self._scaled_image = None
        self._view = view
        self._scene = view.scene
        self._angle = 0
        self._crop = None
        self._transform_image = None
        self._transform_offset = spyral.Vec2D(0, 0)
        self._flip_x = False
        self._flip_y = False
        self._animations = []
        self._progress = {}

        self._scene._register_sprite(self)
        self._scene.apply_style(self)
        self._scene.register('director.render', self.draw)
Example #4
0
    def get_layer_position(self, parent, layer):
        """
        For a given layer (and also that layer's parent/owner, since layer
        alone is ambiguous), identify what the Absolute Position Value is from
        the precomputed cache, allowing for :above and :below modifiers.

        :param parent: The view or scene that has this layer
        :type parent: a View or a Scene (not weakrefs!)
        :param layer: The name of the layer that we're interested in.
        :type layer: string
        :returns: A `float` representing where this layer is relative to others.
        """
        parent = _wref(parent)
        s = layer.split(':')
        layer = s[0]
        offset = 0
        if len(s) > 1:
            mod = s[1]
            if mod == 'above':
                offset = 0.5
            if mod == 'below':
                offset = -0.5
        if (parent, layer) in self.layer_location:
            position = self.layer_location[(parent, layer)]
        elif parent in self.layer_location:
            position = self.layer_location[parent]
        else:
            position = self.layer_location[self.scene]
        return position + offset
Example #5
0
    def __init__(self, group=None):
        """ Adds this sprite to any number of groups by default. """
        _all_sprites.append(_wref(self))
        self._age = 0
        self._static = False
        self._image = None
        self._layer = '__default__'
        self._groups = []
        self._make_static = False
        self._pos = spyral.Vec2D(0, 0)
        self._blend_flags = 0
        self._visible = True
        self._anchor = 'topleft'
        self._offset = spyral.Vec2D(0, 0)
        self._scale = spyral.Vec2D(1.0, 1.0)
        self._scaled_image = None
        self._group = None
        self._angle = 0
        self._transform_image = None
        self._transform_offset = spyral.Vec2D(0, 0)
        self._flip_x = False
        self._flip_y = False
        
        self.on_remove = spyral.Signal()

        if group is not None:
            group.add(self)
Example #6
0
    def __init__(self, size=None, max_ups=None, max_fps=None):
        time_source = time.time
        self.clock = spyral.GameClock(
            time_source=time_source,
            max_fps=max_fps or spyral.director._max_fps,
            max_ups=max_ups or spyral.director._max_ups,
        )
        self.clock.use_wait = True

        self._handlers = defaultdict(lambda: [])
        self._namespaces = set()
        self._event_source = spyral.event.LiveEventHandler()
        self._handling_events = False
        self._events = []
        self._pending = []
        self._greenlets = {}  # Maybe need to weakref dict

        self._style_symbols = {}
        self._style_classes = []
        self._style_properties = defaultdict(lambda: {})
        self._style_functions = {}

        self._style_functions["_get_spyral_path"] = spyral._get_spyral_path

        self._size = None
        self._scale = spyral.Vec2D(1.0, 1.0)  # None
        self._surface = pygame.display.get_surface()
        if size is not None:
            self._set_size(size)
        display_size = self._surface.get_size()
        self._background = spyral.image._new_spyral_surface(display_size)
        self._background.fill((255, 255, 255))
        self._background_version = 0
        self._surface.blit(self._background, (0, 0))
        self._blits = []
        self._dirty_rects = []
        self._clear_this_frame = []
        self._clear_next_frame = []
        self._soft_clear = []
        self._static_blits = {}
        self._invalidating_views = {}
        self._collision_boxes = {}
        self._rect = self._surface.get_rect()

        self._layers = []
        self._child_views = []
        self._layer_tree = _LayerTree(self)
        self._sprites = set()

        spyral.event.register("director.scene.enter", self.redraw, scene=self)
        spyral.event.register("director.update", self._handle_events, scene=self)
        if _GREENLETS_AVAILABLE:
            spyral.event.register("director.update", self._run_actors, ("delta",), scene=self)
        spyral.event.register("spyral.internal.view.changed", self._invalidate_views, scene=self)

        # View interface
        self._scene = _wref(self)
        self._views = []
Example #7
0
    def set_view_layers(self, view, layers):
        """
        Set the layers that will be available for this view or scene.

        :param view: the view have its layer set
        :type view: View (not a weakref) or a Scene (not a weakref)
        :param layers: the name of the layer on the parent
        :type layers: a list of strings
        """
        self.layers[_wref(view)] = list(layers)
        self._precompute_positions()
Example #8
0
    def __init__(self, parent):

        self._size = spyral.Vec2D(parent.size)
        self._output_size = spyral.Vec2D(parent.size)
        self._crop_size = spyral.Vec2D(parent.size)
        self._pos = spyral.Vec2D(0,0)
        self._crop = False
        self._visible = True
        self._parent = _wref(parent)
        self._anchor = 'topleft'
        self._offset = spyral.Vec2D(0,0)
        self._layers = []
        self._layer = None
        self._mask = None

        self._children = set()
        self._child_views = set()
        self._scene = _wref(parent.scene)
        self._scene()._add_view(self)
        self._parent()._add_child(self)
        self._scene()._apply_style(self)
Example #9
0
    def remove_view(self, view):
        """
        Removes all references to this view; it must have previously been added
        to the LayerTree.

        :param view: the View to remove
        :type view: View (not a weakref)
        """
        view = _wref(view)
        del self.tree_height[view]
        del self.layers[view]
        self.child_views[view()._parent].remove(view)
        del self.child_views[view]
        self._precompute_positions()
Example #10
0
 def __init__(self, *groups):
     """ Adds this sprite to any number of groups by default. """
     _all_sprites.append(_wref(self))
     self._age = 0
     self._static = False
     self._image = None
     self._rect = None
     self._layer = '__default__'
     self._groups = []
     self.add(*groups)
     self._make_static = False
     self._pos = (0,0)
     self._double_check = False
     self._blend_flags = 0
     self.visible = True
Example #11
0
    def add_view(self, view):
        """
        Starts keeping track of this view in the LayerTree.

        :param view: the new View to add
        :type view: View (not a weakref)
        """
        parent = view._parent
        view = _wref(view)
        self.layers[view] = []
        self.child_views[view] = []
        self.child_views[parent].append(view)
        self.tree_height[view] = 1
        if len(self.child_views[parent]) == 1:
            self.tree_height[parent] += 1
            while parent != self.scene:
                parent = parent()._parent
                self.tree_height[parent] += 1
        self._precompute_positions()
Example #12
0
    def add_view(self, view):
        """
        Starts keeping track of this view in the LayerTree.

        :param view: the new View to add
        :type view: View (not a weakref)
        """
        parent = view._parent
        view = _wref(view)
        self.layers[view] = []
        self.child_views[view] = []
        self.child_views[parent].append(view)
        self.tree_height[view] = 1
        if len(self.child_views[parent]) == 1:
            self.tree_height[parent] += 1
            while parent != self.scene:
                parent = parent()._parent
                self.tree_height[parent] += 1
        self._precompute_positions()
Example #13
0
    def __init__(self, sock=None, keyfile=None, certfile=None,
                 server_side=False, cert_reqs=CERT_NONE,
                 ssl_version=PROTOCOL_SSLv23, ca_certs=None,
                 do_handshake_on_connect=True,
                 family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None,
                 suppress_ragged_eofs=True, npn_protocols=None, ciphers=None,
                 server_hostname=None,
                 _context=None):
        # pylint:disable=too-many-locals,too-many-statements,too-many-branches
        if _context:
            self._context = _context
        else:
            if server_side and not certfile:
                raise ValueError("certfile must be specified for server-side "
                                 "operations")
            if keyfile and not certfile:
                raise ValueError("certfile must be specified")
            if certfile and not keyfile:
                keyfile = certfile
            self._context = SSLContext(ssl_version)
            self._context.verify_mode = cert_reqs
            if ca_certs:
                self._context.load_verify_locations(ca_certs)
            if certfile:
                self._context.load_cert_chain(certfile, keyfile)
            if npn_protocols:
                self._context.set_npn_protocols(npn_protocols)
            if ciphers:
                self._context.set_ciphers(ciphers)
            self.keyfile = keyfile
            self.certfile = certfile
            self.cert_reqs = cert_reqs
            self.ssl_version = ssl_version
            self.ca_certs = ca_certs
            self.ciphers = ciphers
        # Can't use sock.type as other flags (such as SOCK_NONBLOCK) get
        # mixed in.
        if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
            raise NotImplementedError("only stream sockets are supported")
        if server_side and server_hostname:
            raise ValueError("server_hostname can only be specified "
                             "in client mode")
        if self._context.check_hostname and not server_hostname:
            raise ValueError("check_hostname requires server_hostname")

        self.server_side = server_side
        self.server_hostname = server_hostname
        self.do_handshake_on_connect = do_handshake_on_connect
        self.suppress_ragged_eofs = suppress_ragged_eofs
        connected = False
        if sock is not None:
            socket.__init__(self,
                            family=sock.family,
                            type=sock.type,
                            proto=sock.proto,
                            fileno=sock.fileno())
            self.settimeout(sock.gettimeout())
            # see if it's connected
            try:
                sock.getpeername()
            except socket_error as e:
                if e.errno != errno.ENOTCONN:
                    raise
            else:
                connected = True
            sock.detach()
        elif fileno is not None:
            socket.__init__(self, fileno=fileno)
        else:
            socket.__init__(self, family=family, type=type, proto=proto)

        self._sock._sslsock = _wref(self)
        self._closed = False
        self._sslobj = None
        self._connected = connected
        if connected:
            # create the SSL object
            try:
                self._sslobj = self._context._wrap_socket(self._sock, server_side,
                                                          server_hostname)
                if do_handshake_on_connect:
                    timeout = self.gettimeout()
                    if timeout == 0.0:
                        # non-blocking
                        raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
                    self.do_handshake()

            except socket_error as x:
                self.close()
                raise x
Example #14
0
def registerFinalizer(obj, func):
    r = _wref(obj, _finalizer)
    k = id(r)
    _finalize_refs[k] = (r, func)
    return k
Example #15
0
    def __init__(self, sock=None, keyfile=None, certfile=None,
                 server_side=False, cert_reqs=CERT_NONE,
                 ssl_version=PROTOCOL_SSLv23, ca_certs=None,
                 do_handshake_on_connect=True,
                 family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None,
                 suppress_ragged_eofs=True, npn_protocols=None, ciphers=None,
                 server_hostname=None,
                 _session=None, # 3.6
                 _context=None):
        # pylint:disable=too-many-locals,too-many-statements,too-many-branches
        if _context:
            self._context = _context
        else:
            if server_side and not certfile:
                raise ValueError("certfile must be specified for server-side "
                                 "operations")
            if keyfile and not certfile:
                raise ValueError("certfile must be specified")
            if certfile and not keyfile:
                keyfile = certfile
            self._context = SSLContext(ssl_version)
            self._context.verify_mode = cert_reqs
            if ca_certs:
                self._context.load_verify_locations(ca_certs)
            if certfile:
                self._context.load_cert_chain(certfile, keyfile)
            if npn_protocols:
                self._context.set_npn_protocols(npn_protocols)
            if ciphers:
                self._context.set_ciphers(ciphers)
            self.keyfile = keyfile
            self.certfile = certfile
            self.cert_reqs = cert_reqs
            self.ssl_version = ssl_version
            self.ca_certs = ca_certs
            self.ciphers = ciphers
        # Can't use sock.type as other flags (such as SOCK_NONBLOCK) get
        # mixed in.
        if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
            raise NotImplementedError("only stream sockets are supported")
        if server_side:
            if server_hostname:
                raise ValueError("server_hostname can only be specified "
                                 "in client mode")
            if _session is not None:
                raise ValueError("session can only be specified "
                                 "in client mode")
        if self._context.check_hostname and not server_hostname:
            raise ValueError("check_hostname requires server_hostname")
        self._session = _session
        self.server_side = server_side
        self.server_hostname = server_hostname
        self.do_handshake_on_connect = do_handshake_on_connect
        self.suppress_ragged_eofs = suppress_ragged_eofs
        connected = False
        if sock is not None:
            socket.__init__(self,
                            family=sock.family,
                            type=sock.type,
                            proto=sock.proto,
                            fileno=sock.fileno())
            self.settimeout(sock.gettimeout())
            # see if it's connected
            try:
                sock.getpeername()
            except socket_error as e:
                if e.errno != errno.ENOTCONN:
                    raise
            else:
                connected = True
            sock.detach()
        elif fileno is not None:
            socket.__init__(self, fileno=fileno)
        else:
            socket.__init__(self, family=family, type=type, proto=proto)

        self._sock._sslsock = _wref(self)
        self._closed = False
        self._sslobj = None
        self._connected = connected
        if connected:
            # create the SSL object
            try:
                self._sslobj = self._context._wrap_socket(self._sock, server_side,
                                                          server_hostname)
                if _session is not None: # 3.6
                    self._sslobj = SSLObject(self._sslobj, owner=self, session=self._session)
                if do_handshake_on_connect:
                    timeout = self.gettimeout()
                    if timeout == 0.0:
                        # non-blocking
                        raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
                    self.do_handshake()

            except socket_error as x:
                self.close()
                raise x
Example #16
0
    def __init__(self, size=None, max_ups=None, max_fps=None):
        time_source = time.time
        self.clock = spyral.GameClock(time_source=time_source,
                                      max_fps=max_fps
                                      or spyral.director._max_fps,
                                      max_ups=max_ups
                                      or spyral.director._max_ups)
        self.clock.use_wait = True

        self._handlers = defaultdict(lambda: [])
        self._namespaces = set()
        self._event_source = spyral.event.LiveEventHandler()
        self._handling_events = False
        self._events = []
        self._pending = []
        self._greenlets = {}  # Maybe need to weakref dict

        self._style_symbols = {}
        self._style_classes = []
        self._style_properties = defaultdict(lambda: {})
        self._style_functions = {}

        self._style_functions['_get_spyral_path'] = spyral._get_spyral_path

        self._size = None
        self._scale = spyral.Vec2D(1.0, 1.0)  #None
        self._surface = pygame.display.get_surface()
        if size is not None:
            self._set_size(size)
        display_size = self._surface.get_size()
        self._background = spyral.image._new_spyral_surface(display_size)
        self._background.fill((255, 255, 255))
        self._background_version = 0
        self._surface.blit(self._background, (0, 0))
        self._blits = []
        self._dirty_rects = []
        self._clear_this_frame = []
        self._clear_next_frame = []
        self._soft_clear = []
        self._static_blits = {}
        self._invalidating_views = {}
        self._collision_boxes = {}
        self._rect = self._surface.get_rect()

        self._layers = []
        self._child_views = []
        self._layer_tree = _LayerTree(self)
        self._sprites = set()

        spyral.event.register('director.scene.enter', self.redraw, scene=self)
        spyral.event.register('director.update',
                              self._handle_events,
                              scene=self)
        if _GREENLETS_AVAILABLE:
            spyral.event.register('director.update',
                                  self._run_actors, ('delta', ),
                                  scene=self)
        spyral.event.register('spyral.internal.view.changed',
                              self._invalidate_views,
                              scene=self)

        # View interface
        self._scene = _wref(self)
        self._views = []
Example #17
0
 def _gevent_sock_class(self, family, type, proto, fileno):
     return _contextawaresock(family, type, proto, fileno, _wref(self))
Example #18
0
 def __init__(self, form, name):
     self.__style__ = form.__class__.__name__ + '.' + name
     self.name = name
     self._form = _wref(form)
     spyral.View.__init__(self, form)
     self.mask = spyral.Rect(self.pos, self.size)
Example #19
0
    def __init__(
            self,
            sock=None,
            keyfile=None,
            certfile=None,
            server_side=False,
            cert_reqs=CERT_NONE,
            ssl_version=PROTOCOL_SSLv23,
            ca_certs=None,
            do_handshake_on_connect=True,
            family=AF_INET,
            type=SOCK_STREAM,
            proto=0,
            fileno=None,
            suppress_ragged_eofs=True,
            npn_protocols=None,
            ciphers=None,
            server_hostname=None,
            _session=None,  # 3.6
            _context=None):
        # When a *sock* argument is passed, it is used only for its fileno()
        # and is immediately detach()'d *unless* we raise an error.

        # pylint:disable=too-many-locals,too-many-statements,too-many-branches

        if _context:
            self._context = _context
        else:
            if server_side and not certfile:
                raise ValueError("certfile must be specified for server-side "
                                 "operations")
            if keyfile and not certfile:
                raise ValueError("certfile must be specified")
            if certfile and not keyfile:
                keyfile = certfile
            self._context = SSLContext(ssl_version)
            self._context.verify_mode = cert_reqs
            if ca_certs:
                self._context.load_verify_locations(ca_certs)
            if certfile:
                self._context.load_cert_chain(certfile, keyfile)
            if npn_protocols:
                self._context.set_npn_protocols(npn_protocols)
            if ciphers:
                self._context.set_ciphers(ciphers)
            self.keyfile = keyfile
            self.certfile = certfile
            self.cert_reqs = cert_reqs
            self.ssl_version = ssl_version
            self.ca_certs = ca_certs
            self.ciphers = ciphers
        # Can't use sock.type as other flags (such as SOCK_NONBLOCK) get
        # mixed in.
        if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
            raise NotImplementedError("only stream sockets are supported")
        if server_side:
            if server_hostname:
                raise ValueError("server_hostname can only be specified "
                                 "in client mode")
            if _session is not None:
                raise ValueError("session can only be specified "
                                 "in client mode")
        if self._context.check_hostname and not server_hostname:
            raise ValueError("check_hostname requires server_hostname")
        self._session = _session
        self.server_side = server_side
        self.server_hostname = server_hostname
        self.do_handshake_on_connect = do_handshake_on_connect
        self.suppress_ragged_eofs = suppress_ragged_eofs
        connected = False
        if sock is not None:
            timeout = sock.gettimeout()
            socket.__init__(self,
                            family=sock.family,
                            type=sock.type,
                            proto=sock.proto,
                            fileno=sock.fileno())
            self.settimeout(timeout)
            # When Python 3 sockets are __del__, they close() themselves,
            # including their underlying fd, unless they have been detached.
            # Only detach if we succeed in taking ownership; if we raise an exception,
            # then the user might have no way to close us and release the resources.
            sock.detach()
        elif fileno is not None:
            socket.__init__(self, fileno=fileno)
        else:
            socket.__init__(self, family=family, type=type, proto=proto)

        self._sock._sslsock = _wref(self)
        self._closed = False
        self._sslobj = None
        # see if we're connected
        try:
            self._sock.getpeername()
        except socket_error as e:
            if e.errno != errno.ENOTCONN:
                # This file descriptor is hosed, shared or not.
                # Clean up.
                self.close()
                raise
        else:
            connected = True
        self._connected = connected
        if connected:
            # create the SSL object
            try:
                self._sslobj = self.__create_sslobj(server_side, _session)

                if do_handshake_on_connect:
                    timeout = self.gettimeout()
                    if timeout == 0.0:
                        # non-blocking
                        raise ValueError(
                            "do_handshake_on_connect should not be specified for non-blocking sockets"
                        )
                    self.do_handshake()

            except socket_error as x:
                self.close()
                raise x