|
2 | 2 |
|
3 | 3 | import processing.core.PApplet;
|
4 | 4 | import static processing.core.PConstants.*;
|
| 5 | +import processing.event.MouseEvent; |
| 6 | +import processing.event.KeyEvent; |
5 | 7 |
|
6 | 8 | /**
|
7 |
| - * The purpose of this class is to enable |
8 |
| - * access to processing pre, draw and post loops in |
9 |
| - * ruby-processing as a regular java library class. |
10 |
| - * Also included background, fill and stroke methods. |
11 |
| - * PConstants should also be available from static import |
12 |
| - * @author Martin Prout |
13 |
| - */ |
| 9 | +* The purpose of this class is to enable |
| 10 | +* access to processing pre, draw and post loops in |
| 11 | +* ruby-processing as a regular java library class. |
| 12 | +* Also included background, fill and stroke methods. |
| 13 | +* PConstants should also be available from static import |
| 14 | +* @author Martin Prout |
| 15 | +*/ |
14 | 16 | public abstract class LibraryProxy {
|
15 | 17 |
|
16 |
| - private final PApplet app; |
17 |
| - |
18 |
| - /** |
19 |
| - * Useful accessors |
20 |
| - */ |
21 |
| - public int width, height; |
22 |
| - |
23 |
| - /** |
24 |
| - * |
25 |
| - * @param app PApplet |
26 |
| - */ |
27 |
| - public LibraryProxy(PApplet app) { |
28 |
| - this.app = app; |
29 |
| - this.width = app.width; |
30 |
| - this.height = app.height; |
31 |
| - setActive(true); |
32 |
| - } |
| 18 | + private final PApplet app; |
33 | 19 |
|
34 |
| - /** |
35 |
| - * Extending classes must implement this gives access to, by reflection, |
36 |
| - * processing PApplet pre loop (called before draw) |
37 |
| - */ |
38 |
| - public abstract void pre(); |
39 |
| - |
40 |
| - /** |
41 |
| - * Extending classes must implement this gives access to processing PApplet |
42 |
| - * draw loop |
43 |
| - */ |
44 |
| - public abstract void draw(); |
45 |
| - |
46 |
| - /** |
47 |
| - * Extending classes must implement this gives access to, by reflection, |
48 |
| - * processing PApplet post loop (called after draw) |
49 |
| - */ |
50 |
| - public abstract void post(); |
51 |
| - |
52 |
| - /** |
53 |
| - * Register or unregister reflection methods |
54 |
| - * @param active |
55 |
| - */ |
56 |
| - final void setActive(boolean active) { |
57 |
| - if (active) { |
58 |
| - this.app.registerMethod("pre", this); |
59 |
| - this.app.registerMethod("draw", this); |
60 |
| - this.app.registerMethod("post", this); |
61 |
| - this.app.registerMethod("dispose", this); |
62 |
| - } else { |
63 |
| - this.app.unregisterMethod("pre", this); |
64 |
| - this.app.unregisterMethod("draw", this); |
65 |
| - this.app.unregisterMethod("post", this); |
66 |
| - } |
67 |
| - } |
| 20 | + /** |
| 21 | + * Useful accessors |
| 22 | + */ |
| 23 | + public int width, height; |
68 | 24 |
|
69 |
| - /** |
70 |
| - * Simple signature for background hides need to call app |
71 |
| - * @param col int |
72 |
| - */ |
73 |
| - public void background(int col) { |
74 |
| - this.app.background(col); |
75 |
| - } |
| 25 | + /** |
| 26 | + * |
| 27 | + * @param app PApplet |
| 28 | + */ |
| 29 | + public LibraryProxy(PApplet app) { |
| 30 | + this.app = app; |
| 31 | + this.width = app.width; |
| 32 | + this.height = app.height; |
| 33 | + setActive(true); |
| 34 | + } |
76 | 35 |
|
77 |
| - /** |
78 |
| - * Simple signature for fill hides need to call app |
79 |
| - * @param col int |
80 |
| - */ |
81 |
| - public void fill(int col) { |
82 |
| - this.app.fill(col); |
83 |
| - } |
| 36 | + /** |
| 37 | + * Extending classes can override this, gives access to, by reflection, |
| 38 | + * processing PApplet pre loop (called before draw) |
| 39 | + */ |
| 40 | + public void pre(){} |
84 | 41 |
|
85 |
| - /** |
86 |
| - * Simple signature for stroke hides need to call app |
87 |
| - * @param col int |
88 |
| - */ |
89 |
| - public void stroke(int col) { |
90 |
| - this.app.stroke(col); |
91 |
| - } |
| 42 | + /** |
| 43 | + * Extending classes must implement this gives access to processing PApplet |
| 44 | + * draw loop |
| 45 | + */ |
| 46 | + public abstract void draw(); |
92 | 47 |
|
93 |
| - /** |
94 |
| - * Access applet if we must |
95 |
| - * @return applet PApplet |
96 |
| - */ |
97 |
| - public PApplet app() { |
98 |
| - return this.app; |
99 |
| - } |
| 48 | + /** |
| 49 | + * Extending classes can override this, gives access to, by reflection, |
| 50 | + * processing PApplet post loop (called after draw) |
| 51 | + */ |
| 52 | + public void post(){} |
| 53 | + |
| 54 | + /** |
| 55 | + * Extending classes can override this, gives access to, by reflection, |
| 56 | + * processing PApplet post loop (called after draw) |
| 57 | + */ |
| 58 | + public void keyEvent(KeyEvent e){} |
100 | 59 |
|
101 |
| - /** |
102 |
| - * required for processing |
103 |
| - */ |
104 |
| - public void dispose() { |
105 |
| - setActive(false); |
| 60 | + /** |
| 61 | + * Extending classes can override this, gives access to, by reflection, |
| 62 | + * processing PApplet post loop (called after draw) |
| 63 | + */ |
| 64 | + public void mouseEvent(MouseEvent e){} |
| 65 | + |
| 66 | + /** |
| 67 | + * Register or unregister reflection methods |
| 68 | + * @param active |
| 69 | + */ |
| 70 | + final void setActive(boolean active) { |
| 71 | + if (active) { |
| 72 | + this.app.registerMethod("pre", this); |
| 73 | + this.app.registerMethod("draw", this); |
| 74 | + this.app.registerMethod("post", this); |
| 75 | + this.app.registerMethod("mouseEvent", this); |
| 76 | + this.app.registerMethod("keyEvent", this); |
| 77 | + this.app.registerMethod("dispose", this); |
| 78 | + } else { |
| 79 | + this.app.unregisterMethod("pre", this); |
| 80 | + this.app.unregisterMethod("draw", this); |
| 81 | + this.app.unregisterMethod("post", this); |
| 82 | + this.app.unregisterMethod("mouseEvent", this); |
| 83 | + this.app.unregisterMethod("keyEvent", this); |
106 | 84 | }
|
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Simple signature for background hides need to call app |
| 89 | + * @param col int |
| 90 | + */ |
| 91 | + public void background(int col) { |
| 92 | + this.app.background(col); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Simple signature for fill hides need to call app |
| 97 | + * @param col int |
| 98 | + */ |
| 99 | + public void fill(int col) { |
| 100 | + this.app.fill(col); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Simple signature for stroke hides need to call app |
| 105 | + * @param col int |
| 106 | + */ |
| 107 | + public void stroke(int col) { |
| 108 | + this.app.stroke(col); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Access applet if we must |
| 113 | + * @return applet PApplet |
| 114 | + */ |
| 115 | + public PApplet app() { |
| 116 | + return this.app; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * required for processing |
| 121 | + */ |
| 122 | + public void dispose() { |
| 123 | + setActive(false); |
| 124 | + } |
107 | 125 | }
|
0 commit comments