# Path: p5/core/p5.py

# 
# Path: p5/sketch/events.py
# class KeyEvent(Event):
#     """Encapsulates information about a key event.
# 
#     :param key: The key associated with this event.
#     :type key: str
# 
#     :param pressed: Specifies whether the key is held down or not.
#     :type pressed: bool
# 
#     """
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
# 
#         if self._raw.key is not None:
#             self.key = Key(self._raw.key.name, self._raw.text)
#         else:
#             self.key = Key('UNKNOWN')
# 
#     def _update_builtins(self):
#         builtins.key_is_pressed = self.pressed
#         builtins.key = self.key if self.pressed else None
# 
# Path: p5/sketch/events.py
# class MouseEvent(Event):
#     """A class that encapsulates information about a mouse event.
# 
#     :param x: The x-position of the mouse in the window at the time of
#         the event.
#     :type x: int
# 
#     :param y: The y-position of the mouse in the window at the time of
#         the event.
#     :type y: int
# 
#     :param position: Position of the mouse in the window at the time
#         of the event.
#     :type position: (int, int)
# 
#     :param change: the change in the x and y directions (defaults to
#         (0, 0))
#     :type change: (int, int)
# 
#     :param scroll: the scroll amount in the x and y directions
#          (defaults to (0, 0)).
#     :type scroll: (int, int)
# 
#     :param count: amount by which the mouse whell was dragged.
#     :type count: int
# 
#     :param button: Button information at the time of the event.
#     :type button: MouseButton
# 
#     """
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
# 
#         x, y = self._raw.pos
#         x = max(min(builtins.width, x), 0)
#         y = max(min(builtins.height, builtins.height - y), 0)
#         dx, dy = self._raw.delta
# 
#         if (self._raw.press_event is not None) and (
#                 self._raw.last_event is not None):
#             px, py = self._raw.press_event.pos
#             cx, cy = self._raw.last_event.pos
#             self.change = Position(cx - px, cy - py)
#         else:
#             self.change = Position(0, 0)
# 
#         self.x = max(min(builtins.width, x), 0)
#         self.y = max(min(builtins.height, builtins.height - y), 0)
# 
#         self.position = Position(x, y)
#         self.scroll = Position(int(dx), int(dy))
# 
#         self.count = self.scroll.y
#         self.button = MouseButton(self._raw.buttons)
# 
#     def _update_builtins(self):
#         builtins.pmouse_x = builtins.mouse_x
#         builtins.pmouse_y = builtins.mouse_y
#         builtins.mouse_x = self.x
#         builtins.mouse_y = self.y
#         builtins.mouse_is_pressed = self._active
#         builtins.mouse_is_dragging = (self.change == (0, 0))
#         builtins.mouse_button = self.button if self.pressed else None
# 
#     def __repr__(self):
#         press = 'pressed' if self.pressed else 'not-pressed'
#         return "MouseEvent({} at {})".format(press, self.position)
# 
#     __str__ = __repr__
# 
# Path: p5/sketch/events.py
# class VispyButton(IntEnum):
# class MouseButton:
# class Key:
# class Event:
# class KeyEvent(Event):
# class MouseEvent(Event):
#     LEFT = 1
#     RIGHT = 2
#     MIDDLE = 3
#     def __init__(self, buttons):
#     def buttons(self):
#     def __eq__(self, other):
#     def __neq__(self, other):
#     def __repr__(self):
#     def __init__(self, name, text=''):
#     def __eq__(self, other):
#     def __neq__(self, other):
#     def __str__(self):
#     def __repr__(self):
#     def __init__(self, raw_event, active=False):
#     def modifiers(self):
#     def pressed(self):
#     def is_shift_down(self):
#     def is_ctrl_down(self):
#     def is_alt_down(self):
#     def is_meta_down(self):
#     def _update_builtins(self):
#     def __init__(self, *args, **kwargs):
#     def _update_builtins(self):
#     def __init__(self, *args, **kwargs):
#     def _update_builtins(self):
#     def __repr__(self):


import builtins
from PIL import Image
from vispy import app
from p5.core import p5
from ..events import KeyEvent
from ..events import MouseEvent
from ..events import handler_names
    def _save_buffer(self):
        """Save the renderer buffer to the given file.
        """
        img_data = p5.renderer.fbuffer.read(mode='color', alpha=False)
        img = Image.fromarray(img_data)
        img.save(self._save_fname)
        self._save_flag = False

    def screenshot(self, filename):
        self.queue_screenshot(filename)
        p5.renderer.flush_geometry()
        self._save_buffer()

    def queue_screenshot(self, filename):
        """Save the current frame
        """
        self._save_fname = filename
        self._save_flag = True

    def on_close(self, event):
        app.quit()

    def on_draw(self, event):
        pass

    def on_resize(self, event):
        builtins.width = int(self.size[0])
        builtins.height = int(self.size[1])

        p5.renderer.reset_view()

# Path: p5/core/p5.py

