diogogmt

Untitled

Apr 2nd, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const TOP_LEFT = 1
  2.   , TOP_RIGHT = 2
  3.   , BOTTOM_LEFT = 3
  4.   , BOTTOM_RIGHT = 4;
  5.  
  6. // Shape class
  7. var Shape = Base.extend({
  8.   "x": 0,
  9.   "y": 0,
  10.   "color": null,
  11.   "zindex": 0,
  12.   "lineStyle": 0,
  13.   "lineWeight": 0,
  14.   "selected": true,
  15.  
  16.   "constructor": function (opts) {
  17.     this.x = opts.x;
  18.     this.y = opts.y
  19.     this.lineStyle = opts.lineStyle;
  20.     this.lineWeight = opts.lineWeight;
  21.     this.color = opts.color;
  22.   },
  23.  
  24.   // Setters
  25.   "setX": function (aX) {
  26.     this.x = aX;
  27.   },
  28.  
  29.   "setY": function (aY) {
  30.     this.y = aY;
  31.   },
  32.  
  33.   "setColor": function (aColor) {
  34.     this.color = aColor;
  35.   },
  36.  
  37.   "setZindex": function (aZindex) {
  38.     this.zIndex = aZindex;
  39.   },
  40.  
  41.   "setLineStyle": function (aLineStyle) {
  42.     this.lineStyle = aLineStyle;
  43.   },
  44.  
  45.   "setLineWeight": function (aLineWeight) {
  46.     this.lineWeight = aLineWeight;
  47.   },
  48.  
  49.   "setSelected": function (aSelected) {
  50.     this.selected = aSelected;
  51.   },
  52.  
  53.   "setCoords": function (aCoords) {
  54.     console.log("setCoords");
  55.     this.setX(aCoords.x);
  56.     this.setY(aCoords.y);
  57.   },
  58.  
  59.   "setX": function (aX) {
  60.     this.x = aX;
  61.   },
  62.  
  63.   // Getters
  64.   "getX": function () {
  65.     return this.x;
  66.   },
  67.  
  68.   "getY": function () {
  69.     return this.y;
  70.   },
  71.  
  72.   "getColor": function () {
  73.     return this.color;
  74.   },
  75.  
  76.   "getZindex": function () {
  77.     return this.zIndex;
  78.   },
  79.  
  80.   "getLineStyle": function () {
  81.     return this.lineStyle;
  82.   },
  83.  
  84.   "getLineWeight": function () {
  85.     return this.lineWeight;
  86.   },
  87.  
  88.   "getSelected": function () {
  89.     return this.selected;
  90.   },
  91.  
  92.   "getCoords": function () {
  93.     var that = this;
  94.     return {
  95.       "x": that.getX(),
  96.       "y": that.getY(),
  97.     };
  98.   },
  99.  
  100.   "isSelected": function () {
  101.     return this.selected;
  102.   },
  103.  
  104. }); // End Shape
  105.  
  106. // Rect class
  107. var Rect = Shape.extend({
  108.  
  109.   "width": 0,
  110.   "height": 0,
  111.  
  112.   "maxCoords": {
  113.     "x": 0,
  114.     "y": 0,
  115.   },
  116.  
  117.   "minCoords": {
  118.     "x": 0,
  119.     "y": 0,
  120.   },
  121.  
  122.   "constructor": function (opts) {
  123.     console.log("opts: ", opts);
  124.     this.width = opts.width;
  125.     this.height = opts.height;
  126.     this.base({
  127.     "x": opts.x,
  128.     "y": opts.y,
  129.     "color": opts.color,
  130.     "lineStyle": opts.lineStyle,
  131.     "lineWeight": opts.lineWeight,
  132.     });
  133.   },
  134.  
  135.  
  136.   "setWidth": function (aWidth) {
  137.     this.width = aWidth;
  138.   },
  139.  
  140.   "setHeight": function (aHeight) {
  141.     this.height = aHeight;
  142.   },
  143.  
  144.   "setMinCoords": function (aMaxCoords) {
  145.     console.log("setMinCoords");
  146.     this.minCoords.x = this.x;
  147.     this.minCoords.y = this.y;
  148.   },
  149.  
  150.   "setMaxCoords": function (aMaxCoords) {
  151.     console.log("setMaxCoords");
  152.     this.maxCoords.x = this.x + this.width;
  153.     this.maxCoords.y = this.y + this.height;
  154.   },
  155.  
  156.   "setDimensions": function (aDimensions) {
  157.     console.log("setDimensions");
  158.     console.log("aDimensions: ", aDimensions);
  159.     this.setWidth(aDimensions.width);
  160.     this.setHeight(aDimensions.height);
  161.   },
  162.  
  163.  
  164.   "getWidth": function () {
  165.     return this.width;
  166.   },
  167.  
  168.   "getHeight": function () {
  169.     return this.height;
  170.   },
  171.  
  172.   "getStartCoords": function () {
  173.     return {
  174.       "x": this.maxCoords.x,
  175.       "y": this.maxCoords.y,
  176.     };
  177.   },
  178.  
  179.   "getDimensions": function () {
  180.     return {
  181.       "width": this.width,
  182.       "height": this.height,
  183.     };
  184.   },
  185.  
  186.  
  187.   "resize": function () {
  188.     console.log("\n***RECT resize");
  189.     // pjs.mouseX = 75;
  190.     // pjs.mouseY = 100
  191.  
  192.     console.log("type: ", this.resizePoint);
  193.     console.log("mouse X,Y("+pjs.mouseX+","+pjs.mouseY+")");
  194.     console.log("max X,Y("+this.maxCoords.x+","+this.maxCoords.y+")");
  195.     console.log("this X,Y("+this.x+","+this.y+")");
  196.     console.log("this width,height("+this.width+","+this.height+")");
  197.  
  198.     switch (this.resizePoint) {
  199.       // Check if mouse is going left/right
  200.       case TOP_LEFT:
  201.         if (pjs.mouseX >= this.x) {
  202.           // console.log("pjs.mouseX >= this.x");
  203.           if (pjs.mouseX <= this.maxCoords.x) {
  204.             this.width -= pjs.mouseX - this.x;
  205.             this.x = pjs.mouseX;
  206.             }
  207.             else {
  208.               this.width = pjs.mouseX - this.x;
  209.             }
  210.         }
  211.         else {
  212.           // console.log("else");
  213.           this.width += this.x - pjs.mouseX;
  214.           this.x = pjs.mouseX;
  215.         }
  216.  
  217.         // Check if mouse is going up/down
  218.         if (pjs.mouseY >= this.y) {
  219.           // console.log("pjs.mouseY >= this.y");
  220.           if (pjs.mouseY <= this.maxCoords.y) {
  221.             this.height -= pjs.mouseY - this.y;
  222.             this.y = pjs.mouseY;
  223.           }
  224.           else {
  225.             this.height = pjs.mouseY - this.y;
  226.           }
  227.         }
  228.         else {
  229.           // console.log("else");
  230.           this.height += this.y - pjs.mouseY;
  231.           this.y = pjs.mouseY;
  232.         }
  233.         break;
  234.  
  235.       case TOP_RIGHT:
  236.         if (pjs.mouseX >= this.x + this.width) {
  237.           console.log("pjs.mouseX >= this.x + this.width");
  238.           this.width += pjs.mouseX - (this.x + this.width);
  239.         }
  240.         else {
  241.           console.log("else");
  242.           if (pjs.mouseX <= this.x) {
  243.             console.log("pjs.mouseX <= this.x");
  244.             this.width += this.x - pjs.mouseX;
  245.             this.x = pjs.mouseX;
  246.           }
  247.           else {
  248.             console.log("else");
  249.             if (pjs.mouseX <= this.minCoords.x) {
  250.               console.log("HIT MIN COORDS");
  251.               this.width -= pjs.mouseX - this.x;
  252.               this.x = pjs.mouseX;
  253.             }
  254.             else {
  255.               this.width -= (this.x + this.width) - pjs.mouseX;
  256.             }
  257.           }
  258.         }
  259.  
  260.         if (pjs.mouseY < this.y) {
  261.           console.log("pjs.mouseY < this.y");
  262.           this.height += this.y - pjs.mouseY;
  263.           this.y = pjs.mouseY;
  264.         }
  265.         else {
  266.           console.log("else");
  267.           this.height -= pjs.mouseY - this.y;
  268.           this.y = pjs.mouseY;
  269.         }
  270.         break;
  271.  
  272.       default:
  273.         break;
  274.     }
  275.  
  276.     console.log("this X,Y("+this.x+","+this.y+")");
  277.     console.log("this width,height ("+this.width+","+this.height+")");
  278.     // console.log("this width,height("+this.width+","+this.height+")");
  279.  
  280.   },
  281.  
  282.   "draw": function () {
  283.     // console.log("shape draw");
  284.     pjs.fill(this.color.r, this.color.g, this.color.b);
  285.     pjs.rect(this.x, this.y, this.width, this.height);
  286.  
  287.     if (this.isSelected()) {
  288.       pjs.fill(153);
  289.       pjs.rect(this.x - 5, this.y - 5, 10, 10);
  290.       pjs.rect(this.x + this.width - 5, this.y - 5, 10, 10);
  291.  
  292.       pjs.rect(this.x - 5, this.y + this.height - 5, 10, 10);
  293.       pjs.rect(this.x + this.width -  5, this.y + this.height - 5, 10, 10);
  294.     }
  295.  
  296.   },
  297.  
  298.   isMouseOver: function () {
  299.     var mx = pjs.mouseX;
  300.     var my = pjs.mouseY;
  301.     var n = this.x <= mx && mx <= this.x + this.width &&
  302.       (this.y - this.height / 2) <= my &&
  303.       my <= (this.y + this.height / 2);
  304.  
  305.     // console.log("mx,my("+mx+","+my+")");
  306.     // console.log("return: ", n);
  307.  
  308.  
  309.     return this.x <= mx && mx <= this.x + this.width &&
  310.       (this.y - this.height / 2) <= my &&
  311.       my <= (this.y + this.height / 2);
  312.   },
  313.  
  314.   "shouldResize": function (aCoords) {
  315.     console.log("shouldResize");
  316.     console.log("this x,y ("+this.x+","+this.y+")");
  317.     console.log("this width,height ("+this.width+","+this.height+")");
  318.     console.log("aCoords: ", aCoords);
  319.  
  320.     // TOP LEFT
  321.     x = this.x - 5;
  322.     y = this.y - 5;
  323.     w = x + 10;
  324.     h = y + 10;
  325.  
  326.     x1 = aCoords.x >= x;
  327.     x2 = aCoords.x <= w;
  328.  
  329.     y1 = aCoords.y >= y;
  330.     y2 = aCoords.y <= h;
  331.  
  332.     console.log("x,y ("+x+","+y+")");
  333.     console.log("w,h ("+w+","+h+")");
  334.  
  335.     console.log("x 1,2 ("+x1+","+x2+")");
  336.     console.log("y 1,2 ("+ y1+","+y2+")");
  337.  
  338.     if (x1 && x2 && y1 && y2) {
  339.       console.log("found a match! TOP_LEFT");
  340.       this.resizePoint = TOP_LEFT;
  341.       return true;
  342.     }
  343.  
  344.  
  345.     // TOP RIGHT
  346.     x = this.x + this.width - 5;
  347.     y = this.y - 5;
  348.     w = x + 10;
  349.     h = y + 10;
  350.  
  351.     x1 = aCoords.x >= x;
  352.     x2 = aCoords.x <= w;
  353.  
  354.     y1 = aCoords.y >= y;
  355.     y2 = aCoords.y <= h;
  356.  
  357.     console.log("x,y ("+x+","+y+")");
  358.     console.log("w,h ("+w+","+h+")");
  359.  
  360.     console.log("x 1,2 ("+x1+","+x2+")");
  361.     console.log("y 1,2 ("+ y1+","+y2+")");
  362.  
  363.     if (x1 && x2 && y1 && y2) {
  364.       console.log("found a match! TOP_RIGHT");
  365.       this.resizePoint = TOP_RIGHT;
  366.       return true;
  367.     }
  368.  
  369.     return false;
  370.  
  371.   }
  372.  
  373. }); // End Rect
Advertisement
Add Comment
Please, Sign In to add comment