<commit_before>from __future__ import division
import spyral
import pygame
import math
import operator


@spyral.memoize.SmartMemoize
def _scale(s, factor):
    if factor == (1.0, 1.0):
        return s
    size = s.get_size()
    new_size = (int(math.ceil(size[0] * factor[0])),
                int(math.ceil(size[1] * factor[1])))
    t = pygame.transform.scale(s,
            new_size,
            spyral.util.new_surface(new_size))
    return t


class Camera(object):
    """
    Represents an area to draw to. It can handle automatic scaling with optional
    offsets, layering, and more soon.
    """
    def __init__(self, virtual_size = None,
                       real_size    = None,
                       offset       = (0, 0),
                       layers       = ['all'],
                       root         = False):
        if root:
            self._surface = pygame.display.get_surface()
            if real_size is None or real_size == (0,0):
                self._rsize = self._surface.get_size()
            else:
                self._rsize = real_size
        elif real_size is None:
            raise ValueError("Must specify a real_size.")
        else:
            self._rsize = real_size

        if virtual_size is None or virtual_size == (0,0):
            self._vsize = self._rsize
            self._scale = (1.0, 1.0)
        else:
            self._vsize = virtual_size
            self._scale = (self._rsize[0] / self._vsize[0],
                           self._rsize[1] / self._vsize[1])
        self._background = None
        self._root = root
        self._offset = offset
        self._layers = layers

        if self._root:
            self._background = pygame.surface.Surface(self._rsize)
            self._background.fill((255, 255, 255))
            self._surface.blit(self._background, (0, 0))
            self._blits = []
            self._dirty_rects = []
            self._clear_this_frame = []
            self._clear_next_frame = []
            self._static_blits = {}
            self._rs = self
            self._rect = self._surface.get_rect()
            self._saved_blits = {}
            self._backgrounds = {}

    def make_child(self, virtual_size = None,
                         real_size    = None,
                         offset       = (0, 0),
                         layers       = ['all']):
        """
        Method for creating a new Camera.
        
        | *virtual_size* is a size of the virtual resolution to be used.
        | *real_size* is a size of the resolution with respect to the parent
          camera (not to the physical display, unless the parent camera is the
          root one). This allows multi-level nesting of cameras, if needed.
        | *offset* is a position offset of where this camera should be in the
          parent camera's coordinates.
        | *layers* is a list of layers which should be drawn bottom to top.
        """
        if real_size == (0,0) or real_size == None:
            real_size = self.get_size()
        y = spyral.camera.Camera(virtual_size, real_size, offset, layers, 0)
        y._parent = self
        offset = spyral.point.scale(offset, self._scale)
        y._offset = (offset[0] + self._offset[0],
                     offset[1] + self._offset[1])
        y._scale = (self._scale[0] * y._scale[0],
                    self._scale[1] * y._scale[1])
        y._rect = pygame.Rect(y._offset,
                              spyral.point.scale(real_size, self._scale))
        y._rs = self._rs
        return y

    def get_size(self):
        """
        Returns the virtual size of this camera's display.
        """
        return self._vsize
        
    def get_rect(self):
        """
        Returns a rect the virtual size of this camera's display
        """
        return pygame.rect.Rect((0,0), self._vsize)

    def set_background(self, surface):
        """
        Sets a background for this camera's display.
        """
        if surface.get_size() != self._vsize:
            raise ValueError("Background size must match the display size.")
        if not self._root:
            self._rs._background.blit(_scale(surface, self._scale),
                                      self._offset)
            self._rs._clear_this_frame.append(self._rs._background.get_rect())
        else:
            self._background = surface
            self._clear_this_frame.append(surface.get_rect())

    def _blit(self, surface, position, layer, flags):
        position = spyral.point.scale(position, self._scale)
        position = (position[0] + self._offset[0],
                    position[1] + self._offset[1])
        try:
            layer = self._layers.index(layer)
        except ValueError:
            layer = len(self._layers)

        new_surface = _scale(surface, self._scale)
        r = pygame.Rect(position, new_surface.get_size())

        if self._rect.contains(r):<commit_msg>Try to detect when people are doing the wrong things.<commit_after>from __future__ import division
import spyral
import pygame
import math
import operator
import sys


