Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.07 KB | None | 0 0
  1. import java.lang.reflect.Method;
  2. import processing.core.*;
  3. import processing.event.MouseEvent;
  4. import java.util.ArrayList;
  5.  
  6. public class UI {
  7. public static final int RELEASE = MouseEvent.RELEASE;
  8. public static final int PRESS = MouseEvent.PRESS;
  9. public static final int CLICK = MouseEvent.CLICK;
  10. public static final int DRAG = MouseEvent.DRAG;
  11. ArrayList<Button> elements = new ArrayList<Button>();
  12. PApplet parent;
  13. UI(PApplet parent) {
  14. this.parent = parent;
  15. parent.registerMethod("mouseEvent", this);
  16. //parent.registerMethod("draw", this);
  17. }
  18.  
  19.  
  20. public void display() {
  21.  
  22. //parent.hint(parent.DISABLE_DEPTH_TEST);
  23. for (Button element : elements) {
  24. element.display(parent.g);
  25. }
  26. }
  27. Button add(String methodBaseName, String filename, float x, float y) {
  28. return add( methodBaseName, x, y, 1, 1).setImage(filename, true);
  29. }
  30. Button add(String methodBaseName, String filename, float x, float y, float w, float h) {
  31. return add( methodBaseName, x, y, w, h).setImage(filename, false);
  32. }
  33.  
  34. Button add(String methodBaseName, float x, float y, float w, float h) {
  35. Button element = new Button(parent, methodBaseName, (int) x, (int) y, (int) w, (int) h);
  36. elements.add(element);
  37. return element;
  38. }
  39. Button dragged = null;
  40. public void mouseEvent(MouseEvent event) {
  41. int x = event.getX();
  42. int y = event.getY();
  43.  
  44.  
  45. switch (event.getAction()) {
  46.  
  47. case MouseEvent.RELEASE:
  48. case MouseEvent.PRESS:
  49. case MouseEvent.CLICK:
  50. for (int i = elements.size () -1; i >= 0; i--) {
  51. Button element = elements.get(i);
  52.  
  53.  
  54.  
  55. if (element.visible && element.isInside(x, y) && (element.commandMethod != null)) {
  56. if (event.getAction() == MouseEvent.PRESS)
  57. dragged = element;
  58. else
  59. dragged = null;
  60. try {
  61. element.commandMethod.invoke(parent, element, event.getAction(), x - element.x, y - element.y);
  62. }
  63. catch (Exception ex) {
  64. }
  65. }
  66. }
  67.  
  68. break;
  69.  
  70. case MouseEvent.DRAG:
  71. if (dragged != null && dragged.visible)
  72. try {
  73. dragged.commandMethod.invoke(parent, dragged, event.getAction(), x - dragged.x, y - dragged.y);
  74. }
  75. catch (Exception ex) {
  76. }
  77. break;
  78. case MouseEvent.MOVE:
  79. // umm... forgot
  80.  
  81. break;
  82. }
  83. }
  84.  
  85. public class Button {
  86. int x, y, w, h;
  87. Method commandMethod;
  88. public int buttonColor = 255;
  89. PShape buttonShape;
  90. PImage buttonImage;
  91.  
  92. protected Button(PApplet sketch, String method, int x, int y, int w, int h) {
  93. this.x = x;
  94. this.y = y;
  95. this.w = w;
  96. this.h = h;
  97. setMethod(method, sketch);
  98. }
  99.  
  100. public boolean isInside(int _x, int _y) {
  101. return ( _x > x && _x < x + w && _y > y && _y < y + h);
  102. }
  103. private void setMethod(String method, PApplet sketch) {
  104. try {
  105. commandMethod = sketch.getClass().getMethod(method, //questo definisce il metodo necessario nello sketch
  106. new Class[] {
  107. Button.class,
  108. int.class,
  109. int.class,
  110. int.class
  111. }
  112. );
  113. }
  114. catch (Exception ex) {
  115. // no such method, or an error.. which is fine, just ignore
  116. commandMethod = null;
  117. PApplet.println(ex + "\nPlease implement a " + method + " method in your main sketch if you want to be informed");
  118. }
  119. }
  120.  
  121.  
  122. Button setImage(String filename) {
  123. return setImage(filename, false);
  124. }
  125. Button setImage(String filename, boolean resize) {
  126. if (filename.indexOf(".svg") > 0) {
  127. buttonShape = parent.loadShape(filename);
  128. if (resize) {
  129. w = (int) buttonShape.width;
  130. h = (int) buttonShape.height;
  131. }
  132. } else {
  133. buttonImage = parent.loadImage(filename);
  134. if (resize) {
  135. w = (int) buttonImage.width;
  136. h = (int) buttonImage.height;
  137. }
  138. }
  139. return this;
  140. }
  141. private int alignment = PApplet.LEFT;
  142. void setAlign(int alignment) {
  143. if (this.alignment == alignment)
  144. return;
  145. if (this.alignment == PApplet.LEFT && alignment == PApplet.CENTER)
  146. x -= w/ 2;
  147. else if (this.alignment == PApplet.LEFT && alignment == PApplet.RIGHT)
  148. x -= w;
  149. else if (this.alignment == PApplet.CENTER && alignment == PApplet.LEFT)
  150. x += w / 2;
  151. else if (this.alignment == PApplet.CENTER && alignment == PApplet.RIGHT)
  152. x -= w / 2;
  153. else if (this.alignment == PApplet.RIGHT && alignment == PApplet.LEFT)
  154. x += w;
  155. else if (this.alignment == PApplet.RIGHT && alignment == PApplet.CENTER)
  156. x += w / 2;
  157.  
  158. this.alignment = alignment;
  159. }
  160.  
  161. boolean visible = true;
  162. public void hide() {
  163. visible = false;
  164. }
  165.  
  166. public void show() {
  167. visible = true;
  168. }
  169.  
  170. void display(PGraphics pg) {
  171. if (!visible)
  172. return;
  173. if (buttonImage != null) {
  174. pg.image(buttonImage, x, y, w, h);
  175. } else if (buttonShape == null) {
  176. pg.noStroke();
  177. pg.fill(buttonColor);
  178. pg.rect(x, y, w, h);
  179. } else {
  180. pg.shape(buttonShape, x, y, w, h);
  181. }
  182. }
  183. }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement