SImgTextItem.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <div>
  3. <el-button @click="changemaodian">切换锚点显示状态</el-button>
  4. <el-button @click="changetext">切换文本显示状态</el-button>
  5. <canvas id="editPolygon" width="740" height="400" tabindex="0"></canvas>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import {
  10. SAnchorItem,
  11. SGraphItem,
  12. SGraphScene,
  13. SGraphView,
  14. SImageItem,
  15. SObjectItem,
  16. STextItem
  17. } from "@persagy-web/graph/lib";
  18. import { SItemStatus } from "@persagy-web/big/lib";
  19. import { SColor, SPainter, SRect, SSize, SPoint, SFont } from "@persagy-web/draw/lib";
  20. import { SMouseEvent } from "@persagy-web/base/lib";
  21. import { Anchor } from "@persagy-web/big/lib/types/topology/Anchor";
  22. /**
  23. * 图例item icon
  24. *
  25. * @author 郝洁 <haojie@persagy.com>
  26. */
  27. class SImgTextItem extends SObjectItem {
  28. /** item 状态 */
  29. _status: SItemStatus = SItemStatus.Normal;
  30. get status(): SItemStatus {
  31. return this._status;
  32. } // Get status
  33. set status(v: SItemStatus) {
  34. this._status = v;
  35. // 标准状态时
  36. if (v == SItemStatus.Normal) {
  37. this.moveable = true;
  38. this.textItem.moveable = false;
  39. this.img.moveable = false;
  40. } else if (v == SItemStatus.Edit) {
  41. // 编辑状态时
  42. this.moveable = false;
  43. this.textItem.moveable = true;
  44. this.img.moveable = true;
  45. } else if (v == SItemStatus.Create) {
  46. // 创建状态时
  47. this.moveable = true;
  48. this.textItem.moveable = false;
  49. this.img.moveable = false;
  50. }
  51. this.update();
  52. }
  53. /** 是否显示文字 */
  54. _showText: boolean = true;
  55. get showText(): boolean {
  56. return this._showText;
  57. }
  58. set showText(v: boolean) {
  59. // 当文字是显示状态时
  60. if (v === this._showText) {
  61. return;
  62. }
  63. this._showText = v;
  64. // 是否显示文字
  65. if (v) {
  66. this.textItem.show();
  67. } else {
  68. this.textItem.hide();
  69. }
  70. }
  71. /** 是否被选中 */
  72. get selected(): boolean {
  73. return this._selected && this.selectable && this.enabled;
  74. }
  75. set selected(value: boolean) {
  76. // 如果选择状态未变更
  77. if (this.selected == value) {
  78. return;
  79. }
  80. this._selected = value;
  81. // 选中时
  82. if (value) {
  83. this.img.scale = 1.25;
  84. } else {
  85. this.img.scale = 1;
  86. }
  87. this.update();
  88. }
  89. /** 是否激活 */
  90. _isActive: boolean = false;
  91. get isActive(): boolean {
  92. return this._isActive;
  93. }
  94. set isActive(v: boolean) {
  95. this._isActive = v;
  96. // 激活状态
  97. if (v) {
  98. this.cursor = "pointer";
  99. this.textItem.cursor = "pointer";
  100. this.img.cursor = "pointer";
  101. } else {
  102. this.cursor = "auto";
  103. this.textItem.cursor = "auto";
  104. this.img.cursor = "auto";
  105. }
  106. this.update();
  107. }
  108. /** 激活显示颜色 */
  109. _activeColor: SColor = new SColor("#00000033");
  110. get activeColor(): SColor {
  111. return this._activeColor;
  112. }
  113. set activeColor(v: SColor) {
  114. this._activeColor = v;
  115. this.update();
  116. }
  117. /** X 轴坐标 */
  118. get x(): number {
  119. return this.pos.x;
  120. }
  121. set x(v: number) {
  122. this.pos.x = v;
  123. this.$emit("changePos");
  124. this.update();
  125. }
  126. /** Y 轴坐标 */
  127. get y(): number {
  128. return this.pos.y;
  129. }
  130. set y(v: number) {
  131. this.pos.y = v;
  132. this.$emit("changePos");
  133. this.update();
  134. }
  135. /** icon宽 */
  136. get sWidth(): number {
  137. return this.img.width;
  138. }
  139. set sWidth(v: number) {
  140. this.img.width = v;
  141. this.img.origin = new SPoint(
  142. this.img.width * 0.5,
  143. this.img.height * 0.5
  144. );
  145. this.changeAnchorPoint();
  146. this.update();
  147. }
  148. /** icon高 */
  149. get sHeight(): number {
  150. return this.img.height;
  151. }
  152. set sHeight(v: number) {
  153. this.img.height = v;
  154. this.img.origin = new SPoint(
  155. this.img.width * 0.5,
  156. this.img.height * 0.5
  157. );
  158. this.changeAnchorPoint();
  159. this.update();
  160. }
  161. /** 是否显示锚点 */
  162. private _showAnchor: boolean = false;
  163. get showAnchor(): boolean {
  164. return this._showAnchor;
  165. }
  166. set showAnchor(v: boolean) {
  167. this._showAnchor = v;
  168. this.anchorList.forEach(t => {
  169. t.visible = v;
  170. });
  171. }
  172. /** 文本内容 */
  173. get text(): string {
  174. return this.textItem.text;
  175. }
  176. set text(v: string) {
  177. this.textItem.text = v;
  178. this.update();
  179. }
  180. /** 文本颜色 */
  181. get color(): SColor {
  182. return this.textItem.color;
  183. }
  184. set color(v: SColor) {
  185. this.textItem.color = v;
  186. this.update();
  187. }
  188. /** 文本字体 */
  189. get font(): SFont {
  190. return this.textItem.font;
  191. }
  192. set font(v: SFont) {
  193. this.textItem.font = v;
  194. this.update();
  195. }
  196. /** img Item */
  197. img: SImageItem = new SImageItem(this, 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2134829550,3120755721&fm=26&gp=0.jpg');
  198. /** text Item */
  199. textItem: STextItem = new STextItem(this);
  200. /**
  201. * 构造体
  202. *
  203. * @param parent 父类
  204. * @param data 锚点数据
  205. */
  206. constructor(parent: SGraphItem | null, data?: Anchor[]) {
  207. super(parent);
  208. this.img.width = 32;
  209. this.img.height = 32;
  210. this.img.origin = new SPoint(
  211. this.img.width * 0.5,
  212. this.img.height * 0.5
  213. );
  214. this.img.connect("onMove", this, this.changeAnchorPoint.bind(this));
  215. let anchorPoint;
  216. // 锚点数据存在时
  217. if (data && data.length) {
  218. anchorPoint = data.map(t => {
  219. return {
  220. x: t.pos.x,
  221. y: t.pos.y,
  222. id: t.id
  223. };
  224. });
  225. } else {
  226. anchorPoint = [
  227. { x: this.img.x, y: this.img.y, id: "" },
  228. { x: this.img.x, y: this.img.y, id: "" },
  229. { x: this.img.x, y: this.img.y, id: "" },
  230. { x: this.img.x, y: this.img.y, id: "" }
  231. // { x: this.img.x, y: this.img.y + this.img.height / 2, id: "" },
  232. // { x: this.img.x, y: this.img.y - this.img.height / 2, id: "" },
  233. // { x: this.img.x - this.img.width / 2, y: this.img.y, id: "" },
  234. // { x: this.img.x + this.img.width / 2, y: this.img.y, id: "" }
  235. ];
  236. }
  237. this.anchorList = anchorPoint.map(t => {
  238. let item = new SAnchorItem(this);
  239. if (t.id) {
  240. item.id = t.id;
  241. }
  242. item.moveTo(t.x, t.y);
  243. return item;
  244. });
  245. this.showAnchor = false;
  246. this.textItem.text = "测试文本";
  247. this.textItem.font.size = 12;
  248. // 偏移二分之一文本高度
  249. this.textItem.moveTo(
  250. this.img.width * 0.5,
  251. -(this.font.size * 1.25 * 0.5)
  252. );
  253. this.showSelect = false;
  254. this.moveable = true;
  255. this.selectable = true;
  256. }
  257. /**
  258. * 计算并移动锚点的位置
  259. */
  260. private changeAnchorPoint(): void {
  261. // 判断是否有锚点
  262. if (this.anchorList.length) {
  263. let anchorPoint = [
  264. { x: this.img.x, y: this.img.y },
  265. { x: this.img.x, y: this.img.y },
  266. { x: this.img.x, y: this.img.y },
  267. { x: this.img.x, y: this.img.y }
  268. // { x: this.img.x, y: this.img.y + this.img.height / 2 },
  269. // { x: this.img.x, y: this.img.y - this.img.height / 2 },
  270. // { x: this.img.x - this.img.width / 2, y: this.img.y },
  271. // { x: this.img.x + this.img.width / 2, y: this.img.y }
  272. ];
  273. this.anchorList.forEach((item, index) => {
  274. item.moveTo(anchorPoint[index].x, anchorPoint[index].y);
  275. });
  276. }
  277. }
  278. /**
  279. * 鼠标按下事件
  280. *
  281. * @param event 事件对象
  282. * @return 是否处理事件
  283. */
  284. onMouseDown(event: SMouseEvent): boolean {
  285. console.log(123123123)
  286. // console.log(super.onMouseDown(event));
  287. // 如果为show状态 双击改对象则需改为编辑状态
  288. if (this.status == SItemStatus.Normal) {
  289. return super.onMouseDown(event);
  290. } else if (this.status == SItemStatus.Edit) {
  291. // 编辑状态时
  292. return super.onMouseDown(event);
  293. }
  294. console.log('----------------')
  295. return true;
  296. }
  297. /**
  298. * 宽高发发生变化
  299. *
  300. * @param oldSize 改之前的大小
  301. * @param newSize 改之后大小
  302. */
  303. onResize(oldSize: SSize, newSize: SSize): void {
  304. console.log(arguments);
  305. }
  306. /**
  307. * 鼠标双击事件
  308. *
  309. * @param event 鼠标事件
  310. * @return 是否处理事件
  311. */
  312. onDoubleClick(event: SMouseEvent): boolean {
  313. // 如果为show状态 双击改对象则需改为编辑状态,处于编辑状态时
  314. if (SItemStatus.Normal == this.status) {
  315. this.status = SItemStatus.Edit;
  316. this.grabItem(this);
  317. } else if (SItemStatus.Edit == this.status) {
  318. // 处于编辑状态时
  319. this.status = SItemStatus.Normal;
  320. this.releaseItem();
  321. }
  322. this.update();
  323. return true;
  324. }
  325. /**
  326. * Item 对象边界区域
  327. *
  328. * @return 边界矩阵
  329. */
  330. boundingRect(): SRect {
  331. let rect = this.img
  332. .boundingRect()
  333. .adjusted(this.img.pos.x, this.img.pos.y, 0, 0);
  334. // 显示文字时
  335. if (this.showText) {
  336. rect = rect.unioned(
  337. this.textItem
  338. .boundingRect()
  339. .adjusted(this.textItem.pos.x, this.textItem.pos.y, 0, 0)
  340. );
  341. }
  342. return rect.adjusted(-5, -5, 10, 10);
  343. }
  344. /**
  345. * Item 绘制操作
  346. *
  347. * @param painter 绘制对象
  348. */
  349. onDraw(painter: SPainter): void {
  350. // 编辑状态时
  351. if (this.status == SItemStatus.Edit) {
  352. painter.pen.lineWidth = painter.toPx(1);
  353. painter.pen.lineDash = [painter.toPx(3), painter.toPx(7)];
  354. painter.pen.color = SColor.Black;
  355. painter.brush.color = SColor.Transparent;
  356. painter.drawRect(this.boundingRect());
  357. }
  358. // 处于激活状态时
  359. if (this.isActive) {
  360. painter.pen.color = SColor.Transparent;
  361. painter.brush.color = this.activeColor;
  362. // 是否选中
  363. if (this.selected) {
  364. painter.shadow.shadowBlur = 10;
  365. painter.shadow.shadowColor = this.activeColor;
  366. painter.shadow.shadowOffsetX = 5;
  367. painter.shadow.shadowOffsetY = 5;
  368. painter.drawCircle(
  369. this.img.x,
  370. this.img.y,
  371. (this.sWidth / 2.0 + 3) * 1.25
  372. );
  373. } else {
  374. painter.drawCircle(
  375. this.img.x,
  376. this.img.y,
  377. this.sWidth / 2.0 + 3
  378. );
  379. }
  380. } else {
  381. // 是否选中
  382. if (this.selected) {
  383. painter.pen.color = SColor.Transparent;
  384. painter.brush.color = SColor.Transparent;
  385. painter.shadow.shadowBlur = 10;
  386. painter.shadow.shadowColor = new SColor(`#00000033`);
  387. painter.shadow.shadowOffsetX = 5;
  388. painter.shadow.shadowOffsetY = 5;
  389. painter.drawCircle(this.img.x, this.img.y, this.sWidth / 2.0);
  390. }
  391. }
  392. }
  393. }
  394. export default {
  395. name: "ImgTextItem",
  396. data() {
  397. return {
  398. /** 命令所属的场景类 */
  399. scene: null,
  400. /** 实例化 view */
  401. view: null,
  402. input: '',
  403. };
  404. },
  405. /**
  406. * 页面挂载
  407. */
  408. mounted() {
  409. // @ts-ignore
  410. this.view = new SGraphView("editPolygon");
  411. // @ts-ignore
  412. this.scene = new SGraphScene();
  413. // @ts-ignore
  414. this.view.scene = this.scene;
  415. // @ts-ignore
  416. this.init()
  417. },
  418. methods: {
  419. /**
  420. * 初始化加载
  421. */
  422. init() {
  423. // @ts-ignore
  424. this.item = new SImgTextItem(null);
  425. // @ts-ignore
  426. this.item.moveable = true;
  427. // @ts-ignore
  428. this.scene.addItem(this.item);
  429. // this.view.fitSceneToView();
  430. },
  431. changemaodian() {
  432. // @ts-ignore
  433. this.item.showAnchor = !this.item.showAnchor;
  434. },
  435. changetext() {
  436. // @ts-ignore
  437. this.item.showText = !this.item.showText;
  438. }
  439. }
  440. }
  441. </script>