Pixel-Composer/scripts/node_image_animated/node_image_animated.gml

296 lines
7.7 KiB
Plaintext
Raw Normal View History

function Node_create_Image_Animated(_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_Animated(_x, _y, _group).skipDefault();
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;
}
2022-01-13 05:24:03 +01:00
function Node_create_Image_Animated_path(_x, _y, _path) {
var node = new Node_Image_Animated(_x, _y, PANEL_GRAPH.getCurrentContext()).skipDefault();
2022-01-13 05:24:03 +01:00
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 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;
setAlwaysTimeline(new timelineItemNode_Image_Animated(self));
2022-01-13 05:24:03 +01:00
update_on_frame = true;
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Path("Path", 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-19 03:05:13 +01:00
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Bool("Stretch frame", self, false, "Stretch animation speed to match project length."))
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_Float("Animation speed", self, 1))
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(4, nodeValue_Enum_Scroll("Loop modes", self, 0, ["Loop", "Ping pong", "Hold last frame", "Hide"]))
2023-02-14 05:32:32 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(5, nodeValue_Trigger("Set animation length to match", self, false ))
2024-04-08 07:13:46 +02:00
.setDisplay(VALUE_DISPLAY.button, { name: "Match length", UI : true, onClick: function() {
2022-01-13 05:24:03 +01:00
if(array_length(spr) == 0) return;
2023-10-09 16:07:33 +02:00
TOTAL_FRAMES = array_length(spr);
} });
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(6, nodeValue_Bool("Custom frame order", self, false));
2024-08-18 06:16:20 +02:00
newInput(7, nodeValue_Int("Frame", self, 0));
2023-11-22 05:17:13 +01:00
2024-08-18 09:13:41 +02:00
newInput(8, nodeValue_Enum_Scroll("Canvas size", self, 2, [ "First", "Minimum", "Maximum" ]))
2023-11-22 05:17:13 +01:00
.rejectArray();
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone));
2022-01-13 05:24:03 +01:00
input_display_list = [
2023-11-22 05:17:13 +01:00
["Image", false], 0, 1, 8,
2024-01-09 03:39:40 +01:00
["Animation", false], 5, 4, 2, 3,
["Custom Frame Order", false, 6], 7,
2022-01-13 05:24:03 +01:00
];
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
2024-08-29 03:53:19 +02: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(paths);
2022-01-13 05:24:03 +01:00
if(updatePaths(paths)) {
doUpdate();
2022-01-13 05:24:03 +01:00
return true;
}
return false;
2024-08-29 03:53:19 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-29 03:53:19 +02:00
function updatePaths(paths = path_current) {
2022-01-13 05:24:03 +01:00
if(!is_array(paths) && ds_exists(paths, ds_type_list))
paths = ds_list_to_array(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 = [];
path_current = [];
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++ ) {
2024-03-14 14:35:19 +01:00
var _path = path_get(paths[i]);
if(_path == -1) continue;
array_push(path_current, _path);
setDisplayName(filename_name_only(_path));
2023-03-19 09:17:39 +01:00
2024-03-14 14:35:19 +01:00
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" :
2024-08-29 03:53:19 +02:00
var _real_path = sprite_path_check_depth(path);
var _spr = sprite_add(_real_path, 1, false, false, 0, 0);
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;
}
2024-03-14 14:35:19 +01:00
edit_time = max(edit_time, file_get_modify_s(_path));
2023-12-09 10:49:02 +01:00
array_push(spr, _spr);
2023-03-19 09:17:39 +01:00
break;
}
2022-01-13 05:24:03 +01:00
}
return true;
2024-08-29 03:53:19 +02:00
}
2022-01-13 05:24:03 +01:00
2023-06-05 18:27:53 +02:00
insp1UpdateTooltip = __txt("Refresh");
2024-03-31 05:36:11 +02:00
insp1UpdateIcon = [ THEME.refresh_icon, 1, COLORS._main_value_positive ];
2023-04-07 21:25:27 +02:00
static onInspector1Update = function() { #region
2024-03-14 14:35:19 +01:00
updatePaths(path_get(getInputData(0)));
triggerRender();
} #endregion
static step = function() { #region
var str = getInputData(2);
var _cus = getInputData(6);
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(!_cus);
inputs[3].setVisible(!_cus && !str);
inputs[4].setVisible(!_cus && !str);
2024-03-14 14:35:19 +01:00
if(attributes.file_checker)
for( var i = 0, n = array_length(path_current); i < n; i++ ) {
if(file_get_modify_s(path_current[i]) > edit_time) {
updatePaths();
triggerRender();
break;
}
}
} #endregion
2023-01-17 08:11:55 +01:00
2023-10-09 16:07:33 +02:00
static update = function(frame = CURRENT_FRAME) { #region
2024-03-14 14:35:19 +01:00
var path = path_get(getInputData(0));
if(!array_equals(path_current, path))
2022-01-13 05:24:03 +01:00
updatePaths(path);
2024-03-14 14:35:19 +01:00
2022-01-13 05:24:03 +01:00
if(array_length(spr) == 0) return;
var _pad = getInputData(1);
2022-01-19 06:11:17 +01:00
var _cus = getInputData(6);
var _str = getInputData(2);
var _end = getInputData(4);
2023-10-09 16:07:33 +02:00
var _spd = _str? (TOTAL_FRAMES + 1) / array_length(spr) : 1 / getInputData(3);
if(_spd == 0) _spd = 1;
2023-10-09 16:07:33 +02:00
var _frame = _cus? getInputData(7) : floor(CURRENT_FRAME / _spd);
var _len = array_length(spr);
var _drw = true;
2022-01-13 05:24:03 +01:00
2023-11-22 05:17:13 +01:00
var _siz = getInputData(8);
var sw = sprite_get_width(spr[0]);
var sh = sprite_get_height(spr[0]);
if(_siz) {
for( var i = 1, n = array_length(spr); i < n; i++ ) {
var _sw = sprite_get_width(spr[i]);
var _sh = sprite_get_height(spr[i]);
if(_siz == 1) {
sw = min(_sw, sw);
sh = min(_sh, sh);
} else if(_siz == 2) {
sw = max(_sw, sw);
sh = max(_sh, sh);
}
}
}
var ww = sw;
var hh = sh;
ww += _pad[0] + _pad[2];
hh += _pad[1] + _pad[3];
2022-01-13 05:24:03 +01:00
2024-08-08 06:57:51 +02:00
var surfs = outputs[0].getValue();
2023-03-19 09:17:39 +01:00
surfs = surface_verify(surfs, ww, hh, attrDepth());
2024-08-08 06:57:51 +02:00
outputs[0].setValue(surfs);
2022-12-27 04:00:50 +01:00
2022-01-13 05:24:03 +01:00
switch(_end) {
case ANIMATION_END.loop :
_frame = safe_mod(_frame, _len);
2022-01-13 05:24:03 +01:00
break;
case ANIMATION_END.ping :
_frame = safe_mod(_frame, _len * 2 - 2);
if(_frame >= _len)
_frame = _len * 2 - 2 - _frame;
2022-01-13 05:24:03 +01:00
break;
case ANIMATION_END.hold :
_frame = min(_frame, _len - 1);
break;
case ANIMATION_END.hide :
if(_frame < 0 || _frame >= _len)
_drw = false;
2022-01-13 05:24:03 +01:00
break;
}
2024-03-31 05:36:11 +02:00
var _spr = array_safe_get_fast(spr, _frame, noone);
2023-04-10 20:02:59 +02:00
if(_spr == noone) return;
2023-02-14 05:32:32 +01:00
var curr_w = sprite_get_width(spr[_frame]);
var curr_h = sprite_get_height(spr[_frame]);
2023-11-22 05:17:13 +01:00
var curr_x = _pad[2] + (sw - curr_w) / 2;
var curr_y = _pad[1] + (sh - curr_h) / 2;
surface_set_shader(surfs);
if(_drw) draw_sprite(spr[_frame], 0, curr_x, curr_y);
surface_reset_shader();
} #endregion
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function timelineItemNode_Image_Animated(node) : timelineItemNode(node) constructor {
static drawDopesheet = function(_x, _y, _s, _msx, _msy) {
if(!is_instanceof(node, Node_Image_Animated)) return;
if(!node.attributes.show_timeline) return;
var _sprs = node.spr;
var _spr, _rx, _ry;
for (var i = 0, n = array_length(_sprs); i < n; i++) {
_spr = _sprs[i];
if(!sprite_exists(_spr)) continue;
_rx = _x + (i + 1) * _s;
_ry = h / 2 + _y;
var _sw = sprite_get_width(_spr);
var _sh = sprite_get_height(_spr);
var _ss = h / max(_sw, _sh);
draw_sprite_ext(_spr, 0, _rx - _sw * _ss / 2, _ry - _sh * _ss / 2, _ss, _ss, 0, c_white, .5);
}
}
2024-06-29 12:31:24 +02:00
static drawDopesheetOver = function(_x, _y, _s, _msx, _msy, _hover, _focus) {
if(!is_instanceof(node, Node_Image_Animated)) return;
if(!node.attributes.show_timeline) return;
drawDopesheetOutput(_x, _y, _s, _msx, _msy);
}
static onSerialize = function(_map) {
_map.type = "timelineItemNode_Image_Animated";
}
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
}