mxEdgeBridge.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * Copyright (c) 2017, CTI LOGIC
  3. * Copyright (c) 2006-2017, JGraph Ltd
  4. * Copyright (c) 2006-2017, Gaudenz Alder
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  11. *
  12. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  15. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  16. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  17. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  18. */
  19. //TODO integrate this code in mxConnector
  20. mxConnector.prototype.checkLineIntersection = function (line1StartX, line1StartY, line1EndX, line1EndY,
  21. line2StartX, line2StartY, line2EndX, line2EndY)
  22. {
  23. // if the lines intersect, the result contains the the
  24. // intersection point if both line segment 1 and line segment 2 contain the point
  25. // null otherwise
  26. var denominator = ((line2EndY - line2StartY) * (line1EndX - line1StartX)) - ((line2EndX - line2StartX) * (line1EndY - line1StartY));
  27. if (denominator == 0)
  28. {
  29. return null;
  30. }
  31. else
  32. {
  33. var a = line1StartY - line2StartY;
  34. var b = line1StartX - line2StartX;
  35. var numerator1 = ((line2EndX - line2StartX) * a) - ((line2EndY - line2StartY) * b);
  36. var numerator2 = ((line1EndX - line1StartX) * a) - ((line1EndY - line1StartY) * b);
  37. a = numerator1 / denominator;
  38. b = numerator2 / denominator;
  39. // if we cast these lines infinitely in both directions, they intersect here:
  40. var x = line1StartX + (a * (line1EndX - line1StartX));
  41. var y = line1StartY + (a * (line1EndY - line1StartY));
  42. if (a > 0 && a < 1 && b > 0 && b < 1) // on both lines?
  43. return new mxPoint(x, y);
  44. else
  45. return null;
  46. }
  47. }
  48. //TODO Add curves support for curve edges
  49. //TODO Make the Bridge a curve (e.g. half a circle)
  50. mxConnector.prototype.mxConnectorPaintLine_orig = mxConnector.prototype.paintLine;
  51. mxConnector.prototype.paintLine = function(canvas, pts, rounded)
  52. {
  53. var ptsCln = pts;
  54. //TODO most probably this is not needed!
  55. if (this.state == null || !this.state.cell.isEdge())
  56. {
  57. return null;
  58. }
  59. var state = this.state;
  60. var graph = state.view.graph;
  61. //TODO the shift should be a configuration
  62. var shift = 10; //graph.edgeBridgeSize;
  63. //TODO add mxGraph bridges switch
  64. if (/*graph.enableEdgeBridge &&*/ !graph.isMouseDown)
  65. {
  66. //during DnD we can save the extra calculations
  67. var model = graph.getModel();
  68. var noRedraw = state.redrawEdge;
  69. ptsCln = [];
  70. if (state.oldIntersectIds)
  71. {
  72. var ids = state.oldIntersectIds;
  73. for (var i = 0; i < ids.length; i++)
  74. {
  75. var c = model.getCell(ids[i]);
  76. if (c != null)
  77. {
  78. var eState = graph.view.getState(c);
  79. if (eState != null && eState.style != null)
  80. {
  81. eState.redrawEdge = true;
  82. eState.shape.redraw();
  83. }
  84. }
  85. }
  86. delete state.oldIntersectIds;
  87. }
  88. //for each pair find the intersection
  89. for (var i = 0; i < pts.length - 1; i++)
  90. {
  91. var l2p1 = pts[i];
  92. var l2p2 = pts[i+1];
  93. ptsCln.push(l2p1);
  94. var intersectPt = [];
  95. for (var id in model.cells)
  96. {
  97. var edge = model.cells[id];
  98. if (edge.isEdge() && edge != state.cell)
  99. {
  100. var eState = graph.view.getState(edge);
  101. if (eState != null && eState.absolutePoints != null)
  102. {
  103. var eScale = eState.view.scale;
  104. for (var j = 0; j < eState.absolutePoints.length - 1; j++)
  105. {
  106. var l1p1 = eState.absolutePoints[j];
  107. var l1p2 = eState.absolutePoints[j+1];
  108. //the absolute points of current edge is scaled before calling this function, so we have to scale others to get correct intersections
  109. var result = this.checkLineIntersection(l1p1.x / eScale, l1p1.y / eScale, l1p2.x / eScale, l1p2.y / eScale
  110. , l2p1.x, l2p1.y, l2p2.x, l2p2.y);
  111. if (result != null)
  112. {
  113. var getEdgeOrder = function (edge1, edge2)
  114. {
  115. var p1 = edge1.getParent();
  116. var p2 = edge2.getParent();
  117. if (p1 == p2)
  118. {
  119. return p1.getIndex(edge1) - p1.getIndex(edge2); //+ve edge1 on top, -ve edge2 on top
  120. }
  121. else
  122. {
  123. return 0; //not the same parent, so we cannot determine
  124. }
  125. };
  126. if (getEdgeOrder(state.cell, eState.cell) > 0)
  127. {
  128. //instead of reordering, get the order and the top edge has the bridge
  129. var dx = result.x - l2p2.x;
  130. var dy = result.y - l2p2.y;
  131. var dist = Math.sqrt(dx * dx + dy * dy);
  132. dx /= dist;
  133. dy /= dist;
  134. var getOnLinePointAtDist = function(x1, x2, y1, y2)
  135. {
  136. var dx = x1 - x2;
  137. var dy = y1 - y2;
  138. var d = Math.sqrt(dy * dy + dx * dx);
  139. var t = shift / d;
  140. return new mxPoint((1 - t) * x1 + t * x2, (1 - t) * y1 + t * y2);
  141. }
  142. intersectPt.push(getOnLinePointAtDist(result.x, l2p1.x, result.y, l2p1.y));
  143. intersectPt.push(new mxPoint(result.x + shift * dy, result.y - shift * dx));
  144. intersectPt.push(getOnLinePointAtDist(result.x, l2p2.x, result.y, l2p2.y));
  145. //add others id here since here is the actual bridge!
  146. var otherId = state.cell.id;
  147. if (eState.oldIntersectIds != null)
  148. {
  149. eState.oldIntersectIds.push(otherId);
  150. }
  151. eState.oldIntersectIds= [otherId];
  152. }
  153. else if (!noRedraw)
  154. {
  155. eState.redrawEdge = true;
  156. eState.shape.redraw();
  157. }
  158. }
  159. }
  160. }
  161. }
  162. }
  163. //sort points and then add them
  164. intersectPt.sort(function(p1, p2) {
  165. var dx1 = p1.x - l2p1.x;
  166. var dy1 = p1.y - l2p1.y;
  167. var d1 = dx1 * dx1 + dy1 * dy1;
  168. var dx2 = p2.x - l2p1.x;
  169. var dy2 = p2.y - l2p1.y;
  170. var d2 = dx2 * dx2 + dy2 * dy2;
  171. return d1 - d2;
  172. });
  173. ptsCln.push.apply(ptsCln, intersectPt);
  174. ptsCln.push(l2p2);
  175. }
  176. if (noRedraw)
  177. {
  178. state.redrawEdge = false;
  179. }
  180. }
  181. this.mxConnectorPaintLine_orig.call(this, canvas, ptsCln, rounded);
  182. };