|
| 1 | +# Definitions for compatibility with the p5py processing library |
| 2 | +from processing import * |
| 3 | +import __main__ |
| 4 | + |
| 5 | +# Shape |
| 6 | +from processing import rectMode as rect_mode |
| 7 | +from processing import ellipseMode as ellipse_mode |
| 8 | +from processing import strokeWeight as stroke_weight |
| 9 | +from processing import strokeCap as stroke_cap |
| 10 | +from processing import strokeJoin as stroke_join |
| 11 | +from processing import noStroke as no_stroke |
| 12 | +from processing import noFill as no_fill |
| 13 | + |
| 14 | +# Fonts |
| 15 | +from processing import createFont as create_font |
| 16 | +from processing import loadFont as load_font |
| 17 | +from processing import textFont as text_font |
| 18 | + |
| 19 | +# Text |
| 20 | +from processing import textAlign as text_align |
| 21 | +from processing import textLeading as text_leading |
| 22 | +from processing import textMode as text_mode |
| 23 | +from processing import textSize as text_size |
| 24 | +from processing import textWidth as text_width |
| 25 | + |
| 26 | +# Colour |
| 27 | +from processing import blendColor as blend_color |
| 28 | +from processing import lerpColor as lerp_color |
| 29 | +from processing import color as Color |
| 30 | + |
| 31 | +# Images |
| 32 | +from processing import createImage as create_image |
| 33 | +from processing import imageMode as image_mode |
| 34 | +from processing import loadImage as load_image |
| 35 | +from processing import noTint as no_tint |
| 36 | +from processing import requestImage as request_image |
| 37 | + |
| 38 | +# Environment |
| 39 | +from processing import frameRate as frame_rate |
| 40 | +from processing import noCursor as no_cursor |
| 41 | +from processing import noLoop as no_loop |
| 42 | + |
| 43 | +# Transform |
| 44 | +from processing import applyMatrix as apply_matrix |
| 45 | +from processing import popMatrix as pop_matrix |
| 46 | +from processing import printMatrix as print_matrix |
| 47 | +from processing import pushMatrix as push_matrix |
| 48 | +from processing import resetMatrix as reset_matrix |
| 49 | +from processing import rotateX as rotate_x |
| 50 | +from processing import rotateY as rotate_y |
| 51 | +from processing import pushStyle as push_style |
| 52 | +from processing import popStyle as pop_style |
| 53 | + |
| 54 | +from processing import run as main_run |
| 55 | + |
| 56 | +# Keyboard |
| 57 | + |
| 58 | +def mousePressed(): |
| 59 | + if hasattr(__main__, "mouse_pressed"): |
| 60 | + mouse_pressed = getattr(__main__, "mouse_pressed") |
| 61 | + mouse_pressed() |
| 62 | + |
| 63 | +def mouseReleased(): |
| 64 | + if hasattr(__main__, "mouse_released"): |
| 65 | + mouse_released = getattr(__main__, "mouse_released") |
| 66 | + mouse_released() |
| 67 | + |
| 68 | +__main__.mouse_x = 0 |
| 69 | +__main__.mouse_y = 0 |
| 70 | +__main__.mouse_px = 0 |
| 71 | +__main__.mouse_py = 0 |
| 72 | +__main__.frame_count = 0 |
| 73 | +__main__.frame_rate = 60 |
| 74 | + |
| 75 | +def mouseMoved(): |
| 76 | + __main__.mouse_x = mouse.x |
| 77 | + __main__.mouse_y = mouse.y |
| 78 | + __main__.mouse_px = mouse.px |
| 79 | + __main__.mouse_py = mouse.py |
| 80 | + if hasattr(__main__, "mouse_moved"): |
| 81 | + mouse_moved = getattr(__main__, "mouse_moved") |
| 82 | + mouse_moved() |
| 83 | + |
| 84 | +def mouseDragged(): |
| 85 | + if hasattr(__main__, "mouse_dragged"): |
| 86 | + mouse_dragged = getattr(__main__, "mouse_dragged") |
| 87 | + mouse_dragged() |
| 88 | + |
| 89 | +def new_draw(): |
| 90 | + __main__.frame_count = frameCount |
| 91 | + frameRate = __main__.frame_rate |
| 92 | + old_draw() |
| 93 | + |
| 94 | +def run(): |
| 95 | + global old_draw |
| 96 | + old_draw = __main__.draw |
| 97 | + __main__.draw = new_draw |
| 98 | + main_run() |
| 99 | + |
| 100 | +def grid(): |
| 101 | + pushMatrix() |
| 102 | + stroke(200) |
| 103 | + fill(0) |
| 104 | + line(0, height/2, width, height/2) |
| 105 | + line(width/2, 0, width/2, height) |
| 106 | + x_coords = [0, width/2, width] |
| 107 | + y_coords = [0, height/2, height] |
| 108 | + |
| 109 | + for x in x_coords: |
| 110 | + for y in y_coords: |
| 111 | + show_coord(x, y) |
| 112 | + |
| 113 | + popMatrix() |
| 114 | + |
| 115 | +def circle(x, y, w): |
| 116 | + ellipse(x, y, w, w) |
| 117 | + |
| 118 | +def show_coord(x, y): |
| 119 | + if x == width: |
| 120 | + x_align = RIGHT |
| 121 | + elif x == 0: |
| 122 | + x_align = LEFT |
| 123 | + else: |
| 124 | + x_align = CENTER |
| 125 | + |
| 126 | + if y == height: |
| 127 | + y_align = BASELINE |
| 128 | + elif y == 0: |
| 129 | + y_align = TOP |
| 130 | + else: |
| 131 | + y_align = CENTER |
| 132 | + |
| 133 | + pushStyle() |
| 134 | + fill(100) |
| 135 | + text_align(x_align, y_align) |
| 136 | + text('(' + str(int(x)) + ', ' + str(int(y)) + ')', x, y) |
| 137 | + popStyle() |
| 138 | + |
| 139 | + |
0 commit comments