<commit_before>#!/usr/bin/env python

'''
View code for displaying 2d scenes of maps and sprites
======================================================

---------------
Getting Started
---------------

Creating a simple scene and displaying it:

    >>> import pyglet.window
    >>> import pyglet.scene2d
    >>> m = pyglet.scene2d.Map(32, 32, images=[[0]*4]*4)
    >>> w = pyglet.window.Window(width=m.pxw, height=m.pxh)
    >>> s = pyglet.scene2d.Scene(maps=[m])
    >>> r = pyglet.scene2d.FlatView(s, 0, 0, m.pxw, m.pxh)
    >>> r.debug((0,0))
    >>> w.flip()

------------------
Events and Picking
------------------


'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

import operator

from pyglet.GL.VERSION_1_1 import *
from pyglet.GLU.VERSION_1_1 import *

class FlatView:
    '''Render a flat view of a pyglet.scene2d.Scene.

    Attributes:

        scene           -- a pyglet.scene2d.Scene instance
        x, y, w, h      -- viewport specification
        allow_oob       -- indicates whether the viewport will allow
                           viewing of out-of-bounds tile positions (ie.
                           for which there is no tile image). If set to
                           False then the map will not scroll to attempt
                           to display oob tiles.
        scale, rotation -- scale to apply to the map (rotation in degrees)
        fx, fy          -- pixel point to center in the viewport, subject
                           to OOB checks
    '''
    def __init__(self, scene, x, y, w, h, allow_oob=True, scale=1,
            rotation=0, fx=0, fy=0):
        self.scene = scene
        self.x, self.y = x, y
        self.w, self.h = w, h
        self.allow_oob = allow_oob
        self.scale = scale
        self.rotation = rotation
        self.fx, self.fy = fx, fy

    def get(self, x, y):
        ''' Pick whatever is on the top at the position x, y. '''
        raise NotImplemented()

    def _determine_focus(self):
        '''Determine the focal point of the view based on foxus (fx, fy),
        allow_oob and maps.

        Note that this method does not actually change the focus attributes
        fx and fy.
        '''
        if not self.scene.maps or self.allow_oob: return (self.fx, self.fy)

        # figure the bounds min/max
        map = self.scene.maps[0]
        b_min_x = map.x
        b_min_y = map.y
        b_max_x = map.x + map.pxw
        b_max_y = map.y + map.pxh
        for map in self.scene.maps[1:]:
            b_min_x = min(b_min_x, map.x)
            b_min_y = min(b_min_y, map.y)
            b_max_x = min(b_max_x, map.x + map.pxw)
            b_max_y = min(b_max_y, map.y + map.pxh)

        # figure the view min/max based on focus
        w2 = self.w/2
        h2 = self.h/2
        fx, fy = self.fx, self.fy
        v_min_x = fx - w2
        v_min_y = fy - w2
        x_moved = y_moved = False
        if v_min_x < b_min_x:
            fx += b_min_x - v_min_x
            x_moved = True
        if v_min_y < b_min_y:
            fy += b_min_y - v_min_y
            y_moved = True

        v_max_x = fx + w2
        v_max_y = fy + w2
        if not x_moved and v_max_x > b_max_x:
            fx -= v_max_x - b_max_x
        if not y_moved and v_max_y > b_max_y:
            fy -= v_max_y - b_max_y

        return fx, fy

    def draw(self):
        '''Draw the scene centered (or closest, depending on allow_oob)
        on position which is (x, y). '''
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()
        glViewport(self.x, self.y, self.w, self.h)
        glOrtho(self.x, self.w, self.y, self.h, -50, 50)
        glMatrixMode(GL_MODELVIEW)
        glColor4f(1, 1, 0, 1)

        # sort by depth
        self.scene.maps.sort(key=operator.attrgetter('z'))

        # determine the focus point
        fx, fy = self._determine_focus()

        # now draw
        glPushMatrix()
        glTranslatef(self.w/2-fx, self.h/2-fy, 0)
        for map in self.scene.maps:
            glPushMatrix()
            glTranslatef(map.x, map.y, map.z)
            if hasattr(map, 'edge_length'):
                raise NotImplemented()
            else:
                #print '-'*50
                for column in map.cells:
                    for cell in column:
                        if not cell.tile: continue
                        x, y = cell.topleft
                        glPushMatrix()
                        glTranslatef(x, y, 0)
                        #print (x, y), cell.tile.id
                        cell.tile.texture.draw()
                        glPopMatrix()
            glPopMatrix()
        glPopMatrix()

        glMatrixMode(GL_PROJECTION)
        glPopMatrix()
<commit_msg>whoops<commit_after>#!/usr/bin/env python

'''
View code for displaying 2d scenes of maps and sprites
======================================================

---------------
Getting Started
---------------

