Pixel-Composer/scripts/node_cache_array/node_cache_array.gml

89 lines
2.7 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Cache_Array(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-02-19 13:49:20 +01:00
name = "Cache Array";
use_cache = true;
2023-07-30 13:56:22 +02:00
clearCacheOnChange = false;
2023-02-19 13:49:20 +01:00
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
2023-03-07 14:29:47 +01:00
inputs[| 1] = nodeValue("Start frame", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, -1, "Frame index to start caching, set to -1 to start at the first frame.");
inputs[| 2] = nodeValue("Stop frame", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, -1, "Frame index to stop caching (inclusive), set to -1 to stop at the last frame.");
inputs[| 3] = nodeValue("Step", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 1, "Cache every N frames, set to 1 to cache every frame.");
2023-02-19 13:49:20 +01:00
outputs[| 0] = nodeValue("Cache array", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, []);
input_display_list = [
2023-05-03 21:42:17 +02:00
["Output", true], 0,
2023-03-07 14:29:47 +01:00
["Range", false], 1, 2, 3,
2023-02-19 13:49:20 +01:00
];
2023-03-02 07:59:14 +01:00
cache_loading = false;
cache_content = "";
cache_loading_progress = 0;
2023-03-08 07:35:51 +01:00
insp2UpdateTooltip = "Clear cache";
insp2UpdateIcon = [ THEME.cache, 0, COLORS._main_icon ];
static onInspector2Update = function() { clearCache(); }
2023-03-02 07:59:14 +01:00
static step = function() {
2023-07-30 13:56:22 +02:00
if(!cache_loading) return;
var _content = cache_content[cache_loading_progress];
2023-07-23 20:21:35 +02:00
2023-07-30 13:56:22 +02:00
cached_output[cache_loading_progress] = __surface_array_deserialize(_content);
cache_result[cache_loading_progress] = true;
cache_loading_progress++;
2023-03-02 07:59:14 +01:00
2023-07-30 13:56:22 +02:00
if(cache_loading_progress == array_length(cache_content)) {
cache_loading = false;
update();
2023-03-02 07:59:14 +01:00
}
}
2023-02-19 13:49:20 +01:00
static update = function() {
2023-03-07 14:29:47 +01:00
var ss = [];
2023-07-21 12:40:20 +02:00
var str = inputs[| 1].getValue();
var lst = inputs[| 2].getValue();
2023-03-07 14:29:47 +01:00
var stp = inputs[| 3].getValue();
2023-07-21 12:40:20 +02:00
if(str < 0) str = 1;
if(lst < 0) lst = PROJECT.animator.frames_total;
str -= 1;
lst -= 1;
2023-03-07 14:29:47 +01:00
2023-07-17 19:58:33 +02:00
if(PROJECT.animator.current_frame < str) return;
if(PROJECT.animator.current_frame > lst) return;
2023-03-07 14:29:47 +01:00
if(lst > str && stp > 0)
for( var i = str; i <= lst; i += stp ) {
if(cacheExist(i))
array_push(ss, cached_output[i]);
}
outputs[| 0].setValue(ss);
2023-03-02 07:59:14 +01:00
2023-07-06 19:49:16 +02:00
if(!PROJECT.animator.is_playing) return;
2023-03-02 07:59:14 +01:00
if(!inputs[| 0].value_from) return;
2023-02-19 13:49:20 +01:00
var _surf = inputs[| 0].getValue();
cacheCurrentFrame(_surf);
2023-03-02 07:59:14 +01:00
}
2023-03-05 07:16:44 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-03-02 07:59:14 +01:00
if(cache_loading)
draw_sprite_ui(THEME.loading, 0, xx + w * _s / 2, yy + h * _s / 2, _s, _s, current_time / 2, COLORS._main_icon, 1);
}
static doSerialize = function(_map) {
2023-07-17 19:58:33 +02:00
_map.cache = surface_array_serialize(cached_output);
2023-03-02 07:59:14 +01:00
}
static postDeserialize = function() {
2023-06-13 14:42:06 +02:00
if(!struct_has(load_map, "cache")) return;
2023-07-17 19:58:33 +02:00
cache_content = json_try_parse(load_map.cache);
2023-03-02 07:59:14 +01:00
cache_loading_progress = 0;
cache_loading = true;
2023-02-19 13:49:20 +01:00
}
}