@spyral.memoize.SmartMemoize
def _scale(s, factor):
    if factor == (1.0, 1.0):
        return s
    size = s.get_size()
    new_size = (int(math.ceil(size[0] * factor[0])),
                int(math.ceil(size[1] * factor[1])))
    t = pygame.transform.scale(s,
            new_size,
            spyral.util.new_surface(new_size))
    return t


class Camera(object):
    """
    Represents an area to draw to. It can handle automatic scaling with optional
    offsets, layering, and more soon.
    """
    def __init__(self, virtual_size = None,
                       real_size    = None,
                       offset       = (0, 0),
                       layers       = ['all'],
                       root         = False):
        if root:
            self._surface = pygame.display.get_surface()
            if real_size is None or real_size == (0,0):
                self._rsize = self._surface.get_size()
            else:
                self._rsize = real_size
        elif real_size is None:
            raise ValueError("Must specify a real_size.")
        else:
            self._rsize = real_size

        if virtual_size is None or virtual_size == (0,0):
            self._vsize = self._rsize
            self._scale = (1.0, 1.0)
        else:
            self._vsize = virtual_size
            self._scale = (self._rsize[0] / self._vsize[0],
                           self._rsize[1] / self._vsize[1])
        self._background = None
        self._root = root
        self._offset = offset
        self._layers = layers

        if self._root:
            self._background = pygame.surface.Surface(self._rsize)
            self._background.fill((255, 255, 255))
            self._surface.blit(self._background, (0, 0))
            self._blits = []
            self._dirty_rects = []
            self._clear_this_frame = []
            self._clear_next_frame = []
            self._static_blits = {}
            self._rs = self
            self._rect = self._surface.get_rect()
            self._saved_blits = {}
            self._backgrounds = {}

    def make_child(self, virtual_size = None,
                         real_size    = None,
                         offset       = (0, 0),
                         layers       = ['all']):
        """
        Method for creating a new Camera.
        
        | *virtual_size* is a size of the virtual resolution to be used.
        | *real_size* is a size of the resolution with respect to the parent
          camera (not to the physical display, unless the parent camera is the
          root one). This allows multi-level nesting of cameras, if needed.
        | *offset* is a position offset of where this camera should be in the
          parent camera's coordinates.
        | *layers* is a list of layers which should be drawn bottom to top.
        """
        if real_size == (0,0) or real_size == None:
            real_size = self.get_size()
        y = spyral.camera.Camera(virtual_size, real_size, offset, layers, 0)
        y._parent = self
        offset = spyral.point.scale(offset, self._scale)
        y._offset = (offset[0] + self._offset[0],
                     offset[1] + self._offset[1])
        y._scale = (self._scale[0] * y._scale[0],
                    self._scale[1] * y._scale[1])
        y._rect = pygame.Rect(y._offset,
                              spyral.point.scale(real_size, self._scale))
        y._rs = self._rs
        return y

    def get_size(self):
        """
        Returns the virtual size of this camera's display.
        """
        return self._vsize
        
    def get_rect(self):
        """
        Returns a rect the virtual size of this camera's display
        """
        return pygame.rect.Rect((0,0), self._vsize)

    def set_background(self, surface):
        """
        Sets a background for this camera's display.
        """
        # This is CPython specific, but right now so is Pygame.
        if sys._getframe(1).f_code.co_name == "__init__":
            raise RuntimeError("Background initialization must be done in a scene's on_enter, not __init__")
        if surface.get_size() != self._vsize:
            raise ValueError("Background size must match the display size.")
        if not self._root:
            self._rs._background.blit(_scale(surface, self._scale),
                                      self._offset)
            self._rs._clear_this_frame.append(self._rs._background.get_rect())
        else:
            self._background = surface
            self._clear_this_frame.append(surface.get_rect())

    def _blit(self, surface, position, layer, flags):
        position = spyral.point.scale(position, self._scale)
        position = (position[0] + self._offset[0],
                    position[1] + self._offset[1])
        try:
            layer = self._layers.index(layer)
        except ValueError:
            layer = len(self._layers)

        new_surface = _scale(surface, self._scale)
        r = pygame.Rect(position, new_surface.get_size())

        if self._rect.contains(r):
{'fraction_non_alphanumeric': 0.10173501577287067, 'fraction_numerical': 0.010350946372239747, 'mean_word_length': 1.6767810026385224, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '278796', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2797, 'n_words': 757}