SLineItem.js 10 KB

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