def __init__(self, sketch, renderers=None): self.sketch = sketch if renderers is not None: renderers = [self.renderers_mapping[r] for r in renderers] if not renderers: renderers = [TkRenderer] self.renderers = renderers self.pp = PyProcessing()
class Runner: renderers_mapping = { 'TkRenderer': TkRenderer, } @classmethod def from_sketch_path(cls, sketch_path, **kwargs): spec = importlib.util.spec_from_file_location( "module.name", sketch_path, ) sketch = importlib.util.module_from_spec(spec) spec.loader.exec_module(sketch) return Runner(sketch, **kwargs) @classmethod def from_sketch_source(cls, sketch_source, **kwargs): with tempfile.NamedTemporaryFile('w') as tf: tf.write(sketch_source) return cls.from_sketch_path(tf.name, **kwargs) def __init__(self, sketch, renderers=None, logging_level=40): self.sketch = sketch if renderers is not None: renderers = [self.renderers_mapping[r] for r in renderers] if not renderers: renderers = [TkRenderer] self.renderers = renderers self.pp = PyProcessing() self.pp.logger.setLevel(logging_level) for renderer_class in self.renderers: self.pp.attach_renderer(renderer_class) def run(self): if 'setup' in dir(self.sketch): self.sketch.setup() self.pp.windows.setup() if 'draw' in dir(self.sketch): draw = self.sketch.draw else: def draw(): return callables = { func_name: getattr(self.sketch, func_name) for func_name in dir(self.sketch) if callable(getattr(self.sketch, func_name)) } self.pp.draw_fn = draw self.pp.callables.update(callables) self.pp.start()
def __init__(self, sketch, renderers=None, logging_level=40): self.sketch = sketch if renderers is not None: renderers = [self.renderers_mapping[r] for r in renderers] if not renderers: renderers = [TkRenderer] self.renderers = renderers self.pp = PyProcessing() self.pp.logger.setLevel(logging_level) for renderer_class in self.renderers: self.pp.attach_renderer(renderer_class)
class Runner: renderers_mapping = { 'TkRenderer': TkRenderer, } @classmethod def from_sketch_path(cls, sketch_path, renderers=None): spec = importlib.util.spec_from_file_location( "module.name", sketch_path, ) sketch = importlib.util.module_from_spec(spec) spec.loader.exec_module(sketch) return Runner(sketch, renderers=renderers) @classmethod def from_sketch_source(cls, sketch_source, renderers=None): with tempfile.NamedTemporaryFile('w') as tf: tf.write(sketch_source) return cls.from_sketch_path(tf.name, renderers=renderers) def __init__(self, sketch, renderers=None): self.sketch = sketch if renderers is not None: renderers = [self.renderers_mapping[r] for r in renderers] if not renderers: renderers = [TkRenderer] self.renderers = renderers self.pp = PyProcessing() def run(self): if 'setup' in dir(self.sketch): self.sketch.setup() if 'draw' in dir(self.sketch): draw = self.sketch.draw else: def draw(): return self.pp.draw = draw for renderer_class in self.renderers: self.pp.attach_renderer(renderer_class) self.pp.start()
def thread(thread_name): pp = PyProcessing() callback = pp.callables[thread_name] thread = PyProcessingThread(callback) if not hasattr(callback, '__timeout__'): timeout = None else: timeout = callback.__timeout__ thread.join(timeout)
def cursor(cursor_type): ''' Change the cursor used on the window. :param cursor_type: A cursor type to change to :type cursor_type: Union[pyprocessing.environment.CursorType, int] ''' pp = PyProcessing() pp.renderers.set_cursor(cursor_type)
def rect(x, y, width, height, *args): pp = PyProcessing() if len(args) == 0: pp.windows.set_rectangle(x, y, width, height) elif len(args) == 1: pp.windows.set_rounded_rectangle(x, y, width, height, args[0], args[0], args[0], args[0]) elif len(args) == 4: pp.windows.set_rounded_rectangle(x, y, width, height, args[0], args[1], args[2], args[3]) else: raise ValueError( f'rect() takes 4, 5 or 8 parameters. ({len(args) + 4} given)')
def stroke_cap(cap): pp = PyProcessing() if cap == 1: pp.namespace['stroke_cap_attribute'] = 'butt' elif cap == 2: pp.namespace['stroke_cap_attribute'] = 'round' elif cap == 4: pp.namespace['stroke_cap_attribute'] = 'projecting' else: pp.namespace['stroke_cap_attribute'] = 'butt'
def stroke_join(join): pp = PyProcessing() if join == 2: pp.namespace['stroke_join_attribute'] = 'round' elif join == 8: pp.namespace['stroke_join_attribute'] = 'miter' elif join == 32: pp.namespace['stroke_join_attribute'] = 'bevel' else: pp.namespace['stroke_join_attribute'] = 'bevel'
def size(w, h): pp = PyProcessing() global width global height pp.namespace['width'] = w pp.namespace['height'] = h width = w height = h pp.width = w pp.height = h
def stroke_weight(weight): pp = PyProcessing() pp.namespace['stroke_thickness_attribute'] = weight
def no_cursor(): ''' Hides the cursor from view. ''' pp = PyProcessing() pp.renderers.disable_cursor()
def quad(x1, y1, x2, y2, x3, y3, x4, y4): pp = PyProcessing() corners = [x1, y1, x2, y2, x3, y3, x4, y4] pp.windows.set_polygon(corners)
def fill(color=255): pp = PyProcessing() color = Color(color) pp.namespace['fill'] = color
def __init__(self): self.pp = PyProcessing()
def point(x, y): pp = PyProcessing() pp.windows.set_point(x, y)
def line(x1, y1, x2, y2): pp = PyProcessing() pp.windows.set_line(x1, y1, x2, y2)
def square(x, y, extent): pp = PyProcessing() pp.windows.set_rectangle(x, y, extent, extent)
def circle(x, y, size): pp = PyProcessing() pp.windows.set_ellipse(x, y, size, size)
def arc(x, y, width, height, start, stop, mode='PIE'): pp = PyProcessing() pp.windows.set_arc(x, y, width, height, start, stop, mode)
def ellipse(x, y, width, height): pp = PyProcessing() pp.windows.set_ellipse(x, y, width, height)
def stroke(color=0): pp = PyProcessing() color = Color(color) pp.namespace['stroke'] = color
def frame_rate(fr=60): pp = PyProcessing() pp.namespace['framerate'] = fr
def background(color): pp = PyProcessing() color = Color(color) pp.windows.set_background(color)
def triangle(x1, y1, x2, y2, x3, y3): pp = PyProcessing() corners = [x1, y1, x2, y2, x3, y3] pp.windows.set_polygon(corners)