<commit_before>from __future__ import division
import pyglet
pyglet.options['shadow_window'] = False
pyglet.options['debug_gl'] = False
from pyglet import gl
from struct import unpack
from threading import Thread, Event
import os, math, time

background = pyglet.graphics.OrderedGroup(0)
foreground = pyglet.graphics.OrderedGroup(1)
overlay = pyglet.graphics.OrderedGroup(2)

mainApp = None

# TODO: rework this
class SigStop(Exception):
    pass

def stop_func(func, *args, **kwargs):
    try:
        func(*args, **kwargs)
    except SigStop:
        pass

def block(func):
    def async_func(*args, **kwargs):
        global mainApp
        func_th = Thread(target = stop_func, args = (func,) + args, kwargs = kwargs)
        mainApp.running.append(func_th)
        func_th.start()
    return async_func

def get_pixel(image, x, y):
    rawimage = image.get_image_data()
    format = 'RGBA'
    pitch = rawimage.width * len(format)
    pixels = rawimage.get_data(format, pitch)
    data = unpack("%iB" % (len(format) * rawimage.width * rawimage.height), pixels)
    index = x * len(format) + y * pitch
    return data[index : (index + len(format))]

class Entity(pyglet.event.EventDispatcher):
    
    _speed = 40
    
    def __init__(self, img_file, group=foreground, draggable=True):
        self.group = group
        image = pyglet.resource.image(img_file)
        image.anchor_x = image.width / 2
        image.anchor_y = image.height / 2
        self.sprite = pyglet.sprite.Sprite(image, group=self.group)
        self.draggable = draggable
        self.handlers = 0
        self.world_move_to(0, 0)
        self.rotation = 0
        self.invalidate(False)
    
    def check_pos(self, x, y):
        tmpx = x - self.x
        tmpy = y - self.y
        x = tmpx * math.cos(math.radians(self.rotation)) - tmpy * math.sin(math.radians(self.rotation)) + self.sprite.width / 2
        y = tmpx * math.sin(math.radians(self.rotation)) + tmpy * math.cos(math.radians(self.rotation)) + self.sprite.height / 2
        if x > 0 and y > 0 and x < self.sprite.width and y < self.sprite.height:
            data = get_pixel(self.sprite.image, int(x), int(y))
            return data[3] > 0
        return False
    
    def move(self, x, y):
        self.x += x
        self.y += y
    
    def move_to(self, x, y):
        self.x = x
        self.y = y
    
    def world_move_to(self, x, y):
        self.x = (x + App._size[0] / 2)
        self.y = (y + App._size[1] / 2)
    
    def set_group(self, group=None):
        if group:
            self.sprite.group = group
        else:
            self.sprite.group = self.group
    
    def clean(self):
        for i in range(self.handlers):
            self.pop_handlers()
    
    def register(self, *args, **kwargs):
        self.push_handlers(*args, **kwargs)
        self.handlers += 1
    
    def invalidate(self, running=True):
        self.sprite.x = self.x
        self.sprite.y = self.y
        self.sprite.rotation = self.rotation
        if running:
            time.sleep(1/self._speed)
            hook()
    
    # block events
    def start(self):
        self.dispatch_event('on_start')
    
    def click(self, x, y):
        if self.check_pos(x, y):
            self.dispatch_event('on_click')
    
    # block methods
    def forward(self, len):
        self.x += len * math.cos(math.radians(self.rotation))
        self.y += len * math.sin(-math.radians(self.rotation))
        hook()
    
    def turnRight(self, degrees):
        self.rotation += degrees
        hook()
    
    def turnLeft(self, degrees):
        self.rotation -= degrees
        hook()
    
    def gotoXY(self, x, y):
        self.world_move_to(x, y)
        hook()
    
    def doGlide(self, seconds, x, y):
        steps = seconds * self._speed
        diffX = (x + App._size[0] / 2 - self.x) / steps
        diffY = (y + App._size[1] / 2 - self.y) / steps
        for i in range(int(steps)):
            self.move(diffX, diffY)
            self.invalidate() # animation
        self.world_move_to(x, y)
        self.invalidate()
    
    def wait(self, sec):
        self.invalidate(False)
        time.sleep(sec)
        hook()

