|
@@ -0,0 +1,173 @@
|
|
|
+import { Effect, ModuleControl } from "queenjs";
|
|
|
+import { EditorModule } from "../../module";
|
|
|
+import { DesignComp } from "../../objects/DesignTemp/DesignComp";
|
|
|
+import { Matrix } from "../SelectCtrl/matrix";
|
|
|
+import {ref, onMounted, nextTick} from "vue";
|
|
|
+import { RxValue } from "../ReactCtrl/rxValue";
|
|
|
+import { DisplayObject } from "../SelectCtrl/objects/displayObject";
|
|
|
+import { ObjsContainer } from "../SelectCtrl/ObjsContainer";
|
|
|
+
|
|
|
+
|
|
|
+const KeySpace = 32;
|
|
|
+
|
|
|
+export class EditorCtrl extends ModuleControl<EditorModule> {
|
|
|
+
|
|
|
+
|
|
|
+ state = RxValue.create({
|
|
|
+ width: 1500,
|
|
|
+ height: 1000,
|
|
|
+ pos: {
|
|
|
+ x: 0, y:0
|
|
|
+ },
|
|
|
+
|
|
|
+ scale: 1,
|
|
|
+ mouse: {x: 0, y: 0},
|
|
|
+ keyCode: -1,
|
|
|
+
|
|
|
+ page: {w: 375, h: 650}
|
|
|
+ })
|
|
|
+
|
|
|
+ doms:any = {};
|
|
|
+
|
|
|
+ useEditor() {
|
|
|
+ const EditorRef = ref<HTMLElement>();
|
|
|
+ const ParentRef = ref<HTMLElement>();
|
|
|
+ const PageRef = ref<HTMLElement>();
|
|
|
+ const CanvasRef = ref<HTMLElement>();
|
|
|
+ const init = () => {
|
|
|
+ if (!EditorRef.value || !CanvasRef.value) return;
|
|
|
+ console.log("xxxxxxxx=>" , ParentRef.value, PageRef.value);
|
|
|
+ EditorRef.value.style.width = this.state.width + "px";
|
|
|
+ EditorRef.value.style.height = this.state.height + "px";
|
|
|
+ EditorRef.value.style.position = "absolute";
|
|
|
+ EditorRef.value.style.backgroundColor = "greenyellow";
|
|
|
+ EditorRef.value.style.left = "0";
|
|
|
+ EditorRef.value.style.top = "0";
|
|
|
+
|
|
|
+ CanvasRef.value.style.width = "100%"
|
|
|
+ CanvasRef.value.style.height = "100%"
|
|
|
+ EditorRef.value.style.position = "absolute";
|
|
|
+
|
|
|
+ this.initEditorEvent(EditorRef.value, ParentRef.value as any);
|
|
|
+ }
|
|
|
+
|
|
|
+ onMounted(()=>{
|
|
|
+ setTimeout(init, 200);
|
|
|
+ })
|
|
|
+
|
|
|
+ this.doms = {editor:EditorRef, parent: ParentRef, page:PageRef, canvas: CanvasRef};
|
|
|
+ return this.doms;
|
|
|
+ }
|
|
|
+
|
|
|
+ isKeyDown( key:number) {
|
|
|
+ return this.state.keyCode === key;
|
|
|
+ }
|
|
|
+ isMoving() {
|
|
|
+ return this.isKeyDown(KeySpace);
|
|
|
+ }
|
|
|
+ Obj = new ObjsContainer([]);
|
|
|
+
|
|
|
+ initEditorEvent(editorLayer: HTMLElement , parent:HTMLElement) {
|
|
|
+
|
|
|
+ const Obj = this.Obj;
|
|
|
+ Obj.rect.width = this.state.width;
|
|
|
+ Obj.rect.height = this.state.height;
|
|
|
+
|
|
|
+ // 监听键盘的 keydown 事件
|
|
|
+ document.addEventListener("keydown", (event)=>{
|
|
|
+ this.state.keyCode = event.keyCode;
|
|
|
+ });
|
|
|
+ // 监听键盘的 keyup 事件
|
|
|
+ document.addEventListener("keyup", (event)=>{
|
|
|
+ this.state.keyCode = -1;
|
|
|
+ isMoving = false;
|
|
|
+ });
|
|
|
+
|
|
|
+
|
|
|
+ editorLayer.addEventListener("wheel", (event:any)=>{
|
|
|
+ // 检查滚轮方向
|
|
|
+ let delta = Math.max(-1, Math.min(1, (event.wheelDelta || -event.detail)))
|
|
|
+ console.log( delta );
|
|
|
+
|
|
|
+ let s = this.state.scale + 0.1 * delta
|
|
|
+ if ( s < 0.5 ) s = 0.5;
|
|
|
+ if (s > 2) s = 2.0;
|
|
|
+ this.state.setScale( s)
|
|
|
+ });
|
|
|
+
|
|
|
+ let isMoving = false;
|
|
|
+ let moveX = 0, moveY = 0;
|
|
|
+ editorLayer.addEventListener("mousemove",(e:MouseEvent)=>{
|
|
|
+ this.state.setMouse({x: e.offsetX, y: e.offsetY});
|
|
|
+
|
|
|
+ console.log("move", isMoving);
|
|
|
+
|
|
|
+ if (isMoving) {
|
|
|
+ const xOffset = e.pageX - moveX;
|
|
|
+ const yOffset = e.pageY - moveY;
|
|
|
+ console.log(xOffset, yOffset);
|
|
|
+ this.state.setPos({x: this.state.pos.x + xOffset, y: this.state.pos.y + yOffset});
|
|
|
+
|
|
|
+ moveX = e.pageX;
|
|
|
+ moveY = e.pageY;
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ document.addEventListener("mouseup",(e:MouseEvent)=>{
|
|
|
+ isMoving = false;
|
|
|
+ })
|
|
|
+
|
|
|
+ this.state.onScaleChanged((v)=>{
|
|
|
+ Obj.setPivot2(this.state.width / 2, this.state.height / 2);
|
|
|
+ Obj.scale(v, v);
|
|
|
+ editorLayer.style.transform = `scale(${v})`;
|
|
|
+ editorLayer.style.transformOrigin = "50% 50%";
|
|
|
+ })
|
|
|
+ this.state.onPosChanged((v)=>{
|
|
|
+ editorLayer.style.left= v.x + "px";
|
|
|
+ editorLayer.style.top = v.y + "px";
|
|
|
+ })
|
|
|
+
|
|
|
+ editorLayer.addEventListener("mousedown", (e:MouseEvent)=>{
|
|
|
+ isMoving = this.isKeyDown(KeySpace)
|
|
|
+ if (isMoving) {
|
|
|
+ editorLayer.focus();
|
|
|
+ }
|
|
|
+ moveX = e.pageX;
|
|
|
+ moveY = e.pageY;
|
|
|
+ })
|
|
|
+
|
|
|
+ window.addEventListener("resize", ()=>this.updateEditorSize());
|
|
|
+ this.state.onPageChanged(()=>this.updateCardSize());
|
|
|
+ }
|
|
|
+
|
|
|
+ updateCardSize() {
|
|
|
+ const page = this.doms.page.value as HTMLElement;
|
|
|
+ const state = this.state;
|
|
|
+ page.style.left = (state.width - state.page.w) / 2.0 + "px";
|
|
|
+ page.style.top = (state.height - state.page.h) / 2.0 + "px";
|
|
|
+
|
|
|
+ console.log("size");
|
|
|
+
|
|
|
+ this.updateEditorSize();
|
|
|
+ }
|
|
|
+
|
|
|
+ updateEditorSize() {
|
|
|
+ const page = this.doms.editor.value as HTMLElement;
|
|
|
+ const state = this.state;
|
|
|
+ const parent = this.doms.parent.value as HTMLElement;
|
|
|
+
|
|
|
+ console.log( parent.clientWidth, parent.clientHeight);
|
|
|
+
|
|
|
+ const left = ( parent.clientWidth - state.width) / 2.0
|
|
|
+ const top = (parent.clientHeight - state.height) / 2.0
|
|
|
+
|
|
|
+ // this.Obj.parent.x = 0;
|
|
|
+ // this.Obj.parent.y = 0;
|
|
|
+ // this.Obj.parent.updateTransform();
|
|
|
+ // page.style.transform = this.Obj.parent.worldTransform.getMatrixStr();
|
|
|
+
|
|
|
+ page.style.left = left + "px";
|
|
|
+ page.style.top = top + "px";
|
|
|
+ }
|
|
|
+}
|