SIconTextItem.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import {
  2. SObjectItem,
  3. SImageItem,
  4. STextItem,
  5. SAnchorItem,
  6. SGraphItem
  7. } from "@saga-web/graph/lib";
  8. import { SItemStatus } from "..";
  9. import { SMouseEvent } from "@saga-web/base";
  10. import { SSize, SRect, SPainter, SColor, SFont } from "@saga-web/draw";
  11. /**
  12. * 图例item icon
  13. *
  14. * */
  15. export class SIconTextItem extends SObjectItem {
  16. /** item状态 */
  17. _status: SItemStatus = SItemStatus.Normal;
  18. get status(): SItemStatus {
  19. return this._status;
  20. }
  21. set status(v: SItemStatus) {
  22. this._status = v;
  23. if (v == SItemStatus.Normal) {
  24. this.moveable = true;
  25. this.textItem.moveable = false;
  26. this.img.moveable = false;
  27. } else if (v == SItemStatus.Edit) {
  28. this.moveable = false;
  29. this.textItem.moveable = true;
  30. this.img.moveable = true;
  31. } else if (v == SItemStatus.Create) {
  32. this.moveable = true;
  33. this.textItem.moveable = false;
  34. this.img.moveable = false;
  35. }
  36. this.update();
  37. }
  38. /** 是否显示文字 */
  39. _showText: boolean = true;
  40. get showText(): boolean {
  41. return this._showText;
  42. }
  43. set showText(v: boolean) {
  44. if (v === this._showText) {
  45. return
  46. }
  47. this._showText = v;
  48. if (v) {
  49. this.textItem.show();
  50. } else {
  51. this.textItem.hide();
  52. }
  53. }
  54. /** X轴坐标 */
  55. get x(): number {
  56. return this.pos.x;
  57. } // Get x
  58. set x(v: number) {
  59. this.pos.x = v;
  60. this.$emit("changePos");
  61. this.update();
  62. } // Set x
  63. /** Y轴坐标 */
  64. get y(): number {
  65. return this.pos.y;
  66. } // Get y
  67. set y(v: number) {
  68. this.pos.y = v;
  69. this.$emit("changePos");
  70. this.update();
  71. } // Set y
  72. /** icon宽 */
  73. get sWidth(): number {
  74. return this.img.width;
  75. }
  76. set sWidth(v: number) {
  77. this.img.width = v
  78. this.update();
  79. }
  80. /** icon宽 */
  81. get sHeight(): number {
  82. return this.img.height;
  83. }
  84. set sHeight(v: number) {
  85. this.img.height = v
  86. this.update();
  87. }
  88. /** 是否显示锚点 */
  89. _showAnchor: boolean = false;
  90. get showAnchor(): boolean {
  91. return this._showAnchor;
  92. }
  93. set showAnchor(v: boolean) {
  94. this._showAnchor = v;
  95. this.anchorList.forEach(t => {
  96. t.visible = v;
  97. })
  98. }
  99. get text(): string {
  100. return this.textItem.text;
  101. }
  102. set text(v: string) {
  103. this.textItem.text = v;
  104. this.update();
  105. }
  106. get color(): string {
  107. return this.textItem.color;
  108. }
  109. set color(v: string) {
  110. this.textItem.color = v;
  111. this.update();
  112. }
  113. get font(): SFont {
  114. return this.textItem.font;
  115. }
  116. set font(v: SFont) {
  117. this.textItem.font = v;
  118. this.update();
  119. }
  120. /** img Item */
  121. img: SImageItem = new SImageItem(this);
  122. /** text item */
  123. textItem: STextItem = new STextItem(this);
  124. /**
  125. * 构造体
  126. *
  127. * */
  128. constructor(parent: SGraphItem | null) {
  129. super(parent);
  130. this.img.url = `http://adm.sagacloud.cn:8080/doc/assets/img/logo.png`;
  131. this.img.width = 32;
  132. this.img.height = 32;
  133. let anchorPoint = [{ x: 0, y: this.img.height / 2 }, { x: 0, y: -this.img.height / 2 }, { x: -this.img.width / 2, y: 0 }, { x: this.img.width / 2, y: 0 }];
  134. this.anchorList = anchorPoint.map(t => {
  135. let item = new SAnchorItem(this);
  136. item.moveTo(t.x, t.y);
  137. return item;
  138. });
  139. this.update();
  140. this.textItem.text = "";
  141. this.textItem.backgroundColor = SColor.White;
  142. this.textItem.font.size = 24;
  143. this.textItem.moveTo(26, -6);
  144. this.moveable = true;
  145. this.selectable = true;
  146. this.isTransform = false;
  147. // this.textItem.enabled = false;
  148. // this.img.enabled = false;
  149. }
  150. /**
  151. * 鼠标按下事件
  152. *
  153. * */
  154. onMouseDown(event: SMouseEvent): boolean {
  155. console.log(this)
  156. if (this.status == SItemStatus.Normal) {
  157. return super.onMouseDown(event);
  158. } else if (this.status == SItemStatus.Edit) {
  159. return super.onMouseDown(event);
  160. }
  161. return true;
  162. } // Function onMouseDown()
  163. /**
  164. * 宽高发发生变化
  165. *
  166. * @param oldSize 改之前的大小
  167. * @param newSize 改之后大小
  168. * */
  169. onResize(oldSize: SSize, newSize: SSize) {
  170. console.log(arguments);
  171. } // Function onResize()
  172. /**
  173. * 鼠标双击事件
  174. *
  175. * @param event 鼠标事件
  176. * @return 是否处理事件
  177. * */
  178. onDoubleClick(event: SMouseEvent): boolean {
  179. // 如果位show状态 双击改对象则需改为编辑状态
  180. if (SItemStatus.Normal == this.status) {
  181. this.status = SItemStatus.Edit;
  182. this.grabItem(this);
  183. } else if (SItemStatus.Edit == this.status) {
  184. this.status = SItemStatus.Normal;
  185. this.releaseItem();
  186. }
  187. this.update();
  188. return true;
  189. } // Function onDoubleClick()
  190. /**
  191. * 宽高发生变化
  192. *
  193. * @return SRect 所有子对象的并集
  194. * */
  195. boundingRect(): SRect {
  196. let rect = this.img.boundingRect().adjusted(this.img.pos.x, this.img.pos.y, 0, 0);
  197. if (this.showText) {
  198. rect = rect.unioned(this.textItem.boundingRect().adjusted(this.textItem.pos.x, this.textItem.pos.y, 0, 0));
  199. }
  200. return rect.adjusted(-5, -5, 10, 10);
  201. } // Function boundingRect()
  202. /**
  203. * Item绘制操作
  204. *
  205. * @param painter painter对象
  206. */
  207. onDraw(painter: SPainter): void {
  208. if (this.status == SItemStatus.Normal) {
  209. if (this.selected) {
  210. painter.pen.lineWidth = painter.toPx(1);
  211. painter.pen.lineDash = [
  212. painter.toPx(3),
  213. painter.toPx(7)
  214. ];
  215. painter.pen.color = new SColor("#000000");
  216. painter.brush.color = SColor.Transparent;
  217. painter.drawRect(this.boundingRect());
  218. }
  219. } else if (this.status == SItemStatus.Edit) {
  220. painter.pen.lineWidth = painter.toPx(1);
  221. painter.pen.lineDash = [
  222. painter.toPx(3),
  223. painter.toPx(7)
  224. ];
  225. painter.pen.color = SColor.Red;
  226. painter.brush.color = SColor.Transparent;
  227. painter.drawRect(this.boundingRect());
  228. }
  229. } // Function onDraw()
  230. }