Entity.register_event_type('on_start')
Entity.register_event_type('on_click')

class App(object):

    _scale = 1
    _size = (480, 360)

    def __init__(self, create_window=True):
        if create_window:
            self.window = pyglet.window.Window(resizable=True)
            dispatcher = self.window
        else:
            context = FakeContext()
            context.set_current()
            dispatcher = widget
            self.window = None
        
        dispatcher.push_handlers(
            on_draw=self.on_draw,
            on_resize=widget.on_resize,
            on_mouse_press=self.on_mouse_press,
            on_mouse_release=self.on_mouse_release,
            on_mouse_drag=self.on_mouse_drag,
        )
        
        self.batch = pyglet.graphics.Batch()
        self.dragging = None
        self.paused = Event()
        self.paused.set()
        self.mouse_pressed = False # TODO: rework this
        
        self.entities = []
        self.running = []
        self.stopping = False
    
    def add_entity(self, entity):
        entity.sprite.batch = self.batch
        self.entities.append(entity)
    
    def reset(self):
        self.stop()
        for entity in self.entities:
            entity.clean()
        del self.entities[:]
    
    def start(self):
        for entity in self.entities:
            entity.start()
    
    def stop(self):
        self.stopping = True
        for thread in self.running:
            thread.join()
        self.stopping = False
        del self.running[:]
    
    def update(self):
        self.running = [t for t in self.running if t.is_alive()]
    
    def on_draw(self):<commit_msg>do not reset the sprites when reloading the code<commit_after>from __future__ import division
import pyglet
pyglet.options['shadow_window'] = False
pyglet.options['debug_gl'] = False
from pyglet import gl
from struct import unpack
from threading import Thread, Event
import os, math, time

background = pyglet.graphics.OrderedGroup(0)
foreground = pyglet.graphics.OrderedGroup(1)
overlay = pyglet.graphics.OrderedGroup(2)

mainApp = None

# TODO: rework this
class SigStop(Exception):
    pass

def stop_func(func, *args, **kwargs):
    try:
        func(*args, **kwargs)
    except SigStop:
        pass

def block(func):
    def async_func(*args, **kwargs):
        global mainApp
        func_th = Thread(target = stop_func, args = (func,) + args, kwargs = kwargs)
        mainApp.running.append(func_th)
        func_th.start()
    return async_func

def get_pixel(image, x, y):
    rawimage = image.get_image_data()
    format = 'RGBA'
    pitch = rawimage.width * len(format)
    pixels = rawimage.get_data(format, pitch)
    data = unpack("%iB" % (len(format) * rawimage.width * rawimage.height), pixels)
    index = x * len(format) + y * pitch
    return data[index : (index + len(format))]

class EntityData:
    def __init__(self):
        self.x = 0
        self.y = 0
        self.rotation = 0

