SplineControlPoints.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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("scripts/Draw/Spline/Spline.js");
  20. /**
  21. * \class SplineControlPoints
  22. * \brief Spline from control points and degree.
  23. * \ingroup ecma_draw_spline
  24. */
  25. function SplineControlPoints(guiAction) {
  26. Spline.call(this, guiAction);
  27. this.spline = undefined;
  28. this.degree = 3;
  29. this.closed = false;
  30. this.setUiOptions("SplineControlPoints.ui");
  31. }
  32. SplineControlPoints.prototype = new Spline();
  33. SplineControlPoints.State = {
  34. SettingFirstPoint : 0,
  35. SettingNextPoint : 1
  36. };
  37. SplineControlPoints.prototype.beginEvent = function() {
  38. Spline.prototype.beginEvent.call(this);
  39. this.setState(SplineControlPoints.State.SettingFirstPoint);
  40. this.updateButtonStates();
  41. };
  42. SplineControlPoints.prototype.setState = function(state) {
  43. Spline.prototype.setState.call(this, state);
  44. this.getDocumentInterface().setClickMode(RAction.PickCoordinate);
  45. this.setCrosshairCursor();
  46. var appWin = RMainWindowQt.getMainWindow();
  47. switch (this.state) {
  48. case SplineControlPoints.State.SettingFirstPoint:
  49. var trFirstPoint = qsTr("First control point");
  50. this.setCommandPrompt(trFirstPoint);
  51. this.setLeftMouseTip(trFirstPoint);
  52. this.setRightMouseTip(EAction.trCancel);
  53. this.spline = new RSpline();
  54. this.spline.setDegree(this.degree);
  55. break;
  56. case SplineControlPoints.State.SettingNextPoint:
  57. var trNextPoint = qsTr("Next control point");
  58. this.setCommandPrompt(trNextPoint);
  59. this.setLeftMouseTip(trNextPoint);
  60. if (this.spline.isValid()) {
  61. this.setRightMouseTip(EAction.trDone);
  62. }
  63. else {
  64. this.setRightMouseTip(EAction.trCancel);
  65. }
  66. break;
  67. }
  68. EAction.showSnapTools();
  69. };
  70. SplineControlPoints.prototype.showUiOptions = function(resume) {
  71. Draw.prototype.showUiOptions.call(this, resume);
  72. this.updateButtonStates();
  73. };
  74. SplineControlPoints.prototype.escapeEvent = function() {
  75. var di = this.getDocumentInterface();
  76. switch (this.state) {
  77. case SplineControlPoints.State.SettingFirstPoint:
  78. EAction.prototype.escapeEvent.call(this);
  79. break;
  80. case SplineControlPoints.State.SettingNextPoint:
  81. var op = this.getOperation(false);
  82. if (!isNull(op)) {
  83. di.applyOperation(op);
  84. }
  85. this.setState(SplineControlPoints.State.SettingFirstPoint);
  86. this.updateButtonStates();
  87. break;
  88. }
  89. };
  90. SplineControlPoints.prototype.pickCoordinate = function(event, preview) {
  91. var point;
  92. var di = this.getDocumentInterface();
  93. switch (this.state) {
  94. case SplineControlPoints.State.SettingFirstPoint:
  95. if (!preview) {
  96. point = event.getModelPosition();
  97. this.spline.appendControlPoint(point);
  98. di.setRelativeZero(point);
  99. this.setState(SplineControlPoints.State.SettingNextPoint);
  100. }
  101. break;
  102. case SplineControlPoints.State.SettingNextPoint:
  103. point = event.getModelPosition();
  104. this.spline.appendControlPoint(point);
  105. if (preview) {
  106. this.updatePreview();
  107. //this.spline.removeLastControlPoint(false);
  108. this.spline.removeLastControlPoint();
  109. }
  110. else {
  111. di.setRelativeZero(point);
  112. this.setState(SplineControlPoints.State.SettingNextPoint);
  113. }
  114. break;
  115. }
  116. if (!preview) {
  117. this.updateButtonStates();
  118. }
  119. };
  120. SplineControlPoints.prototype.getOperation = function(preview) {
  121. if (isNull(this.spline)) {
  122. return undefined;
  123. }
  124. var spline = this.spline.clone();
  125. spline.setPeriodic(this.closed);
  126. //if (this.closed) {
  127. //spline.close();
  128. //}
  129. var e = new RSplineEntity(
  130. this.getDocument(),
  131. new RSplineData(spline)
  132. );
  133. if (!e.isValid()) {
  134. destr(e);
  135. return undefined;
  136. }
  137. /*
  138. // create bezier segments instead of NURBS:
  139. var op = new RAddObjectsOperation();
  140. var beziers = spline.getBezierSegments();
  141. for (var i=0; i<beziers.length; i++) {
  142. var e = new RSplineEntity(
  143. this.getDocument(),
  144. new RSplineData(beziers[i])
  145. );
  146. op.addObject(e);
  147. }
  148. return op;
  149. */
  150. return new RAddObjectOperation(e, this.getToolTitle());
  151. };
  152. SplineControlPoints.prototype.getAuxPreview = function() {
  153. var ret = [];
  154. if (isNull(this.spline)) {
  155. return ret;
  156. }
  157. var controlPoints = this.spline.getControlPoints();
  158. for (var i=0; i<controlPoints.length; ++i) {
  159. var v1 = controlPoints[i];
  160. var v2 = controlPoints[(i+1)%controlPoints.length];
  161. if (i+1 < controlPoints.length || this.closed) {
  162. ret.push(new RLine(v1, v2));
  163. }
  164. }
  165. return ret;
  166. };
  167. SplineControlPoints.prototype.slotClosedChanged = function(value) {
  168. this.closed = value;
  169. //if (!isNull(this.spline)) {
  170. // this.spline.setClosed(this.closed);
  171. //}
  172. this.updatePreview(true);
  173. };
  174. SplineControlPoints.prototype.slotDegreeChanged = function(value) {
  175. var optionsToolBar = EAction.getOptionsToolBar();
  176. var degreeCombo = optionsToolBar.findChild("Degree");
  177. this.degree = parseInt(degreeCombo.currentText, 10);
  178. if (!isNull(this.spline)) {
  179. this.spline.setDegree(this.degree);
  180. }
  181. this.updatePreview(true);
  182. };
  183. SplineControlPoints.prototype.slotUndo = function() {
  184. if (this.spline.countControlPoints() >= 2) {
  185. this.spline.removeLastControlPoint();
  186. //this.getDocumentInterface().undo();
  187. //this.point1 = this.pointList[this.pointList.length - 1];
  188. //this.getDocumentInterface().clearPreview();
  189. //this.getDocumentInterface().previewOperation(this.getOperation(false));
  190. }
  191. this.updateButtonStates();
  192. this.updatePreview(true);
  193. };
  194. SplineControlPoints.prototype.updateButtonStates = function() {
  195. var optionsToolBar = EAction.getOptionsToolBar();
  196. var w = optionsToolBar.findChild("Undo");
  197. if (!isNull(this.spline) && isFunction(this.spline.countControlPoints) && this.spline.countControlPoints() > 1) {
  198. w.enabled = true;
  199. } else {
  200. w.enabled = false;
  201. }
  202. /*
  203. var w = optionsToolBar.findChild("Close");
  204. if (isNull(this.spline) || this.spline.countControlPoints() < 3) {
  205. w.enabled = false;
  206. } else {
  207. w.enabled = true;
  208. }
  209. */
  210. };