SBaseCircleEdit.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2016-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SColor, SPainter, SPoint, SRect } from "@persagy-web/draw";
  27. import { SMouseButton, SMouseEvent, SUndoStack } from "@persagy-web/base";
  28. import { SItemStatus } from "@persagy-web/big";;
  29. import {
  30. SLineStyle,
  31. SGraphItem, SGraphPointListInsert
  32. } from "@persagy-web/graph/";
  33. import { SGraphEdit } from "..";
  34. import { Marker } from "../type/Marker";
  35. /**
  36. * 折线编辑类
  37. *
  38. * @author 韩耀龙 <han_yao_long@163.com>
  39. */
  40. export class SBaseCircleEdit extends SGraphEdit {
  41. /** 编辑相关操作的数据 */
  42. data: Marker
  43. _status: SItemStatus = SItemStatus.Normal;
  44. get status(): SItemStatus {
  45. return this._status;
  46. } // Get status
  47. set status(v: SItemStatus) {
  48. this._status = v;
  49. this.undoStack.clear();
  50. this.update();
  51. } // Set status
  52. /** 矩形两个对角 */
  53. private _line: SPoint[] = [];
  54. get line(): SPoint[] {
  55. return this._line;
  56. } // Get line
  57. set line(arr: SPoint[]) {
  58. this._line = arr;
  59. this.update();
  60. } // Get line
  61. /** 矩形左上角 */
  62. private _leftTop: SPoint = new SPoint();
  63. /** 矩形右下角 */
  64. private _rightBottom: SPoint = new SPoint();
  65. /** 绘制矩形的圆角半径 */
  66. private _radius: number = 0;
  67. get radius(): number {
  68. return this._radius;
  69. } // Get radius
  70. set radius(v: number) {
  71. if (v == this._radius) {
  72. return;
  73. }
  74. this._radius = v;
  75. this.update();
  76. } // Set radius
  77. /** 线条颜色 */
  78. _strokeColor: SColor = SColor.Black;
  79. get strokeColor(): SColor {
  80. return this._strokeColor;
  81. } // Get strokeColor
  82. set strokeColor(v: SColor) {
  83. this._strokeColor = v;
  84. this.update();
  85. } // Set strokeColor
  86. /** 填充色 */
  87. _fillColor: SColor = new SColor("#2196f3");
  88. get fillColor(): SColor {
  89. return this._fillColor;
  90. } // Get fillColor
  91. set fillColor(v: SColor) {
  92. this._fillColor = v;
  93. this.update();
  94. } // Set fillColor
  95. /** 边框样式 */
  96. _lineStyle: SLineStyle = SLineStyle.Solid;
  97. get lineStyle(): SLineStyle {
  98. return this._lineStyle;
  99. } // Get lineStyle
  100. set lineStyle(v: SLineStyle) {
  101. this._lineStyle = v;
  102. this.update();
  103. } // Set lineStyle
  104. /** 线条宽度 */
  105. _lineWidth: number = 1;
  106. get lineWidth(): number {
  107. return this._lineWidth;
  108. } // Get lineWidth
  109. set lineWidth(v: number) {
  110. this._lineWidth = v;
  111. this.update();
  112. } // Set lineWidth
  113. /** undo/redo堆栈 */
  114. private undoStack: SUndoStack = new SUndoStack();
  115. /**
  116. * 构造函数
  117. *
  118. * @param parent 指向父对象
  119. * @param data 矩形数据
  120. */
  121. constructor(parent: SGraphItem | null, data: Marker) {
  122. super(parent);
  123. this.data = data;
  124. if (data.style) {
  125. // 关于顶点
  126. if (data.style.line) {
  127. let setPointList: SPoint[];
  128. setPointList = data.style.line.map(i => {
  129. return new SPoint(i.x, i.y)
  130. });
  131. this.line = setPointList;
  132. this.calRect()
  133. console.log('this.line', this.line)
  134. }
  135. }
  136. if (data.style && data.style.default) {
  137. // 颜色
  138. if (data.style.default.strokeColor) {
  139. this.strokeColor = new SColor(data.style.default.strokeColor)
  140. }
  141. // 线宽
  142. if (data.style.default.lineWidth) {
  143. this.lineWidth = data.style.default.lineWidth
  144. }
  145. // 线样式
  146. if (data.style.default.lineStyle) {
  147. this.lineStyle = data.style.default.lineStyle
  148. }
  149. }
  150. } // Constructor
  151. /**
  152. * 大小改变
  153. */
  154. resize(oldSize: SRect, newSize: SRect): void {
  155. const xs = newSize.width / oldSize.width;
  156. const ys = newSize.height / oldSize.height;
  157. this.line = this.line.map(t => {
  158. t.x = t.x * xs;
  159. t.y = t.y * ys;
  160. return t
  161. })
  162. this.calRect()
  163. this.update()
  164. } // Function resize()
  165. /**
  166. * 鼠标按下事件
  167. *
  168. * @param event 鼠标事件
  169. * @return 是否处理事件
  170. */
  171. onMouseDown(event: SMouseEvent): boolean {
  172. if (event.buttons == SMouseButton.LeftButton) {
  173. if (this.status == SItemStatus.Create) {
  174. this.addPoint(new SPoint(event.x, event.y));
  175. return true;
  176. } else {
  177. return super.onMouseDown(event);
  178. }
  179. }
  180. return true;
  181. } // Function onMouseDown()
  182. /**
  183. * 鼠标移动事件
  184. *
  185. * @param event 鼠标事件
  186. * @return 是否处理事件
  187. */
  188. onMouseMove(event: SMouseEvent): boolean {
  189. if (this.status == SItemStatus.Create) {
  190. if (this.line[0] instanceof SPoint) {
  191. this.line[1] = new SPoint(event.x, event.y);
  192. this.calRect()
  193. }
  194. } else {
  195. return super.onMouseMove(event);
  196. }
  197. this.update();
  198. return true;
  199. } // Function onMouseMove()
  200. /**
  201. * 鼠标抬起事件
  202. *
  203. * @param event 事件参数
  204. * @return boolean
  205. */
  206. onMouseUp(event: SMouseEvent): boolean {
  207. if (this.status != SItemStatus.Create) {
  208. super.onMouseUp(event);
  209. }
  210. return true;
  211. } // Function onMouseUp()
  212. /**
  213. * 计算矩形的左上角和右下角
  214. */
  215. private calRect(): void {
  216. if (this.line.length > 1) {
  217. const fi = this.line[0];
  218. const se = this.line[1];
  219. let minx, maxx, miny, maxy;
  220. if (fi.x < se.x) {
  221. minx = fi.x;
  222. maxx = se.x;
  223. } else {
  224. minx = se.x;
  225. maxx = fi.x;
  226. }
  227. if (fi.y < se.y) {
  228. miny = fi.y;
  229. maxy = se.y;
  230. } else {
  231. miny = se.y;
  232. maxy = fi.y;
  233. }
  234. this._leftTop = new SPoint(minx, miny)
  235. this._rightBottom = new SPoint(maxx, maxy)
  236. }
  237. } // Function calRect()
  238. /**
  239. * 添加点至数组中
  240. *
  241. * @param p 添加的点
  242. */
  243. private addPoint(p: SPoint): void {
  244. if (this.line.length < 2) {
  245. this.line.push(p);
  246. this.recordAction(SGraphPointListInsert, [this.line, p]);
  247. } else {
  248. this.line[1] = p;
  249. this.recordAction(SGraphPointListInsert, [this.line, p, 1]);
  250. this.status = SItemStatus.Normal;
  251. this.releaseItem();
  252. this.$emit("finishCreated");
  253. }
  254. this.calRect()
  255. this.update();
  256. } // Function addPoint()
  257. /**
  258. * 记录相关动作并推入栈中
  259. *
  260. * @param SGraphCommand 相关命令类
  261. * @param any 对应传入参数
  262. */
  263. protected recordAction(SGraphCommand: any, any: any[]): void {
  264. // 记录相关命令并推入堆栈中
  265. const command = new SGraphCommand(this.scene, this, ...any);
  266. this.undoStack.push(command);
  267. } // Function recordAction()
  268. /**
  269. * Item对象边界区域
  270. *
  271. * @return SRect 外接矩阵
  272. */
  273. boundingRect(): SRect {
  274. if (this.line.length > 1) {
  275. this.calRect()
  276. return new SRect(this._leftTop, this._rightBottom);
  277. }
  278. return new SRect()
  279. } // Function boundingRect()
  280. /**
  281. * 撤销操作
  282. */
  283. undo(): void {
  284. if (this._status != SItemStatus.Normal) {
  285. this.undoStack.undo();
  286. }
  287. } // Function undo()
  288. /**
  289. * 重做操作
  290. */
  291. redo(): void {
  292. if (this._status != SItemStatus.Normal) {
  293. this.undoStack.redo();
  294. }
  295. } // Function redo()
  296. /**
  297. * 取消操作执行
  298. */
  299. cancelOperate(): void {
  300. if (this.status == SItemStatus.Create) {
  301. this.parent = null;
  302. this.releaseItem();
  303. } else if (this.status == SItemStatus.Edit) {
  304. this.status = SItemStatus.Normal;
  305. this.releaseItem();
  306. }
  307. } // Function cancelOperate()
  308. /**
  309. * 返回对象储存的相关数据
  310. *
  311. * @return 实体类数据
  312. */
  313. toData(): any {
  314. const Line = [{ x: this.line[0].x, y: this.line[0].y }, { x: this.line[1].x, y: this.line[1].y }];
  315. this.data.style.line = Line;
  316. this.data.style.default.lineWidth = this.lineWidth;
  317. this.data.style.default.lineStyle = this.lineStyle;
  318. this.data.style.default.strokeColor = this.strokeColor.value;
  319. this.data.style.default.fillColor = this.fillColor.value;
  320. return this.data
  321. }
  322. /**
  323. * Item绘制操作
  324. *
  325. * @param painter painter对象
  326. */
  327. onDraw(painter: SPainter): void {
  328. if (this.line.length == 2) {
  329. this.calRect()
  330. painter.pen.color = this.strokeColor;
  331. painter.brush.color = this.fillColor;
  332. painter.pen.lineWidth = this.lineWidth;
  333. if (this.lineStyle == SLineStyle.Dashed) {
  334. painter.pen.lineDash = [
  335. painter.toPx(this.lineWidth * 3),
  336. painter.toPx(this.lineWidth * 7)
  337. ];
  338. } else if (this.lineStyle == SLineStyle.Dotted) {
  339. painter.pen.lineDash = [
  340. painter.toPx(this.lineWidth * 2),
  341. painter.toPx(this.lineWidth * 2)
  342. ];
  343. }
  344. painter.drawEllipse(
  345. (this._leftTop.x + this._rightBottom.x) / 2,
  346. (this._leftTop.y + this._rightBottom.y) / 2,
  347. (this._rightBottom.x - this._leftTop.x) / 2,
  348. (this._rightBottom.y - this._leftTop.y) / 2
  349. );
  350. }
  351. } // Function onDraw()
  352. } // Class SCircleEdit