class Entity(pyglet.event.EventDispatcher):
    
    _speed = 40
    
    def __init__(self, img_file, group=foreground, draggable=True):
        self.group = group
        image = pyglet.resource.image(img_file)
        image.anchor_x = image.width / 2
        image.anchor_y = image.height / 2
        self.sprite = pyglet.sprite.Sprite(image, group=self.group)
        self.draggable = draggable
        self.handlers = 0
    
    def set_data(self, data):
        self.data = data
        self.invalidate(False)
    
    def check_pos(self, x, y):
        tmpx = x - self.data.x - App._size[0] / 2
        tmpy = y - self.data.y - App._size[1] / 2
        x = tmpx * math.cos(math.radians(self.data.rotation)) - tmpy * math.sin(math.radians(self.data.rotation)) + self.sprite.width / 2
        y = tmpx * math.sin(math.radians(self.data.rotation)) + tmpy * math.cos(math.radians(self.data.rotation)) + self.sprite.height / 2
        if x > 0 and y > 0 and x < self.sprite.width and y < self.sprite.height:
            data = get_pixel(self.sprite.image, int(x), int(y))
            return data[3] > 0
        return False
    
    def move(self, x, y):
        self.data.x += x
        self.data.y += y
    
    def move_to(self, x, y):
        self.data.x = x - App._size[0] / 2
        self.data.y = y - App._size[1] / 2
    
    def world_move_to(self, x, y):
        self.data.x = x
        self.data.y = y
    
    def set_group(self, group=None):
        if group:
            self.sprite.group = group
        else:
            self.sprite.group = self.group
    
    def clean(self):
        for i in range(self.handlers):
            self.pop_handlers()
    
    def register(self, *args, **kwargs):
        self.push_handlers(*args, **kwargs)
        self.handlers += 1
    
    def invalidate(self, running=True):
        self.sprite.x = self.data.x + App._size[0] / 2
        self.sprite.y = self.data.y + App._size[1] / 2
        self.sprite.rotation = self.data.rotation
        if running:
            time.sleep(1/self._speed)
            hook()
    
    # block events
    def start(self):
        self.dispatch_event('on_start')
    
    def click(self, x, y):
        if self.check_pos(x, y):
            self.dispatch_event('on_click')
    
    # block methods
    def forward(self, len):
        self.data.x += len * math.cos(math.radians(self.data.rotation))
        self.data.y += len * math.sin(-math.radians(self.data.rotation))
        hook()
    
    def turnRight(self, degrees):
        self.data.rotation += degrees
        hook()
    
    def turnLeft(self, degrees):
        self.data.rotation -= degrees
        hook()
    
    def gotoXY(self, x, y):
        self.world_move_to(x, y)
        hook()
    
    def doGlide(self, seconds, x, y):
        steps = seconds * self._speed
        diffX = (x - self.data.x) / steps
        diffY = (y / 2 - self.data.y) / steps
        for i in range(int(steps)):
            self.move(diffX, diffY)
            self.invalidate() # animation
        self.world_move_to(x, y)
        self.invalidate(False)
        hook()
    
    def wait(self, sec):
        self.invalidate(False)
        time.sleep(sec)
        hook()

Entity.register_event_type('on_start')
Entity.register_event_type('on_click')

class App(object):

    _scale = 1
    _size = (480, 360)

    def __init__(self, create_window=True):
        if create_window:
            self.window = pyglet.window.Window(resizable=True)
            dispatcher = self.window
        else:
            context = FakeContext()
            context.set_current()
            dispatcher = widget
            self.window = None
        
        dispatcher.push_handlers(
            on_draw=self.on_draw,
            on_resize=widget.on_resize,
            on_mouse_press=self.on_mouse_press,
            on_mouse_release=self.on_mouse_release,
            on_mouse_drag=self.on_mouse_drag,
        )
        
        self.batch = pyglet.graphics.Batch()
        self.dragging = None
        self.paused = Event()
        self.paused.set()
        self.mouse_pressed = False # TODO: rework this
        
        self.entities = []
        self.entity_data = {}
        self.running = []
        self.stopping = False
    
    def add_entity(self, entity):
        entity.sprite.batch = self.batch
        name = entity.__class__.__name__
        if name not in self.entity_data:
            self.entity_data[name] = EntityData()
        entity.set_data(self.entity_data[name])
        self.entities.append(entity)
    
    def reset(self):
        self.stop()
        for entity in self.entities:
            entity.clean()
        del self.entities[:]
    
    def start(self):
        for entity in self.entities:
            entity.start()
    
    def stop(self):
        self.stopping = True
        for thread in self.running:
            thread.join()
        self.stopping = False
        del self.running[:]
    
    def update(self):
        self.running = [t for t in self.running if t.is_alive()]
    
    def on_draw(self):
{'fraction_non_alphanumeric': 0.11601440969379401, 'fraction_numerical': 0.005976747994105126, 'mean_word_length': 1.9865525672371638, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6238905', 'n_tokens_mistral': 3996, 'n_tokens_neox': 3948, 'n_words': 871}