SBaseRectEdit.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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 SBaseRectEdit extends SGraphEdit {
  41. /** 是否绘制完成 */
  42. _status: SItemStatus = SItemStatus.Normal;
  43. get status(): SItemStatus {
  44. return this._status;
  45. } // Get status
  46. set status(v: SItemStatus) {
  47. this._status = v;
  48. this.undoStack.clear();
  49. this.update();
  50. } // Set status
  51. width: number = 0;
  52. height: number = 0;
  53. /** 矩形两个对角 */
  54. private _line: SPoint[] = [];
  55. get line(): SPoint[] {
  56. return this._line;
  57. } // Get line
  58. set line(arr: SPoint[]) {
  59. this._line = arr;
  60. this.update();
  61. } // Get line
  62. /** 矩形左上角 */
  63. private _leftTop: SPoint = new SPoint();
  64. /** 矩形右下角 */
  65. private _rightBottom: SPoint = new SPoint();
  66. /** 绘制矩形的圆角半径 */
  67. private _radius: number = 0;
  68. get radius(): number {
  69. return this._radius;
  70. } // Get radius
  71. set radius(v: number) {
  72. if (v == this._radius) {
  73. return;
  74. }
  75. this._radius = v;
  76. this.update();
  77. } // Set radius
  78. /** 线条颜色 */
  79. _strokeColor: SColor = SColor.Black;
  80. get strokeColor(): SColor {
  81. return this._strokeColor;
  82. } // Get strokeColor
  83. set strokeColor(v: SColor) {
  84. this._strokeColor = v;
  85. this.update();
  86. } // Set strokeColor
  87. /** 填充色 */
  88. _fillColor: SColor = new SColor("#2196f3");
  89. get fillColor(): SColor {
  90. return this._fillColor;
  91. } // Get fillColor
  92. set fillColor(v: SColor) {
  93. this._fillColor = v;
  94. this.update();
  95. } // Set fillColor
  96. /** 边框样式 */
  97. _lineStyle: SLineStyle = SLineStyle.Solid;
  98. get lineStyle(): SLineStyle {
  99. return this._lineStyle;
  100. } // Get lineStyle
  101. set lineStyle(v: SLineStyle) {
  102. this._lineStyle = v;
  103. this.update();
  104. } // Set lineStyle
  105. /** 线条宽度 */
  106. _lineWidth: number = 1;
  107. get lineWidth(): number {
  108. return this._lineWidth;
  109. } // Get lineWidth
  110. set lineWidth(v: number) {
  111. this._lineWidth = v;
  112. this.update();
  113. } // Set lineWidth
  114. /** undo/redo堆栈 */
  115. private undoStack: SUndoStack = new SUndoStack();
  116. /** 传入数据 */
  117. data: Marker
  118. /**
  119. * 构造函数
  120. *
  121. * @param parent 指向父对象
  122. * @param data 矩形数据
  123. */
  124. constructor(parent: SGraphItem | null, data: Marker) {
  125. super(parent);
  126. this.data = data;
  127. if (data.style && data.style.default) {
  128. // 关于顶点
  129. if (data.style.default.line) {
  130. let setPointList: SPoint[];
  131. setPointList = data.style.default.line.map(i => {
  132. return new SPoint(i.x, i.y)
  133. });
  134. this.line = setPointList;
  135. }
  136. // 颜色
  137. if (data.style.default.strokeColor) {
  138. this.strokeColor = new SColor(data.style.default.strokeColor)
  139. }
  140. if (data.style.default.fillColor) {
  141. this.fillColor = new SColor(data.style.default.fillColor)
  142. }
  143. // 线宽
  144. if (data.style.default.lineWidth) {
  145. this.lineWidth = data.style.default.lineWidth
  146. }
  147. // 线样式
  148. if (data.style.default.lineStyle) {
  149. this.lineStyle = data.style.default.lineStyle
  150. }
  151. }
  152. } // Constructor
  153. /**
  154. * 大小改变
  155. */
  156. resize(oldSize: SRect, newSize: SRect): void {
  157. const xs = newSize.width / oldSize.width;
  158. const ys = newSize.height / oldSize.height;
  159. this.line = this.line.map(t => {
  160. t.x = t.x * xs;
  161. t.y = t.y * ys;
  162. return t
  163. })
  164. this.calRect()
  165. this.update()
  166. }
  167. /**
  168. * 鼠标按下事件
  169. *
  170. * @param event 鼠标事件
  171. * @return 是否处理事件
  172. * */
  173. onMouseDown(event: SMouseEvent): boolean {
  174. if (event.buttons == SMouseButton.LeftButton) {
  175. if (this.status == SItemStatus.Create) {
  176. this.addPoint(new SPoint(event.x, event.y));
  177. return true;
  178. } else {
  179. return super.onMouseDown(event);
  180. }
  181. }
  182. return true;
  183. } // Function onMouseDown()
  184. /**
  185. * 鼠标移动事件
  186. *
  187. * @param event 鼠标事件
  188. * @return 是否处理事件
  189. * */
  190. onMouseMove(event: SMouseEvent): boolean {
  191. if (this.status == SItemStatus.Create) {
  192. if (this.line[0] instanceof SPoint) {
  193. this.line[1] = new SPoint(event.x, event.y);
  194. this.calRect()
  195. }
  196. } else {
  197. return super.onMouseMove(event);
  198. }
  199. this.update();
  200. return true;
  201. } // Function onMouseMove()
  202. /**
  203. * 鼠标抬起事件
  204. *
  205. * @param event 事件参数
  206. * @return boolean
  207. */
  208. onMouseUp(event: SMouseEvent): boolean {
  209. if (this.status != SItemStatus.Create) {
  210. super.onMouseUp(event);
  211. }
  212. return true;
  213. } // Function onMouseUp()
  214. /**
  215. * 计算矩形的左上角和右下角
  216. */
  217. private calRect(): void {
  218. if (this.line.length > 1) {
  219. const fi = this.line[0];
  220. const se = this.line[1];
  221. let minx, maxx, miny, maxy;
  222. if (fi.x < se.x) {
  223. minx = fi.x;
  224. maxx = se.x;
  225. } else {
  226. minx = se.x;
  227. maxx = fi.x;
  228. }
  229. if (fi.y < se.y) {
  230. miny = fi.y;
  231. maxy = se.y;
  232. } else {
  233. miny = se.y;
  234. maxy = fi.y;
  235. }
  236. this._leftTop = new SPoint(minx, miny)
  237. this._rightBottom = new SPoint(maxx, maxy)
  238. }
  239. } // Function calRect
  240. /**
  241. * 添加点至数组中
  242. *
  243. * @param p 添加的点
  244. * */
  245. private addPoint(p: SPoint): void {
  246. if (this.line.length < 2) {
  247. this.line.push(p);
  248. this.recordAction(SGraphPointListInsert, [this.line, p]);
  249. } else {
  250. this.line[1] = p;
  251. this.recordAction(SGraphPointListInsert, [this.line, p, 1]);
  252. this.status = SItemStatus.Normal;
  253. this.releaseItem();
  254. this.$emit("finishCreated");
  255. this.width = this.boundingRect().width;
  256. this.height = this.boundingRect().height;
  257. }
  258. this.calRect()
  259. this.update();
  260. } // Function addPoint()
  261. /**
  262. * 记录相关动作并推入栈中
  263. *
  264. * @param SGraphCommand 相关命令类
  265. * @param any 对应传入参数
  266. */
  267. protected recordAction(SGraphCommand: any, any: any[]): void {
  268. // 记录相关命令并推入堆栈中
  269. const command = new SGraphCommand(this.scene, this, ...any);
  270. this.undoStack.push(command);
  271. } // Function recordAction()
  272. /**
  273. * Item对象边界区域
  274. *
  275. * @return SRect 外接矩阵
  276. * */
  277. boundingRect(): SRect {
  278. if (this.line.length > 1) {
  279. this.calRect()
  280. return new SRect(this._leftTop, this._rightBottom);
  281. }
  282. return new SRect()
  283. } // Function boundingRect()
  284. /**
  285. * 撤销操作
  286. *
  287. */
  288. undo(): void {
  289. if (this._status != SItemStatus.Normal) {
  290. this.undoStack.undo();
  291. }
  292. } // Function undo()
  293. /**
  294. * 重做操作
  295. *
  296. */
  297. redo(): void {
  298. if (this._status != SItemStatus.Normal) {
  299. this.undoStack.redo();
  300. }
  301. } // Function redo()
  302. /**
  303. * 取消操作执行
  304. *
  305. * */
  306. cancelOperate(): void {
  307. if (this.status == SItemStatus.Create) {
  308. this.parent = null;
  309. this.releaseItem();
  310. } else if (this.status == SItemStatus.Edit) {
  311. this.status = SItemStatus.Normal;
  312. this.releaseItem();
  313. }
  314. } // Function cancelOperate()
  315. toData() {
  316. const Line: any = [];
  317. this.line.forEach(item => {
  318. Line.push({ x: item.x, y: item.y })
  319. })
  320. this.data.style.default.line = Line;
  321. this.data.style.default.lineWidth = this.lineWidth;
  322. this.data.style.default.lineStyle = this.lineStyle;
  323. this.data.style.default.strokeColor = this.strokeColor.value;
  324. this.data.style.default.fillColor = this.fillColor.value;
  325. return this.data
  326. }
  327. /**
  328. * Item绘制操作
  329. *
  330. * @param painter painter对象
  331. */
  332. onDraw(painter: SPainter): void {
  333. if (this.line.length == 2) {
  334. this.calRect()
  335. painter.pen.color = this.strokeColor;
  336. painter.brush.color = this.fillColor;
  337. painter.pen.lineWidth = this.lineWidth;
  338. if (this.lineStyle == SLineStyle.Dashed) {
  339. painter.pen.lineDash = [
  340. painter.toPx(this.lineWidth * 3),
  341. painter.toPx(this.lineWidth * 7)
  342. ];
  343. } else if (this.lineStyle == SLineStyle.Dotted) {
  344. painter.pen.lineDash = [
  345. painter.toPx(this.lineWidth * 2),
  346. painter.toPx(this.lineWidth * 2)
  347. ];
  348. }
  349. if (this.radius != 0) {
  350. painter.drawRoundRect(this._leftTop, this._rightBottom, this.radius);
  351. } else {
  352. painter.drawRect(this._leftTop, this._rightBottom);
  353. }
  354. }
  355. } // Function onDraw()
  356. } // Class SRectEdit