SZoneLegendItem.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.textItem.isTransform = false
  26. this.zOrder = ItemOrder.polygonOrder
  27. this.data = data
  28. this.id = data.ID
  29. this.name = data.Name
  30. this.text = data.Name
  31. if (data) {
  32. this.setPointList = []
  33. let setPointList
  34. if (data.OutLine) {
  35. if (data.OutLine[0] instanceof SPoint) {
  36. // @ts-ignore
  37. this.setPointList = data.OutLine
  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('#')
  51. ? new SColor(data.Properties.StrokeColor)
  52. : new SColor(hexify(data.Properties.StrokeColor))
  53. }
  54. if (data.Properties.FillColor) {
  55. this.fillColor = data.Properties.FillColor.includes('#')
  56. ? new SColor(data.Properties.FillColor)
  57. : new SColor(hexify(data.Properties.FillColor))
  58. }
  59. if (data.Properties.TextPos) {
  60. this.textItem.moveTo(data.Properties.TextPos.X, data.Properties.TextPos.Y)
  61. }
  62. if (data.Properties.color) {
  63. this.color = new SColor(data.Properties.color)
  64. }
  65. if (data.Properties.font) {
  66. this.font = new SFont('sans-serif', data.Properties.font)
  67. }
  68. // if( data.Properties.LineDash){
  69. // this.LineDash =this._legend.Properties.LineDash
  70. // }
  71. }
  72. // 监听多边形创建完成事件,并动态计算文本位置
  73. this.connect('finishCreated', this, () => {
  74. // 计算文本位置
  75. let x = this.getPointList.reduce((pre, cur, index, arr) => {
  76. return pre + cur.x / arr.length
  77. }, 0),
  78. y = this.getPointList.reduce((pre, cur, index, arr) => {
  79. return pre + cur.y / arr.length
  80. }, 0)
  81. this.textItem.moveTo(x, y)
  82. })
  83. }
  84. get text() {
  85. return this.textItem.text
  86. }
  87. set text(v) {
  88. this.textItem.text = v
  89. this.update()
  90. }
  91. get color() {
  92. return this.textItem.color
  93. }
  94. set color(v) {
  95. this.textItem.color = v
  96. }
  97. get font() {
  98. return this.textItem.font
  99. }
  100. set font(v) {
  101. this.textItem.font = v
  102. }
  103. get status() {
  104. return this._status
  105. }
  106. // 编辑当前状态
  107. set status(value) {
  108. this._status = value
  109. // 如果状态为show则需清栈
  110. if (value == SItemStatus.Normal) {
  111. // 切换显示状态显示文本
  112. this.showText = true
  113. // 切换显示状态不可移动文本
  114. this.textItem.moveable = false
  115. if (this.undoStack) {
  116. this.undoStack.clear()
  117. }
  118. } else if (value == SItemStatus.Edit) {
  119. // 切换编辑状态显示文本
  120. this.showText = true
  121. // 切换编辑状态可移动文本
  122. this.textItem.moveable = true
  123. } else if (value == SItemStatus.Create) {
  124. // 切换创建状态不显示文本
  125. this.showText = false
  126. // 切换创建状态不可移动文本
  127. this.textItem.moveable = false
  128. }
  129. this.update()
  130. }
  131. get showText() {
  132. return this._showText
  133. }
  134. set showText(v) {
  135. if (v === this._showText) {
  136. return
  137. }
  138. this._showText = v
  139. if (v) {
  140. this.textItem.show()
  141. } else {
  142. this.textItem.hide()
  143. }
  144. }
  145. get maskFlag() {
  146. return this._maskFlag
  147. }
  148. set maskFlag(v) {
  149. if (v === this._maskFlag) {
  150. return
  151. }
  152. this._maskFlag = v
  153. this.update()
  154. }
  155. toData() {
  156. this.data.Pos = { X: this.x, Y: this.y }
  157. this.data.Name = this.name
  158. this.data.Properties.FillColor = this.fillColor.value
  159. this.data.Properties.StrokeColor = this.strokeColor.value
  160. this.data.Properties.LineWidth = this.lineWidth
  161. this.data.OutLine = this.getPointList.map((pos) => {
  162. return {
  163. X: pos.x,
  164. Y: pos.y,
  165. }
  166. })
  167. this.data.Properties.TextPos = { X: this.textItem.x, Y: this.textItem.y }
  168. this.data.Properties.font = this.font.size
  169. this.data.Properties.color = this.color.value
  170. return this.data
  171. }
  172. onDraw(painter) {
  173. if (this.maskFlag && this.status == SItemStatus.Normal) {
  174. let color = new SColor(this.strokeColor)
  175. color.alpha = color.alpha / 2
  176. let brushcolor = new SColor(this.fillColor)
  177. brushcolor.alpha = brushcolor.alpha / 2
  178. painter.pen.color = color
  179. painter.pen.lineCapStyle = SLineCapStyle.Square
  180. painter.pen.lineWidth = painter.toPx(this._lineWidth)
  181. painter.brush.color = brushcolor
  182. // @ts-ignore
  183. painter.drawPolygon([...this.pointList])
  184. } else {
  185. this.selected = true
  186. super.onDraw(painter)
  187. }
  188. }
  189. } // Class SZoneLegendItem