Advertisement
Jhynjhiruu

Untitled

Aug 7th, 2018
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var WHITE = color(255, 255, 255);
  2. var BLACK = color(0, 0, 0);
  3. var RED = color(255, 0, 0);
  4. var GREEN = color(0, 255, 0);
  5. var BLUE = color(0, 0, 255);
  6. var YELLOW = color(255, 255, 0);
  7. var CYAN = color(0, 255, 255);
  8. var MAGENTA = color(255, 0, 255); //Define standard colours
  9. var buttons = []; //Set up buttons array for filling later
  10. var mousePressed = function() {
  11.     for(var i in buttons) {
  12.         buttons[i].checkClick();
  13.     }
  14. };
  15. var mouseReleased = function() {
  16.     for(var i in buttons) {
  17.         buttons[i].mouse = false;
  18.     }
  19. };
  20. var Button = function(x, y, w, h, r, ncolour, hcolour, ccolour, normal, callback, callbackParam, debug) {
  21.     this.x = x;
  22.     this.y = y;
  23.     this.w = w;
  24.     this.h = h;
  25.     this.r = r;
  26.     this.ncolour = ncolour;
  27.     this.hcolour = hcolour;
  28.     this.ccolour = ccolour;
  29.     this.normal = normal;
  30.     this.callback = callback;
  31.     this.callbackParam = callbackParam;
  32.     this.state = this.normal;
  33.     this.debug = debug;
  34.     this.mouse = false;
  35.     this.canCallback = true;
  36. };
  37. Button.prototype.get = function() {
  38.     return this.state;
  39. };
  40. Button.prototype.checkMouse = function() {
  41.     if(mouseX >= this.x && mouseX <= this.x + this.w && mouseY >= this.y && mouseY <= this.y + this.h && !mouseIsPressed) {
  42.         this.mouseOver = true;
  43.     }
  44.     else {
  45.         this.mouseOver = false;
  46.     }
  47. };
  48. Button.prototype.checkClick = function() {
  49.     this.mouse = false;
  50.     if(this.mouseOver) {
  51.         this.mouse = true;
  52.     }
  53. };
  54. Button.prototype.checkStatus = function() {
  55.     this.state = this.normal;
  56.     if(this.mouseOver) {
  57.         this.state = !this.state;
  58.     }
  59. };
  60. Button.prototype.setColour = function() {
  61.     fill(this.ncolour);
  62.     if(this.mouse) {
  63.         fill(this.ccolour);
  64.     }
  65.     else if(this.state) {
  66.         fill(this.hcolour);
  67.     }
  68. };
  69. Button.prototype.update = function() {
  70.     this.checkMouse();
  71.     this.checkStatus();
  72.     this.setColour();
  73.     rect(this.x, this.y, this.w, this.h, this.r);
  74.     if(this.mouse && this.canCallback) {
  75.         this.canCallback = false;
  76.         if(this.callback !== null) {
  77.             this.callback(this.callbackParam);
  78.         }
  79.     }
  80.     if(!this.mouse) {
  81.         this.canCallback = true;
  82.     }
  83.     if(this.debug) {
  84.         fill(255, 0, 0);
  85.         this.debugText = [];
  86.         for(var i in this) {
  87.             this.do = true;
  88.             this.disallowed = ["constructor", "get", "callback", "setColour", "update", "__id", "checkStatus", "checkMouse", "checkClick"];
  89.             for(var j in this.disallowed) {
  90.                 if(i === this.disallowed[j]) {
  91.                     this.do = false;
  92.                 }
  93.             }
  94.             if(this.do) {
  95.                 this.debugText.push(this[i]);
  96.             }
  97.         }
  98.         text(this.debugText, this.x, this.y);
  99.     }
  100. };
  101. var printData = function(data) {
  102.     println(data);
  103. };
  104. for(var i = 0; i < 5; i++) {
  105.     buttons.push(new Button(0, i * 20, 20, 20, 5, GREEN, RED, CYAN, false, printData, "Button " + i, true));
  106. }
  107. var draw = function() {
  108.     background(WHITE);
  109.     for(var i = 0; i < 5; i++) {
  110.         buttons[i].update();
  111.     }
  112. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement