SIconTextItem.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 } 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. this.update();
  24. }
  25. /** 是否显示文字 */
  26. _showText: boolean = true;
  27. get showText(): boolean {
  28. return this._showText;
  29. }
  30. set showText(v: boolean) {
  31. if (v === this._showText) {
  32. return;
  33. }
  34. this._showText = v;
  35. if (v) {
  36. this.textItem.show();
  37. } else {
  38. this.textItem.hide();
  39. }
  40. }
  41. /** X轴坐标 */
  42. get x(): number {
  43. return this.pos.x;
  44. } // Get x
  45. set x(v: number) {
  46. this.pos.x = v;
  47. this.$emit("changePos");
  48. this.update();
  49. } // Set x
  50. /** Y轴坐标 */
  51. get y(): number {
  52. return this.pos.y;
  53. } // Get y
  54. set y(v: number) {
  55. this.pos.y = v;
  56. this.$emit("changePos");
  57. this.update();
  58. } // Set y
  59. /** 是否显示锚点 */
  60. _showAnchor: boolean = false;
  61. get showAnchor(): boolean {
  62. return this._showAnchor;
  63. }
  64. set showAnchor(v: boolean) {
  65. this._showAnchor = v;
  66. this.anchorList.forEach(t => {
  67. t.visible = v;
  68. });
  69. }
  70. get text(): string {
  71. return this.textItem.text;
  72. }
  73. set text(v: string) {
  74. this.textItem.text = v;
  75. this.update();
  76. }
  77. /** img Item */
  78. img: SImageItem = new SImageItem(this);
  79. /** text item */
  80. textItem: STextItem = new STextItem(this);
  81. /**
  82. * 构造体
  83. *
  84. * */
  85. constructor(parent: SGraphItem | null) {
  86. super(parent);
  87. this.img.url = `http://adm.sagacloud.cn:8080/doc/assets/img/logo.png`;
  88. this.img.width = 32;
  89. this.img.height = 32;
  90. let anchorPoint = [
  91. { x: 0, y: this.img.height / 2 },
  92. { x: 0, y: -this.img.height / 2 },
  93. { x: -this.img.width / 2, y: 0 },
  94. { x: this.img.width / 2, y: 0 }
  95. ];
  96. this.anchorList = anchorPoint.map(t => {
  97. let item = new SAnchorItem(this);
  98. item.moveTo(t.x, t.y);
  99. return item;
  100. });
  101. this.update();
  102. this.textItem.text = "请填写文本";
  103. this.textItem.moveTo(16, -6);
  104. this.moveable = true;
  105. this.selectable = true;
  106. this.isTransform = false;
  107. this.textItem.enabled = false;
  108. this.img.enabled = false;
  109. }
  110. /**
  111. * 鼠标按下事件
  112. *
  113. * */
  114. onMouseDown(event: SMouseEvent): boolean {
  115. console.log(this);
  116. if (this.status == SItemStatus.Normal) {
  117. return super.onMouseDown(event);
  118. } else if (this.status == SItemStatus.Edit) {
  119. return super.onMouseDown(event);
  120. }
  121. return true;
  122. } // Function onMouseDown()
  123. /**
  124. * 宽高发发生变化
  125. *
  126. * @param oldSize 改之前的大小
  127. * @param newSize 改之后大小
  128. * */
  129. onResize(oldSize: SSize, newSize: SSize) {
  130. console.log(arguments);
  131. } // Function onResize()
  132. /**
  133. * 鼠标双击事件
  134. *
  135. * @param event 鼠标事件
  136. * @return 是否处理事件
  137. * */
  138. onDoubleClick(event: SMouseEvent): boolean {
  139. this.status = SItemStatus.Edit;
  140. return true;
  141. } // Function onDoubleClick()
  142. /**
  143. * 宽高发生变化
  144. *
  145. * @return SRect 所有子对象的并集
  146. * */
  147. boundingRect(): SRect {
  148. let rect = this.img
  149. .boundingRect()
  150. .adjusted(this.img.pos.x, this.img.pos.y, 0, 0);
  151. if (this.showText) {
  152. rect = rect.unioned(
  153. this.textItem
  154. .boundingRect()
  155. .adjusted(this.textItem.pos.x, this.textItem.pos.y, 0, 0)
  156. );
  157. }
  158. return rect;
  159. } // Function boundingRect()
  160. /**
  161. * Item绘制操作
  162. *
  163. * @param painter painter对象
  164. */
  165. onDraw(painter: SPainter): void {
  166. painter.pen.lineWidth = painter.toPx(1);
  167. painter.pen.color = new SColor("#00FF00");
  168. painter.brush.color = SColor.Transparent;
  169. painter.drawRect(this.boundingRect());
  170. } // Function onDraw()
  171. }