SnapMiddleManual.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/Snap/Snap.js");
  20. /**
  21. * \class SnapMiddleManual
  22. * \ingroup ecma_snap
  23. * \brief Snap to middle points between two points.
  24. */
  25. function SnapMiddleManual(guiAction) {
  26. Snap.call(this, guiAction);
  27. this.State = {
  28. PickingPoint1 : 0,
  29. PickingPoint2 : 1
  30. };
  31. this.state = this.State.PickingPoint1;
  32. this.point1 = undefined;
  33. this.point2 = undefined;
  34. }
  35. SnapMiddleManual.prototype = new Snap();
  36. SnapMiddleManual.prototype.beginEvent = function() {
  37. Snap.prototype.beginEvent.call(this);
  38. this.di = this.getDocumentInterface();
  39. this.di.setSnap(new RSnapAuto());
  40. this.di.setClickMode(RAction.PickCoordinate);
  41. this.base = this.getOverrideBase();
  42. if (isNull(this.base)) {
  43. qWarning("No base action to override");
  44. this.terminate();
  45. return;
  46. }
  47. if (this.base.getClickMode()!=RAction.PickCoordinate) {
  48. qWarning("No coordinate requested at this point");
  49. this.terminate();
  50. }
  51. };
  52. SnapMiddleManual.prototype.coordinateEvent = function(event) {
  53. if (this.state===this.State.PickingPoint1) {
  54. this.point1 = event.getModelPosition();
  55. this.state = this.State.PickingPoint2;
  56. this.di.setRelativeZero(this.point1);
  57. }
  58. else {
  59. this.point2 = event.getModelPosition();
  60. var mp = this.point1.operator_add(this.point2).operator_divide(2.0);
  61. mp = this.restrict(mp);
  62. if (!isNull(this.base)) {
  63. this.base.coordinateEvent(
  64. new RCoordinateEvent(
  65. mp,
  66. event.getGraphicsScene(),
  67. event.getGraphicsView()
  68. )
  69. );
  70. }
  71. this.terminate();
  72. }
  73. };
  74. SnapMiddleManual.prototype.coordinateEventPreview = function(event) {
  75. if (this.state===this.State.PickingPoint2) {
  76. this.point2 = event.getModelPosition();
  77. var mp = this.point1.operator_add(this.point2).operator_divide(2.0);
  78. mp = this.restrict(mp);
  79. if (!isNull(this.base)) {
  80. this.base.coordinateEventPreview(
  81. new RCoordinateEvent(
  82. mp,
  83. event.getGraphicsScene(),
  84. event.getGraphicsView()
  85. )
  86. );
  87. var line = new RLine(this.point1, this.point2);
  88. this.di.addAuxShapeToPreview(line);
  89. this.di.setCursorPosition(mp);
  90. }
  91. }
  92. };