Commit e8a0824f authored by 李维's avatar 李维

fixed the bug of not support touch pc

parent 13a73373
......@@ -90,6 +90,7 @@ export class PlayComponent implements OnInit, OnDestroy {
g_formData; // 核心表单数据
g_teacherFlag = false; // 默认角色
g_currentUser;
g_firstTouch = true;
// ------------------------------------
// ------------ 私有数据 ------------
......@@ -955,70 +956,93 @@ export class PlayComponent implements OnInit, OnDestroy {
initListener() {
const element = this.canvas.nativeElement;
this.g_winResizeEventStream.pipe(debounceTime(500)).subscribe(data => {
this.renderAfterResize();
});
if (this.IsPC()) {
this.canvas.nativeElement.addEventListener("mousedown", event => {
setMxMyByMouse(event);
this.mapDown(event);
});
this.canvas.nativeElement.addEventListener("mousemove", event => {
setMxMyByMouse(event);
this.mapMove(event);
});
this.canvas.nativeElement.addEventListener("mouseup", event => {
setMxMyByMouse(event);
this.mapUp(event);
});
const setMxMyByMouse = event => {
this.g_clickX = event.offsetX;
this.g_clickY = event.offsetY;
};
} else {
this.canvas.nativeElement.addEventListener("touchstart", event => {
setMxMyByTouch(event);
this.mapDown(event);
});
this.canvas.nativeElement.addEventListener("touchmove", event => {
setMxMyByTouch(event);
this.mapMove(event);
});
this.canvas.nativeElement.addEventListener("touchend", event => {
setMxMyByTouch(event);
this.mapUp(event);
});
this.canvas.nativeElement.addEventListener("touchcancel", event => {
setMxMyByTouch(event);
this.mapUp(event);
});
const setMxMyByTouch = event => {
if (event.touches.length <= 0) {
return;
}
if (this.g_canvasLeft == null) {
setParentOffset();
}
this.g_clickX = event.touches[0].pageX - this.g_canvasLeft;
this.g_clickY = event.touches[0].pageY - this.g_canvasTop;
};
const setParentOffset = () => {
const rect = this.canvas.nativeElement.getBoundingClientRect();
this.g_canvasLeft = rect.left;
this.g_canvasTop = rect.top;
};
}
const addTouchListener = () => {
element.addEventListener('touchstart', touchDownFunc);
element.addEventListener('touchmove', touchMoveFunc);
element.addEventListener('touchend', touchUpFunc);
element.addEventListener('touchcancel', touchUpFunc);
};
const removeTouchListener = () => {
element.removeEventListener('touchstart', touchDownFunc);
element.removeEventListener('touchmove', touchMoveFunc);
element.removeEventListener('touchend', touchUpFunc);
element.removeEventListener('touchcancel', touchUpFunc);
};
const addMouseListener = () => {
element.addEventListener('mousedown', mouseDownFunc);
element.addEventListener('mousemove', mouseMoveFunc);
element.addEventListener('mouseup', mouseUpFunc);
};
const removeMouseListener = () => {
element.removeEventListener('mousedown', mouseDownFunc);
element.removeEventListener('mousemove', mouseMoveFunc);
element.removeEventListener('mouseup', mouseUpFunc);
};
const touchDownFunc = (e) => {
if (this.g_firstTouch) {
this.g_firstTouch = false;
removeMouseListener();
}
setMxMyByTouch(e);
this.mapDown(e);
};
const touchMoveFunc = (e) => {
setMxMyByTouch(e);
this.mapMove(e);
};
const touchUpFunc = (e) => {
setMxMyByTouch(e);
this.mapUp(e);
};
const mouseDownFunc = (e) => {
if (this.g_firstTouch) {
this.g_firstTouch = false;
removeTouchListener();
}
setMxMyByMouse(e);
this.mapDown(e);
};
const mouseMoveFunc = (e) => {
setMxMyByMouse(e);
this.mapMove(e);
};
const mouseUpFunc = (e) => {
setMxMyByMouse(e);
this.mapUp(e);
};
const setMxMyByTouch = event => {
if (event.touches.length <= 0) {
return;
}
if (this.g_canvasLeft == null) {
setParentOffset();
}
this.g_clickX = event.touches[0].pageX - this.g_canvasLeft;
this.g_clickY = event.touches[0].pageY - this.g_canvasTop;
};
const setParentOffset = () => {
const rect = this.canvas.nativeElement.getBoundingClientRect();
this.g_canvasLeft = rect.left;
this.g_canvasTop = rect.top;
};
const setMxMyByMouse = (event) => {
this.g_clickX = event.offsetX;
this.g_clickY = event.offsetY;
};
addMouseListener();
addTouchListener();
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment