UNPKG

@drincs/pixi-vn

Version:

Pixi'VN is a npm package that provides various features for creating visual novels.

260 lines (255 loc) 8.41 kB
import { Text } from 'pixi.js'; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); // src/decorators/EventDecorator.ts var registeredEvents = {}; function getEventTypeById(eventId) { try { let eventType = registeredEvents[eventId]; if (!eventType) { console.error(`[Pixi'VN] Event ${eventId} not found`); return; } new eventType(); return eventType; } catch (e) { console.error(`[Pixi'VN] Error while getting Event ${eventId}`, e); return; } } function getEventInstanceById(eventId) { try { let eventType = registeredEvents[eventId]; if (!eventType) { console.error(`[Pixi'VN] Event ${eventId} not found`); return; } let event = new eventType(); return event; } catch (e) { console.error(`[Pixi'VN] Error while getting Event ${eventId}`, e); return; } } function getFillGradientFillPattern(prop, propName) { if (!(prop instanceof Object)) { return prop; } console.warn(`[Pixi'VN] CanvasText.style.${propName} is a FillGradient or FillPattern, this is not supported yet.`, prop); return void 0; } function getTextStyle(style) { return { align: style.align, breakWords: style.breakWords, dropShadow: style.dropShadow, fill: getFillGradientFillPattern(style.stroke, "fill"), fontFamily: style.fontFamily, fontSize: style.fontSize, fontStyle: style.fontStyle, fontVariant: style.fontVariant, fontWeight: style.fontWeight, leading: style.leading, letterSpacing: style.letterSpacing, lineHeight: style.lineHeight, padding: style.padding, stroke: getFillGradientFillPattern(style.stroke, "stroke"), textBaseline: style.textBaseline, trim: style.trim, whiteSpace: style.whiteSpace, wordWrap: style.wordWrap, wordWrapWidth: style.wordWrapWidth }; } // src/classes/canvas/CanvasContainer.ts function getMemoryContainer(element) { let className = "CanvasContainer"; if (element.hasOwnProperty("pixivnId")) { className = element.pixivnId; } return { pixivnId: className, elements: [], width: element.width, height: element.height, isRenderGroup: element.isRenderGroup, blendMode: element.blendMode, tint: element.tint, alpha: element.alpha, angle: element.angle, renderable: element.renderable, rotation: element.rotation, scale: { x: element.scale.x, y: element.scale.y }, pivot: { x: element.pivot.x, y: element.pivot.y }, position: { x: element.position.x, y: element.position.y }, skew: { x: element.skew.x, y: element.skew.y }, visible: element.visible, x: element.x, y: element.y, boundsArea: element.boundsArea, cursor: element.cursor, eventMode: element.eventMode, interactive: element.interactive, interactiveChildren: element.interactiveChildren, hitArea: element.hitArea }; } function setMemoryContainer(element, memory) { memory.isRenderGroup && (element.isRenderGroup = memory.isRenderGroup); memory.blendMode && (element.blendMode = memory.blendMode); memory.tint && (element.tint = memory.tint); memory.alpha && (element.alpha = memory.alpha); memory.angle && (element.angle = memory.angle); memory.renderable && (element.renderable = memory.renderable); memory.rotation && (element.rotation = memory.rotation); if (memory.scale) { if (typeof memory.scale === "number") { element.scale.set(memory.scale, memory.scale); } else { element.scale.set(memory.scale.x, memory.scale.y); } } if (memory.pivot) { if (typeof memory.pivot === "number") { element.pivot.set(memory.pivot, memory.pivot); } else { element.pivot.set(memory.pivot.x, memory.pivot.y); } } memory.position && element.position.set(memory.position.x, memory.position.y); memory.skew && element.skew.set(memory.skew.x, memory.skew.y); memory.visible && (element.visible = memory.visible); memory.x && (element.x = memory.x); memory.y && (element.y = memory.y); memory.boundsArea && (element.boundsArea = memory.boundsArea); memory.cursor && (element.cursor = memory.cursor); memory.eventMode && (element.eventMode = memory.eventMode); memory.interactive && (element.interactive = memory.interactive); memory.interactiveChildren && (element.interactiveChildren = memory.interactiveChildren); memory.hitArea && (element.hitArea = memory.hitArea); memory.width && (element.width = memory.width); memory.height && (element.height = memory.height); } // src/classes/canvas/CanvasText.ts var CANVAS_TEXT_ID = "CanvasText"; var CanvasText = class extends Text { constructor(options) { super(options); this.pixivnId = CANVAS_TEXT_ID; this._onEvents = {}; this.pixivnId = this.constructor.prototype.pixivnId || CANVAS_TEXT_ID; } get memory() { return getMemoryText(this); } set memory(value) { setMemoryText(this, value); } get onEvents() { return this._onEvents; } /** * is same function as on(), but it keeps in memory the children. * @param event The event type, e.g., 'click', 'mousedown', 'mouseup', 'pointerdown', etc. * @param eventClass The class that extends CanvasEvent. * @returns * @example * ```typescript * \@eventDecorator() * export class EventTest extends CanvasEvent<CanvasText> { * override fn(event: CanvasEventNamesType, text: CanvasText): void { * if (event === 'pointerdown') { * text.scale.x *= 1.25; * text.scale.y *= 1.25; * } * } * } * ``` * * ```typescript * const text = new CanvasText(); * text.text = "Hello World" * * text.eventMode = 'static'; * text.cursor = 'pointer'; * text.onEvent('pointerdown', EventTest); * * GameWindowManager.addCanvasElement("text", text); * ``` */ onEvent(event, eventClass) { let id = eventClass.prototype.id; let instance = getEventInstanceById(id); this._onEvents[event] = id; if (instance) { super.on(event, () => { instance.fn(event, this); }); } return this; } /** * on() does not keep in memory the event class, use onEvent() instead * @deprecated * @private * @param event * @param fn * @param context */ on(event, fn, context) { return super.on(event, fn, context); } }; function getMemoryText(element) { let temp = getMemoryContainer(element); return __spreadProps(__spreadValues({}, temp), { pixivnId: element.pixivnId, anchor: { x: element.anchor.x, y: element.anchor.y }, text: element.text, resolution: element.resolution, style: getTextStyle(element.style), roundPixels: element.roundPixels, onEvents: element.onEvents }); } function setMemoryText(element, memory) { setMemoryContainer(element, memory); if (memory.anchor) { if (typeof memory.anchor === "number") { element.anchor.set(memory.anchor, memory.anchor); } else { element.anchor.set(memory.anchor.x, memory.anchor.y); } } memory.text && (element.text = memory.text); memory.resolution && (element.resolution = memory.resolution); memory.style && (element.style = memory.style); memory.roundPixels && (element.roundPixels = memory.roundPixels); for (let event in memory.onEvents) { let id = memory.onEvents[event]; let instance = getEventTypeById(id); if (instance) { element.onEvent(event, instance); } } } export { CANVAS_TEXT_ID, CanvasText as default, getMemoryText, setMemoryText }; //# sourceMappingURL=CanvasText.mjs.map //# sourceMappingURL=CanvasText.mjs.map