Pixel-Composer/scripts/node_image_gif/node_image_gif.gml

142 lines
3.5 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_create_Image_gif(_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_filename(".gif", "");
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_gif(_x, _y, _group);
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;
}
function Node_create_Image_gif_path(_x, _y, path) {
if(!file_exists(path)) return noone;
2023-01-17 08:11:55 +01:00
var node = new Node_Image_gif(_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-23 04:08:16 +01:00
return node;
2022-01-13 05:24:03 +01:00
}
2023-02-28 09:43:01 +01:00
function Node_Image_gif(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-03-19 09:17:39 +01:00
name = "Image GIF";
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;
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_load, ["*.gif", ""]);
2023-03-28 06:58:28 +02:00
inputs[| 1] = nodeValue("Set animation length to gif", self, JUNCTION_CONNECT.input, VALUE_TYPE.trigger, 0)
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.button, [ function() {
if(!spr) return;
if(!sprite_exists(spr)) return;
ANIMATOR.frames_total = sprite_get_number(spr);
ANIMATOR.framerate = 12;
}, "Match length"] );
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
outputs[| 1] = nodeValue("Path", self, JUNCTION_CONNECT.output, VALUE_TYPE.path, "")
2022-01-19 14:48:30 +01:00
.setVisible(true, true);
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2022-01-13 05:24:03 +01:00
spr = noone;
path_current = "";
loading = 0;
spr_builder = noone;
on_dragdrop_file = function(path) {
if(updatePaths(path)) {
doUpdate();
2022-01-13 05:24:03 +01:00
return true;
}
return false;
}
2023-03-28 06:58:28 +02:00
static onInspector1Update = function() {
2023-01-17 08:11:55 +01:00
var path = inputs[| 0].getValue();
if(path == "") return;
updatePaths(path);
update();
}
2022-01-13 05:24:03 +01:00
function updatePaths(path) {
path = try_get_path(path);
if(path == -1) return false;
2023-03-19 09:17:39 +01:00
var ext = string_lower(filename_ext(path));
2022-01-13 05:24:03 +01:00
var _name = string_replace(filename_name(path), filename_ext(path), "");
switch(ext) {
case ".gif":
outputs[| 1].setValue(path);
if(spr) sprite_delete(spr);
sprite_add_gif(path, function(_spr) {
spr_builder = _spr;
loading = 2;
});
loading = 1;
if(path_current == "")
first_update = true;
path_current = path;
return true;
}
return false;
}
static step = function() {
if(loading == 2 && spr_builder != noone) {
if(spr_builder.building()) {
spr = spr_builder._spr;
doUpdate();
2022-01-13 05:24:03 +01:00
loading = 0;
delete spr_builder;
gc_collect();
}
}
}
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;
2023-01-17 08:11:55 +01:00
if(path_current != path) updatePaths(path);
2022-01-13 05:24:03 +01:00
if(!spr || !sprite_exists(spr)) return;
var ww = sprite_get_width(spr);
var hh = sprite_get_height(spr);
2023-03-19 09:17:39 +01:00
var _outsurf = outputs[| 0].getValue();
_outsurf = surface_verify(_outsurf, ww, hh, attrDepth());
2022-12-27 04:00:50 +01:00
outputs[| 0].setValue(_outsurf);
2022-01-13 05:24:03 +01:00
surface_set_target(_outsurf);
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, ANIMATOR.current_frame, 0, 0);
2023-02-14 05:32:32 +01:00
BLEND_NORMAL;
2022-01-13 05:24:03 +01:00
surface_reset_target();
}
2023-03-05 07:16:44 +01:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-01-04 02:30:04 +01:00
if(loading)
2022-11-18 03:20:31 +01:00
draw_sprite_ui(THEME.loading, 0, xx + w * _s / 2, yy + h * _s / 2, _s, _s, current_time / 2, COLORS._main_icon, 1);
2022-01-13 05:24:03 +01:00
}
static onDestroy = function() {
if(sprite_exists(spr))
sprite_flush(spr);
}
}