2023-03-07 14:29:47 +01:00
function Node_Path_Builder(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Path Builder";
previewable = false;
w = 96;
2023-07-31 20:06:44 +02:00
length = 0;
lengthAcc = [];
2023-03-07 14:29:47 +01:00
2023-10-27 15:42:17 +02:00
lines = [];
connected = false;
2023-03-07 14:29:47 +01:00
inputs[| 0] = nodeValue("Point array", self, JUNCTION_CONNECT.input, VALUE_TYPE.float, [])
.setVisible(true, true)
.setArrayDepth(2);
2023-03-11 01:40:17 +01:00
inputs[| 1] = nodeValue("Connected", self, JUNCTION_CONNECT.input, VALUE_TYPE.boolean, false, "If set to true, will draw a single path from one point to another. If not set will treat each pair of points as an individual line.");
2023-03-07 14:29:47 +01:00
outputs[| 0] = nodeValue("Path", self, JUNCTION_CONNECT.output, VALUE_TYPE.pathnode, self);
2023-10-09 16:07:33 +02:00
static drawOverlay = function(active, _x, _y, _s, _mx, _my, _snx, _sny) { #region
2023-07-31 20:06:44 +02:00
draw_set_color(COLORS._main_accent);
2023-10-27 15:42:17 +02:00
if(connected) {
for( var i = 1, n = array_length(lines); i < n; i++ ) {
var p0 = lines[i - 1];
var p1 = lines[i - 0];
2023-07-31 20:06:44 +02:00
draw_line(_x + p0[0] * _s, _y + p0[1] * _s,
_x + p1[0] * _s, _y + p1[1] * _s);
}
} else {
2023-10-27 15:42:17 +02:00
var len = floor(array_length(lines) / 2) * 2;
2023-07-31 20:06:44 +02:00
for( var i = 0; i < len; i += 2 ) {
2023-10-27 15:42:17 +02:00
var p0 = lines[i + 0];
var p1 = lines[i + 1];
2023-07-31 20:06:44 +02:00
draw_line(_x + p0[0] * _s, _y + p0[1] * _s,
_x + p1[0] * _s, _y + p1[1] * _s);
}
}
2023-10-09 16:07:33 +02:00
} #endregion
2023-07-31 20:06:44 +02:00
2023-10-09 16:07:33 +02:00
static getLineCount = function() { #region
2023-10-27 15:42:17 +02:00
return connected? 1 : floor(array_length(lines) / 2);
2023-10-09 16:07:33 +02:00
} #endregion
2023-03-07 14:29:47 +01:00
2023-10-09 16:07:33 +02:00
static getSegmentCount = function() { #region
2023-10-27 15:42:17 +02:00
return connected? array_length(lines) - 1 : 1;
2023-10-09 16:07:33 +02:00
} #endregion
2023-07-31 20:06:44 +02:00
static getLength = function(index) { return is_array(length)? array_safe_get(length, index) : length; }
static getAccuLength = function(index) { return array_safe_get(lengthAcc, index, []); }
2023-10-09 16:07:33 +02:00
static getPointRatio = function(_rat, _ind = 0) { #region
2023-03-07 14:29:47 +01:00
var _p0, _p1;
var _x, _y;
2023-10-27 15:42:17 +02:00
if(connected) {
var _st = _rat * (array_length(lines) - 1);
_p0 = array_safe_get(lines, floor(_st) + 0,, ARRAY_OVERFLOW._default);
_p1 = array_safe_get(lines, floor(_st) + 1,, ARRAY_OVERFLOW._default);
2023-03-07 14:29:47 +01:00
2023-08-31 16:58:13 +02:00
if(!is_array(_p0)) return new __vec2();
if(!is_array(_p1)) return new __vec2();
2023-03-11 01:40:17 +01:00
2023-03-07 14:29:47 +01:00
_x = lerp(_p0[0], _p1[0], frac(_st));
_y = lerp(_p0[1], _p1[1], frac(_st));
2023-08-31 16:58:13 +02:00
return new __vec2( _x, _y );
2023-03-07 14:29:47 +01:00
} else {
2023-10-27 15:42:17 +02:00
_p0 = array_safe_get(lines, _ind * 2 + 0,, ARRAY_OVERFLOW._default);
_p1 = array_safe_get(lines, _ind * 2 + 1,, ARRAY_OVERFLOW._default);
2023-03-07 14:29:47 +01:00
2023-08-31 16:58:13 +02:00
if(!is_array(_p0)) return new __vec2();
if(!is_array(_p1)) return new __vec2();
2023-03-11 01:40:17 +01:00
2023-03-07 14:29:47 +01:00
_x = lerp(_p0[0], _p1[0], _rat);
_y = lerp(_p0[1], _p1[1], _rat);
2023-08-31 16:58:13 +02:00
return new __vec2( _x, _y );
2023-03-07 14:29:47 +01:00
}
2023-10-09 16:07:33 +02:00
} #endregion
2023-03-07 14:29:47 +01:00
2023-10-09 16:07:33 +02:00
static getPointDistance = function(_dist, ind = 0) { #region
2023-10-27 15:42:17 +02:00
if(connected) return getPointRatio(_dist / length);
else return getPointRatio(_dist / length[ind], ind);
2023-10-09 16:07:33 +02:00
} #endregion
2023-07-31 20:06:44 +02:00
2023-10-09 16:07:33 +02:00
static getBoundary = function() { #region
2023-08-02 19:11:57 +02:00
var boundary = new BoundingBox();
2023-10-27 15:42:17 +02:00
var lines = getInputData(0);
for( var i = 0, n = array_length(lines); i < n; i++ )
boundary.addPoint(lines[i][0], lines[i][1]);
2023-08-02 19:11:57 +02:00
return boundary;
2023-10-09 16:07:33 +02:00
} #endregion
2023-08-02 19:11:57 +02:00
2023-10-09 16:07:33 +02:00
static update = function() { #region
2023-10-27 15:42:17 +02:00
lines = [];
array_spread(getInputData(0), lines, 1);
connected = getInputData(1);
2023-03-07 14:29:47 +01:00
2023-10-27 15:42:17 +02:00
if(connected) {
2023-07-31 20:06:44 +02:00
length = 0;
lengthAcc = [];
2023-10-27 15:42:17 +02:00
for( var i = 1, n = array_length(lines); i < n; i++ ) {
var p0 = lines[i - 1];
var p1 = lines[i - 0];
2023-07-31 20:06:44 +02:00
var dist = point_distance(p0[0], p0[1], p1[0], p1[1]);
length += dist;
array_push(lengthAcc, length);
}
} else {
2023-10-27 15:42:17 +02:00
length = [];
2023-07-31 20:06:44 +02:00
lengthAcc = [];
2023-10-27 15:42:17 +02:00
var len = floor(array_length(lines) / 2) * 2;
2023-07-31 20:06:44 +02:00
for( var i = 0; i < len; i += 2 ) {
2023-10-27 15:42:17 +02:00
var p0 = lines[i + 0];
var p1 = lines[i + 1];
2023-07-31 20:06:44 +02:00
2023-10-27 15:42:17 +02:00
var p0x = array_safe_get(p0, 0);
var p0y = array_safe_get(p0, 1);
var p1x = array_safe_get(p1, 0);
var p1y = array_safe_get(p1, 1);
p0x = is_real(p0x)? p0x : 0;
p0y = is_real(p0y)? p0y : 0;
p1x = is_real(p1x)? p1x : 0;
p1y = is_real(p1y)? p1y : 0;
lines[i + 0] = [ p0x, p0y ];
lines[i + 1] = [ p1x, p1y ];
var dist = point_distance(p0x, p0y, p1x, p1y);
2023-07-31 20:06:44 +02:00
array_push(length, dist);
array_push(lengthAcc, [ dist ]);
}
}
2023-03-07 14:29:47 +01:00
outputs[| 0].setValue(self);
2023-10-09 16:07:33 +02:00
} #endregion
2023-03-07 14:29:47 +01:00
2023-10-09 16:07:33 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) { #region
2023-03-07 14:29:47 +01:00
var bbox = drawGetBbox(xx, yy, _s);
draw_sprite_fit(s_node_path_builder, 0, bbox.xc, bbox.yc, bbox.w, bbox.h);
2023-10-09 16:07:33 +02:00
} #endregion
2023-03-07 14:29:47 +01:00
}