DrawBasedOnRectanglePP.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("Draw.js");
  20. /**
  21. * \class DrawBasedOnRectanglePP
  22. *
  23. * \brief Base class for drawing tools that draw something based on
  24. * a rectangular shape with given width, height and angle. The tool
  25. * also supports reference points at the corners, top, left, right,
  26. * bottom and middle.
  27. *
  28. * \ingroup ecma_draw
  29. */
  30. function DrawBasedOnRectanglePP(guiAction) {
  31. EAction.call(this, guiAction);
  32. this.corner1 = undefined;
  33. this.corner2 = undefined;
  34. this.rotate = true;
  35. this.rotation = 0.0;
  36. this.previewRectangle = false;
  37. this.square = false;
  38. }
  39. DrawBasedOnRectanglePP.prototype = new EAction();
  40. DrawBasedOnRectanglePP.includeBasePath = includeBasePath;
  41. DrawBasedOnRectanglePP.State = {
  42. SettingCorner1 : 0,
  43. SettingCorner2 : 1
  44. };
  45. DrawBasedOnRectanglePP.prototype.beginEvent = function() {
  46. EAction.prototype.beginEvent.call(this);
  47. this.setState(DrawBasedOnRectanglePP.State.SettingCorner1);
  48. };
  49. DrawBasedOnRectanglePP.prototype.setState = function(state) {
  50. EAction.prototype.setState.call(this, state);
  51. var di = this.getDocumentInterface();
  52. if (isNull(di)) {
  53. return;
  54. }
  55. di.setClickMode(RAction.PickCoordinate);
  56. this.setCrosshairCursor();
  57. var appWin = RMainWindowQt.getMainWindow();
  58. switch (this.state) {
  59. case DrawBasedOnRectanglePP.State.SettingCorner1:
  60. this.corner1 = undefined;
  61. this.corner2 = undefined;
  62. var trFirstCorner = qsTr("First corner");
  63. this.setCommandPrompt(trFirstCorner);
  64. this.setLeftMouseTip(trFirstCorner);
  65. this.setRightMouseTip(EAction.trCancel);
  66. break;
  67. case DrawBasedOnRectanglePP.State.SettingCorner2:
  68. this.corner2 = undefined;
  69. var trSecondCorner = qsTr("Second corner");
  70. this.setCommandPrompt(trSecondCorner);
  71. this.setLeftMouseTip(trSecondCorner);
  72. this.setRightMouseTip(EAction.trCancel);
  73. break;
  74. }
  75. EAction.showSnapTools();
  76. };
  77. DrawBasedOnRectanglePP.prototype.escapeEvent = function() {
  78. switch (this.state) {
  79. case DrawBasedOnRectanglePP.State.SettingCorner1:
  80. EAction.prototype.escapeEvent.call(this);
  81. break;
  82. case DrawBasedOnRectanglePP.State.SettingCorner2:
  83. this.setState(DrawBasedOnRectanglePP.State.SettingCorner1);
  84. break;
  85. }
  86. };
  87. DrawBasedOnRectanglePP.prototype.pickCoordinate = function(event, preview) {
  88. var di = this.getDocumentInterface();
  89. switch (this.state) {
  90. case DrawBasedOnRectanglePP.State.SettingCorner1:
  91. this.corner1 = event.getModelPosition();
  92. if (!preview) {
  93. di.setRelativeZero(this.corner1);
  94. this.setState(DrawBasedOnRectanglePP.State.SettingCorner2);
  95. }
  96. break;
  97. case DrawBasedOnRectanglePP.State.SettingCorner2:
  98. this.corner2 = event.getModelPosition();
  99. if (preview) {
  100. this.updatePreview();
  101. }
  102. else {
  103. di.setRelativeZero(this.corner2);
  104. var op = this.getOperation(false);
  105. if (!isNull(op)) {
  106. di.applyOperation(op);
  107. this.setState(DrawBasedOnRectanglePP.State.SettingCorner1);
  108. }
  109. }
  110. break;
  111. }
  112. };
  113. DrawBasedOnRectanglePP.prototype.getCorners = function() {
  114. if (isNull(this.corner1) || isNull(this.corner2)) {
  115. return [];
  116. }
  117. var ret;
  118. var c2 = this.corner2;
  119. var rotate = !(this.rotate===false || RMath.fuzzyCompare(this.rotation, 0.0));
  120. if (rotate) {
  121. // rotate c2 around c1:
  122. c2 = c2.getRotated(-this.rotation, this.corner1);
  123. }
  124. // square:
  125. if (isShiftPressed()) {
  126. var w = c2.x - this.corner1.x;
  127. var h = c2.y - this.corner1.y;
  128. var s = Math.max(Math.abs(w), Math.abs(h));
  129. if (w<0) {
  130. c2.x = this.corner1.x - s;
  131. }
  132. else {
  133. c2.x = this.corner1.x + s;
  134. }
  135. if (h<0) {
  136. c2.y = this.corner1.y - s;
  137. }
  138. else {
  139. c2.y = this.corner1.y + s;
  140. }
  141. }
  142. ret = [
  143. new RVector(this.corner1.x, this.corner1.y),
  144. new RVector(c2.x, this.corner1.y),
  145. new RVector(c2.x, c2.y),
  146. new RVector(this.corner1.x, c2.y)
  147. ];
  148. if (rotate) {
  149. // rotate corners:
  150. for (var i=0; i<ret.length; i++) {
  151. ret[i].rotate(this.rotation, this.corner1);
  152. }
  153. }
  154. return ret;
  155. // }
  156. // else {
  157. // var l1 = new RLine(this.corner1, this.rotation, 1.0);
  158. // var l2 = new RLine(this.corner2, this.rotation + Math.PI/2, 1.0);
  159. // var ips = RShape.getIntersectionPointsLL(l1, l2, false);
  160. // if (ips.length!==1) {
  161. // return [];
  162. // }
  163. // var c3 = ips[0];
  164. // var v = this.corner1.operator_subtract(c3);
  165. // var c4 = this.corner2.operator_add(v);
  166. // return [ this.corner1, c3, this.corner2, c4 ];
  167. // }
  168. };
  169. DrawBasedOnRectanglePP.prototype.getCenter = function() {
  170. return RVector.getAverage(this.corner1, this.corner2);
  171. };
  172. DrawBasedOnRectanglePP.prototype.getWidth = function() {
  173. if (RMath.fuzzyCompare(this.rotation, 0.0)) {
  174. return Math.abs(this.corner1.x-this.corner2.x);
  175. }
  176. else {
  177. var c = this.getCorners();
  178. return c[0].getDistanceTo(c[1]);
  179. }
  180. };
  181. DrawBasedOnRectanglePP.prototype.getHeight = function() {
  182. if (RMath.fuzzyCompare(this.rotation, 0.0)) {
  183. return Math.abs(this.corner1.y-this.corner2.y);
  184. }
  185. else {
  186. var c = this.getCorners();
  187. return c[1].getDistanceTo(c[2]);
  188. }
  189. };
  190. DrawBasedOnRectanglePP.prototype.getAuxPreview = function() {
  191. if (this.previewRectangle!==true) {
  192. return undefined;
  193. }
  194. var shapes = [];
  195. var corners = this.getCorners();
  196. for (var i=0; i<corners.length; ++i) {
  197. shapes.push(new RLine(corners[i], corners[(i+1)%corners.length]));
  198. }
  199. return shapes;
  200. };