SZoneLegendItem.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { SColor, SFont, SPoint, SLineCapStyle } from "@saga-web/draw";
  2. import { STextItem } from '@saga-web/graph/lib';
  3. import { hexify } from "@/components/mapClass/until";
  4. import { SItemStatus, ItemOrder, SPolygonItem } from '@saga-web/big/lib';
  5. /**
  6. * 图例节点Item(区域类型)
  7. *
  8. * * @author 张宇(taohuzy@163.com)
  9. */
  10. export class SZoneLegendItem extends SPolygonItem {
  11. /**
  12. * 构造函数
  13. *
  14. * @param parent 指向父对象
  15. * @param data 图例节点对象数据
  16. */
  17. constructor(parent, data) {
  18. super(parent);
  19. /** text item */
  20. this.textItem = new STextItem(this);
  21. /** 是否显示文字 */
  22. this._showText = true;
  23. /** 是否蒙版遮罩 */
  24. this._maskFlag = false;
  25. this.zOrder = ItemOrder.polygonOrder;
  26. this.data = data;
  27. this.id = data.ID;
  28. this.name = data.Name;
  29. this.text = data.Name;
  30. if (data) {
  31. this.setPointList = [];
  32. let setPointList;
  33. if (data.OutLine) {
  34. if (data.OutLine[0] instanceof SPoint) {
  35. // @ts-ignore
  36. this.setPointList = data.OutLine;
  37. }
  38. else {
  39. setPointList = data.OutLine.map(i => {
  40. return (new SPoint(i.X, i.Y));
  41. });
  42. this.setPointList = setPointList;
  43. }
  44. }
  45. // 设置线宽
  46. if (data.Properties.LineWidth) {
  47. this.lineWidth = data.Properties.LineWidth;
  48. }
  49. if (data.Properties.StrokeColor) {
  50. this.strokeColor = data.Properties.StrokeColor.includes('#') ? new SColor(data.Properties.StrokeColor) : new SColor(hexify(data.Properties.StrokeColor));
  51. }
  52. if (data.Properties.FillColor) {
  53. this.fillColor = data.Properties.FillColor.includes('#') ? new SColor(data.Properties.FillColor) : new SColor(hexify(data.Properties.FillColor));
  54. }
  55. if (data.Properties.TextPos) {
  56. this.textItem.moveTo(data.Properties.TextPos.X, data.Properties.TextPos.Y);
  57. }
  58. if (data.Properties.color) {
  59. this.color = new SColor(data.Properties.color);
  60. }
  61. if (data.Properties.font) {
  62. this.font = new SFont("sans-serif", data.Properties.font);
  63. }
  64. // if( data.Properties.LineDash){
  65. // this.LineDash =this._legend.Properties.LineDash
  66. // }
  67. }
  68. // 监听多边形创建完成事件,并动态计算文本位置
  69. this.connect("finishCreated", this, () => {
  70. // 计算文本位置
  71. let x = this.getPointList.reduce((pre, cur, index, arr) => {
  72. return pre + (cur.x / arr.length);
  73. }, 0), y = this.getPointList.reduce((pre, cur, index, arr) => {
  74. return pre + (cur.y / arr.length);
  75. }, 0);
  76. this.textItem.moveTo(x, y);
  77. });
  78. }
  79. get text() {
  80. return this.textItem.text;
  81. }
  82. set text(v) {
  83. this.textItem.text = v;
  84. this.update();
  85. }
  86. get color() {
  87. return this.textItem.color;
  88. }
  89. set color(v) {
  90. this.textItem.color = v;
  91. }
  92. get font() {
  93. return this.textItem.font;
  94. }
  95. set font(v) {
  96. this.textItem.font = v;
  97. }
  98. get status() {
  99. return this._status;
  100. }
  101. // 编辑当前状态
  102. set status(value) {
  103. this._status = value;
  104. // 如果状态为show则需清栈
  105. if (value == SItemStatus.Normal) {
  106. // 切换显示状态显示文本
  107. this.showText = true;
  108. // 切换显示状态不可移动文本
  109. this.textItem.moveable = false;
  110. if (this.undoStack) {
  111. this.undoStack.clear();
  112. }
  113. }
  114. else if (value == SItemStatus.Edit) {
  115. // 切换编辑状态显示文本
  116. this.showText = true;
  117. // 切换编辑状态可移动文本
  118. this.textItem.moveable = true;
  119. }
  120. else if (value == SItemStatus.Create) {
  121. // 切换创建状态不显示文本
  122. this.showText = false;
  123. // 切换创建状态不可移动文本
  124. this.textItem.moveable = false;
  125. }
  126. this.update();
  127. }
  128. ;
  129. get showText() {
  130. return this._showText;
  131. }
  132. set showText(v) {
  133. if (v === this._showText) {
  134. return;
  135. }
  136. this._showText = v;
  137. if (v) {
  138. this.textItem.show();
  139. }
  140. else {
  141. this.textItem.hide();
  142. }
  143. }
  144. get maskFlag() {
  145. return this._maskFlag;
  146. }
  147. set maskFlag(v) {
  148. if (v === this._maskFlag) {
  149. return;
  150. }
  151. this._maskFlag = v;
  152. this.update();
  153. }
  154. toData() {
  155. this.data.Pos = { X: this.x, Y: this.y };
  156. this.data.Name = this.name;
  157. this.data.Properties.FillColor = this.fillColor.value;
  158. this.data.Properties.StrokeColor = this.strokeColor.value;
  159. this.data.Properties.LineWidth = this.lineWidth;
  160. this.data.OutLine = this.getPointList.map(pos => {
  161. return {
  162. X: pos.x,
  163. Y: pos.y
  164. };
  165. });
  166. this.data.Properties.TextPos = { X: this.textItem.x, Y: this.textItem.y };
  167. this.data.Properties.font = this.font.size;
  168. this.data.Properties.color = this.color.value;
  169. return this.data;
  170. }
  171. onDraw(painter) {
  172. if (this.maskFlag && this.status == SItemStatus.Normal) {
  173. let color = new SColor(this.strokeColor);
  174. color.alpha = 100;
  175. let brushcolor = new SColor(this.fillColor);
  176. brushcolor.alpha = 100;
  177. painter.pen.color = color;
  178. painter.pen.lineCapStyle = SLineCapStyle.Square;
  179. painter.pen.lineWidth = painter.toPx(this._lineWidth);
  180. painter.brush.color = brushcolor;
  181. // @ts-ignore
  182. painter.drawPolygon([...this.pointList]);
  183. }
  184. else {
  185. super.onDraw(painter);
  186. }
  187. }
  188. } // Class SZoneLegendItem