SBaseTextEdit.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2016-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SPainter, SRect, SColor, SFont, SPoint, SSize } from "@persagy-web/draw";
  27. import { SGraphItem, STextOrigin, SLineStyle, SAnchorItem } from "@persagy-web/graph/";
  28. import { SItemStatus } from "@persagy-web/big";
  29. import { SGraphEdit } from ".."
  30. import { Marker } from "../type/Marker";
  31. import { ItemOrder } from '@persagy-web/big/lib';
  32. import { SMouseEvent } from '@persagy-web/base/lib';
  33. /**
  34. * 文本编辑类
  35. *
  36. * @author 韩耀龙 <han_yao_long@163.com>
  37. */
  38. export class SBaseTextEdit extends SGraphEdit {
  39. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. //属性
  41. /**编辑相关操作的数据 */
  42. data: Marker | null = null;
  43. /** 存储相关的信息数据 */
  44. propertyData: any
  45. /** 起始锚点 */
  46. startItem: SGraphItem | null = null;
  47. /** 结束锚点 */
  48. endItem: SGraphItem | null = null;
  49. /** 编辑状态 */
  50. protected _status: SItemStatus = SItemStatus.Normal;
  51. get status(): SItemStatus {
  52. return this._status;
  53. } //Get status
  54. set status(value: SItemStatus) {
  55. const oldStatus = this._status;
  56. const newStatus = value;
  57. this._status = value;
  58. //状态变更触发事件
  59. this.$emit('StatusChange', oldStatus, newStatus)
  60. this.update();
  61. } //Set status
  62. /** 记录 painter */
  63. private _painter: SPainter | null = null;
  64. /** 文本内容 */
  65. private _text: string = "";
  66. get text(): string {
  67. return this._text;
  68. } //Get text
  69. set text(v: string) {
  70. this._text = v;
  71. this._textArr = this.text.split(/\n/g);
  72. this.drawFormatText();
  73. this.update();
  74. } //Set text
  75. /** 切割后的文本数组 */
  76. private _textArr: string[] = [];
  77. /** 锚点 list */
  78. anchorList: SAnchorItem[] = [];
  79. /** 宽度 */
  80. private _width: number = 64;
  81. get width(): number {
  82. return this._width;
  83. } //Get width
  84. set width(v: number) {
  85. if (v > 0) {
  86. if (v != this._width) {
  87. let w = this._width;
  88. this._width = v;
  89. this.onResize(
  90. new SSize(w, this._height),
  91. new SSize(this._width, this._height)
  92. );
  93. }
  94. }
  95. } //Set width
  96. /** 高度 */
  97. private _height: number = 64;
  98. get height(): number {
  99. return this._height;
  100. } //Get height
  101. set height(v: number) {
  102. if (v > 0) {
  103. if (v != this._height) {
  104. let h = this._height;
  105. this._height = v;
  106. this.onResize(
  107. new SSize(this._width, h),
  108. new SSize(this._width, this._height)
  109. );
  110. }
  111. }
  112. } //Set height
  113. /** 原点 */
  114. origin = new SPoint();
  115. /** 文本颜色 */
  116. private _color: SColor = new SColor("#333333");
  117. get color(): SColor {
  118. return this._color;
  119. } //Get color
  120. set color(v: SColor) {
  121. this._color = v;
  122. this.update();
  123. } //Set color
  124. /** 字体 */
  125. private _font: SFont = new SFont("sans-serif", 12);
  126. get font(): SFont {
  127. return this._font;
  128. } //Get font
  129. set font(v: SFont) {
  130. this._font = v;
  131. this.drawFormatText();
  132. this.update();
  133. } //Set font
  134. /** 背景色 */
  135. private _backgroundColor: SColor = SColor.Transparent;
  136. get backgroundColor(): SColor {
  137. return this._backgroundColor;
  138. } //Get backgroundColor
  139. set backgroundColor(v: SColor) {
  140. this._backgroundColor = v;
  141. this.update();
  142. } //Set backgroundColor
  143. /** 边框色 */
  144. private _strokeColor: SColor = SColor.Transparent;
  145. get strokeColor(): SColor {
  146. return this._strokeColor;
  147. } //Get strokeColor
  148. set strokeColor(v: SColor) {
  149. this._strokeColor = v;
  150. this.update();
  151. } //Set strokeColor
  152. /** 边框宽度 */
  153. private _lineWidth: number = 1;
  154. get lineWidth(): number {
  155. return this._lineWidth;
  156. } //Get lineWidth
  157. set lineWidth(v: number) {
  158. this._lineWidth = v;
  159. this.update();
  160. } //Set lineWidth
  161. /** 边框样式 */
  162. private _borderStyle: SLineStyle = SLineStyle.None;
  163. get borderStyle(): SLineStyle {
  164. return this._borderStyle;
  165. } //Get borderStyle
  166. set borderStyle(v: SLineStyle) {
  167. this._borderStyle = v;
  168. this.update();
  169. } //Set borderStyle
  170. /** 原点位置 */
  171. private _originPosition: STextOrigin = STextOrigin.LeftTop;
  172. get originPosition(): STextOrigin {
  173. return this._originPosition;
  174. } //Get originPosition
  175. set originPosition(v: STextOrigin) {
  176. this._originPosition = v;
  177. this.update();
  178. } //Set originPosition
  179. /** 文本绘制最大宽 */
  180. maxWidth: number | undefined = undefined;
  181. /**
  182. * 构造函数
  183. *
  184. * @param parent 指向父 Item
  185. * @param data 文本数据
  186. */
  187. constructor(parent: SGraphItem | null, data: Marker | null) {
  188. super(parent);
  189. if (data) {
  190. this.initData(data)
  191. }
  192. } // Constructor
  193. /**
  194. * 初始化 当data传入时设置相关状态
  195. *
  196. * @param data Mark 数据
  197. */
  198. initData(data: Marker) {
  199. this.showSelect = false;
  200. this.zOrder = ItemOrder.textOrder;
  201. this.isTransform = false;
  202. this.data = data;
  203. this.name = data.name;
  204. this.moveTo(data.pos.x, data.pos.y);
  205. if (data.size) {
  206. this.width = data.size.width;
  207. this.height = data.size.height;
  208. }
  209. if (data.style && data.style.default) {
  210. // 高度
  211. if (data.style.default.zorder) {
  212. this.zOrder = data.style.default.zorder;
  213. }
  214. // 文本
  215. if (data.style.default.text) {
  216. this.text = data.style.default.text;
  217. }
  218. // 文本颜色
  219. if (data.style.default.strokeColor) {
  220. this.strokeColor = new SColor(data.style.default.strokeColor);
  221. }
  222. // 字体大小
  223. if (data.style.default.font) {
  224. this.font = new SFont("sans-serif", data.style.default.font);
  225. }
  226. // 背景色
  227. if (data.style.default.backgroundColor) {
  228. this.backgroundColor = new SColor(data.style.default.backgroundColor);
  229. }
  230. }
  231. }
  232. /**
  233. * 宽高发发生变化
  234. *
  235. * @param oldSize 改之前的大小
  236. * @param newSize 改之后大小
  237. */
  238. protected onResize(oldSize: SSize, newSize: SSize) { } // Function onResize()
  239. /**
  240. * 对象边界区域
  241. *
  242. * @return 边界区域
  243. */
  244. boundingRect(): SRect {
  245. return new SRect(
  246. -this.origin.x,
  247. -this.origin.y,
  248. this.width,
  249. this.height
  250. );
  251. } // Function boundingRect()
  252. /**
  253. * 移动后处理所有坐标,并肩原点置为场景原点
  254. *
  255. * @param x x 坐标
  256. * @param y y 坐标
  257. */
  258. // moveToOrigin(x: number, y: number): void {
  259. // this.moveTo(this.x + x, this.y + y);
  260. // } // Function moveToOrigin()
  261. /**
  262. * 鼠标按下事件
  263. *
  264. * @param event 保存事件参数
  265. * @return 是否处理事件
  266. */
  267. onMouseDown(event: SMouseEvent): boolean {
  268. this.$emit('textSelect')
  269. super.onMouseDown(event);
  270. return this.moveable;
  271. } // Function onMouseDown()
  272. /**
  273. * 释放鼠标事件
  274. *
  275. * @param event 保存事件参数
  276. * @return 是否处理事件
  277. */
  278. onMouseUp(event: SMouseEvent): boolean {
  279. super.onMouseUp(event);
  280. return this.moveable;
  281. } // Function onMouseUp()
  282. /**
  283. * 绘制显示状态文本 Item
  284. *
  285. * @param painter 绘制类
  286. */
  287. protected drawShowText(painter: SPainter): void {
  288. painter.translate(-this.origin.x, -this.origin.y);
  289. //绘制矩形轮廓
  290. if (this.selected) {
  291. painter.shadow.shadowBlur = 10;
  292. painter.shadow.shadowColor = new SColor(`#00000033`);
  293. painter.shadow.shadowOffsetX = 5;
  294. painter.shadow.shadowOffsetY = 5;
  295. } else {
  296. painter.shadow.shadowColor = SColor.Transparent;
  297. }
  298. painter.brush.color = this.backgroundColor;
  299. painter.pen.lineWidth = this.lineWidth;
  300. painter.pen.color = this.strokeColor;
  301. painter.drawRect(0, 0, this.width, this.height);
  302. //绘制文本
  303. painter.brush.color = new SColor(this.color);
  304. painter.shadow.shadowColor = SColor.Transparent;
  305. painter.font = this.font;
  306. this._textArr.forEach((text: string, index: number) => {
  307. painter.drawText(
  308. text,
  309. this.font.size / 4,
  310. index * (this.font.size * 1.25) + this.font.size / 4,
  311. this.maxWidth
  312. );
  313. });
  314. } // Function drawShowText()
  315. /**
  316. * 根据换行切割文本,绘制多行并计算外轮廓宽高
  317. */
  318. protected drawFormatText(): void {
  319. if (this._painter instanceof SPainter) {
  320. this._painter.save();
  321. this._painter.font = this.font;
  322. let textMaxWidth = 0;
  323. let fontSize: number = this.font.size;
  324. this._textArr.forEach((text: string, index: number) => {
  325. let textWidth: number = this._painter
  326. ? this._painter.textWidth(text) + fontSize / 2
  327. : fontSize / 2;
  328. if (textWidth > textMaxWidth) {
  329. textMaxWidth = textWidth;
  330. }
  331. });
  332. // 在绘制文本后计算文本的宽高
  333. this.width = textMaxWidth;
  334. this.height = fontSize * 1.25 * this._textArr.length + fontSize / 8;
  335. // 设置原点位置(默认左上角)
  336. if (this.originPosition == STextOrigin.Center) {
  337. this.origin = new SPoint(this.width / 2, this.height / 2);
  338. }
  339. this._painter.restore();
  340. }
  341. } // Function drawFormatText()
  342. /**
  343. * 返回对象储存的相关数据
  344. *
  345. * @return 对象储存的相关数据
  346. */
  347. toData(): any {
  348. if (this.data.size) {
  349. this.data.size.width = this.width;
  350. this.data.size.height = this.height;
  351. }
  352. this.data.pos.x = this.pos.x;
  353. this.data.pos.y = this.pos.y;
  354. this.data.style.default.zorder = this.zOrder;
  355. this.data.style.default.text = this.text;
  356. this.data.style.default.strokeColor = this.strokeColor.value;
  357. this.data.style.default.font = this.font.size;
  358. this.data.style.default.backgroundColor = this.backgroundColor.value;
  359. return this.data;
  360. } // Function toData()
  361. /**
  362. * Item 绘制操作
  363. *
  364. * @param painter 绘画类
  365. */
  366. onDraw(painter: SPainter): void {
  367. if (!(this._painter instanceof SPainter)) {
  368. this._painter = painter;
  369. this.drawFormatText();
  370. }
  371. this.drawShowText(painter);
  372. } // Function onDraw()
  373. } // Class SBaseTextEdit