Pixel-Composer/scripts/node_image_sequence/node_image_sequence.gml

250 lines
5.9 KiB
Text
Raw Normal View History

function Node_create_Image_Sequence(_x, _y, _group = noone) {
2022-01-13 05:24:03 +01:00
var path = "";
2024-05-03 13:40:46 +02:00
if(NODE_NEW_MANUAL) {
2023-12-07 15:08:09 +01:00
path = get_open_filenames_compat("image|*.png;*.jpg", "");
2023-02-28 09:43:01 +01:00
key_release();
2022-01-13 05:24:03 +01:00
if(path == "") return noone;
}
var node = new Node_Image_Sequence(_x, _y, _group).skipDefault();
2023-02-14 05:32:32 +01:00
var paths = string_splice(path, "\n");
2024-08-08 06:57:51 +02:00
node.inputs[0].setValue(paths);
2024-05-03 13:40:46 +02:00
if(NODE_NEW_MANUAL) node.doUpdate();
2022-01-13 05:24:03 +01:00
return node;
}
function Node_create_Image_Sequence_path(_x, _y, _path) {
var node = new Node_Image_Sequence(_x, _y, PANEL_GRAPH.getCurrentContext()).skipDefault();
2024-08-08 06:57:51 +02:00
node.inputs[0].setValue(_path);
node.doUpdate();
2022-01-13 05:24:03 +01:00
return node;
}
2022-01-13 05:24:03 +01:00
enum CANVAS_SIZE {
individual,
minimum,
maximum
}
enum CANVAS_SIZING {
padding,
scale
}
2023-02-28 09:43:01 +01:00
function Node_Image_Sequence(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-03-19 09:17:39 +01:00
name = "Image Array";
2022-01-13 05:24:03 +01:00
spr = [];
2022-11-18 03:20:31 +01:00
color = COLORS.node_blend_input;
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Path("Paths", self, []))
2023-12-07 15:08:09 +01:00
.setDisplay(VALUE_DISPLAY.path_array, { filter: ["image|*.png;*.jpg", ""] });
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Padding("Padding", self, [0, 0, 0, 0]))
2023-02-14 05:32:32 +01:00
.rejectArray();
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Enum_Scroll("Canvas size", self, 0, [ "Individual", "Minimum", "Maximum" ]))
2023-02-14 05:32:32 +01:00
.rejectArray();
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(3, nodeValue_Enum_Scroll("Sizing method", self, 0, [ "Padding / Crop", "Scale" ]))
2023-02-14 05:32:32 +01:00
.rejectArray();
2022-01-13 05:24:03 +01:00
input_display_list = [
2023-02-23 07:02:19 +01:00
["Array settings", false], 0, 1, 2, 3
2022-01-13 05:24:03 +01:00
];
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, []));
newOutput(1, nodeValue_Output("Paths", self, VALUE_TYPE.path, [] )).
2022-01-13 05:24:03 +01:00
setVisible(true, true);
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2024-03-14 14:35:19 +01:00
path_current = [];
edit_time = 0;
attributes.file_checker = true;
array_push(attributeEditors, [ "File Watcher", function() { return attributes.file_checker; },
new checkBox(function() { attributes.file_checker = !attributes.file_checker; }) ]);
2022-01-13 05:24:03 +01:00
on_drop_file = function(path) {
2022-01-13 05:24:03 +01:00
if(directory_exists(path)) {
with(dialogCall(o_dialog_drag_folder, WIN_W / 2, WIN_H / 2)) {
dir_paths = path;
target = other;
}
return true;
}
2024-05-16 09:42:34 +02:00
var paths = paths_to_array_ext(path);
2023-09-28 13:15:29 +02:00
2024-08-08 06:57:51 +02:00
inputs[0].setValue(path);
if(updatePaths()) {
doUpdate();
2022-01-13 05:24:03 +01:00
return true;
}
return false;
}
2022-01-13 05:24:03 +01:00
2024-11-26 05:52:57 +01:00
setTrigger(1, __txt("Refresh"), [ THEME.refresh_icon, 1, COLORS._main_value_positive ], function() /*=>*/ { updatePaths(); triggerRender(); });
2023-01-17 08:11:55 +01:00
function updatePaths() {
var _paths = getInputData(0);
var paths = path_get(_paths);
path_current = array_clone(paths);
2022-01-13 05:24:03 +01:00
for(var i = 0; i < array_length(spr); i++) {
if(spr[i] && sprite_exists(spr[i]))
sprite_delete(spr[i]);
}
2024-03-14 14:35:19 +01:00
spr = [];
2022-01-13 05:24:03 +01:00
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(paths); i < n; i++ ) {
var path = paths[i];
2022-01-13 05:24:03 +01:00
if(path == -1) continue;
2024-03-14 14:35:19 +01:00
2023-03-19 09:17:39 +01:00
var ext = string_lower(filename_ext(path));
2023-11-23 13:39:35 +01:00
setDisplayName(filename_name_only(path));
edit_time = max(edit_time, file_get_modify_s(path));
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
switch(ext) {
case ".png" :
case ".jpg" :
case ".jpeg" :
var _spr = sprite_add_map(path);
2023-12-09 10:49:02 +01:00
if(_spr == -1) {
2024-06-26 06:38:57 +02:00
var _txt = $"Image node: File not a valid image.";
logNode(_txt); noti_warning(_txt);
2023-12-09 10:49:02 +01:00
return false;
}
array_push(spr, _spr);
2023-03-19 09:17:39 +01:00
break;
}
2022-01-13 05:24:03 +01:00
}
2024-08-08 06:57:51 +02:00
outputs[1].setValue(paths);
2022-01-13 05:24:03 +01:00
return true;
}
2022-01-13 05:24:03 +01:00
static step = function() {
2024-03-14 14:35:19 +01:00
if(attributes.file_checker)
for( var i = 0, n = array_length(path_current); i < n; i++ ) {
var _ed = file_get_modify_s(path_current[i]);
if(_ed > edit_time) {
2024-03-14 14:35:19 +01:00
updatePaths();
triggerRender();
break;
}
}
}
2024-03-14 14:35:19 +01:00
static update = function(frame = CURRENT_FRAME) {
2024-08-08 06:57:51 +02:00
var path = inputs[0].getValue();
2024-03-14 14:35:19 +01:00
if(!array_equals(path_current, path))
updatePaths();
2022-01-13 05:24:03 +01:00
var pad = getInputData(1);
var can = getInputData(2);
2024-08-08 06:57:51 +02:00
inputs[3].setVisible(can != CANVAS_SIZE.individual);
2022-01-13 05:24:03 +01:00
var siz = getInputData(3);
2022-01-13 05:24:03 +01:00
var ww = -1, hh = -1;
2022-01-13 05:24:03 +01:00
var _ww = -1, _hh = -1;
2024-08-08 06:57:51 +02:00
var surfs = outputs[0].getValue();
var amo = array_length(spr);
for(var i = amo; i < array_length(surfs); i++)
surface_free(surfs[i]);
array_resize(surfs, amo);
2022-01-13 05:24:03 +01:00
for(var i = 0; i < amo; i++) {
2022-01-13 05:24:03 +01:00
var _spr = spr[i];
var _w = sprite_get_width(_spr);
var _h = sprite_get_height(_spr);
switch(can) {
case CANVAS_SIZE.minimum :
ww = ww == -1? _w : min(ww, _w);
hh = hh == -1? _h : min(hh, _h);
2022-01-13 05:24:03 +01:00
break;
2022-01-13 05:24:03 +01:00
case CANVAS_SIZE.maximum :
ww = ww == -1? _w : max(ww, _w);
hh = hh == -1? _h : max(hh, _h);
2022-01-13 05:24:03 +01:00
break;
}
}
2022-01-13 05:24:03 +01:00
_ww = ww;
_hh = hh;
ww += pad[0] + pad[2];
hh += pad[1] + pad[3];
for(var i = 0; i < array_length(spr); i++) {
var _spr = spr[i];
switch(can) {
case CANVAS_SIZE.individual :
ww = sprite_get_width(_spr) + pad[0] + pad[2];
2022-01-13 05:24:03 +01:00
hh = sprite_get_height(_spr) + pad[1] + pad[3];
surfs[i] = surface_verify(surfs[i], ww, hh, attrDepth());
2022-01-13 05:24:03 +01:00
surface_set_target(surfs[i]);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2023-02-14 05:32:32 +01:00
BLEND_OVERRIDE;
2022-01-13 05:24:03 +01:00
draw_sprite(_spr, 0, pad[2], pad[1]);
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2022-01-13 05:24:03 +01:00
surface_reset_target();
break;
2022-01-13 05:24:03 +01:00
case CANVAS_SIZE.maximum :
case CANVAS_SIZE.minimum :
surfs[i] = surface_verify(surfs[i], ww, hh, attrDepth());
2022-01-13 05:24:03 +01:00
var _w = sprite_get_width(_spr);
var _h = sprite_get_height(_spr);
if(siz == CANVAS_SIZING.scale) {
var ss = min(_ww / _w, _hh / _h);
var sw = (ww - _w * ss) / 2;
var sh = (hh - _h * ss) / 2;
surface_set_target(surfs[i]);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2023-02-14 05:32:32 +01:00
BLEND_OVERRIDE;
2022-01-13 05:24:03 +01:00
draw_sprite_ext(_spr, 0, sw, sh, ss, ss, 0, c_white, 1);
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2022-01-13 05:24:03 +01:00
surface_reset_target();
2022-01-13 05:24:03 +01:00
} else {
var xx = (ww - _w) / 2;
var yy = (hh - _h) / 2;
surface_set_target(surfs[i]);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2023-02-14 05:32:32 +01:00
BLEND_OVERRIDE;
2022-01-13 05:24:03 +01:00
draw_sprite(_spr, 0, xx, yy);
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2022-01-13 05:24:03 +01:00
surface_reset_target();
}
break;
}
}
2024-08-08 06:57:51 +02:00
outputs[0].setValue(surfs);
}
static dropPath = function(path) {
if(!is_array(path)) path = [ path ];
2024-08-08 06:57:51 +02:00
inputs[0].setValue(path);
}
2022-01-13 05:24:03 +01:00
}