Pixel-Composer/scripts/node_image_animated/node_image_animated.gml

191 lines
5.0 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_create_Image_Animated(_x, _y, _group = noone) {
2022-01-13 05:24:03 +01:00
var path = "";
2023-01-17 08:11:55 +01:00
if(!LOADING && !APPENDING && !CLONING) {
2022-01-13 05:24:03 +01:00
path = get_open_filenames(".png", "");
2023-02-28 09:43:01 +01:00
key_release();
2022-01-13 05:24:03 +01:00
if(path == "") return noone;
}
2022-12-13 09:20:36 +01:00
var node = new Node_Image_Animated(_x, _y, _group);
2022-01-13 05:24:03 +01:00
var paths = paths_to_array(path);
node.inputs[| 0].setValue(paths);
node.doUpdate();
2022-01-13 05:24:03 +01:00
2022-12-13 09:20:36 +01:00
//ds_list_add(PANEL_GRAPH.nodes_list, node);
2022-01-13 05:24:03 +01:00
return node;
}
function Node_create_Image_Animated_path(_x, _y, _path) {
2023-01-17 08:11:55 +01:00
var node = new Node_Image_Animated(_x, _y, PANEL_GRAPH.getCurrentContext());
2022-01-13 05:24:03 +01:00
node.inputs[| 0].setValue(_path);
node.doUpdate();
2022-01-13 05:24:03 +01:00
2022-12-13 09:20:36 +01:00
//ds_list_add(PANEL_GRAPH.nodes_list, node);
2022-01-13 05:24:03 +01:00
return node;
}
enum ANIMATION_END {
loop,
ping,
hold,
hide
}
2023-02-28 09:43:01 +01:00
function Node_Image_Animated(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-03-19 09:17:39 +01:00
name = "Animation";
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
update_on_frame = true;
always_output = true;
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Path", self, JUNCTION_CONNECT.input, VALUE_TYPE.path, [])
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.path_array, ["*.png", ""]);
2023-02-14 05:32:32 +01:00
inputs[| 1] = nodeValue("Padding", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, [0, 0, 0, 0])
.setDisplay(VALUE_DISPLAY.padding)
.rejectArray();
2022-01-19 03:05:13 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("Stretch frame", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false)
.rejectArray();
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 3] = nodeValue("Frame duration", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 1)
.rejectArray();
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 4] = nodeValue("Animation end", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
.setDisplay(VALUE_DISPLAY.enum_scroll, ["Loop", "Ping pong", "Hold last frame", "Hide"])
.rejectArray();
inputs[| 5] = nodeValue("Set animation length to match", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.button, [ function() {
if(array_length(spr) == 0) return;
ANIMATOR.frames_total = array_length(spr);
}, "Match length"] );
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2022-01-13 05:24:03 +01:00
input_display_list = [
["Image", false], 0, 1,
["Animation", false], 5, 2, 3, 4,
];
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2022-01-13 05:24:03 +01:00
path_loaded = [];
on_dragdrop_file = function(path) {
if(directory_exists(path)) {
with(dialogCall(o_dialog_drag_folder, WIN_W / 2, WIN_H / 2)) {
dir_paths = path;
target = other;
}
return true;
}
var paths = paths_to_array(path);
if(updatePaths(paths)) {
doUpdate();
2022-01-13 05:24:03 +01:00
return true;
}
return false;
}
function updatePaths(paths) {
if(!is_array(paths) && ds_exists(paths, ds_type_list))
paths = ds_list_to_array(paths);
for(var i = 0; i < array_length(spr); i++) {
if(spr[i] && sprite_exists(spr[i]))
sprite_delete(spr[i]);
}
spr = [];
path_loaded = array_create(array_length(paths));
for( var i = 0; i < array_length(paths); i++ ) {
path_loaded[i] = paths[i];
var path = try_get_path(paths[i]);
if(path == -1) continue;
2023-03-19 09:17:39 +01:00
display_name = string_replace(filename_name(path), filename_ext(path), "");
var ext = string_lower(filename_ext(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" :
array_push(spr, sprite_add(path, 1, false, false, 0, 0));
break;
}
2022-01-13 05:24:03 +01:00
}
return true;
}
2023-02-14 05:32:32 +01:00
static onInspectorUpdate = function() {
2023-01-17 08:11:55 +01:00
var path = inputs[| 0].getValue();
if(path == "") return;
updatePaths(path);
update();
}
2023-02-14 05:32:32 +01:00
static update = function(frame = ANIMATOR.current_frame) {
2022-01-13 05:24:03 +01:00
var path = inputs[| 0].getValue();
if(path == "") return;
if(is_array(path) && !array_equals(path, path_loaded))
updatePaths(path);
if(array_length(spr) == 0) return;
var pad = inputs[| 1].getValue();
var str = inputs[| 2].getValue();
2022-01-19 06:11:17 +01:00
inputs[| 3].setVisible(!str);
inputs[| 4].setVisible(!str);
2022-01-13 05:24:03 +01:00
var spd = str? (ANIMATOR.frames_total + 1) / array_length(spr) : inputs[| 3].getValue();
var _end = inputs[| 4].getValue();
if(spd == 0) spd = 1;
var ww = sprite_get_width(spr[0]);
var hh = sprite_get_height(spr[0]);
ww += pad[0] + pad[2];
hh += pad[1] + pad[3];
var surfs = outputs[| 0].getValue();
2023-03-19 09:17:39 +01:00
surfs = surface_verify(surfs, ww, hh, attrDepth());
2022-12-27 04:00:50 +01:00
outputs[| 0].setValue(surfs);
2023-02-14 05:32:32 +01:00
var _frame = floor(ANIMATOR.current_frame / spd);
2022-01-13 05:24:03 +01:00
switch(_end) {
case ANIMATION_END.loop :
2023-02-14 05:32:32 +01:00
_frame = safe_mod(_frame, array_length(spr));
2022-01-13 05:24:03 +01:00
break;
case ANIMATION_END.ping :
2023-02-14 05:32:32 +01:00
_frame = safe_mod(_frame, array_length(spr) * 2 - 2);
if(_frame >= array_length(spr))
_frame = array_length(spr) * 2 - 2 - _frame;
2022-01-13 05:24:03 +01:00
break;
case ANIMATION_END.hold :
2023-02-14 05:32:32 +01:00
_frame = min(_frame, array_length(spr) - 1);
2022-01-13 05:24:03 +01:00
break;
}
2023-02-14 05:32:32 +01:00
var curr_w = sprite_get_width(spr[_frame]);
var curr_h = sprite_get_height(spr[_frame]);
2022-01-13 05:24:03 +01:00
var curr_x = pad[2] + (ww - curr_w) / 2;
var curr_y = pad[1] + (hh - curr_h) / 2;
surface_set_target(surfs);
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
if(_end == ANIMATION_END.hide) {
2023-02-14 05:32:32 +01:00
if(_frame < array_length(spr))
draw_sprite(spr[_frame], 0, curr_x, curr_y);
2022-01-13 05:24:03 +01:00
} else
2023-02-14 05:32:32 +01:00
draw_sprite(spr[_frame], 0, curr_x, curr_y);
BLEND_NORMAL;
2022-01-13 05:24:03 +01:00
surface_reset_target();
}
}