fivem-js
Version:
Javascript/Typescript wrapper for the FiveM natives
220 lines (219 loc) • 9.45 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIMenuGridPanel = void 0;
const utils_1 = require("../../../../utils");
const _1 = require("./");
const __1 = require("../../../");
const enums_1 = require("../../../../enums");
const Game_1 = require("../../../../Game");
class UIMenuGridPanel extends _1.AbstractUIMenuPanel {
constructor(topText, leftText, rightText, bottomText, circlePosition) {
super();
this._isCircleLocked = false;
this._pressed = false;
this._lockXAxis = false;
this._lockYAxis = false;
this._setCirclePosition = circlePosition || new utils_1.Point(0.5, 0.5);
this.background = new __1.Sprite('commonmenu', 'gradient_bgd', new utils_1.Point(), new utils_1.Size(431, 275));
this._grid = new __1.Sprite('pause_menu_pages_char_mom_dad', 'nose_grid', new utils_1.Point(), new utils_1.Size(200, 200));
this._circle = new __1.Sprite('mpinventory', 'in_world_circle', new utils_1.Point(), new utils_1.Size(20, 20));
this.TopText = topText;
this.LeftText = leftText;
this.RightText = rightText;
this.BottomText = bottomText;
}
get TopText() {
return this._topText ? this._topText.caption : null;
}
set TopText(value) {
this._setText('_topText', value);
}
get LeftText() {
return this._leftText ? this._leftText.caption : null;
}
set LeftText(value) {
this._setText('_leftText', value);
}
get RightText() {
return this._rightText ? this._rightText.caption : null;
}
set RightText(value) {
this._setText('_rightText', value);
}
get BottomText() {
return this._bottomText ? this._bottomText.caption : null;
}
set BottomText(value) {
this._setText('_bottomText', value);
}
get CirclePosition() {
return new utils_1.Point(Math.round(((this._circle.pos.X - (this._grid.pos.X + 20) + this._circle.size.width / 2) /
(this._grid.size.width - 40)) *
100) / 100, Math.round(((this._circle.pos.Y - (this._grid.pos.Y + 20) + this._circle.size.height / 2) /
(this._grid.size.height - 40)) *
100) / 100);
}
set CirclePosition(position) {
this.CirclePositionX = position.X;
this.CirclePositionY = position.Y;
}
set CirclePositionX(x) {
if (this._isCircleLocked && this._lockXAxis) {
return;
}
x = x >= 0 && x <= 1 ? x : 0;
this._setCirclePosition.X = x;
this._circle.pos.X =
this._grid.pos.X + 20 + (this._grid.size.width - 40) * x - this._circle.size.width / 2;
}
set CirclePositionY(y) {
if (this._isCircleLocked && this._lockYAxis) {
return;
}
y = y >= 0 && y <= 1 ? y : 0;
this._setCirclePosition.Y = y;
this._circle.pos.Y =
this._grid.pos.Y + 20 + (this._grid.size.height - 40) * y - this._circle.size.height / 2;
}
get LockXAxis() {
return this._lockXAxis;
}
set LockXAxis(value) {
this._lockXAxis = value;
if (value) {
if (this._lockYAxis) {
this._lockYAxis = false;
}
this.CirclePositionX = 0.5;
}
}
get LockYAxis() {
return this._lockYAxis;
}
set LockYAxis(value) {
this._lockYAxis = value;
if (value) {
if (this._lockXAxis) {
this._lockXAxis = false;
}
this.CirclePositionY = 0.5;
}
}
updateParentItem() {
const last = this._lastCirclePosition;
const current = this.CirclePosition;
if (!last || last.X !== current.X || last.Y !== current.Y) {
this._lastCirclePosition = current;
this.ParentMenu.panelActivated.emit(this.parentItem, this, current);
this.parentItem.panelActivated.emit(this, current);
}
}
setVerticalPosition(y) {
super.setVerticalPosition(y);
this._grid.pos.Y = y + 37.5;
if (this._topText) {
this._topText.pos.Y = y + 5;
}
if (this._leftText) {
this._leftText.pos.Y = y + 120;
}
if (this._rightText) {
this._rightText.pos.Y = y + 120;
}
if (this._bottomText) {
this._bottomText.pos.Y = y + 240;
}
}
draw() {
if (this.enabled) {
super.draw();
const x = this.parentItem.offset.X + this.ParentMenu.WidthOffset / 2;
this._grid.pos.X = x + 115.5;
if (!this._isCircleLocked) {
this.CirclePosition = this._setCirclePosition;
this._isCircleLocked = true;
}
this._grid.draw(__1.Menu.screenResolution);
this._circle.draw(__1.Menu.screenResolution);
if (this._topText) {
this._topText.pos.X = x + 215.5;
this._topText.draw(undefined, __1.Menu.screenResolution);
}
if (this._leftText) {
this._leftText.pos.X = x + 57.75;
this._leftText.draw(undefined, __1.Menu.screenResolution);
}
if (this._rightText) {
this._rightText.pos.X = x + 373.25;
this._rightText.draw(undefined, __1.Menu.screenResolution);
}
if (this._bottomText) {
this._bottomText.pos.X = x + 215.5;
this._bottomText.draw(undefined, __1.Menu.screenResolution);
}
this._processControls();
}
}
_setText(name, value) {
if (value && value.trim() !== '') {
if (!this[name]) {
this[name] = new __1.Text(value, new utils_1.Point(), 0.35, utils_1.Color.white, enums_1.Font.ChaletLondon, enums_1.Alignment.Centered);
}
else {
this[name].caption = value;
}
}
else if (this[name]) {
delete this[name];
}
}
_processControls() {
if (!this._pressed &&
Game_1.Game.isDisabledControlJustPressed(0, enums_1.Control.Attack) &&
this.ParentMenu.isMouseInBounds(this._grid.pos, this._grid.size)) {
this._pressed = true;
(() => __awaiter(this, void 0, void 0, function* () {
const drawOffset = this.ParentMenu.DrawOffset;
while (Game_1.Game.isDisabledControlPressed(0, enums_1.Control.Attack)) {
yield new Promise(resolve => setTimeout(resolve, 0));
let cX = (GetControlNormal(0, enums_1.Control.CursorX) - drawOffset.X) * __1.Menu.screenWidth;
let cY = (GetControlNormal(0, enums_1.Control.CursorY) - drawOffset.Y) * __1.Menu.screenHeight;
cX -= this._circle.size.width / 2;
cY -= this._circle.size.height / 2;
this._circle.pos.X =
cX > this._grid.pos.X + 10 + this._grid.size.width - (this._lockXAxis ? 120 : 40)
? this._grid.pos.X + 10 + this._grid.size.width - (this._lockXAxis ? 120 : 40)
: cX < this._grid.pos.X + (this._lockXAxis ? 100 : 20) - this._circle.size.width / 2
? this._grid.pos.X + (this._lockXAxis ? 100 : 20) - this._circle.size.width / 2
: cX;
this._circle.pos.Y =
cY > this._grid.pos.Y + 10 + this._grid.size.height - (this._lockYAxis ? 120 : 40)
? this._grid.pos.Y + 10 + this._grid.size.height - (this._lockYAxis ? 120 : 40)
: cY < this._grid.pos.Y + (this._lockYAxis ? 100 : 20) - this._circle.size.height / 2
? this._grid.pos.Y + (this._lockYAxis ? 100 : 20) - this._circle.size.height / 2
: cY;
}
this.updateParentItem();
this._pressed = false;
}))();
const interval = setInterval(() => __awaiter(this, void 0, void 0, function* () {
if (Game_1.Game.isDisabledControlPressed(0, enums_1.Control.Attack)) {
this.updateParentItem();
}
else {
clearInterval(interval);
}
}), 75);
}
}
}
exports.UIMenuGridPanel = UIMenuGridPanel;