diff --git a/autoexec.lua b/_autoexec.lua similarity index 100% rename from autoexec.lua rename to _autoexec.lua diff --git a/_test.lua b/_test.lua new file mode 100644 index 0000000..6a19d34 --- /dev/null +++ b/_test.lua @@ -0,0 +1,8 @@ +local rootPath = utils.get_appdata_path("PopstarDevs", "2Take1Menu") .. "\\scripts\\" + +function load(file) + local chunk, status = loadfile(rootPath .. file) + if chunk then chunk() end +end + +load("ts-lua\\dist\\test.lua") \ No newline at end of file diff --git a/test.lua b/test.lua deleted file mode 100644 index df4d60f..0000000 --- a/test.lua +++ /dev/null @@ -1,22 +0,0 @@ ---[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] -fps = 60 -duration = 5 -menu.add_feature( - "test", - "action", - 0, - function() - do - local i = 0 - while i < (fps * duration) do - scriptdraw.draw_rect( - v2(0, 0), - v2(1, 1), - 4294967295 - ) - system.wait(0) - i = i + 1 - end - end - end -) diff --git a/ts-lua/dist/test.lua b/ts-lua/dist/test.lua index df4d60f..88ede4f 100644 --- a/ts-lua/dist/test.lua +++ b/ts-lua/dist/test.lua @@ -1,6 +1,338 @@ --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] -fps = 60 -duration = 5 + +local ____modules = {} +local ____moduleCache = {} +local ____originalRequire = require +local function require(file) + if ____moduleCache[file] then + return ____moduleCache[file] + end + if ____modules[file] then + ____moduleCache[file] = ____modules[file]() + return ____moduleCache[file] + else + if ____originalRequire then + return ____originalRequire(file) + else + error("module '" .. file .. "' not found") + end + end +end +____modules = { +["index"] = function() --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] + end, +["classes.Feature"] = function() --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +-- Lua Library inline imports +function __TS__Class(self) + local c = {prototype = {}} + c.prototype.__index = c.prototype + c.prototype.constructor = c + return c +end + +function __TS__ArrayPush(arr, ...) + local items = {...} + for ____, item in ipairs(items) do + arr[#arr + 1] = item + end + return #arr +end + +function __TS__ArraySplice(list, ...) + local len = #list + local actualArgumentCount = select("#", ...) + local start = select(1, ...) + local deleteCount = select(2, ...) + local actualStart + if start < 0 then + actualStart = math.max(len + start, 0) + else + actualStart = math.min(start, len) + end + local itemCount = math.max(actualArgumentCount - 2, 0) + local actualDeleteCount + if actualArgumentCount == 0 then + actualDeleteCount = 0 + elseif actualArgumentCount == 1 then + actualDeleteCount = len - actualStart + else + actualDeleteCount = math.min( + math.max(deleteCount or 0, 0), + len - actualStart + ) + end + local out = {} + do + local k = 0 + while k < actualDeleteCount do + local from = actualStart + k + if list[from + 1] then + out[k + 1] = list[from + 1] + end + k = k + 1 + end + end + if itemCount < actualDeleteCount then + do + local k = actualStart + while k < (len - actualDeleteCount) do + local from = k + actualDeleteCount + local to = k + itemCount + if list[from + 1] then + list[to + 1] = list[from + 1] + else + list[to + 1] = nil + end + k = k + 1 + end + end + do + local k = len + while k > ((len - actualDeleteCount) + itemCount) do + list[k] = nil + k = k - 1 + end + end + elseif itemCount > actualDeleteCount then + do + local k = len - actualDeleteCount + while k > actualStart do + local from = (k + actualDeleteCount) - 1 + local to = (k + itemCount) - 1 + if list[from + 1] then + list[to + 1] = list[from + 1] + else + list[to + 1] = nil + end + k = k - 1 + end + end + end + local j = actualStart + for i = 3, actualArgumentCount do + list[j + 1] = select(i, ...) + j = j + 1 + end + do + local k = #list - 1 + while k >= ((len - actualDeleteCount) + itemCount) do + list[k + 1] = nil + k = k - 1 + end + end + return out +end + +function __TS__ClassExtends(target, base) + target.____super = base + local staticMetatable = setmetatable({__index = base}, base) + setmetatable(target, staticMetatable) + local baseMetatable = getmetatable(base) + if baseMetatable then + if type(baseMetatable.__index) == "function" then + staticMetatable.__index = baseMetatable.__index + end + if type(baseMetatable.__newindex) == "function" then + staticMetatable.__newindex = baseMetatable.__newindex + end + end + setmetatable(target.prototype, base.prototype) + if type(base.prototype.__index) == "function" then + target.prototype.__index = base.prototype.__index + end + if type(base.prototype.__newindex) == "function" then + target.prototype.__newindex = base.prototype.__newindex + end + if type(base.prototype.__tostring) == "function" then + target.prototype.__tostring = base.prototype.__tostring + end +end + +local ____exports = {} +____exports.FeatureParent = __TS__Class() +local FeatureParent = ____exports.FeatureParent +FeatureParent.name = "FeatureParent" +function FeatureParent.prototype.____constructor(self, name, parent) + self.parentid = 0 + self.children = {} + self.name = name + if parent then + self.parent = parent + self.parentid = parent.id + self.parent:addChild(self) + end + print( + self:type() + ) + self.feat = menu.add_feature( + name, + self:type(), + self.parentid, + function() return self:select() end + ) + self.id = self.feat.id +end +function FeatureParent.prototype.type(self) + return "parent" +end +function FeatureParent.prototype.hidden(self) + return Boolean(nil, self.feat.hidden) +end +function FeatureParent.prototype.sethidden(self, val) + self.feat.hidden = val +end +function FeatureParent.prototype.select(self) +end +function FeatureParent.prototype.addChild(self, feat) + do + local i = 0 + while i < #self.children do + if self.children[i + 1]:equals(feat) then + return + end + i = i + 1 + end + end + __TS__ArrayPush(self.children, feat) +end +function FeatureParent.prototype.removeChild(self, feat) + do + local i = 0 + while i < #self.children do + if self.children[i + 1]:equals(feat) then + __TS__ArraySplice(self.children, i, 1) + end + i = i + 1 + end + end +end +function FeatureParent.prototype.equals(self, feat) + return self.id == feat.id +end +____exports.FeatureAction = __TS__Class() +local FeatureAction = ____exports.FeatureAction +FeatureAction.name = "FeatureAction" +__TS__ClassExtends(FeatureAction, ____exports.FeatureParent) +function FeatureAction.prototype.____constructor(self, name, parent, handler) + FeatureAction.____super.prototype.____constructor(self, name, parent) + if handler then + self.handler = handler + end +end +function FeatureAction.prototype.type(self) + return "action" +end +function FeatureAction.prototype.select(self) + if self.handler then + self:handler(self) + end +end +function FeatureAction.prototype.setHandler(self, handler) + self.handler = handler +end +____exports.FeatureToggle = __TS__Class() +local FeatureToggle = ____exports.FeatureToggle +FeatureToggle.name = "FeatureToggle" +__TS__ClassExtends(FeatureToggle, ____exports.FeatureAction) +function FeatureToggle.prototype.____constructor(self, name, parent, defaultvalue, handler) + if defaultvalue == nil then + defaultvalue = false + end + FeatureToggle.____super.prototype.____constructor(self, name, parent, handler) + self:setvalue(defaultvalue) +end +function FeatureToggle.prototype.type(self) + return "toggle" +end +function FeatureToggle.prototype.value(self) + return self.feat.on +end +function FeatureToggle.prototype.setvalue(self, val) + self.feat.on = val +end +function FeatureToggle.prototype.toggle(self) + self.feat.on = not self.feat.on +end +____exports.FeatureNum = __TS__Class() +local FeatureNum = ____exports.FeatureNum +FeatureNum.name = "FeatureNum" +__TS__ClassExtends(FeatureNum, ____exports.FeatureAction) +function FeatureNum.prototype.____constructor(self, name, parent, defaultnum, handler) + if defaultnum == nil then + defaultnum = 0 + end + FeatureNum.____super.prototype.____constructor(self, name, parent, handler) + self:setnum(defaultnum) +end +function FeatureNum.prototype.type(self) + return "action_value_i" +end +function FeatureNum.prototype.num(self) + return self.feat.value_i +end +function FeatureNum.prototype.setnum(self, val) + self.feat.value_i = val +end +function FeatureNum.prototype.min(self) + return self.feat.min_i +end +function FeatureNum.prototype.setmin(self, val) + self.feat.min_i = math.floor(val + 0.5) +end +function FeatureNum.prototype.max(self) + return self.feat.max_i +end +function FeatureNum.prototype.setmax(self, val) + self.feat.max_i = math.floor(val + 0.5) +end +function FeatureNum.prototype.step(self) + return self.feat.mod_i +end +function FeatureNum.prototype.setstep(self, val) + self.feat.mod_i = math.floor(val + 0.5) +end +____exports.FeatureNumToggle = __TS__Class() +local FeatureNumToggle = ____exports.FeatureNumToggle +FeatureNumToggle.name = "FeatureNumToggle" +__TS__ClassExtends(FeatureNumToggle, ____exports.FeatureNum) +function FeatureNumToggle.prototype.____constructor(self, name, parent, defaultnum, defaultvalue, handler) + if defaultnum == nil then + defaultnum = 0 + end + if defaultvalue == nil then + defaultvalue = false + end + FeatureNumToggle.____super.prototype.____constructor(self, name, parent, defaultnum, handler) + self:setvalue(defaultvalue) +end +function FeatureNumToggle.prototype.type(self) + return "value_i" +end +function FeatureNumToggle.prototype.value(self) + return self.feat.on +end +function FeatureNumToggle.prototype.setvalue(self, val) + self.feat.on = val +end +function FeatureNumToggle.prototype.toggle(self) + self.feat.on = not self.feat.on +end +return ____exports + end, +["test"] = function() --[[ Generated with https://github.com/TypeScriptToLua/TypeScriptToLua ]] +-- Lua Library inline imports +function __TS__New(target, ...) + local instance = setmetatable({}, target.prototype) + instance:____constructor(...) + return instance +end + +local ____exports = {} +local ____Feature = require("classes.Feature") +local FeatureAction = ____Feature.FeatureAction +local FeatureParent = ____Feature.FeatureParent +local fps = 60 +local duration = 5 menu.add_feature( "test", "action", @@ -20,3 +352,32 @@ menu.add_feature( end end ) +local testParent = __TS__New(FeatureParent, "class") +__TS__New( + FeatureAction, + "testClass", + testParent, + function(____, f) + if f.parent then + print( + (("parent: " .. f.parent.name) .. "\nid: ") .. tostring(f.parent.id) + ) + end + do + local i = 0 + while i < (fps * duration) do + scriptdraw.draw_rect( + v2(0, 0), + v2(1, 1), + 4294967295 + ) + system.wait(0) + i = i + 1 + end + end + end +) +return ____exports + end, +} +return require("test") diff --git a/ts-lua/package-lock.json b/ts-lua/package-lock.json index bfbaa54..d6a27f1 100644 --- a/ts-lua/package-lock.json +++ b/ts-lua/package-lock.json @@ -124,7 +124,7 @@ }, "node_modules/types-ts-2take1": { "version": "1.0.0", - "resolved": "git+https://git@git.zomo.dev/zomo/types-ts-2take1#39850d8660e101c26dd793f7464340bcfd4ed723", + "resolved": "git+https://git@git.zomo.dev/zomo/types-ts-2take1#dc5f38b2321b7c6d8adecf8c037c7a5650d48a1d", "dev": true, "license": "UNLICENSED" }, @@ -252,7 +252,7 @@ "dev": true }, "types-ts-2take1": { - "version": "git+https://git@git.zomo.dev/zomo/types-ts-2take1#39850d8660e101c26dd793f7464340bcfd4ed723", + "version": "git+https://git@git.zomo.dev/zomo/types-ts-2take1#dc5f38b2321b7c6d8adecf8c037c7a5650d48a1d", "dev": true, "from": "types-ts-2take1@git+https://git@git.zomo.dev/zomo/types-ts-2take1" }, diff --git a/ts-lua/package.json b/ts-lua/package.json index fc741e2..d78a586 100644 --- a/ts-lua/package.json +++ b/ts-lua/package.json @@ -2,7 +2,7 @@ "private": true, "scripts": { "build": "tstl && $npm_package_scripts_postbuild", - "postbuild": "cp dist/test.lua ../test.lua", + "postbuild": "cp ./dist/test.lua ../", "dev": "tstl --watch" }, "devDependencies": { diff --git a/ts-lua/src/classes/Feature.ts b/ts-lua/src/classes/Feature.ts new file mode 100644 index 0000000..ab3d8a4 --- /dev/null +++ b/ts-lua/src/classes/Feature.ts @@ -0,0 +1,164 @@ +type AnyFeature = FeatureParent|FeatureAction|FeatureToggle|FeatureNum; + +export class FeatureParent { + + type(): menu.featureTypes { return "parent"; } + + name: string; + id: number; + parent?: FeatureParent|null; + parentid: number = 0; + children: AnyFeature[] = []; + + feat: Feat; + + hidden(): boolean { + return Boolean(this.feat.hidden); + } + sethidden(val: boolean) { + this.feat.hidden = val; + } + + select() {} + + constructor(name: string, parent?: FeatureParent|null) { + this.name = name; + + if (parent) { + this.parent = parent; + this.parentid = parent.id; + this.parent.addChild(this); + } + + print(this.type()); + this.feat = menu.add_feature(name, this.type(), this.parentid, () => this.select()); + this.id = this.feat.id; + } + + /* children */ + + addChild(feat: AnyFeature) { + for (let i = 0; i < this.children.length; i++) + if (this.children[i].equals(feat)) + return; + this.children.push(feat); + } + + removeChild(feat: AnyFeature) { + for (let i = 0; i < this.children.length; i++) + if (this.children[i].equals(feat)) + this.children.splice(i, 1); + } + + /* util */ + + equals(feat: AnyFeature): boolean { + return this.id === feat.id; + } + +} + +export class FeatureAction extends FeatureParent { + + type(): menu.featureTypes { return "action"; } + + handler?: (feat: this) => void; + + select() { + if (this.handler) + this.handler(this); + } + + constructor(name: string, parent?: FeatureParent|null, handler?: (feat: AnyFeature) => void) { + super(name, parent); + + if (handler) + this.handler = handler; + } + + setHandler(handler: (feat: AnyFeature) => void) { + this.handler = handler; + } + +} + +export class FeatureToggle extends FeatureAction { + + type(): menu.featureTypes { return "toggle"; } + + value(): boolean { + return this.feat.on; + } + setvalue(val: boolean) { + this.feat.on = val; + } + toggle() { + this.feat.on = !this.feat.on; + } + + constructor(name: string, parent?: FeatureParent|null, defaultvalue = false, handler?: (feat: AnyFeature) => void) { + super(name, parent, handler); + this.setvalue(defaultvalue); + } + +} + +export class FeatureNum extends FeatureAction { + + type(): menu.featureTypes { return "action_value_i"; } + + num(): number { + return this.feat.value_i; + } + setnum(val: number) { + this.feat.value_i = val; + } + + min(): number { + return this.feat.min_i; + } + setmin(val: number) { + this.feat.min_i = Math.round(val); + } + + max(): number { + return this.feat.max_i; + } + setmax(val: number) { + this.feat.max_i = Math.round(val); + } + + step(): number { + return this.feat.mod_i; + } + setstep(val: number) { + this.feat.mod_i = Math.round(val); + } + + constructor(name: string, parent?: FeatureParent|null, defaultnum = 0, handler?: (feat: AnyFeature) => void) { + super(name, parent, handler); + this.setnum(defaultnum); + } + +} + +export class FeatureNumToggle extends FeatureNum { + + type(): menu.featureTypes { return "value_i"; } + + value(): boolean { + return this.feat.on; + } + setvalue(val: boolean) { + this.feat.on = val; + } + toggle() { + this.feat.on = !this.feat.on; + } + + constructor(name: string, parent?: FeatureParent|null, defaultnum = 0, defaultvalue = false, handler?: (feat: AnyFeature) => void) { + super(name, parent, defaultnum, handler); + this.setvalue(defaultvalue); + } + +} diff --git a/ts-lua/src/index.ts b/ts-lua/src/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/ts-lua/src/test.ts b/ts-lua/src/test.ts index a1388c6..918095c 100644 --- a/ts-lua/src/test.ts +++ b/ts-lua/src/test.ts @@ -1,6 +1,8 @@ const fps = 60, duration = 5 //seconds +import { FeatureAction, FeatureParent } from './classes/Feature'; + menu.add_feature('test', 'action', 0, () => { for (let i = 0; i < fps * duration; i++) { @@ -11,3 +13,19 @@ menu.add_feature('test', 'action', 0, () => { } }) + +let testParent = new FeatureParent('class'); + +new FeatureAction('testClass', testParent, (f) => { + + if (f.parent) + print(`parent: ${f.parent.name}\nid: ${f.parent.id}`) + + for (let i = 0; i < fps * duration; i++) { + + scriptdraw.draw_rect(v2(0, 0), v2(1, 1), 0xffffffff) + system.wait(0) + + } + +}) diff --git a/ts-lua/tsconfig.json b/ts-lua/tsconfig.json index 13877ab..8ad8e53 100644 --- a/ts-lua/tsconfig.json +++ b/ts-lua/tsconfig.json @@ -5,13 +5,14 @@ "target": "esnext", "lib": ["esnext"], "moduleResolution": "node", - "types": ["lua-types/5.4"], + "types": ["lua-types/5.4", "types-ts-2take1"], "strict": true, - "typeRoots": [ - "./types" - ] + "typeRoots": [] }, "tstl": { - "luaTarget": "JIT" + "luaTarget": "universal", + "luaLibImport": "inline", + "luaBundle": "test.lua", + "luaBundleEntry": "./src/test.ts", }, } \ No newline at end of file