Home Reference Source

vgui/10-node.js

/** Represents a placeholder for a more specific Node type
 * Should not be used directly
 */
export class Node {
	/**
	 * Creates a Node with a given VPANEL id
	 */
	constructor(id) {
		if (!id)
			throw new Error("Invalid Panel id");
		this._id = id;

		if (Node._cache == undefined)
			Node._cache = {};
	}

	/**
	 * Returns a Node that is the parent of this node. If there is no such node, like if this node is the top of the tree or if doesn't participate in a tree, this property returns null.
	 */
	get parentNode() {
		var id = __vgui_GetParent(this._id);
		if (id === 0)
			return null;
		var node = new Node(id);
		return node._specialize();
	}

	/**
	 * @access protected
	 * Returns the name of the C++ class that implements this control
	 */
	get _className() {
		return __vgui_GetType(this._id)
	}

	/**
	 * Returns the children of this node
	 */
	get childNodes() {
		var noChildren = __vgui_GetChildCount(this._id);
		var result = [];
		for(var i = 0; i < noChildren; i++) {
			var id = __vgui_GetNthChild(this._id, i);
			if (id == 0)
				continue;

			result.push(new Node(id)._specialize());
		}
		return result;
	}

	/**
	 * Returns a string representation of the Node
	 */
	toString() {
		return this._className;
	}

	/**
	 * Return a specialized version of this node based on Panel type
	 * Tries to return the same object given the same id
	 * Does not support ingame panel deletion (might cause duplicate ids)
	 * @access private
	 */
	_specialize() {
		if (Node._cache[this._id] != undefined)
			return Node._cache[this._id];
		var result = this;
		switch (this._className) {
			case "Label":
				result = new Label(this._id);
				break;
			case "CVguiJavascriptContext":
				result = new JsChild(this._id);
				break;
			case "Panel":
				result = new Panel(this._id);
				break;
			case "EditablePanel":
				result = new EditablePanel(this._id);
				break;
			case "CEmpVguiSteamUser":
				result = new SteamUser(this._id);
				break;
			case "Button":
				result = new Button(this._id);
				break;
			case "CAvatarImagePanel":
				result = new CAvatarImage(this._id);
				break;
			case "ImagePanel":
				result = new ImagePanel(this._id);
				break;
			default:
				result = new Element(this._id);
				console.log("Unknown element class " + this._className + " found. Assuming Element");
				break;
		}

		Node._cache[this._id] = result;
		return result;
	}

	_getAttrInt(key) {
		return __vgui_GetSettingInt(this._id, key);
	}
	_getAttrString(key) {
		return __vgui_GetSettingString(this._id, key);
	}
	_getAttrFloat(key) {
		return __vgui_GetSettingFloat(this._id, key);
	}

	_setAttrInt(key, value) {
		__vgui_SetSettingInt(this._id, key, value);
	}
	_setAttrString(key, value) {
		__vgui_SetSettingString(this._id, key, value);
	}
	_setAttrFloat(key, value) {
		__vgui_SetSettingFloat(this._id, key, value);
	}
}