SZoneLegendItem.ts 8.5 KB

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