Home Reference Source

vgui/15-source-event.js

/**
 * Base event for Source Engine events
 */
export class SourceEvent extends Event {
	constructor(type, data = {}) {
		if (!type)
			throw new Error("Need event type parameter")
		super();
		if (!(data instanceof KeyValues))
			data = KeyValues.fromObject(data)
		this.data = data;
		this._type = type;
	}

	/**
	 * @override
	 */
	get type() {
		if (this._type)
			return this._type + "Event"
		return "SourceEvent";
	}

	/**
	 * Wether this event should be thrown back to the source engine after passing through here
	 */
	get canEscape() {
		return false;
	}

	/**
	 * Creates a more specific event type if one exists.
	 * Defaults to SourceEvent otherwise.
	 */
	static __createSpecific(type, data) {
		const classType = SourceEvent.__eventTypes[type];
		if (classType)
			return new classType.__fromSource(type, data)
		console.info("Received event type " + type + " but have no corresponding class. Defaulting to SourceEvent")
		return new SourceEvent(type, data);
	}
}

SourceEvent.__eventTypes = {}