lfs-akairo
Version:
LFS Akairo is a framework designed to simplify the creation of InSim applications.
314 lines (313 loc) • 9.22 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var button_exports = {};
__export(button_exports, {
Button: () => Button
});
module.exports = __toCommonJS(button_exports);
var import_packets = require("node-insim/packets");
const _Button = class _Button {
constructor(player) {
this.player = player;
/** Array of child buttons */
this.childs = [];
this.resetProperties();
}
/**
* Sets the button's id
* @param id Button id
*/
setId(id) {
this.id = id;
return this.queueUpdate();
}
/**
* Sets the button's style
* @param style Function that returns the button style
*/
setStyle(style) {
this.style = style;
return this.queueUpdate();
}
/**
* Sets the button's title
* @param title Function that returns the title string
*/
setTitle(title) {
this.title = title;
return this.queueUpdate();
}
/**
* Sets the button's textbox title
* @param caption Function that returns the textbox title string
*/
setCaption(caption) {
this.caption = caption;
return this.queueUpdate();
}
/**
* Sets the input field length
* @param length Function that returns the length value
*/
setLength(length) {
this.length = length;
return this.queueUpdate();
}
/**
* Sets the button's width
* @param width Function that returns the width value
*/
setWidth(width) {
this.width = width;
return this.queueUpdate();
}
/**
* Sets the button's height
* @param height Function that returns the height value
*/
setHeight(height) {
this.height = height;
return this.queueUpdate();
}
/**
* Sets the button's left position
* @param left Function that returns the left position value
*/
setLeft(left) {
this.left = left;
return this.queueUpdate();
}
/**
* Sets the button's top position
* @param top Function that returns the top position value
*/
setTop(top) {
this.top = top;
return this.queueUpdate();
}
/**
* Sets whether the button should only be clicked once
* @param clickOnce Function that returns the clickOnce boolean
*/
setClickOnce(clickOnce) {
this.clickOnce = clickOnce;
return this.queueUpdate();
}
/**
* Sets the button's visibility
* @param isVisible Function that returns the visibility boolean
*/
setIsVisible(isVisible) {
this.isVisible = isVisible;
return this.queueUpdate();
}
/**
* Adds a click event handler to the button
* @param callback Function to be called when the button is clicked
*/
onClick(callback) {
var _a;
const type = ((_a = this.caption()) != null ? _a : this.length()) ? import_packets.PacketType.ISP_BTT : import_packets.PacketType.ISP_BTC;
const bind = (packet) => {
if (packet.ClickID === this.id() && packet.UCID === this.player.uniqueId) {
const text = packet instanceof import_packets.IS_BTT ? packet.Text : "";
if (this.clickOnce()) {
unbind();
}
callback({ text, button: this, unbind });
}
};
const unbind = () => {
this.player.akairo.insim.removeListener(type, bind);
this.unbind = void 0;
};
this.unbind = unbind;
this.player.akairo.insim.addListener(type, bind);
return this;
}
/**
* Appends a child button to this button
* @param callback A single button callback (already instantiated)
* @param disableAutoUpdate Whether to disable automatic state updates
*/
append(callback, disableAutoUpdate) {
const button = this.player.button();
button.style = this.style;
button.isVisible = this.isVisible;
button.parent = this;
button.create(disableAutoUpdate);
this.childs.push(button);
callback(button);
return this;
}
/**
* Removes a child button
* @param button The button to remove
*/
remove(button) {
const index = this.childs.findIndex((c) => c.id() === button.id());
if (index !== -1) {
this.childs[index].destroy();
this.childs.splice(index, 1);
}
return this;
}
/**
* Creates the button and starts auto-update if enabled
* @param disableAutoUpdate Whether to disable automatic state updates
*/
create(disableAutoUpdate) {
if (typeof this.id() === "undefined") return null;
if (!disableAutoUpdate) {
this.manageAutoUpdate();
}
this.update();
return this;
}
/**
* Updates the button's state and appearance
*/
update() {
var _a, _b;
if (typeof this.id() === "undefined" || typeof this.player === "undefined" || this.width() <= 0 || this.height() <= 0) {
return this;
}
this.player.akairo.insim.send(
new import_packets.IS_BTN({
ClickID: Math.min(Math.max(this.id(), 0), 239),
BStyle: this.isVisible() ? this.style() : import_packets.ButtonStyle.ISB_LEFT,
Text: `\0${(_a = this.caption()) != null ? _a : "\b"}\0${this.isVisible() ? this.title() : ""}`,
TypeIn: Math.min(Math.max(this.length(), 0), 255),
W: Math.min(this.width(), 200),
H: Math.min(this.height(), 200),
L: Math.min(this.left(), 200),
T: Math.min(this.top(), 200),
UCID: this.player ? this.player.uniqueId : 255,
ReqI: 2
})
);
(_b = this.onUpdateHandler) == null ? void 0 : _b.call(this, this);
this.updateChildren();
return this;
}
/**
* Destroys the button, removing it from display
*/
destroy() {
if (typeof this.id() === "undefined") {
return this;
}
if (this.player.buttons.has(this.id())) {
this.player.buttons.delete(this.id());
}
this.destroyAllChildren();
this.player.akairo.insim.send(
new import_packets.IS_BFN({
UCID: this.player.uniqueId,
ClickID: Math.min(Math.max(this.id(), 0), 239)
})
);
this.cleanupProperties();
return this;
}
/**
* Sets a handler for update events
* @param callback Function to be called when the button is updated
*/
onUpdate(callback) {
this.onUpdateHandler = callback;
}
manageAutoUpdate() {
var _a, _b;
let buttons = _Button.autoUpdateButtons.get(this.player.akairo);
if (!buttons) {
buttons = /* @__PURE__ */ new Set();
_Button.autoUpdateButtons.set(this.player.akairo, buttons);
}
buttons.add(this);
if (!_Button.autoUpdateInterval.has(this.player.akairo)) {
const interval = setInterval(() => {
const currentButtons = _Button.autoUpdateButtons.get(this.player.akairo);
if (!currentButtons || currentButtons.size === 0) {
clearInterval(interval);
_Button.autoUpdateInterval.delete(this.player.akairo);
return;
}
currentButtons.forEach((button) => button.update());
}, (_b = (_a = this.player.akairo.settings) == null ? void 0 : _a.interface) != null ? _b : 1e3);
_Button.autoUpdateInterval.set(this.player.akairo, interval);
}
}
queueUpdate() {
var _a;
if (!((_a = _Button.autoUpdateButtons.get(this.player.akairo)) == null ? void 0 : _a.has(this))) {
this.update();
}
return this;
}
updateChildren() {
for (const child of this.childs) {
child.update();
}
}
destroyAllChildren() {
for (const child of this.childs) {
child.destroy();
}
}
cleanupProperties() {
var _a;
const buttons = _Button.autoUpdateButtons.get(this.player.akairo);
if (buttons) {
buttons.delete(this);
if (buttons.size === 0) {
const interval = _Button.autoUpdateInterval.get(this.player.akairo);
if (interval) {
clearInterval(interval);
_Button.autoUpdateInterval.delete(this.player.akairo);
}
}
}
(_a = this.unbind) == null ? void 0 : _a.call(this);
this.resetProperties();
for (const child of this.childs) {
child.destroy();
}
}
resetProperties() {
this.id = () => void 0;
this.style = () => void 0;
this.title = () => "";
this.caption = () => void 0;
this.length = () => 0;
this.width = () => 0;
this.height = () => 0;
this.left = () => 0;
this.top = () => 0;
this.clickOnce = () => false;
this.isVisible = () => true;
}
};
_Button.autoUpdateButtons = /* @__PURE__ */ new WeakMap();
_Button.autoUpdateInterval = /* @__PURE__ */ new WeakMap();
let Button = _Button;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Button
});