UNPKG

fivem-js

Version:

Javascript/Typescript wrapper for the FiveM natives

109 lines (108 loc) 4.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UIMenuListItem = void 0; const __1 = require("../../"); const enums_1 = require("../../../enums"); const utils_1 = require("../../../utils"); const _1 = require("./"); class UIMenuListItem extends _1.UIMenuItem { constructor(text, items, startIndex = 0, description, arrowOnlyOnSelected = true) { super(text, description); this.listChanged = new utils_1.LiteEvent(); this.listSelected = new utils_1.LiteEvent(); this.supportsRightBadge = false; this.supportsRightLabel = false; this._index = 0; this._items = []; this._leftArrow = new __1.Sprite('commonmenu', 'arrowleft', new utils_1.Point(), new utils_1.Size(30, 30)); this._rightArrow = new __1.Sprite('commonmenu', 'arrowright', new utils_1.Point(), new utils_1.Size(30, 30)); this._itemText = new __1.Text('', new utils_1.Point(), 0.35, utils_1.Color.white, enums_1.Font.ChaletLondon, enums_1.Alignment.Right); this.ArrowOnlyOnSelected = arrowOnlyOnSelected; this.Items = items; this.Index = startIndex; } get Items() { return this._items; } set Items(value) { if (!value) { throw new Error("Items can't be null"); } this._items = value; } get SelectedItem() { return this.Items[this.Index]; } set SelectedItem(value) { const index = this.Items.findIndex(i => i.id === value.id); if (index >= 0) { this.Index = index; } } get SelectedValue() { const item = this.SelectedItem; return item ? item.value : null; } get Index() { return this._index % this.Items.length; } set Index(value) { if (!this._items.length) { return; } value = value < 0 ? this._items.length - 1 : value > this._items.length - 1 ? 0 : value; this._index = value; delete this._textWidth; } get ArrowOnlyOnSelected() { return this._arrowOnlyOnSelected; } set ArrowOnlyOnSelected(value) { this._arrowOnlyOnSelected = value; } get IsMouseInBoundsOfLeftArrow() { return this.parent.isMouseInBounds(this._leftArrow.pos, this._leftArrow.size); } get IsMouseInBoundsOfRightArrow() { return this.parent.isMouseInBounds(this._rightArrow.pos, this._rightArrow.size); } setVerticalPosition(y) { const yOffset = y + this.offset.Y + 147; this._leftArrow.pos.Y = yOffset; this._rightArrow.pos.Y = yOffset; this._itemText.pos.Y = yOffset; super.setVerticalPosition(y); } draw() { super.draw(); if (this._textWidth === undefined) { const caption = this._getSelectedItemCaption(); this._itemText.caption = caption; this._textWidth = utils_1.measureString(caption, this._itemText.font, this._itemText.scale, __1.Menu.screenWidth); } this._rightArrow.pos.X = this.offset.X + this.parent.WidthOffset + 400; this._itemText.pos.X = this._rightArrow.pos.X + 5; this._itemText.color = this.enabled ? this.selected ? this.HighlightedForeColor : this.ForeColor : new utils_1.Color(255, 163, 159, 148); if (this._arrowOnlyOnSelected && !this.selected) { this._itemText.pos.X += this._rightArrow.size.width / 2; } else { this._leftArrow.color = this._itemText.color; this._rightArrow.color = this._itemText.color; this._leftArrow.pos.X = this._itemText.pos.X - this._textWidth - this._leftArrow.size.width + 5; this._leftArrow.draw(__1.Menu.screenResolution); this._rightArrow.draw(__1.Menu.screenResolution); } this._itemText.draw(undefined, __1.Menu.screenResolution); } _getSelectedItemCaption() { const item = this.SelectedItem; return item ? item.name : ''; } } exports.UIMenuListItem = UIMenuListItem;