lazy-widgets
Version:
Typescript retained mode GUI for the HTML canvas API
124 lines • 6.52 kB
JavaScript
import { FocusType } from '../core/FocusType.js';
import { PointerEvent } from './PointerEvent.js';
import { DynMsg } from '../core/Strings.js';
/**
* The scrolling mode that determines how the {@link PointerWheelEvent#deltaX},
* {@link PointerWheelEvent#deltaY} and {@link PointerWheelEvent#deltaZ} values
* are interpreted.
*
* @category Event
*/
export var PointerWheelMode;
(function (PointerWheelMode) {
/** In this mode, delta values are measured in pixels. */
PointerWheelMode[PointerWheelMode["Pixel"] = 0] = "Pixel";
/**
* In this mode, delta values are measured in line heights. The height of a
* line is supplied as an argument to the
* {@link PointerWheelEvent#getDeltaPixels} method.
*/
PointerWheelMode[PointerWheelMode["Line"] = 1] = "Line";
/**
* In this mode, delta values are measured in {@link Widget} dimensions,
* minus {@link PointerWheelEvent.PageLinesError | a few lines} or
* {@link PointerWheelEvent.PagePercentError | a percentage of the dimensions},
* whichever is smaller. Both line height and dimensions are supplied as
* arguments to the {@link PointerWheelEvent#getDeltaPixels} method.
*/
PointerWheelMode[PointerWheelMode["Page"] = 2] = "Page";
})(PointerWheelMode || (PointerWheelMode = {}));
/**
* Convert DOM WheelEvent.deltaMode to {@link PointerWheelMode}, or null if the
* DOM delta mode is unknown.
*
* @category Event
*/
export function parseDOMDeltaMode(domDeltaMode) {
switch (domDeltaMode) {
case 0:
return PointerWheelMode.Pixel;
case 1:
return PointerWheelMode.Line;
case 2:
return PointerWheelMode.Page;
default:
return null;
}
}
/**
* A pointer wheel {@link PointerEvent}.
*
* Has a focus type of {@link FocusType.Pointer} and does not need focus.
*
* @category Event
*/
export class PointerWheelEvent extends PointerEvent {
constructor(x, y, deltaX, deltaY, deltaZ, deltaMode, fromDrag, shift, ctrl, alt, source, target = null) {
super(x, y, shift, ctrl, alt, source, target);
this.type = PointerWheelEvent.type;
this.focusType = FocusType.Pointer;
this.deltaX = deltaX;
this.deltaY = deltaY;
this.deltaZ = deltaZ;
this.deltaMode = deltaMode;
this.fromDrag = fromDrag;
}
correctOffset(xOffset, yOffset) {
return new PointerWheelEvent(this.x - xOffset, this.y - yOffset, this.deltaX, this.deltaY, this.deltaZ, this.deltaMode, this.fromDrag, this.shift, this.ctrl, this.alt, this.source, this.target);
}
cloneWithTarget(target) {
return new PointerWheelEvent(this.x, this.y, this.deltaX, this.deltaY, this.deltaZ, this.deltaMode, this.fromDrag, this.shift, this.ctrl, this.alt, this.source, target);
}
/**
* Get the scroll delta in pixels, even if the
* {@link PointerWheelEvent#deltaMode} is not
* {@link PointerWheelMode.Pixel}.
*
* @param forceLimit - Should the delta be limited by {@link PointerWheelEvent.PageLinesError} and {@link PointerWheelEvent.PagePercentError}, if {@link PointerWheelEvent#deltaMode} is not {@link PointerWheelMode.Page}?
* @param lineHeight - The full height (line height with spacing) of a line, used for page {@link PointerWheelEvent#deltaMode}, or for limiting the delta
* @param containerWidth - The width of the container, used for page {@link PointerWheelEvent#deltaMode}, or for limiting the delta
* @param containerHeight - The height of the container, used for page {@link PointerWheelEvent#deltaMode}, or for limiting the delta
* @param containerDepth - The depth of the container, used for page {@link PointerWheelEvent#deltaMode}, or for limiting the delta. Only used for custom containers/widgets with a Z-axis
* @returns Returns a 3-tuple containing the x, y and z components, repectively, of the wheel event in pixels.
*/
getDeltaPixels(forceLimit, lineHeight, containerWidth, containerHeight, containerDepth = 0) {
let limitX = Infinity, limitY = Infinity, limitZ = Infinity;
if (forceLimit || this.deltaMode !== PointerWheelMode.Page) {
const linesError = lineHeight * PointerWheelEvent.PageLinesError;
limitX = containerWidth - Math.min(containerWidth * PointerWheelEvent.PagePercentError, linesError);
limitY = containerHeight - Math.min(containerHeight * PointerWheelEvent.PagePercentError, linesError);
limitZ = containerDepth - Math.min(containerDepth * PointerWheelEvent.PagePercentError, linesError);
}
switch (this.deltaMode) {
case PointerWheelMode.Pixel:
return [
Math.min(limitX, Math.abs(this.deltaX)) * Math.sign(this.deltaX),
Math.min(limitY, Math.abs(this.deltaY)) * Math.sign(this.deltaY),
Math.min(limitZ, Math.abs(this.deltaZ)) * Math.sign(this.deltaZ)
];
case PointerWheelMode.Line:
return [
Math.min(limitX, Math.abs(this.deltaX) * lineHeight) * Math.sign(this.deltaX),
Math.min(limitY, Math.abs(this.deltaY) * lineHeight) * Math.sign(this.deltaY),
Math.min(limitZ, Math.abs(this.deltaZ) * lineHeight) * Math.sign(this.deltaZ)
];
case PointerWheelMode.Page:
{
const linesError = lineHeight * PointerWheelEvent.PageLinesError;
return [
(Math.abs(this.deltaX) * containerWidth - Math.min(containerWidth * PointerWheelEvent.PagePercentError, linesError)) * Math.sign(this.deltaX),
(Math.abs(this.deltaY) * containerHeight - Math.min(containerHeight * PointerWheelEvent.PagePercentError, linesError)) * Math.sign(this.deltaY),
(Math.abs(this.deltaZ) * containerDepth - Math.min(containerDepth * PointerWheelEvent.PagePercentError, linesError)) * Math.sign(this.deltaZ)
];
}
default:
throw new Error(DynMsg.INVALID_ENUM(this.deltaMode, 'PointerWheelMode', 'deltaMode'));
}
}
}
PointerWheelEvent.type = 'pointer-wheel';
/** The amount of lines to remove from a page scroll */
PointerWheelEvent.PageLinesError = 3;
/** The percentage of a page to remove from a page scroll */
PointerWheelEvent.PagePercentError = 0.1;
//# sourceMappingURL=PointerWheelEvent.js.map