index.ts 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. import { ModuleControl } from "queenjs";
  2. import { reactive } from "vue";
  3. import { EditorModule } from "../../module";
  4. import { DesignComp } from "../../objects/DesignTemp/DesignComp";
  5. import { Matrix } from "./matrix";
  6. import { GroupActionCtrl } from "../TransferCtrl/GroupCtrl";
  7. import { Point } from "./objects/point";
  8. import { ObjsContainer } from "./ObjsContainer";
  9. import Event from "./event";
  10. import { DisplayObject } from "./objects/displayObject";
  11. import { CompObject } from "./compObj";
  12. import { each, eachRight } from "lodash";
  13. import { string } from "vue-types";
  14. import { Project, VectorLenth } from "./objects/mathUtils";
  15. /**
  16. * 页面画布空间进行选择
  17. */
  18. const MODE_SEL_RECT = 1;
  19. const MODE_MOVING = 2;
  20. const MODE_ROTATE = 3;
  21. const MODE_SCALE = 4;
  22. const MODE_NONE = 0;
  23. export class SelectCtrl extends ModuleControl<EditorModule> {
  24. transEvent = {
  25. startX: 0,
  26. startY: 0,
  27. offsetX: 0,
  28. offsetY: 0,
  29. width: 0,
  30. height: 0,
  31. };
  32. transferStyle = reactive({
  33. showGizmo: false,
  34. width: 0,
  35. height: 0,
  36. matrix: "matrix(1,0,0,1,0,0)",
  37. matrixInvert: "matrix(1,0,0,1,0,0)",
  38. });
  39. selected: any[] = []; //选中的所有组件ids
  40. mouseDownSelects: any[] = []; //鼠标按下时选中的
  41. pageEl?: HTMLElement;
  42. selCanvas = {} as HTMLCanvasElement;
  43. _downed = false;
  44. _selCtx = {} as CanvasRenderingContext2D;
  45. _state = MODE_SEL_RECT;
  46. _selDownX = 0;
  47. _selDownY = 0;
  48. _selBox = {} as DOMRect;
  49. _selCanvaseSize = { w: 0, h: 0 };
  50. _downClientX = 0;
  51. _downClientY = 0;
  52. //groupCtrl = new GroupActionCtrl(this.module);
  53. bus = new Event();
  54. viewport?: HTMLElement;
  55. initEvents(
  56. pageEl: HTMLElement,
  57. selCanvas: HTMLCanvasElement,
  58. viewport: HTMLElement
  59. ) {
  60. this.viewport = viewport;
  61. this.pageEl = pageEl;
  62. this.selCanvas = selCanvas;
  63. const b = selCanvas.getBoundingClientRect();
  64. selCanvas.width = b.width * 2;
  65. selCanvas.height = b.height * 2;
  66. this._selCtx = selCanvas.getContext("2d") as CanvasRenderingContext2D;
  67. this._selCanvaseSize.w = selCanvas.width * 2;
  68. this._selCanvaseSize.h = selCanvas.height * 2;
  69. document.addEventListener("mousedown", this.onDocMouseDown.bind(this), {capture: true});
  70. document.addEventListener("mousemove", this.onDocMouseMove.bind(this));
  71. document.addEventListener("mouseup", this.onDocMouseUp.bind(this));
  72. window.addEventListener("resize", this.onResize.bind(this));
  73. }
  74. _mouseDownFlag = "";
  75. onDocMouseDown(e: MouseEvent) {
  76. if (!this.pageEl || !this.selCanvas) return;
  77. if (this.store.textEditingState) {
  78. return
  79. }
  80. let box = this.pageEl.getBoundingClientRect();
  81. const pageX = e.clientX - box?.left;
  82. const pageY = e.clientY - box?.top;
  83. const card = this.store.currStreamCard.$el;
  84. box = card.getBoundingClientRect();
  85. const cardX = pageX;
  86. const cardY = e.clientY - box.top;
  87. const sel = this.selCanvas.getBoundingClientRect();
  88. const selX = e.clientX - sel.left;
  89. const sely = e.clientY - sel.top;
  90. this._selDownX = selX;
  91. this._selDownY = sely;
  92. this._selBox = sel;
  93. this._downClientX = e.clientX;
  94. this._downClientY = e.clientY;
  95. console.log(cardX, selX, cardY, sely);
  96. this._downed = true;
  97. this._mouseDownFlag = this.getDivFlag(e.target as any);
  98. if (!this._mouseDownFlag) {
  99. //选框点击判断
  100. let isClickSelRect = false;
  101. if (this.selected.length > 0) {
  102. isClickSelRect = this.objContainer?.testClick(cardX, cardY) as boolean;
  103. if (isClickSelRect) {
  104. this._state = MODE_MOVING;
  105. }
  106. }
  107. if (!isClickSelRect) {
  108. //判断是否有点击到card stream
  109. const comps = this.compClickTest(e);
  110. this.mouseDownSelects = comps;
  111. console.log("comps=>", comps);
  112. if (comps.length < 1) {
  113. const view = this.viewport?.getBoundingClientRect() as any;
  114. const isOut =
  115. e.clientX < view.left ||
  116. e.clientX > view.right ||
  117. e.clientY < view.top ||
  118. e.clientY > view.bottom;
  119. if (!isOut) {
  120. this._state = MODE_SEL_RECT;
  121. }
  122. } else {
  123. this._state = MODE_MOVING;
  124. const obj = this.compMap[comps[0].id];
  125. this.selecteObjs([new CompObject(obj)]);
  126. }
  127. }
  128. } else if (this._mouseDownFlag == "rotate") {
  129. this._state = MODE_ROTATE;
  130. } else if (this._mouseDownFlag == "move") {
  131. this._state = MODE_MOVING;
  132. } else if (this._mouseDownFlag.indexOf("scale") > -1) {
  133. this._state = MODE_SCALE;
  134. }
  135. this._movePreClientX = this._downClientX;
  136. this._movePreClientY = this._downClientY;
  137. }
  138. getDivFlag(div: HTMLElement) {
  139. let c: any = div;
  140. if (!c) return;
  141. let i = 0;
  142. do {
  143. if (c.editable) return c.editable;
  144. c = div.parentElement;
  145. i += 1;
  146. if (i > 3) {
  147. return;
  148. }
  149. } while (c);
  150. }
  151. compClickTest(e: MouseEvent) {
  152. const cards = this.store.streamCardIds;
  153. let n = cards.length;
  154. const compMap = this.store.designData.compMap;
  155. //@ts-ignore
  156. const pbox = this.pageEl.getBoundingClientRect();
  157. const pageX = e.clientX - pbox?.left;
  158. const Out = [];
  159. while (n--) {
  160. const cardComp = compMap[cards[n]];
  161. const box = cardComp.$el.getBoundingClientRect();
  162. const cardY = e.clientY - box.top;
  163. const cardChilds = cardComp.children.default || [];
  164. for (const key of cardChilds) {
  165. const c = compMap[key];
  166. const m = Matrix.createFromDiv(c.$el);
  167. const localp = m.applyInverse(new Point(pageX, cardY));
  168. const cw = this.helper.designSizeToPx(c.layout.size?.[0] as number);
  169. const ch = this.helper.designSizeToPx(c.layout.size?.[1] as number);
  170. const out =
  171. localp.x < 0 || localp.x > cw || localp.y < 0 || localp.y > ch;
  172. if (!out) {
  173. Out.push({
  174. id: key,
  175. el: c.$el,
  176. cardX: pageX,
  177. cardY: cardY,
  178. cardId: cards[n],
  179. startMatrix: m,
  180. });
  181. }
  182. }
  183. }
  184. return Out;
  185. }
  186. streamCardClickTest(e: MouseEvent) {
  187. const cards = this.store.streamCardIds;
  188. let n = cards.length;
  189. const compMap = this.store.designData.compMap;
  190. //@ts-ignore
  191. const pbox = this.pageEl.getBoundingClientRect();
  192. const pageX = e.clientX - pbox?.left;
  193. if (pageX < 0 || pageX > pbox.width) return "";
  194. while (n--) {
  195. const card = compMap[cards[n]];
  196. const box = card.$el.getBoundingClientRect();
  197. if (e.clientY >= box.top && e.clientY <= box.bottom)
  198. return { id: cards[n], x: pageX, y: e.clientY - box.top };
  199. }
  200. return "";
  201. }
  202. _moveSelectUpdated = false;
  203. updateSelects() {
  204. if (this._moveSelectUpdated) return;
  205. this._moveSelectUpdated = true;
  206. //鼠标按下并移动中 修正当前选中的对象
  207. if (this.selected.length < 1) {
  208. //没有被选中的
  209. this.selected = this.mouseDownSelects;
  210. } else {
  211. //当前有选中的
  212. let findSeleted = false;
  213. let n = this.selected.length;
  214. if (this.mouseDownSelects.length > 0) {
  215. while (n--) {
  216. const item = this.mouseDownSelects.find(
  217. (item) => item.id == this.selected[n].id
  218. );
  219. if (item) findSeleted = true;
  220. }
  221. }
  222. if (!findSeleted) {
  223. this.selected = this.mouseDownSelects;
  224. }
  225. }
  226. if (this.selected.length > 0) {
  227. this._state = MODE_MOVING;
  228. }
  229. }
  230. translate(xOffset: number, yOffset: number) {
  231. const objContainer = this.objContainer as ObjsContainer;
  232. objContainer.translate(xOffset, yOffset);
  233. this.upgateGizmoStyle();
  234. }
  235. movingMousemove(e: MouseEvent) {
  236. const objContainer = this.objContainer as ObjsContainer;
  237. objContainer.translate(
  238. e.clientX - this._movePreClientX,
  239. e.clientY - this._movePreClientY
  240. );
  241. this.upgateGizmoStyle();
  242. }
  243. _movePreClientX = 0;
  244. _movePreClientY = 0;
  245. onDocMouseMove(e: MouseEvent) {
  246. if (!this.pageEl) return;
  247. if (!this._downed) {
  248. this.checkHover();
  249. return;
  250. }
  251. switch (this._state) {
  252. case MODE_SEL_RECT: //选框模式
  253. this.drawSelRect(e);
  254. break;
  255. case MODE_MOVING:
  256. this.movingMousemove(e);
  257. break;
  258. case MODE_ROTATE:
  259. this.rotateMousemove(e);
  260. break;
  261. case MODE_SCALE:
  262. this.scaleMousemove(e);
  263. }
  264. this._movePreClientY = e.clientY;
  265. this._movePreClientX = e.clientX;
  266. }
  267. get compMap() {
  268. return this.store.designData.compMap;
  269. }
  270. onDocMouseUp(e: MouseEvent) {
  271. let isClick = false;
  272. const dx = Math.abs(e.clientX - this._downClientX);
  273. const dy = Math.abs(e.clientY - this._downClientY);
  274. if (dx < 2 && dy < 2 && !this.store.textEditingState ) {
  275. isClick = true;
  276. }
  277. if (isClick) {
  278. this._state = MODE_NONE;
  279. if (this.mouseDownSelects.length < 1) this.selecteObjs([]);
  280. else {
  281. const objs = this.mouseDownSelects.map(
  282. (item) => new CompObject(this.compMap[item.id])
  283. );
  284. this.selecteObjs(objs);
  285. }
  286. }
  287. console.log("up");
  288. if (this._state == MODE_SEL_RECT && !isClick) {
  289. //选择空间转 streamCard空间
  290. const card = this.store.currStreamCard;
  291. const box = card.$el.getBoundingClientRect();
  292. this.rectSelect(
  293. this._lastSelRect[0] - box.left,
  294. this._lastSelRect[1] - box.top,
  295. this._lastSelRect[2],
  296. this._lastSelRect[3]
  297. );
  298. }
  299. if (this._state == MODE_ROTATE) {
  300. this.rotateMouseUp(e);
  301. }
  302. if (this._state == MODE_SCALE) {
  303. this.scaleMouseUp(e);
  304. }
  305. this._state = MODE_NONE;
  306. this._downed = false;
  307. this._moveSelectUpdated = false;
  308. this._selCtx?.clearRect(
  309. 0,
  310. 0,
  311. this._selCanvaseSize.w,
  312. this._selCanvaseSize.h
  313. );
  314. this.upgateGizmoStyle();
  315. }
  316. rectSelect(x: number, y: number, width: number, height: number) {
  317. const childs =
  318. this.compMap[this.store.currStreamCardId].children.default || [];
  319. let n = childs.length;
  320. const outs = [];
  321. while (n--) {
  322. const o = new CompObject(this.compMap[childs[n]]);
  323. if (o.testRect({ x, y, w: width, h: height }, true)) {
  324. //相交
  325. outs.push(o);
  326. }
  327. }
  328. console.log(outs);
  329. this.selecteObjs(outs);
  330. }
  331. upgateGizmoStyle() {
  332. if (this.selected.length < 1) {
  333. this.transferStyle.showGizmo = false;
  334. return;
  335. }
  336. this.transferStyle.showGizmo = false;
  337. const selector = this.objContainer as ObjsContainer;
  338. if (!selector) {
  339. return;
  340. }
  341. this.transferStyle.showGizmo = true;
  342. let obj = selector.parent;
  343. let w = selector.rect.width,
  344. h = selector.rect.height;
  345. let tmp = new Matrix();
  346. tmp.copyFrom(obj.worldTransform);
  347. let matrix = `matrix(${tmp.a},${tmp.b},${tmp.c},${tmp.d},${tmp.tx},${tmp.ty})`;
  348. tmp.invert();
  349. let matrixInvert = `matrix(${tmp.a},${tmp.b},${tmp.c},${tmp.d},0,0)`;
  350. this.transferStyle.width = w;
  351. this.transferStyle.height = h;
  352. this.transferStyle.matrix = matrix;
  353. this.transferStyle.matrixInvert = matrixInvert;
  354. }
  355. selectId(id: string) {
  356. //选中ids之前 id对应组件必须已经渲染
  357. console.log("selectId=>", id);
  358. }
  359. _lastSelRect = [0, 0, 0, 0];
  360. drawSelRect(e: MouseEvent) {
  361. const ctx = this._selCtx;
  362. const dx = this._selDownX;
  363. const dy = this._selDownY;
  364. const currX = e.clientX - this._selBox.left;
  365. const currY = e.clientY - this._selBox.top;
  366. const x = Math.min(currX, dx),
  367. y = Math.min(dy, currY);
  368. ctx.clearRect(0, 0, this._selCanvaseSize.w, this._selCanvaseSize.h);
  369. ctx.fillStyle = "rgba(232, 139, 0, 0.16)";
  370. const w = Math.abs(currX - dx);
  371. const h = Math.abs(currY - dy);
  372. ctx.fillRect(x * 2, y * 2, w * 2, h * 2);
  373. ctx.lineWidth = 2;
  374. ctx.strokeStyle = "#E88B00";
  375. ctx.strokeRect(x * 2, y * 2, w * 2, h * 2);
  376. this._lastSelRect[0] = x + this._selBox.left;
  377. this._lastSelRect[1] = y + this._selBox.top;
  378. this._lastSelRect[2] = w;
  379. this._lastSelRect[3] = h;
  380. }
  381. checkHover() {
  382. this.selCanvas;
  383. }
  384. onResize() {
  385. const b = this.selCanvas.getBoundingClientRect();
  386. this.selCanvas.width = b.width * 2;
  387. this.selCanvas.height = b.height * 2;
  388. this._selCtx = this.selCanvas.getContext("2d") as CanvasRenderingContext2D;
  389. this._selCanvaseSize.w = b.width * 2;
  390. this._selCanvaseSize.h = b.height * 2;
  391. }
  392. //
  393. checkIntersect(compId: string, e: MouseEvent) {
  394. const currCard = this.store.currStreamCard.$el;
  395. const comp = this.store.designData.compMap[compId];
  396. //排除坐标没有在streamCard空间内的坐标
  397. //把当前的card坐标转为 组件的自己local坐标判断是否在方框外面
  398. const cardBox = currCard.getBoundingClientRect();
  399. const cardX = e.clientX - cardBox.left;
  400. const cardY = e.clientY - cardBox.top;
  401. //const m = Matrix.createFromComp(comp.layout.transform)
  402. }
  403. objContainer?: ObjsContainer;
  404. selecteObjs(objs: any[], ContainerBox?: ObjsContainer) {
  405. if (this.selected.length == 0 && objs.length == 0) return;
  406. if (
  407. this.selected.length == 1 &&
  408. objs.length == 1 &&
  409. this.selected[0] == objs[0]
  410. )
  411. return;
  412. if (objs.length == 1) {
  413. this.actions.pickComp(objs[0].comp.id);
  414. }
  415. // objs = this.getSceneObjOrderArr(objs);
  416. const preObjContainer = this.objContainer;
  417. if (this.objContainer) {
  418. this.objContainer.destroy();
  419. this.objContainer = undefined;
  420. }
  421. let newObjContainer = undefined;
  422. if (objs.length > 0 && objs[0]) {
  423. newObjContainer = ContainerBox ? ContainerBox : new ObjsContainer(objs);
  424. if (ContainerBox) {
  425. objs.forEach((obj) => {
  426. ContainerBox.parent.addChildWorldNoChange(obj);
  427. });
  428. ContainerBox.selected = objs;
  429. ContainerBox.parent.updateTransform();
  430. }
  431. }
  432. this.objContainer = newObjContainer;
  433. const pre = this.selected.slice(0);
  434. this.selected = objs;
  435. // if (history) {
  436. // this.editor.history.record({
  437. // undo: () => {
  438. // this.selected = pre;
  439. // if (preObjContainer) {
  440. // let parent = preObjContainer.parent;
  441. // pre.forEach(obj => {
  442. // parent.addChildWorldNoChange(obj);
  443. // });
  444. // parent.updateTransform();
  445. // this.objContainer = preObjContainer;
  446. // } else {
  447. // this.objContainer = null;
  448. // }
  449. // this.emitChange();
  450. // }, redo: () => {
  451. // this.selected = objs;
  452. // if (preObjContainer) {
  453. // preObjContainer.destroy();
  454. // }
  455. // if (newObjContainer) {
  456. // let parent = newObjContainer.parent;
  457. // objs.forEach(obj => {
  458. // parent.addChildWorldNoChange(obj);
  459. // });
  460. // parent.updateTransform();
  461. // this.objContainer = newObjContainer;
  462. // } else {
  463. // this.objContainer = null;
  464. // }
  465. // this.emitChange();
  466. // }
  467. // })
  468. // }
  469. this.emitChange();
  470. this.upgateGizmoStyle();
  471. return this.selected;
  472. }
  473. emitChange() {
  474. const selected = this.selected;
  475. if (selected.length && selected[0]) {
  476. this.bus.emit("showProps", selected[0].from);
  477. } else {
  478. this.bus.emit("showProps");
  479. }
  480. this.bus.emit("selectedChange");
  481. }
  482. rotateCenter?: { x: number; y: number };
  483. ratatePre = 0;
  484. objinitAngleRad = 0;
  485. rotateCmd = false;
  486. lastRad = 0;
  487. rotateMousemove(e: MouseEvent) {
  488. const card = this.store.currStreamCard;
  489. const rect = card.$el.getBoundingClientRect();
  490. let StartX = e.clientX - rect.left;
  491. let StartY = e.clientY - rect.top;
  492. const objContainer = this.objContainer as ObjsContainer;
  493. //获取当前屏幕坐标和选框中心点坐标,计算旋转值
  494. if (!this.rotateCenter) {
  495. //let rect = this.objContainer.parent.getBounds(false);
  496. let center = objContainer.setPivot(4);
  497. this.rotateCenter = center;
  498. let vec = { x: StartX - center.x, y: StartY - center.y };
  499. let angle = Math.atan2(vec.y, vec.x);
  500. if (angle < 0) angle += 2 * Math.PI;
  501. this.ratatePre = angle;
  502. this.objinitAngleRad = objContainer.parent.rotation;
  503. this.rotateCmd = true;
  504. return;
  505. }
  506. let center = this.rotateCenter;
  507. let vec = { x: StartX - center.x, y: StartY - center.y };
  508. let angle = Math.atan2(vec.y, vec.x);
  509. if (angle < 0) angle += 2 * Math.PI;
  510. let dta = this.objinitAngleRad + angle - this.ratatePre;
  511. if (e.shiftKey) {
  512. //规整到0 90 180 270
  513. if (dta < 0) dta += 2 * Math.PI;
  514. let Deg45 = Math.PI / 4.0;
  515. let Deg90 = Math.PI / 2.0;
  516. let Deg135 = Deg45 * 3;
  517. let Deg225 = Deg45 * 5;
  518. let Deg270 = Deg45 * 6;
  519. let Deg315 = Deg45 * 7;
  520. if (dta < Deg45) {
  521. dta = 0;
  522. } else if (dta < Deg135) {
  523. dta = Deg90;
  524. } else if (dta < Deg225) {
  525. dta = Math.PI;
  526. } else if (dta < Deg315) {
  527. dta = Deg270;
  528. } else {
  529. dta = 0;
  530. }
  531. }
  532. this.lastRad = dta;
  533. console.log("rotate=>", dta);
  534. objContainer.rotate(dta);
  535. // this.emit("translateChange", this.objContainer)
  536. this.upgateGizmoStyle();
  537. }
  538. rotateMouseUp(e: MouseEvent) {
  539. this.rotateCenter = undefined;
  540. if (!this.rotateCmd) return;
  541. let scope = this;
  542. let last = this.lastRad;
  543. let initrad = scope.objinitAngleRad;
  544. // this.editor.history.record({undo:function(){
  545. // scope.objContainer.setPivot(4);
  546. // scope.objContainer.rotate( initrad );
  547. // scope.emitChange();
  548. // }, redo:function(){
  549. // scope.objContainer.setPivot(4);
  550. // scope.objContainer.rotate( last );
  551. // scope.emitChange();
  552. // }});
  553. this.rotateCmd = false;
  554. }
  555. //缩放选中的对象
  556. scalePivot?: any;
  557. scaleIndex = 0;
  558. mainAxisVector = { x: 0, y: 0 };
  559. initScale = { x: 1, y: 1 };
  560. mainAxisVectorLenth = 0;
  561. xAxisVector = { x: 1, y: 1 };
  562. xAxisVectorLength = 0;
  563. yAxisVector = { x: 1, y: 1 };
  564. yAxisVectorLength = 0;
  565. scaleCmd = false;
  566. lastScale = { x: 1, y: 1 };
  567. initScaleWith = {w: 0, h:0};
  568. scaleMousemove(event: MouseEvent) {
  569. let dirIndexs = [
  570. "scaleBottomright",
  571. "scaleBottomleft",
  572. "scaleTopleft",
  573. "scaleTopright",
  574. ];
  575. let dirOrth = ["scaleright", "scaleleft", "scalebottom", "scaletop"];
  576. const rect = this.store.currStreamCard.$el.getBoundingClientRect();
  577. let StartX = event.clientX - rect.left;
  578. let StartY = event.clientY - rect.top;
  579. const objContainer = this.objContainer as ObjsContainer;
  580. //获取当前屏幕坐标和选框中心点坐标,计算旋转值
  581. if (!this.scalePivot) {
  582. let dir = this._mouseDownFlag;
  583. let scaleIndex = dirIndexs.indexOf(dir);
  584. if (scaleIndex == -1) {
  585. scaleIndex = dirOrth.indexOf(dir);
  586. if (scaleIndex == 2) scaleIndex = 0;
  587. }
  588. let pivot = objContainer.setPivot(scaleIndex);
  589. this.scaleIndex = scaleIndex;
  590. this.scalePivot = pivot;
  591. this.mainAxisVector = { x: StartX - pivot.x, y: StartY - pivot.y };
  592. let scale = objContainer.parent.scale;
  593. this.initScale = { x: scale.x, y: scale.y };
  594. this.initScaleWith = {w: objContainer.width, h: objContainer.height}
  595. this.mainAxisVectorLenth = VectorLenth(
  596. this.mainAxisVector.x,
  597. this.mainAxisVector.y
  598. );
  599. let ret = objContainer.getPivotXY(scaleIndex);
  600. this.xAxisVector = ret.x;
  601. this.xAxisVectorLength = VectorLenth(ret.x.x, ret.x.y);
  602. this.yAxisVector = ret.y;
  603. this.yAxisVectorLength = VectorLenth(ret.y.x, ret.y.y);
  604. return;
  605. }
  606. this.scaleCmd = true;
  607. let center = this.scalePivot;
  608. let vec = { x: StartX - center.x, y: StartY - center.y };
  609. if (event.shiftKey) {
  610. //按住shift 自由缩放
  611. let dtaX = Project(vec, this.xAxisVector) / this.xAxisVectorLength;
  612. let dtaY = Project(vec, this.yAxisVector) / this.yAxisVectorLength;
  613. this.lastScale.x = dtaX * this.initScale.x;
  614. this.lastScale.y = dtaY * this.initScale.y;
  615. // objContainer.scale(this.lastScale.x, this.lastScale.y);
  616. const currW = this.initScaleWith.w * this.lastScale.x;
  617. objContainer.scaleSize(currW, this.lastScale.y * this.initScaleWith.h);
  618. } else {
  619. let mainVec = this.mainAxisVector;
  620. let dtaScale = Project(vec, mainVec) / this.mainAxisVectorLenth;
  621. let i = dirOrth.indexOf(this._mouseDownFlag);
  622. if (i == -1) {
  623. this.lastScale.x = dtaScale * this.initScale.x;
  624. this.lastScale.y = dtaScale * this.initScale.y;
  625. // objContainer.scale(this.lastScale.x, this.lastScale.y);
  626. const currW = this.initScaleWith.w * this.lastScale.x;
  627. objContainer.scaleSize(currW, this.initScaleWith.h *this.lastScale.y);
  628. // objContainer.scaleHeight(, dtaScale);
  629. } else if (i == 0 || i == 1) {
  630. this.lastScale.x = dtaScale * this.initScale.x;
  631. // objContainer.scaleX(this.lastScale.x);
  632. const currW = this.initScaleWith.w * this.lastScale.x;
  633. objContainer.scaleWidth(currW);
  634. } else if (i == 2 || i == 3) {
  635. this.lastScale.y = dtaScale * this.initScale.y;
  636. // objContainer.scaleY(this.lastScale.y);
  637. objContainer.scaleHeight(this.lastScale.y * this.initScaleWith.h);
  638. }
  639. }
  640. this.upgateGizmoStyle();
  641. }
  642. scaleMouseUp(event: MouseEvent) {
  643. this.scalePivot = undefined;
  644. if (this.scaleCmd) {
  645. let preScale = { x: this.initScale.x, y: this.initScale.y };
  646. let scaleIndex = this.scaleIndex;
  647. let lastScale = { x: this.lastScale.x, y: this.lastScale.y };
  648. // this.objContainer?.applyScaleToChildSize();
  649. // this.upgateGizmoStyle();
  650. // this.editor.history.record({
  651. // undo:()=>{
  652. // this.objContainer.setPivot( scaleIndex );
  653. // this.objContainer.scale(preScale.x, preScale.y);
  654. // this.emitChange();
  655. // },
  656. // redo:()=>{
  657. // this.objContainer.setPivot( scaleIndex );
  658. // this.objContainer.scale(lastScale.x, lastScale.y);
  659. // this.emitChange();
  660. // }
  661. // });
  662. this.scaleCmd = false;
  663. // this.emit("objSizeChanged");
  664. // this.editor.draw();
  665. }
  666. // this.emitTransformed = false;
  667. }
  668. }