Creating a simple scene and displaying it:

    >>> import pyglet.window
    >>> import pyglet.scene2d
    >>> m = pyglet.scene2d.Map(32, 32, images=[[0]*4]*4)
    >>> w = pyglet.window.Window(width=m.pxw, height=m.pxh)
    >>> s = pyglet.scene2d.Scene(maps=[m])
    >>> r = pyglet.scene2d.FlatView(s, 0, 0, m.pxw, m.pxh)
    >>> r.debug((0,0))
    >>> w.flip()

------------------
Events and Picking
------------------


'''

__docformat__ = 'restructuredtext'
__version__ = '$Id$'

import operator

from pyglet.GL.VERSION_1_1 import *
from pyglet.GLU.VERSION_1_1 import *

class FlatView:
    '''Render a flat view of a pyglet.scene2d.Scene.

    Attributes:

        scene           -- a pyglet.scene2d.Scene instance
        x, y, w, h      -- viewport specification
        allow_oob       -- indicates whether the viewport will allow
                           viewing of out-of-bounds tile positions (ie.
                           for which there is no tile image). If set to
                           False then the map will not scroll to attempt
                           to display oob tiles.
        scale, rotation -- scale to apply to the map (rotation in degrees)
        fx, fy          -- pixel point to center in the viewport, subject
                           to OOB checks
    '''
    def __init__(self, scene, x, y, w, h, allow_oob=True, scale=1,
            rotation=0, fx=0, fy=0):
        self.scene = scene
        self.x, self.y = x, y
        self.w, self.h = w, h
        self.allow_oob = allow_oob
        self.scale = scale
        self.rotation = rotation
        self.fx, self.fy = fx, fy

    def get(self, x, y):
        ''' Pick whatever is on the top at the position x, y. '''
        raise NotImplemented()

    def _determine_focus(self):
        '''Determine the focal point of the view based on foxus (fx, fy),
        allow_oob and maps.

        Note that this method does not actually change the focus attributes
        fx and fy.
        '''
        if not self.scene.maps or self.allow_oob: return (self.fx, self.fy)

        # figure the bounds min/max
        map = self.scene.maps[0]
        b_min_x = map.x
        b_min_y = map.y
        b_max_x = map.x + map.pxw
        b_max_y = map.y + map.pxh
        for map in self.scene.maps[1:]:
            b_min_x = min(b_min_x, map.x)
            b_min_y = min(b_min_y, map.y)
            b_max_x = min(b_max_x, map.x + map.pxw)
            b_max_y = min(b_max_y, map.y + map.pxh)

        # figure the view min/max based on focus
        w2 = self.w/2
        h2 = self.h/2
        fx, fy = self.fx, self.fy
        v_min_x = fx - w2
        v_min_y = fy - w2
        x_moved = y_moved = False
        if v_min_x < b_min_x:
            fx += b_min_x - v_min_x
            x_moved = True
        if v_min_y < b_min_y:
            fy += b_min_y - v_min_y
            y_moved = True

        v_max_x = fx + w2
        v_max_y = fy + w2
        if not x_moved and v_max_x > b_max_x:
            fx -= v_max_x - b_max_x
        if not y_moved and v_max_y > b_max_y:
            fy -= v_max_y - b_max_y

        return fx, fy

    def draw(self):
        '''Draw the scene centered (or closest, depending on allow_oob)
        on position which is (x, y). '''
        glMatrixMode(GL_PROJECTION)
        glPushMatrix()
        glLoadIdentity()
        glViewport(self.x, self.y, self.w, self.h)
        glOrtho(self.x, self.w, self.y, self.h, -50, 50)
        glMatrixMode(GL_MODELVIEW)
        glColor4f(1, 1, 0, 1)

        # sort by depth
        self.scene.maps.sort(key=operator.attrgetter('z'))

        # determine the focus point
        fx, fy = self._determine_focus()

        # now draw
        glPushMatrix()
        glTranslatef(self.w/2-fx, self.h/2-fy, 0)
        for map in self.scene.maps:
            glPushMatrix()
            glTranslatef(map.x, map.y, map.z)
            if hasattr(map, 'edge_length'):
                raise NotImplemented()
            else:
                for column in map.cells:
                    for cell in column:
                        if not cell.tile: continue
                        x, y = cell.bottomleft
                        glPushMatrix()
                        glTranslatef(x, y, 0)
                        cell.tile.texture.draw()
                        glPopMatrix()
            glPopMatrix()
        glPopMatrix()

        glMatrixMode(GL_PROJECTION)
        glPopMatrix()

{'fraction_non_alphanumeric': 0.12318531169940222, 'fraction_numerical': 0.01067463706233988, 'mean_word_length': 1.5376489707475622, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 55, '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': '3062904', 'n_tokens_mistral': 3183, 'n_tokens_neox': 3050, 'n_words': 840}