Rect.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.drawing.tools.Rect"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.drawing.tools.Rect"] = true;
  8. dojo.provide("dojox.drawing.tools.Rect");
  9. dojox.drawing.tools.Rect = dojox.drawing.util.oo.declare(
  10. // summary:
  11. // Class for a drawable rectangle
  12. //
  13. dojox.drawing.stencil.Rect,
  14. function(){
  15. // summary: constructor
  16. },
  17. {
  18. draws:true,
  19. onDrag: function(/*EventObject*/obj){
  20. // summary: See stencil._Base.onDrag
  21. //
  22. var s = obj.start, e = obj;
  23. var x = s.x < e.x ? s.x : e.x,
  24. y = s.y < e.y ? s.y : e.y,
  25. w = s.x < e.x ? e.x-s.x : s.x-e.x,
  26. h = s.y < e.y ? e.y-s.y : s.y-e.y;
  27. if(this.keys.shift){ w = h = Math.max(w,h); }
  28. if(this.keys.alt){
  29. x-=w; y-=h; w*=2; h*=2;
  30. x = Math.max(x, 0);
  31. y = Math.max(y, 0);
  32. }
  33. this.setPoints ([
  34. {x:x, y:y}, // TL
  35. {x:x+w, y:y}, // TR
  36. {x:x+w, y:y+h}, // BR
  37. {x:x, y:y+h} // BL
  38. ]);
  39. this.render();
  40. },
  41. onUp: function(/*EventObject*/obj){
  42. // summary: See stencil._Base.onUp
  43. //
  44. if(this.created || !this._downOnCanvas){ return; }
  45. this._downOnCanvas = false;
  46. //Default shape on single click
  47. if(!this.shape){
  48. var s = obj.start;
  49. var e = this.minimumSize*4;
  50. this.setPoints([
  51. {x:s.x, y:s.y},
  52. {x:s.x+e, y:s.y},
  53. {x:s.x+e, y:s.y+e},
  54. {x:s.x, y:s.y+e}
  55. ]);
  56. this.render();
  57. }else{
  58. // if too small, need to reset
  59. var o = this.data;
  60. if(o.width<this.minimumSize && o.height < this.minimumSize){
  61. this.remove(this.shape, this.hit);
  62. return;
  63. }
  64. }
  65. this.onRender(this);
  66. }
  67. }
  68. );
  69. dojox.drawing.tools.Rect.setup = {
  70. // summary: See stencil._Base ToolsSetup
  71. //
  72. name:"dojox.drawing.tools.Rect",
  73. tooltip:'<span class="drawingTipTitle">Rectangle Tool</span><br/>'
  74. + '<span class="drawingTipDesc">SHIFT - constrain to square</span>',
  75. iconClass:"iconRect"
  76. };
  77. dojox.drawing.register(dojox.drawing.tools.Rect.setup, "tool");
  78. }