polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
192 lines • 6.48 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MouseHandler = void 0;
class MouseHandler {
constructor() {
this.state = {
lastPosition: { x: 0, y: 0 },
isDragging: false,
dragStart: undefined,
pressedButtons: new Set(),
lastClickTime: 0,
lastClickPosition: undefined,
hoveredElement: undefined,
};
this.DOUBLE_CLICK_THRESHOLD_MS = 500;
this.DOUBLE_CLICK_DISTANCE_THRESHOLD = 5;
this.DRAG_THRESHOLD = 3;
}
static normalizeMouseEvent(rawEvent) {
return {
action: rawEvent.action || 'mousedown',
x: rawEvent.x || 0,
y: rawEvent.y || 0,
button: rawEvent.button || 'left',
shift: !!rawEvent.shift,
ctrl: !!rawEvent.ctrl,
};
}
processMouseEvent(event) {
const now = Date.now();
const result = {
event,
isDoubleClick: false,
isDragStart: false,
isDragMove: false,
isDragEnd: false,
dragDistance: undefined,
};
switch (event.action) {
case 'mousedown':
this.handleMouseDown(event, now, result);
break;
case 'mouseup':
this.handleMouseUp(event, result);
break;
case 'mousemove':
this.handleMouseMove(event, result);
break;
case 'wheelup':
case 'wheeldown':
this.handleWheel(event);
break;
}
this.state.lastPosition = { x: event.x, y: event.y };
return result;
}
handleMouseDown(event, now, result) {
this.state.pressedButtons.add(event.button);
if (event.button === 'left') {
const timeDiff = now - this.state.lastClickTime;
const positionDiff = this.state.lastClickPosition
? this.calculateDistance(event, this.state.lastClickPosition)
: Infinity;
if (timeDiff < this.DOUBLE_CLICK_THRESHOLD_MS &&
positionDiff < this.DOUBLE_CLICK_DISTANCE_THRESHOLD) {
result.isDoubleClick = true;
this.state.lastClickTime = 0;
this.state.lastClickPosition = undefined;
}
else {
this.state.lastClickTime = now;
this.state.lastClickPosition = { x: event.x, y: event.y };
}
}
this.state.dragStart = { x: event.x, y: event.y };
}
handleMouseUp(event, result) {
this.state.pressedButtons.delete(event.button);
if (this.state.isDragging) {
result.isDragEnd = true;
this.state.isDragging = false;
this.state.dragStart = undefined;
}
}
handleMouseMove(event, result) {
if (!this.state.isDragging &&
this.state.dragStart &&
this.state.pressedButtons.has('left')) {
const distance = this.calculateDistance(event, this.state.dragStart);
if (distance > this.DRAG_THRESHOLD) {
this.state.isDragging = true;
result.isDragStart = true;
}
}
if (this.state.isDragging) {
result.isDragMove = true;
if (this.state.dragStart) {
result.dragDistance = this.calculateDistance(event, this.state.dragStart);
}
}
}
handleWheel(event) {
this.state.lastPosition = { x: event.x, y: event.y };
}
calculateDistance(point1, point2) {
const dx = point1.x - point2.x;
const dy = point1.y - point2.y;
return Math.sqrt(dx * dx + dy * dy);
}
static isPointInBounds(point, bounds) {
return point.x >= bounds.x &&
point.x < bounds.x + bounds.width &&
point.y >= bounds.y &&
point.y < bounds.y + bounds.height;
}
static isMouseEventInBounds(event, bounds) {
return MouseHandler.isPointInBounds({ x: event.x, y: event.y }, bounds);
}
static getRelativePosition(point, bounds) {
return {
x: point.x - bounds.x,
y: point.y - bounds.y,
};
}
isDragging() {
return this.state.isDragging;
}
getCurrentPosition() {
return { ...this.state.lastPosition };
}
getPressedButtons() {
return Array.from(this.state.pressedButtons);
}
isButtonPressed(button) {
return this.state.pressedButtons.has(button);
}
getDragStart() {
return this.state.dragStart ? { ...this.state.dragStart } : undefined;
}
setHover(elementId) {
this.state.hoveredElement = elementId;
}
clearHover() {
this.state.hoveredElement = undefined;
}
getHoveredElement() {
return this.state.hoveredElement;
}
reset() {
this.state = {
lastPosition: { x: 0, y: 0 },
isDragging: false,
dragStart: undefined,
pressedButtons: new Set(),
lastClickTime: 0,
lastClickPosition: undefined,
hoveredElement: undefined,
};
}
static createBounds(x, y, width, height) {
return { x, y, width, height };
}
static expandBounds(bounds, margin) {
return {
x: bounds.x - margin,
y: bounds.y - margin,
width: bounds.width + 2 * margin,
height: bounds.height + 2 * margin,
};
}
static boundsIntersect(bounds1, bounds2) {
return bounds1.x < bounds2.x + bounds2.width &&
bounds1.x + bounds1.width > bounds2.x &&
bounds1.y < bounds2.y + bounds2.height &&
bounds1.y + bounds1.height > bounds2.y;
}
static getIntersection(bounds1, bounds2) {
if (!MouseHandler.boundsIntersect(bounds1, bounds2)) {
return null;
}
const x = Math.max(bounds1.x, bounds2.x);
const y = Math.max(bounds1.y, bounds2.y);
const width = Math.min(bounds1.x + bounds1.width, bounds2.x + bounds2.width) - x;
const height = Math.min(bounds1.y + bounds1.height, bounds2.y + bounds2.height) - y;
return { x, y, width, height };
}
destroy() {
this.reset();
}
}
exports.MouseHandler = MouseHandler;
//# sourceMappingURL=mouse-handler.js.map