SPolylineItem.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SPolylineItem = void 0;
  4. const draw_1 = require("@persagy-web/draw");
  5. const base_1 = require("@persagy-web/base");
  6. const __1 = require("..");
  7. const SMathUtil_1 = require("../utils/SMathUtil");
  8. const lib_1 = require("@persagy-web/graph/lib");
  9. class SPolylineItem extends lib_1.SGraphItem {
  10. constructor(parent, list) {
  11. super(parent);
  12. this.minX = Number.MAX_SAFE_INTEGER;
  13. this.maxX = Number.MIN_SAFE_INTEGER;
  14. this.minY = Number.MAX_SAFE_INTEGER;
  15. this.maxY = Number.MIN_SAFE_INTEGER;
  16. this.pointList = [];
  17. this._status = __1.SItemStatus.Normal;
  18. this.lastPoint = null;
  19. this._strokeColor = draw_1.SColor.Black;
  20. this._fillColor = new draw_1.SColor("#2196f3");
  21. this._lineWidth = 1;
  22. this.dis = 10;
  23. this.sceneDis = 10;
  24. this.curIndex = -1;
  25. this.curPoint = null;
  26. this.undoStack = new base_1.SUndoStack();
  27. if (list instanceof draw_1.SPoint) {
  28. this.pointList.push(list);
  29. }
  30. else {
  31. this.pointList = list;
  32. }
  33. }
  34. get status() {
  35. return this._status;
  36. }
  37. set status(v) {
  38. this._status = v;
  39. this.undoStack.clear();
  40. this.update();
  41. }
  42. get strokeColor() {
  43. return this._strokeColor;
  44. }
  45. set strokeColor(v) {
  46. this._strokeColor = v;
  47. this.update();
  48. }
  49. get fillColor() {
  50. return this._fillColor;
  51. }
  52. set fillColor(v) {
  53. this._fillColor = v;
  54. this.update();
  55. }
  56. get lineWidth() {
  57. return this._lineWidth;
  58. }
  59. set lineWidth(v) {
  60. this._lineWidth = v;
  61. this.update();
  62. }
  63. addPoint(p, index) {
  64. if (index && this.canHandle(index)) {
  65. this.pointList.splice(index, 0, p);
  66. this.recordAction(lib_1.SGraphPointListInsert, [
  67. this.pointList,
  68. p,
  69. index
  70. ]);
  71. }
  72. else {
  73. this.pointList.push(p);
  74. this.recordAction(lib_1.SGraphPointListInsert, [this.pointList, p]);
  75. }
  76. this.update();
  77. }
  78. canHandle(index) {
  79. return index >= 0 && index <= this.pointList.length;
  80. }
  81. deletePoint(index) {
  82. if (this.canHandle(index) && this.pointList.length > 2) {
  83. const p = new draw_1.SPoint(this.pointList[this.curIndex].x, this.pointList[this.curIndex].y);
  84. this.pointList.splice(index, 1);
  85. this.recordAction(lib_1.SGraphPointListDelete, [
  86. this.pointList,
  87. p,
  88. index
  89. ]);
  90. this.curIndex = -1;
  91. this.curPoint = null;
  92. this.update();
  93. }
  94. }
  95. onMouseDown(event) {
  96. this.curIndex = -1;
  97. this.curPoint = null;
  98. if (event.shiftKey) {
  99. event = this.compare(event);
  100. }
  101. if (event.buttons == 1) {
  102. if (this.status == __1.SItemStatus.Create) {
  103. this.addPoint(new draw_1.SPoint(event.x, event.y));
  104. return true;
  105. }
  106. else if (this.status == __1.SItemStatus.Edit) {
  107. this.findNearestPoint(new draw_1.SPoint(event.x, event.y));
  108. if (this.curIndex < 0) {
  109. this.findAddPos(new draw_1.SPoint(event.x, event.y));
  110. }
  111. if (event.altKey && this.canHandle(this.curIndex)) {
  112. this.deletePoint(this.curIndex);
  113. }
  114. this.update();
  115. return true;
  116. }
  117. else {
  118. return super.onMouseDown(event);
  119. }
  120. }
  121. return super.onMouseDown(event);
  122. }
  123. onMouseMove(event) {
  124. if (event.shiftKey) {
  125. event = this.compare(event);
  126. }
  127. if (this.status == __1.SItemStatus.Create) {
  128. if (this.lastPoint) {
  129. this.lastPoint.x = event.x;
  130. this.lastPoint.y = event.y;
  131. }
  132. else {
  133. this.lastPoint = new draw_1.SPoint(event.x, event.y);
  134. }
  135. this.update();
  136. return true;
  137. }
  138. else if (this.status == __1.SItemStatus.Edit) {
  139. if (event.buttons == 1) {
  140. if (this.canHandle(this.curIndex)) {
  141. this.pointList[this.curIndex].x = event.x;
  142. this.pointList[this.curIndex].y = event.y;
  143. }
  144. }
  145. this.update();
  146. return true;
  147. }
  148. else {
  149. return super.onMouseMove(event);
  150. }
  151. }
  152. onMouseUp(event) {
  153. if (this.status == __1.SItemStatus.Edit) {
  154. if (this.curIndex > -1) {
  155. const p = new draw_1.SPoint(this.pointList[this.curIndex].x, this.pointList[this.curIndex].y);
  156. this.recordAction(lib_1.SGraphPointListUpdate, [
  157. this.pointList,
  158. this.curPoint,
  159. p,
  160. this.curIndex
  161. ]);
  162. }
  163. }
  164. else if (this.status == __1.SItemStatus.Normal) {
  165. this.moveToOrigin(this.x, this.y);
  166. return super.onMouseUp(event);
  167. }
  168. return true;
  169. }
  170. onDoubleClick(event) {
  171. if (this.status == __1.SItemStatus.Normal) {
  172. this.status = __1.SItemStatus.Edit;
  173. this.grabItem(this);
  174. }
  175. else if (this.status == __1.SItemStatus.Edit) {
  176. this.status = __1.SItemStatus.Normal;
  177. this.releaseItem();
  178. }
  179. else if (this.status == __1.SItemStatus.Create) {
  180. if (this.pointList.length > 1) {
  181. this.status = __1.SItemStatus.Normal;
  182. this.releaseItem();
  183. this.$emit("finishCreated");
  184. }
  185. }
  186. this.$emit("onDoubleClick", event);
  187. return true;
  188. }
  189. onKeyUp(event) {
  190. if (event.keyCode == base_1.SKeyCode.Enter) {
  191. if (this.pointList.length > 1) {
  192. if (this.status == __1.SItemStatus.Create) {
  193. this.$emit("finishCreated");
  194. }
  195. this.status = __1.SItemStatus.Normal;
  196. this.releaseItem();
  197. }
  198. }
  199. if (event.keyCode == base_1.SKeyCode.Delete &&
  200. this.status == __1.SItemStatus.Edit) {
  201. this.deletePoint(this.curIndex);
  202. }
  203. }
  204. moveToOrigin(x, y) {
  205. super.moveToOrigin(x, y);
  206. this.pointList = this.pointList.map(t => {
  207. t.x = t.x + x;
  208. t.y = t.y + y;
  209. return t;
  210. });
  211. this.x = 0;
  212. this.y = 0;
  213. }
  214. findNearestPoint(p) {
  215. let len = this.sceneDis;
  216. for (let i = 0; i < this.pointList.length; i++) {
  217. let dis = SMathUtil_1.SMathUtil.pointDistance(p.x, p.y, this.pointList[i].x, this.pointList[i].y);
  218. if (dis < len) {
  219. len = dis;
  220. this.curIndex = i;
  221. this.curPoint = new draw_1.SPoint(this.pointList[this.curIndex].x, this.pointList[this.curIndex].y);
  222. }
  223. }
  224. }
  225. findAddPos(p) {
  226. let len = SMathUtil_1.SMathUtil.pointToLine(p, new draw_1.SLine(this.pointList[0], this.pointList[1])), index = 0;
  227. if (this.pointList.length > 2) {
  228. for (let i = 1; i < this.pointList.length - 1; i++) {
  229. let dis = SMathUtil_1.SMathUtil.pointToLine(p, new draw_1.SLine(this.pointList[i], this.pointList[i + 1]));
  230. if (dis.MinDis < len.MinDis) {
  231. len = dis;
  232. index = i;
  233. }
  234. }
  235. }
  236. if (len.MinDis < this.sceneDis) {
  237. if (len.Point) {
  238. this.addPoint(len.Point, index + 1);
  239. }
  240. }
  241. }
  242. compare(event) {
  243. if (this.pointList.length) {
  244. let last = new draw_1.SPoint(event.x, event.y);
  245. if (this.status == __1.SItemStatus.Create) {
  246. last = this.pointList[this.pointList.length - 1];
  247. }
  248. else if (this.status == __1.SItemStatus.Edit) {
  249. if (this.curIndex > 1) {
  250. last = this.pointList[this.curIndex - 1];
  251. }
  252. }
  253. const dx = Math.abs(event.x - last.x);
  254. const dy = Math.abs(event.y - last.y);
  255. if (dy > dx) {
  256. event.x = last.x;
  257. }
  258. else {
  259. event.y = last.y;
  260. }
  261. }
  262. return event;
  263. }
  264. recordAction(SGraphCommand, any) {
  265. const command = new SGraphCommand(this.scene, this, ...any);
  266. this.undoStack.push(command);
  267. }
  268. boundingRect() {
  269. if (this.pointList.length) {
  270. this.minX = this.pointList[0].x;
  271. this.maxX = this.pointList[0].x;
  272. this.minY = this.pointList[0].y;
  273. this.maxY = this.pointList[0].y;
  274. this.pointList.forEach(it => {
  275. let x = it.x, y = it.y;
  276. if (x < this.minX) {
  277. this.minX = x;
  278. }
  279. if (y < this.minY) {
  280. this.minY = y;
  281. }
  282. if (x > this.maxX) {
  283. this.maxX = x;
  284. }
  285. if (y > this.maxY) {
  286. this.maxY = y;
  287. }
  288. });
  289. }
  290. return new draw_1.SRect(this.minX, this.minY, this.maxX - this.minX, this.maxY - this.minY);
  291. }
  292. contains(x, y) {
  293. let p = new draw_1.SPoint(x, y);
  294. for (let i = 1; i < this.pointList.length; i++) {
  295. let PTL = SMathUtil_1.SMathUtil.pointToLine(p, new draw_1.SLine(this.pointList[i - 1].x, this.pointList[i - 1].y, this.pointList[i].x, this.pointList[i].y));
  296. if (PTL.MinDis < this.sceneDis) {
  297. return true;
  298. }
  299. }
  300. return false;
  301. }
  302. undo() {
  303. if (this._status != __1.SItemStatus.Normal) {
  304. this.undoStack.undo();
  305. }
  306. }
  307. redo() {
  308. if (this._status != __1.SItemStatus.Normal) {
  309. this.undoStack.redo();
  310. }
  311. }
  312. cancelOperate() {
  313. if (this.status == __1.SItemStatus.Create) {
  314. this.parent = null;
  315. this.releaseItem();
  316. }
  317. else if (this.status == __1.SItemStatus.Edit) {
  318. this.status = __1.SItemStatus.Normal;
  319. this.releaseItem();
  320. }
  321. }
  322. drawBaseLine(painter) {
  323. painter.pen.color = this.strokeColor;
  324. painter.drawPolyline(this.pointList);
  325. }
  326. onDraw(painter) {
  327. this.sceneDis = painter.toPx(this.dis);
  328. painter.pen.lineWidth = painter.toPx(this.lineWidth);
  329. if (this.status == __1.SItemStatus.Create && this.lastPoint) {
  330. this.drawBaseLine(painter);
  331. painter.drawLine(this.pointList[this.pointList.length - 1], this.lastPoint);
  332. this.pointList.forEach((t, i) => {
  333. painter.brush.color = draw_1.SColor.White;
  334. if (i == this.curIndex) {
  335. painter.brush.color = this.fillColor;
  336. }
  337. painter.drawCircle(t.x, t.y, painter.toPx(5));
  338. });
  339. }
  340. else if (this.status == __1.SItemStatus.Edit) {
  341. this.drawBaseLine(painter);
  342. this.pointList.forEach((t, i) => {
  343. painter.brush.color = draw_1.SColor.White;
  344. if (i == this.curIndex) {
  345. painter.brush.color = this.fillColor;
  346. }
  347. painter.drawCircle(t.x, t.y, painter.toPx(5));
  348. });
  349. }
  350. else {
  351. if (this.selected) {
  352. painter.pen.lineWidth = painter.toPx(this.lineWidth * 2);
  353. painter.shadow.shadowBlur = 10;
  354. painter.shadow.shadowColor = new draw_1.SColor(`#00000033`);
  355. painter.shadow.shadowOffsetX = 5;
  356. painter.shadow.shadowOffsetY = 5;
  357. }
  358. this.drawBaseLine(painter);
  359. }
  360. }
  361. }
  362. exports.SPolylineItem = SPolylineItem;