# 
# Path: p5/sketch/events.py
# class KeyEvent(Event):
#     """Encapsulates information about a key event.
# 
#     :param key: The key associated with this event.
#     :type key: str
# 
#     :param pressed: Specifies whether the key is held down or not.
#     :type pressed: bool
# 
#     """
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
# 
#         if self._raw.key is not None:
#             self.key = Key(self._raw.key.name, self._raw.text)
#         else:
#             self.key = Key('UNKNOWN')
# 
#     def _update_builtins(self):
#         builtins.key_is_pressed = self.pressed
#         builtins.key = self.key if self.pressed else None
# 
# Path: p5/sketch/events.py
# class MouseEvent(Event):
#     """A class that encapsulates information about a mouse event.
# 
#     :param x: The x-position of the mouse in the window at the time of
#         the event.
#     :type x: int
# 
#     :param y: The y-position of the mouse in the window at the time of
#         the event.
#     :type y: int
# 
#     :param position: Position of the mouse in the window at the time
#         of the event.
#     :type position: (int, int)
# 
#     :param change: the change in the x and y directions (defaults to
#         (0, 0))
#     :type change: (int, int)
# 
#     :param scroll: the scroll amount in the x and y directions
#          (defaults to (0, 0)).
#     :type scroll: (int, int)
# 
#     :param count: amount by which the mouse whell was dragged.
#     :type count: int
# 
#     :param button: Button information at the time of the event.
#     :type button: MouseButton
# 
#     """
# 
#     def __init__(self, *args, **kwargs):
#         super().__init__(*args, **kwargs)
# 
#         x, y = self._raw.pos
#         x = max(min(builtins.width, x), 0)
#         y = max(min(builtins.height, builtins.height - y), 0)
#         dx, dy = self._raw.delta
# 
#         if (self._raw.press_event is not None) and (
#                 self._raw.last_event is not None):
#             px, py = self._raw.press_event.pos
#             cx, cy = self._raw.last_event.pos
#             self.change = Position(cx - px, cy - py)
#         else:
#             self.change = Position(0, 0)
# 
#         self.x = max(min(builtins.width, x), 0)
#         self.y = max(min(builtins.height, builtins.height - y), 0)
# 
#         self.position = Position(x, y)
#         self.scroll = Position(int(dx), int(dy))
# 
#         self.count = self.scroll.y
#         self.button = MouseButton(self._raw.buttons)
# 
#     def _update_builtins(self):
#         builtins.pmouse_x = builtins.mouse_x
#         builtins.pmouse_y = builtins.mouse_y
#         builtins.mouse_x = self.x
#         builtins.mouse_y = self.y
#         builtins.mouse_is_pressed = self._active
#         builtins.mouse_is_dragging = (self.change == (0, 0))
#         builtins.mouse_button = self.button if self.pressed else None
# 
#     def __repr__(self):
#         press = 'pressed' if self.pressed else 'not-pressed'
#         return "MouseEvent({} at {})".format(press, self.position)
# 
#     __str__ = __repr__
# 
# Path: p5/sketch/events.py
# class VispyButton(IntEnum):
# class MouseButton:
# class Key:
# class Event:
# class KeyEvent(Event):
# class MouseEvent(Event):
#     LEFT = 1
#     RIGHT = 2
#     MIDDLE = 3
#     def __init__(self, buttons):
#     def buttons(self):
#     def __eq__(self, other):
#     def __neq__(self, other):
#     def __repr__(self):
#     def __init__(self, name, text=''):
#     def __eq__(self, other):
#     def __neq__(self, other):
#     def __str__(self):
#     def __repr__(self):
#     def __init__(self, raw_event, active=False):
#     def modifiers(self):
#     def pressed(self):
#     def is_shift_down(self):
#     def is_ctrl_down(self):
#     def is_alt_down(self):
#     def is_meta_down(self):
#     def _update_builtins(self):
#     def __init__(self, *args, **kwargs):
#     def _update_builtins(self):
#     def __init__(self, *args, **kwargs):
#     def _update_builtins(self):
#     def __repr__(self):


# Path: p5/sketch/Vispy2DRenderer/base.py
import builtins
from PIL import Image
from vispy import app
from p5.core import p5
from ..events import KeyEvent
from ..events import MouseEvent
from ..events import handler_names

    def _save_buffer(self):
        """Save the renderer buffer to the given file.
        """
        img_data = p5.renderer.fbuffer.read(mode='color', alpha=False)
        img = Image.fromarray(img_data)
        img.save(self._save_fname)
        self._save_flag = False

    def screenshot(self, filename):
        self.queue_screenshot(filename)
        p5.renderer.flush_geometry()
        self._save_buffer()

    def queue_screenshot(self, filename):
        """Save the current frame
        """
        self._save_fname = filename
        self._save_flag = True

    def on_close(self, event):
        app.quit()

    def on_draw(self, event):
        pass

    def on_resize(self, event):
        builtins.width = int(self.size[0])
        builtins.height = int(self.size[1])

        p5.renderer.reset_view()

        with p5.renderer.draw_loop():