Pixel-Composer/scripts/node_image/node_image.gml

182 lines
4.9 KiB
Plaintext
Raw Normal View History

2023-12-09 10:49:02 +01:00
function Node_create_Image(_x, _y, _group = noone) { #region
2022-01-13 05:24:03 +01:00
var path = "";
2023-01-17 08:11:55 +01:00
if(!LOADING && !APPENDING && !CLONING) {
2023-12-07 15:08:09 +01:00
path = get_open_filename("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;
}
2022-12-13 09:20:36 +01:00
var node = new Node_Image(_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
return node;
2023-12-09 10:49:02 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-12-09 10:49:02 +01:00
function Node_create_Image_path(_x, _y, path) { #region
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(path)) return noone;
2022-01-13 05:24:03 +01:00
2023-01-17 08:11:55 +01:00
var node = new Node_Image(_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
return node;
2023-12-09 10:49:02 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2023-02-28 09:43:01 +01:00
function Node_Image(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-11-23 13:39:35 +01:00
name = "Image";
color = COLORS.node_blend_input;
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Path", self, JUNCTION_CONNECT.input, VALUE_TYPE.path, "")
2024-02-15 14:23:26 +01:00
.setDisplay(VALUE_DISPLAY.path_load, { filter: "image|*.png;*.jpg" });
2022-01-19 03:05:13 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 1] = nodeValue("Padding", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, [0, 0, 0, 0])
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.padding);
2022-01-19 03:05:13 +01:00
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-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
first_update = false;
spr = [];
2024-02-15 14:23:26 +01:00
path_current = [];
2024-03-14 14:35:19 +01:00
edit_time = 0;
2022-01-13 05:24:03 +01:00
2024-03-14 14:35:19 +01:00
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
2023-12-09 10:49:02 +01:00
on_drop_file = function(path) { #region
2023-09-28 13:15:29 +02:00
inputs[| 0].setValue(path);
2022-01-13 05:24:03 +01:00
if(updatePaths(path)) {
doUpdate();
2022-01-13 05:24:03 +01:00
return true;
}
return false;
2023-12-09 10:49:02 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
2024-02-15 14:23:26 +01:00
function createSprite(path) { #region
if(path == -1) return noone;
2022-01-13 05:24:03 +01:00
2024-02-15 14:23:26 +01:00
var ext = string_lower(filename_ext(path));
var _name = filename_name_only(path);
2022-01-13 05:24:03 +01:00
switch(ext) {
case ".png":
case ".jpg":
case ".jpeg":
case ".gif":
2023-04-15 14:48:29 +02:00
setDisplayName(_name);
2024-03-14 14:35:19 +01:00
var spr = sprite_add(path, 1, false, false, 0, 0);
2022-01-13 05:24:03 +01:00
2023-12-09 10:49:02 +01:00
if(spr == -1) {
noti_warning($"Image node: File not a valid image.");
2024-02-15 14:23:26 +01:00
break;
2023-12-09 10:49:02 +01:00
}
2024-03-14 14:35:19 +01:00
edit_time = max(edit_time, file_get_modify_s(path));
2024-02-15 14:23:26 +01:00
return spr;
}
return noone;
} #endregion
2024-03-14 14:35:19 +01:00
function updatePaths(path = path_current) { #region
2024-02-15 14:23:26 +01:00
if(array_empty(path_current)) first_update = true;
for( var i = 0, n = array_length(spr); i < n; i++ )
sprite_delete(spr[i]);
2024-03-14 14:35:19 +01:00
spr = [];
2024-02-15 14:23:26 +01:00
if(!is_array(path)) path = [ path ];
for( var i = 0, n = array_length(path); i < n; i++ ) {
2024-03-14 14:35:19 +01:00
path_current[i] = path_get(path[i]);
2024-02-15 14:23:26 +01:00
var s = createSprite(path[i]);
if(s) array_push(spr, s);
2022-01-13 05:24:03 +01:00
}
2023-12-09 10:49:02 +01:00
} #endregion
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
2023-12-09 10:49:02 +01:00
static onInspector1Update = function() { #region
2024-03-14 14:35:19 +01:00
updatePaths(path_get(getInputData(0)));
triggerRender();
} #endregion
static step = function() { #region
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;
}
}
2023-12-09 10:49:02 +01:00
} #endregion
2023-01-17 08:11:55 +01:00
2023-12-09 10:49:02 +01:00
static update = function(frame = CURRENT_FRAME) { #region
2024-03-14 14:35:19 +01:00
var path = path_get(getInputData(0));
var pad = getInputData(1);
2024-02-11 14:53:33 +01:00
2024-02-15 14:23:26 +01:00
outputs[| 1].setValue(path);
2024-03-14 14:35:19 +01:00
if(!array_equals(path_current, path))
2024-02-15 14:23:26 +01:00
updatePaths(path);
2022-01-13 05:24:03 +01:00
2024-02-15 14:23:26 +01:00
if(array_empty(spr)) return;
2022-01-13 05:24:03 +01:00
2024-03-14 14:35:19 +01:00
var _arr = array_length(spr) > 1;
2024-02-11 14:53:33 +01:00
var _outsurf = outputs[| 0].getValue();
2022-01-13 05:24:03 +01:00
2024-02-15 14:23:26 +01:00
if(!is_array(_outsurf)) _outsurf = [ _outsurf ];
for( var i = 0, n = array_length(spr); i < n; i++ ) {
var _spr = spr[i];
if(!sprite_exists(_spr)) continue;
var ww = sprite_get_width(_spr) + pad[0] + pad[2];
var hh = sprite_get_height(_spr) + pad[1] + pad[3];
2024-03-31 05:36:11 +02:00
var _surf = array_safe_get_fast(_outsurf, i);
2024-02-15 14:23:26 +01:00
_surf = surface_verify(_surf, ww, hh, attrDepth());
surface_set_shader(_surf, noone);
draw_sprite(_spr, 0, pad[2], pad[1]);
surface_reset_shader();
_outsurf[i] = _surf;
}
outputs[| 0].setValue(_arr? _outsurf : _outsurf[0]);
2022-01-13 05:24:03 +01:00
2024-02-15 14:23:26 +01:00
#region splice
if(!first_update) return;
first_update = false;
2022-11-21 06:38:44 +01:00
2024-02-15 14:23:26 +01:00
if(LOADING || APPENDING) return;
if(string_pos("strip", display_name) == 0) return;
2023-06-04 12:38:40 +02:00
2024-02-15 14:23:26 +01:00
var sep_pos = string_pos("strip", display_name) + 5;
var sep = string_copy(display_name, sep_pos, string_length(display_name) - sep_pos + 1);
var amo = toNumber(string_digits(sep));
2022-11-21 06:38:44 +01:00
2024-02-15 14:23:26 +01:00
if(amo == 0) return;
var ww = sprite_get_width(spr[0]) / amo;
var hh = sprite_get_height(spr[0]);
2022-01-13 05:24:03 +01:00
2022-12-13 09:20:36 +01:00
var _splice = nodeBuild("Node_Image_Sheet", x + w + 64, y);
2022-11-21 06:38:44 +01:00
_splice.inputs[| 0].setFrom(outputs[| 0], false);
2024-02-15 14:23:26 +01:00
_splice.inputs[| 1].setValue([ ww, hh ]);
2022-11-21 06:38:44 +01:00
_splice.inputs[| 2].setValue(amo);
2023-02-14 05:32:32 +01:00
_splice.inputs[| 3].setValue([ amo, 1 ]);
2023-03-28 06:58:28 +02:00
_splice.inspector1Update();
2024-02-15 14:23:26 +01:00
#endregion
2023-12-09 10:49:02 +01:00
} #endregion
2022-01-13 05:24:03 +01:00
}