Pixel-Composer/scripts/addon_function/addon_function.gml

82 lines
2.3 KiB
Text
Raw Normal View History

2023-05-03 21:42:17 +02:00
function addonContextGenerator(_addon, _function) constructor {
self._addon = _addon;
self._function = _function;
static populate = function() {
var _items = lua_call(_addon.thread, _function);
var arr = [];
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(_items); i < n; i++ ) {
2023-05-03 21:42:17 +02:00
var _item = _items[i];
if(_item == -1)
array_push(arr, -1);
else {
2023-05-07 20:55:13 +02:00
if(struct_has(_item, "callback")) {
var _addonItem = new addonContextItem(_addon, _item.name, _item.callback);
array_push(arr, _addonItem.menu_item);
} else if(struct_has(_item, "content")) {
var _subArr = []
for( var j = 0; j < array_length(_item.content); j++ ) {
2023-05-16 21:28:16 +02:00
var _it = _item.content[j];
if(_it == -1)
array_push(_subArr, -1);
else if(struct_has(_it, "callback")) {
var _addonItem = new addonContextItem(_addon, _it.name, _it.callback);
array_push(_subArr, _addonItem.menu_item);
}
2023-05-07 20:55:13 +02:00
}
var _addonItem = new addonContextSubMenu(_item.name, _subArr);
array_push(arr, _addonItem.menu_item);
}
}
2023-05-03 21:42:17 +02:00
}
return arr;
}
}
function addonContextItem(_addon, _name, _function) constructor {
2024-08-18 13:13:40 +02:00
self._addon = _addon;
self._name = _name;
2023-05-03 21:42:17 +02:00
self._function = _function;
2024-08-18 13:13:40 +02:00
menu_item = menuItem(_name, function(_data) { lua_call(_addon.thread, self._function, lua_byref(_data.context, true)); });
2023-05-03 21:42:17 +02:00
}
2023-05-07 20:55:13 +02:00
function addonContextSubMenu(_name, _content) constructor {
2024-08-18 13:13:40 +02:00
self.name = _name;
2023-05-07 20:55:13 +02:00
self.content = _content;
2024-08-18 13:13:40 +02:00
menu_item = menuItem(name, function(_dat) { return submenuCall(_dat, content); }).setIsShelf();
2023-05-07 20:55:13 +02:00
}
function addonTrigger(_addon, _openDialog = true) {
2023-06-17 14:30:49 +02:00
if(addonActivated(_addon)) addonUnload(_addon);
else addonLoad(_addon, _openDialog);
}
function addonActivated(_addon) {
var _name = filename_name_only(_addon);
with(_addon_custom) if(name == _name) return true;
return false;
}
function addonLoad(_addon, _openDialog = true) {
2023-05-03 21:42:17 +02:00
var _name = filename_name_only(_addon);
var addonPath = DIRECTORY + "Addons/" + _name;
2023-05-07 20:55:13 +02:00
if(!directory_exists(addonPath)) return;
2023-06-17 14:30:49 +02:00
with(_addon_custom) if(name == _name) return;
2023-05-03 21:42:17 +02:00
with(instance_create(0, 0, _addon_custom))
2023-05-07 20:55:13 +02:00
init(addonPath, _openDialog);
2023-05-03 21:42:17 +02:00
}
2023-06-17 14:30:49 +02:00
function addonUnload(_addon) {
2023-05-03 21:42:17 +02:00
var _name = filename_name_only(_addon);
var addonPath = DIRECTORY + "Addons/" + _name;
2023-06-17 14:30:49 +02:00
if(!directory_exists(addonPath)) return;
with(_addon_custom) if(name == _name) instance_destroy();
2023-05-03 21:42:17 +02:00
}