SIconTextItem.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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, SPoint } from "@saga-web/draw";
  11. import { Anchor } from "../types/topology/Anchor";
  12. /**
  13. * 图例item icon
  14. *
  15. * */
  16. export class SIconTextItem extends SObjectItem {
  17. /** item状态 */
  18. _status: SItemStatus = SItemStatus.Normal;
  19. get status(): SItemStatus {
  20. return this._status;
  21. }
  22. set status(v: SItemStatus) {
  23. this._status = v;
  24. if (v == SItemStatus.Normal) {
  25. this.moveable = true;
  26. this.textItem.moveable = false;
  27. this.img.moveable = false;
  28. } else if (v == SItemStatus.Edit) {
  29. this.moveable = false;
  30. this.textItem.moveable = true;
  31. this.img.moveable = true;
  32. } else if (v == SItemStatus.Create) {
  33. this.moveable = true;
  34. this.textItem.moveable = false;
  35. this.img.moveable = false;
  36. }
  37. this.update();
  38. }
  39. /** 是否显示文字 */
  40. _showText: boolean = true;
  41. get showText(): boolean {
  42. return this._showText;
  43. }
  44. set showText(v: boolean) {
  45. if (v === this._showText) {
  46. return;
  47. }
  48. this._showText = v;
  49. if (v) {
  50. this.textItem.show();
  51. } else {
  52. this.textItem.hide();
  53. }
  54. }
  55. /** 是否被选中 */
  56. get selected(): boolean {
  57. return this._selected && this.selectable && this.enabled;
  58. } // Get selected
  59. set selected(value: boolean) {
  60. // 如果选择状态未变更
  61. if (this.selected == value) {
  62. return;
  63. }
  64. this._selected = value;
  65. if (value) {
  66. this.img.scale = 1.25;
  67. } else {
  68. this.img.scale = 1
  69. }
  70. this.update();
  71. } // Set selected
  72. /** 是否激活 */
  73. _isActive: boolean = false;
  74. get isActive(): boolean {
  75. return this._isActive;
  76. } // Get isActive
  77. set isActive(v: boolean) {
  78. this._isActive = v;
  79. this.update();
  80. } // Set isActive
  81. /** 激活显示颜色 */
  82. _activeColor: SColor = new SColor("#00000033");
  83. get activeColor(): SColor {
  84. return this._activeColor;
  85. } // Get activeColor
  86. set activeColor(v: SColor) {
  87. this._activeColor = v;
  88. this.update();
  89. } // Set activeColor
  90. /** X轴坐标 */
  91. get x(): number {
  92. return this.pos.x;
  93. } // Get x
  94. set x(v: number) {
  95. this.pos.x = v;
  96. this.$emit("changePos");
  97. this.update();
  98. } // Set x
  99. /** Y轴坐标 */
  100. get y(): number {
  101. return this.pos.y;
  102. } // Get y
  103. set y(v: number) {
  104. this.pos.y = v;
  105. this.$emit("changePos");
  106. this.update();
  107. } // Set y
  108. /** icon宽 */
  109. get sWidth(): number {
  110. return this.img.width;
  111. }
  112. set sWidth(v: number) {
  113. this.img.width = v;
  114. this.img.origin = new SPoint(this.img.width * 0.5, this.img.height * 0.5);
  115. this.changeAhchorPoint();
  116. this.update();
  117. }
  118. /** icon宽 */
  119. get sHeight(): number {
  120. return this.img.height;
  121. }
  122. set sHeight(v: number) {
  123. this.img.height = v;
  124. this.img.origin = new SPoint(this.img.width * 0.5, this.img.height * 0.5);
  125. this.changeAhchorPoint();
  126. this.update();
  127. }
  128. /** 是否显示锚点 */
  129. private _showAnchor: boolean = false;
  130. get showAnchor(): boolean {
  131. return this._showAnchor;
  132. }
  133. set showAnchor(v: boolean) {
  134. this._showAnchor = v;
  135. this.anchorList.forEach(t => {
  136. t.visible = v;
  137. });
  138. }
  139. get text(): string {
  140. return this.textItem.text;
  141. }
  142. set text(v: string) {
  143. this.textItem.text = v;
  144. this.update();
  145. }
  146. get color(): SColor {
  147. return this.textItem.color;
  148. }
  149. set color(v: SColor) {
  150. this.textItem.color = v;
  151. this.update();
  152. }
  153. get font(): SFont {
  154. return this.textItem.font;
  155. }
  156. set font(v: SFont) {
  157. this.textItem.font = v;
  158. this.update();
  159. }
  160. /** img Item */
  161. img: SImageItem = new SImageItem(this);
  162. /** text item */
  163. textItem: STextItem = new STextItem(this);
  164. /**
  165. * 构造体
  166. *
  167. * */
  168. constructor(parent: SGraphItem | null, data?: Anchor[]) {
  169. super(parent);
  170. this.img.url = `http://adm.sagacloud.cn:8080/doc/assets/img/logo.png`;
  171. this.img.width = 32;
  172. this.img.height = 32;
  173. this.img.origin = new SPoint(this.img.width * 0.5, this.img.height * 0.5);
  174. this.img.connect("onMove", this, this.changeAhchorPoint.bind(this));
  175. let anchorPoint;
  176. if (data && data.length) {
  177. anchorPoint = data.map(t => {
  178. return {
  179. x: t.Pos.X,
  180. y: t.Pos.Y,
  181. id: t.ID
  182. };
  183. });
  184. } else {
  185. anchorPoint = [
  186. { x: this.img.x, y: this.img.y + this.img.height / 2, id: "" },
  187. { x: this.img.x, y: this.img.y - this.img.height / 2, id: "" },
  188. { x: this.img.x - this.img.width / 2, y: this.img.y, id: "" },
  189. { x: this.img.x + this.img.width / 2, y: this.img.y, id: "" }
  190. ];
  191. }
  192. this.anchorList = anchorPoint.map(t => {
  193. let item = new SAnchorItem(this);
  194. if (t.id) {
  195. item.id = t.id;
  196. }
  197. item.moveTo(t.x, t.y);
  198. return item;
  199. });
  200. this.showAnchor = false;
  201. this.textItem.text = "";
  202. this.textItem.font.size = 12;
  203. // 偏移二分之一文本高度
  204. this.textItem.moveTo((this.img.width * 0.5) + 2, -(this.textItem.height * 0.5));
  205. this.moveable = true;
  206. this.selectable = true;
  207. }
  208. /**
  209. * 计算并移动锚点的位置
  210. *
  211. * */
  212. private changeAhchorPoint(): void {
  213. // 判断是否有锚点
  214. if (this.anchorList.length) {
  215. let anchorPoint = [
  216. { x: this.img.x, y: this.img.y + this.img.height / 2 },
  217. { x: this.img.x, y: this.img.y - this.img.height / 2 },
  218. { x: this.img.x - this.img.width / 2, y: this.img.y },
  219. { x: this.img.x + this.img.width / 2, y: this.img.y }
  220. ];
  221. this.anchorList.forEach((item, index) => {
  222. item.moveTo(anchorPoint[index].x, anchorPoint[index].y);
  223. });
  224. }
  225. } // Function changeAhchorPoint()
  226. /**
  227. * 鼠标按下事件
  228. *
  229. * */
  230. onMouseDown(event: SMouseEvent): boolean {
  231. if (this.status == SItemStatus.Normal) {
  232. return super.onMouseDown(event);
  233. } else if (this.status == SItemStatus.Edit) {
  234. return super.onMouseDown(event);
  235. }
  236. return true;
  237. } // Function onMouseDown()
  238. /**
  239. * 宽高发发生变化
  240. *
  241. * @param oldSize 改之前的大小
  242. * @param newSize 改之后大小
  243. * */
  244. onResize(oldSize: SSize, newSize: SSize) {
  245. console.log(arguments);
  246. } // Function onResize()
  247. /**
  248. * 鼠标双击事件
  249. *
  250. * @param event 鼠标事件
  251. * @return 是否处理事件
  252. * */
  253. onDoubleClick(event: SMouseEvent): boolean {
  254. // 如果位show状态 双击改对象则需改为编辑状态
  255. if (SItemStatus.Normal == this.status) {
  256. this.status = SItemStatus.Edit;
  257. this.grabItem(this);
  258. } else if (SItemStatus.Edit == this.status) {
  259. this.status = SItemStatus.Normal;
  260. this.releaseItem();
  261. }
  262. this.update();
  263. return true;
  264. } // Function onDoubleClick()
  265. /**
  266. * 宽高发生变化
  267. *
  268. * @return SRect 所有子对象的并集
  269. * */
  270. boundingRect(): SRect {
  271. let rect = this.img
  272. .boundingRect()
  273. .adjusted(this.img.pos.x, this.img.pos.y, 0, 0);
  274. if (this.showText) {
  275. rect = rect.unioned(
  276. this.textItem
  277. .boundingRect()
  278. .adjusted(this.textItem.pos.x, this.textItem.pos.y, 0, 0)
  279. );
  280. }
  281. return rect.adjusted(-5, -5, 10, 10);
  282. } // Function boundingRect()
  283. /**
  284. * Item绘制操作
  285. *
  286. * @param painter painter对象
  287. */
  288. onDraw(painter: SPainter): void {
  289. if (this.status == SItemStatus.Edit) {
  290. painter.pen.lineWidth = painter.toPx(1);
  291. painter.pen.lineDash = [painter.toPx(3), painter.toPx(7)];
  292. painter.pen.color = SColor.Black;
  293. painter.brush.color = SColor.Transparent;
  294. painter.drawRect(this.boundingRect());
  295. }
  296. if (this.isActive) {
  297. painter.pen.color = SColor.Transparent;
  298. painter.brush.color = this.activeColor;
  299. if (this.selected) {
  300. painter.shadow.shadowBlur = 10;
  301. painter.shadow.shadowColor = this.activeColor;
  302. painter.shadow.shadowOffsetX = 5;
  303. painter.shadow.shadowOffsetY = 5;
  304. painter.drawCircle(this.img.x, this.img.y, (this.sWidth / 2.0 + 3) * 1.25);
  305. } else {
  306. painter.drawCircle(this.img.x, this.img.y, this.sWidth / 2.0 + 3);
  307. }
  308. } else {
  309. if (this.selected) {
  310. painter.pen.color = SColor.Transparent;
  311. painter.brush.color = SColor.Transparent;
  312. painter.shadow.shadowBlur = 10;
  313. painter.shadow.shadowColor = new SColor(`#00000033`);
  314. painter.shadow.shadowOffsetX = 5;
  315. painter.shadow.shadowOffsetY = 5;
  316. painter.drawCircle(this.img.x, this.img.y, this.sWidth / 2.0);
  317. }
  318. }
  319. } // Function onDraw()
  320. }