Path.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // wrapped by build app
  2. define("dojox/drawing/tools/Path", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.drawing.tools.Path");
  4. dojox.drawing.tools.Path = dojox.drawing.util.oo.declare(
  5. // summary:
  6. // Class for a drawable Path
  7. //
  8. dojox.drawing.stencil.Path,
  9. function(){
  10. // summary: constructor
  11. this.pathMode = "";
  12. this.currentPathMode = "";
  13. this._started = false;
  14. this.oddEvenClicks = 0;
  15. },
  16. {
  17. draws:true,
  18. onDown: function(obj){
  19. if(!this._started){
  20. this.onStartPath(obj);
  21. }
  22. },
  23. makeSubPath: function(_closePath){
  24. if(_closePath){
  25. if(this.currentPathMode=="Q"){
  26. this.points.push({
  27. x:this.points[0].x,
  28. y:this.points[0].y
  29. });
  30. }
  31. this.points.push({t:"Z"});
  32. this.render();
  33. }
  34. this.currentPathMode = "";
  35. this.pathMode = "M";
  36. },
  37. onStartPath: function(obj){
  38. this._started = true;
  39. this.revertRenderHit = this.renderHit;
  40. this.renderHit = false;
  41. this.closePath = false;
  42. this.mouse.setEventMode("PathEdit");
  43. this.closePoint = {x:obj.x, y:obj.y};
  44. this._kc1 = this.connect(this.keys, "onEsc", this, function(){
  45. this.onCompletePath(false);
  46. });
  47. this._kc2 = this.connect(this.keys, "onKeyUp", this, function(evt){
  48. switch(evt.letter){
  49. case "c":
  50. this.onCompletePath(true); break;
  51. case "l": this.pathMode = "L"; break;
  52. case "m": this.makeSubPath(false); break;
  53. case "q": this.pathMode = "Q"; break;
  54. case "s": this.pathMode = "S"; break;
  55. case "z": this.makeSubPath(true); break;
  56. }
  57. //console.log("KEY:", evt.letter);
  58. });
  59. },
  60. onCompletePath:function(_closePath){
  61. this.remove(this.closeGuide, this.guide);
  62. var box = this.getBounds();
  63. if(box.w<this.minimumSize && box.h<this.minimumSize){
  64. this.remove(this.hit, this.shape, this.closeGuide);
  65. this._started = false;
  66. this.mouse.setEventMode("");
  67. this.setPoints([]);
  68. return;
  69. }
  70. if(_closePath){
  71. if(this.currentPathMode=="Q"){
  72. this.points.push({
  73. x:this.points[0].x,
  74. y:this.points[0].y
  75. });
  76. }
  77. this.closePath = true;
  78. }
  79. this.renderHit = this.revertRenderHit;
  80. this.renderedOnce = true;
  81. this.onRender(this);
  82. this.disconnect([this._kc1, this._kc2]);
  83. this.mouse.setEventMode("");
  84. this.render();
  85. //console.log(this.stringPath);
  86. },
  87. onUp: function(/*EventObject*/obj){
  88. //console.log(" Path UP", obj.mid, "within:", obj.withinCanvas)
  89. if(!this._started || !obj.withinCanvas){ return; }
  90. if(this.points.length>2 && this.closeRadius>this.util.distance(obj.x, obj.y, this.closePoint.x, this.closePoint.y)){
  91. this.onCompletePath(true);
  92. }else {
  93. var p = {
  94. x:obj.x,
  95. y:obj.y
  96. };
  97. this.oddEvenClicks++;
  98. if(this.currentPathMode != this.pathMode){
  99. if(this.pathMode=="Q"){
  100. p.t = "Q";
  101. this.oddEvenClicks = 0;
  102. }else if(this.pathMode=="L"){
  103. p.t = "L";
  104. }else if(this.pathMode=="M"){
  105. p.t = "M";
  106. this.closePoint = {x:obj.x, y:obj.y};
  107. }
  108. this.currentPathMode = this.pathMode;
  109. }
  110. this.points.push(p);
  111. if(this.points.length>1){
  112. this.remove(this.guide);
  113. this.render();
  114. }
  115. }
  116. //console.log(this.stringPath);
  117. },
  118. createGuide: function(obj){
  119. if(!this.points.length){ return; }
  120. var realPoints = [].concat(this.points);
  121. var pt = {
  122. x:obj.x,
  123. y:obj.y
  124. };
  125. if(this.currentPathMode=="Q" && this.oddEvenClicks % 2){
  126. // On a Q curve, every other click needs to be a
  127. // straight line - the inbetween Q coords don't render
  128. pt.t = "L"; // this is not permanent
  129. }
  130. this.points.push(pt);
  131. this.render();
  132. this.points = realPoints;
  133. var dist = this.util.distance(obj.x, obj.y, this.closePoint.x, this.closePoint.y);
  134. if(this.points.length>1){
  135. if(dist<this.closeRadius && !this.closeGuide){
  136. var c = {
  137. cx:this.closePoint.x,
  138. cy:this.closePoint.y,
  139. rx:this.closeRadius,
  140. ry:this.closeRadius
  141. }
  142. this.closeGuide = this.container.createEllipse(c)
  143. .setFill(this.closeColor);
  144. }else if(dist>this.closeRadius && this.closeGuide){
  145. this.remove(this.closeGuide);
  146. this.closeGuide = null;
  147. }
  148. }
  149. },
  150. onMove: function(obj){
  151. if(!this._started){ return; }
  152. this.createGuide(obj);
  153. },
  154. onDrag: function(obj){
  155. if(!this._started){ return; }
  156. this.createGuide(obj);
  157. }
  158. }
  159. );
  160. dojox.drawing.tools.Path.setup = {
  161. // summary: See Base ToolsSetup
  162. //
  163. name:"dojox.drawing.tools.Path",
  164. tooltip:"Path Tool",
  165. iconClass:"iconLine"
  166. };
  167. dojox.drawing.register(dojox.drawing.tools.Path.setup, "tool");
  168. });