SCustomLegendItem.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { SGraphItem, SLineStyle, STextOrigin } from "@saga-web/graph/lib";
  2. import { Legend } from '../types/Legend';
  3. import { SPainter, SColor, SFont, SPoint, SLineCapStyle } from "@saga-web/draw";
  4. import { STextItem } from '@saga-web/graph/lib';
  5. import { hexify, uuid } from "@/components/mapClass/until"
  6. import { SItemStatus, ItemOrder, SPolygonItem } from '@saga-web/big/lib';
  7. import { SMouseEvent } from "@saga-web/base/lib";
  8. /**
  9. * 图例节点Item(区域类型)
  10. *
  11. * * @author 张宇(taohuzy@163.com)
  12. */
  13. export class SCustomLegendItem extends SPolygonItem {
  14. /** 图例节点对象数据 */
  15. data: Legend;
  16. /** text item */
  17. textItem: STextItem = new STextItem(this);
  18. get text(): string {
  19. return this.textItem.text;
  20. }
  21. set text(v: string) {
  22. this.textItem.text = v;
  23. this.update();
  24. }
  25. get color(): SColor {
  26. return this.textItem.color;
  27. }
  28. set color(v: SColor) {
  29. this.textItem.color = v;
  30. }
  31. get font(): SFont {
  32. return this.textItem.font;
  33. }
  34. set font(v: SFont) {
  35. this.textItem.font = v;
  36. }
  37. get status(): SItemStatus {
  38. return this._status;
  39. }
  40. // 编辑当前状态
  41. set status(value: SItemStatus) {
  42. this._status = value;
  43. // 如果状态为show则需清栈
  44. if (value == SItemStatus.Normal) {
  45. // 切换显示状态显示文本
  46. this.showText = true;
  47. // 切换显示状态不可移动文本
  48. this.textItem.moveable = false;
  49. if (this.undoStack) {
  50. this.undoStack.clear();
  51. }
  52. } else if (value == SItemStatus.Edit) {
  53. // 切换编辑状态显示文本
  54. this.showText = true;
  55. // 切换编辑状态可移动文本
  56. this.textItem.moveable = true;
  57. } else if (value == SItemStatus.Create) {
  58. // 切换创建状态不显示文本
  59. this.showText = false;
  60. // 切换创建状态不可移动文本
  61. this.textItem.moveable = false;
  62. }
  63. this.update();
  64. };
  65. /** 是否激活 */
  66. _isActive: boolean = false;
  67. get isActive(): boolean {
  68. return this._isActive;
  69. } // Get isActive
  70. set isActive(v: boolean) {
  71. this._isActive = v;
  72. if (v) {
  73. this.cursor = "pointer";
  74. } else {
  75. this.cursor = "auto";
  76. }
  77. this.update();
  78. } // Set isActive
  79. /** 是否显示文字 */
  80. _showText: boolean = true;
  81. get showText(): boolean {
  82. return this._showText;
  83. }
  84. set showText(v: boolean) {
  85. if (v === this._showText) {
  86. return
  87. }
  88. this._showText = v;
  89. if (v) {
  90. this.textItem.show();
  91. } else {
  92. this.textItem.hide();
  93. }
  94. }
  95. /** 是否蒙版遮罩 */
  96. _maskFlag: boolean = false;
  97. get maskFlag(): boolean {
  98. return this._maskFlag;
  99. }
  100. set maskFlag(v: boolean) {
  101. if (v === this._maskFlag) {
  102. return
  103. }
  104. this._maskFlag = v;
  105. this.update()
  106. }
  107. /**
  108. * 构造函数
  109. *
  110. * @param parent 指向父对象
  111. * @param data 图例节点对象数据
  112. */
  113. constructor(parent: SGraphItem | null, data: Legend) {
  114. super(parent);
  115. this.textItem.originPosition = STextOrigin.Centrum;
  116. this.textItem.isTransform = false;
  117. this.zOrder = ItemOrder.polygonOrder;
  118. this.data = data;
  119. if (!data.ID) {
  120. data.ID = uuid()
  121. }
  122. this.id = data.ID;
  123. this.name = data.Name;
  124. this.text = data.Name;
  125. if (data) {
  126. this.setPointList = [];
  127. let setPointList: SPoint[];
  128. if (data.OutLine) {
  129. if (data.OutLine[0] instanceof SPoint) {
  130. // @ts-ignore
  131. this.setPointList = data.OutLine;
  132. } else {
  133. setPointList = data.OutLine.map(i => {
  134. return (new SPoint(i.X, i.Y))
  135. })
  136. this.setPointList = setPointList;
  137. }
  138. }
  139. if (data.Properties.Zorder) {
  140. this.zOrder = data.Properties.Zorder;
  141. }
  142. // 设置线宽
  143. if (data.Properties.LineWidth) {
  144. this.lineWidth = data.Properties.LineWidth;
  145. }
  146. if (data.Properties.StrokeColor) {
  147. this.strokeColor = data.Properties.StrokeColor.includes('#') ? new SColor(data.Properties.StrokeColor) : new SColor(hexify(data.Properties.StrokeColor));
  148. }
  149. if (data.Properties.FillColor) {
  150. this.fillColor = data.Properties.FillColor.includes('#') ? new SColor(data.Properties.FillColor) : new SColor(hexify(data.Properties.FillColor))
  151. }
  152. if (data.Properties.TextPos) {
  153. this.textItem.moveTo(data.Properties.TextPos.X, data.Properties.TextPos.Y);
  154. }
  155. if (data.Properties.color) {
  156. this.color = new SColor(data.Properties.color);
  157. }
  158. if (data.Properties.font) {
  159. this.font = new SFont("sans-serif", data.Properties.font);
  160. }
  161. if (data.Properties && data.Properties.IsActive) {
  162. this.isActive = data.Properties.IsActive;
  163. }
  164. if (data.AttachObjectIds && data.AttachObjectIds.length) {
  165. this.isActive = true;
  166. }
  167. switch (data.Properties.LineDash) {
  168. case "solid":
  169. this.lineStyle = SLineStyle.Solid;
  170. break;
  171. case "dotted":
  172. this.lineStyle = SLineStyle.Dotted;
  173. break;
  174. case "dashed":
  175. this.lineStyle = SLineStyle.Dashed;
  176. break;
  177. default:
  178. this.lineStyle = SLineStyle.Solid;
  179. }
  180. }
  181. // 监听多边形创建完成事件,并动态计算文本位置
  182. this.connect("finishCreated", this, () => {
  183. // 计算文本位置
  184. let x: number = this.getPointList.reduce((pre, cur, index, arr) => {
  185. return pre + (cur.x / arr.length)
  186. }, 0),
  187. y: number = this.getPointList.reduce((pre, cur, index, arr) => {
  188. return pre + (cur.y / arr.length)
  189. }, 0);
  190. this.textItem.moveTo(x, y);
  191. })
  192. }
  193. /**
  194. * 鼠标按下事件
  195. *
  196. * @param event 保存事件参数
  197. * @return boolean
  198. */
  199. onMouseDown(event: SMouseEvent): boolean {
  200. if (event.buttons == 1)
  201. this.$emit("legendItemClick", event);
  202. return super.onMouseDown(event);
  203. } // Function onMouseDown()
  204. toData(): any {
  205. this.data.Pos = { X: this.x, Y: this.y };
  206. this.data.Name = this.name;
  207. this.data.Properties.Zorder = this.zOrder;
  208. this.data.Properties.FillColor = this.fillColor.value;
  209. this.data.Properties.StrokeColor = this.strokeColor.value;
  210. this.data.Properties.LineWidth = this.lineWidth;
  211. switch (this.lineStyle) {
  212. case SLineStyle.Solid:
  213. this.data.Properties.LineDash = "solid";
  214. break;
  215. case SLineStyle.Dotted:
  216. this.data.Properties.LineDash = "dotted";
  217. break;
  218. case SLineStyle.Dashed:
  219. this.data.Properties.LineDash = "dashed";
  220. break;
  221. default:
  222. this.data.Properties.LineDash = "solid";
  223. }
  224. this.data.OutLine = this.getPointList.map(pos => {
  225. return {
  226. X: pos.x,
  227. Y: pos.y
  228. }
  229. });
  230. this.data.Properties.TextPos = {X: this.textItem.x, Y: this.textItem.y};
  231. this.data.Properties.font = this.font.size;
  232. this.data.Properties.color = this.color.value;
  233. this.data.Properties.IsActive = this.isActive;
  234. return this.data;
  235. }
  236. onDraw(painter: SPainter) {
  237. if (this.maskFlag && this.status == SItemStatus.Normal) {
  238. let color = new SColor(this.strokeColor)
  239. color.alpha = color.alpha / 5
  240. let brushcolor = new SColor(this.fillColor)
  241. brushcolor.alpha = brushcolor.alpha / 5
  242. painter.pen.color = color
  243. painter.pen.lineCapStyle = SLineCapStyle.Square
  244. painter.pen.lineWidth = painter.toPx(this.lineWidth)
  245. painter.brush.color = brushcolor
  246. if (this.selected) {
  247. painter.pen.lineWidth = painter.toPx(this.lineWidth * 2);
  248. painter.shadow.shadowBlur = 10;
  249. painter.shadow.shadowColor = new SColor(`#00000033`);
  250. painter.shadow.shadowOffsetX = 5;
  251. painter.shadow.shadowOffsetY = 5;
  252. }
  253. // @ts-ignore
  254. painter.drawPolygon([...this.pointList]);
  255. } else {
  256. super.onDraw(painter);
  257. }
  258. }
  259. } // Class SZoneLegendItem