SIconTextItem.ts 11 KB

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