Pixel-Composer/scripts/node_display_image/node_display_image.gml

151 lines
3.7 KiB
Text
Raw Normal View History

2023-11-25 08:54:35 +01:00
function Node_create_Display_Image(_x, _y, _group = noone) { #region
2022-12-10 05:06:01 +01:00
var path = "";
2024-05-03 13:40:46 +02:00
if(NODE_NEW_MANUAL) {
2024-05-16 15:28:45 +02:00
path = get_open_filename_pxc("image|*.png;*.jpg", "");
2023-02-28 09:43:01 +01:00
key_release();
2022-12-10 05:06:01 +01:00
if(path == "") return noone;
}
2022-12-13 09:20:36 +01:00
var node = new Node_Display_Image(_x, _y, _group);
2022-12-10 05:06:01 +01:00
node.inputs[| 0].setValue(path);
node.doUpdate();
return node;
2023-11-25 08:54:35 +01:00
} #endregion
2022-12-10 05:06:01 +01:00
2023-11-25 08:54:35 +01:00
function Node_create_Display_Image_path(_x, _y, path) { #region
2023-12-08 03:50:09 +01:00
if(!file_exists_empty(path)) return noone;
2022-12-10 05:06:01 +01:00
2023-01-17 08:11:55 +01:00
var node = new Node_Display_Image(_x, _y, PANEL_GRAPH.getCurrentContext());
2022-12-10 05:06:01 +01:00
node.inputs[| 0].setValue(path);
node.doUpdate();
return node;
2023-11-25 08:54:35 +01:00
} #endregion
2022-12-10 05:06:01 +01:00
2023-02-28 09:43:01 +01:00
function Node_Display_Image(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-11-25 08:54:35 +01:00
name = "Display Image";
auto_height = false;
2022-12-10 05:06:01 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Path", self, JUNCTION_CONNECT.input, VALUE_TYPE.path, "")
2023-11-25 08:54:35 +01:00
.setVisible(false)
2023-12-07 15:08:09 +01:00
.setDisplay(VALUE_DISPLAY.path_load, { filter: "image|*.png;*.jpg" })
2023-02-14 05:32:32 +01:00
.rejectArray();
2022-12-10 05:06:01 +01:00
2023-11-25 08:54:35 +01:00
inputs[| 1] = nodeValue("Position", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ x, y ])
.setDisplay(VALUE_DISPLAY.vector)
.rejectArray();
inputs[| 2] = nodeValue("Scale", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 1, 1 ])
.setDisplay(VALUE_DISPLAY.vector)
.rejectArray();
inputs[| 3] = nodeValue("Smooth transform", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, true)
.rejectArray();
input_display_list = [ 0,
["Display", false], 1, 2, 3,
]
2022-12-10 05:06:01 +01:00
spr = noone;
path_current = "";
first_update = false;
2023-11-25 08:54:35 +01:00
smooth = true;
pos_x = x;
pos_y = y;
sca_x = 1;
sca_y = 1;
sca_dx = 1;
sca_dy = 1;
static move = function(_x, _y, _s) { #region
if(x == _x && y == _y) return;
if(!LOADING) PROJECT.modified = true;
x = _x;
y = _y;
if(inputs[| 1].setValue([ _x, _y ]))
UNDO_HOLDING = true;
} #endregion
static onInspector1Update = function() { #region
var path = getInputData(0);
2023-01-17 08:11:55 +01:00
if(path == "") return;
updatePaths(path);
2023-11-25 08:54:35 +01:00
update();
} #endregion
2023-01-17 08:11:55 +01:00
2023-11-25 08:54:35 +01:00
function updatePaths(path) { #region
2024-03-14 14:35:19 +01:00
path = path_get(path);
2022-12-10 05:06:01 +01:00
if(path == -1) return false;
2023-02-14 05:32:32 +01:00
var ext = string_lower(filename_ext(path));
2022-12-10 05:06:01 +01:00
var _name = string_replace(filename_name(path), filename_ext(path), "");
switch(ext) {
case ".png":
case ".jpg":
case ".jpeg":
case ".gif":
name = _name;
if(spr) sprite_delete(spr);
spr = sprite_add(path, 1, false, false, 0, 0);
if(path_current == "")
first_update = true;
path_current = path;
return true;
}
return false;
2023-11-25 08:54:35 +01:00
} #endregion
2022-12-10 05:06:01 +01:00
2023-11-25 08:54:35 +01:00
static update = function(frame = CURRENT_FRAME) { #region
var path = getInputData(0);
2023-11-25 08:54:35 +01:00
var posi = getInputData(1);
var scal = getInputData(2);
smooth = getInputData(3);
2022-12-10 05:06:01 +01:00
if(path == "") return;
2023-01-17 08:11:55 +01:00
if(path_current != path) updatePaths(path);
2022-12-10 05:06:01 +01:00
if(!spr || !sprite_exists(spr)) return;
2023-11-25 08:54:35 +01:00
pos_x = posi[0];
pos_y = posi[1];
sca_x = scal[0];
sca_y = scal[1];
} #endregion
2022-12-10 05:06:01 +01:00
2023-11-25 08:54:35 +01:00
static drawNodeBase = function(xx, yy, _s) { #region
2022-12-10 05:06:01 +01:00
if(!spr || !sprite_exists(spr)) return;
draw_sprite_uniform(spr, 0, xx, yy, _s);
2023-11-25 08:54:35 +01:00
} #endregion
2022-12-10 05:06:01 +01:00
2023-11-25 08:54:35 +01:00
static drawNode = function(_x, _y, _mx, _my, _s) { #region
if(spr == noone) return noone;
2023-11-25 08:54:35 +01:00
x = smooth? lerp_float(x, pos_x, 4) : pos_x;
y = smooth? lerp_float(y, pos_y, 4) : pos_y;
sca_dx = smooth? lerp_float(sca_dx, sca_x, 4) : sca_x;
sca_dy = smooth? lerp_float(sca_dy, sca_y, 4) : sca_y;
w = sprite_get_width(spr) * sca_dx;
h = sprite_get_height(spr) * sca_dy;
2022-12-10 05:06:01 +01:00
var xx = x * _s + _x;
var yy = y * _s + _y;
2023-11-25 08:54:35 +01:00
draw_sprite_stretched_ext(spr, 0, xx, yy, w * _s, h * _s, c_white, 1);
2022-12-10 05:06:01 +01:00
if(active_draw_index > -1) {
draw_sprite_stretched_ext(bg_sel_spr, 0, xx, yy, w * _s, h * _s, COLORS._main_accent, 1);
active_draw_index = -1;
}
return noone;
2023-11-25 08:54:35 +01:00
} #endregion
2022-12-10 05:06:01 +01:00
}