Pixel-Composer/scripts/node_path_trim/node_path_trim.gml
2024-05-02 17:13:35 +07:00

96 lines
3.0 KiB
Plaintext

function Node_Path_Trim(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Trim Path";
setDimension(96, 48);;
inputs[| 0] = nodeValue("Path", self, JUNCTION_CONNECT.input, VALUE_TYPE.pathnode, noone)
.setVisible(true, true);
inputs[| 1] = nodeValue("Range", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [ 0, 1 ])
.setDisplay(VALUE_DISPLAY.slider_range);
outputs[| 0] = nodeValue("Path", self, JUNCTION_CONNECT.output, VALUE_TYPE.pathnode, self);
cached_pos = ds_map_create();
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) { #region
var _path = getInputData(0);
if(_path && struct_has(_path, "drawOverlay")) _path.drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
draw_set_color(COLORS._main_icon);
var _amo = getLineCount();
for( var i = 0; i < _amo; i++ ) {
var _len = getLength(_amo);
var _stp = 1 / clamp(_len * _s, 1, 64);
var ox, oy, nx, ny;
var _p = new __vec2();
for( var j = 0; j < 1; j += _stp ) {
_p = getPointRatio(j, i, _p);
nx = _x + _p.x * _s;
ny = _y + _p.y * _s;
if(j > 0) draw_line_width(ox, oy, nx, ny, 3);
ox = nx;
oy = ny;
}
}
} #endregion
static getLineCount = function() { #region
var _path = getInputData(0);
return struct_has(_path, "getLineCount")? _path.getLineCount() : 1;
} #endregion
static getSegmentCount = function(ind = 0) { #region
var _path = getInputData(0);
return struct_has(_path, "getSegmentCount")? _path.getSegmentCount(ind) : 0;
} #endregion
static getLength = function(ind = 0) { #region
var _path = getInputData(0);
return struct_has(_path, "getLength")? _path.getLength(ind) : 0;
} #endregion
static getAccuLength = function(ind = 0) { #region
var _path = getInputData(0);
return struct_has(_path, "getAccuLength")? _path.getAccuLength(ind) : [];
} #endregion
static getPointRatio = function(_rat, ind = 0, out = undefined) { #region
if(out == undefined) out = new __vec2(); else { out.x = 0; out.y = 0; }
var _path = getInputData(0);
var _rng = getInputData(1);
if(is_array(_path)) {
_path = array_safe_get_fast(_path, ind);
ind = 0;
}
if(!is_struct(_path) || !struct_has(_path, "getPointRatio"))
return out;
_rat = _rng[0] + _rat * (_rng[1] - _rng[0]);
return _path.getPointRatio(_rat, ind, out);
} #endregion
static getPointDistance = function(_dist, ind = 0, out = undefined) { return getPointRatio(_dist / getLength(), ind, out); }
static getBoundary = function(ind = 0) { #region
var _path = getInputData(0);
return struct_has(_path, "getBoundary")? _path.getBoundary(ind) : new BoundingBox( 0, 0, 1, 1 );
} #endregion
static update = function() { #region
ds_map_clear(cached_pos);
outputs[| 0].setValue(self);
} #endregion
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
var bbox = drawGetBbox(xx, yy, _s);
draw_sprite_fit(s_node_path_trim, 0, bbox.xc, bbox.yc, bbox.w, bbox.h);
} #endregion
}