Pixel-Composer/scripts/node_path_shift/node_path_shift.gml

97 lines
3 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Path_Shift(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Shift Path";
2025-01-01 02:12:36 +01:00
setDimension(96, 48);
2023-02-14 02:48:33 +01:00
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_PathNode("Path", self, noone))
2023-02-14 02:48:33 +01:00
.setVisible(true, true);
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Float("Distance", self, 0));
2023-02-14 02:48:33 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Path", self, VALUE_TYPE.pathnode, self));
2023-02-14 02:48:33 +01:00
cached_pos = ds_map_create();
2025-01-16 04:39:54 +01:00
curr_path = noone;
is_path = false;
curr_shift = noone;
2024-08-13 13:17:45 +02:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
2025-01-16 04:39:54 +01:00
if(curr_path && struct_has(curr_path, "drawOverlay"))
curr_path.drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny);
2024-01-22 14:23:35 +01:00
draw_set_color(COLORS._main_icon);
var _amo = getLineCount();
2025-01-16 04:39:54 +01:00
var _p = new __vec2P();
2024-01-22 14:23:35 +01:00
for( var i = 0; i < _amo; i++ ) {
var _len = getLength(_amo);
var _stp = 1 / clamp(_len * _s, 1, 64);
var ox, oy, nx, ny;
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;
}
}
2024-08-13 13:17:45 +02:00
}
2024-01-22 14:23:35 +01:00
2025-01-16 04:39:54 +01:00
static getLineCount = function( ) /*=>*/ {return is_path? curr_path.getLineCount() : 1};
static getSegmentCount = function(ind = 0) /*=>*/ {return is_path? curr_path.getSegmentCount(ind) : 0};
static getLength = function(ind = 0) /*=>*/ {return is_path? curr_path.getLength(ind) : 0};
static getAccuLength = function(ind = 0) /*=>*/ {return is_path? curr_path.getAccuLength(ind) : []};
static getBoundary = function(ind = 0) /*=>*/ {return is_path? curr_path.getBoundary(ind) : new BoundingBox( 0, 0, 1, 1 )};
2023-03-19 09:17:39 +01:00
2024-08-13 13:17:45 +02:00
static getPointRatio = function(_rat, ind = 0, out = undefined) {
2025-01-15 07:55:00 +01:00
if(out == undefined) out = new __vec2P(); else { out.x = 0; out.y = 0; }
2023-10-29 06:29:10 +01:00
2024-12-28 07:29:49 +01:00
var _cKey = $"{string_format(_rat, 0, 6)},{ind}";
if(ds_map_exists(cached_pos, _cKey)) {
var _p = cached_pos[? _cKey];
out.x = _p.x;
out.y = _p.y;
2025-01-15 07:55:00 +01:00
out.weight = _p.weight;
return out;
}
2025-01-16 04:39:54 +01:00
if(!is_path) return out;
2023-02-14 02:48:33 +01:00
2025-01-16 04:39:54 +01:00
var _p0 = curr_path.getPointRatio(clamp(_rat - 0.001, 0, 0.999999), ind);
var _p = curr_path.getPointRatio(_rat, ind);
var _p1 = curr_path.getPointRatio(clamp(_rat + 0.001, 0, 0.999999), ind);
2023-02-14 02:48:33 +01:00
2023-03-19 09:17:39 +01:00
var dir = point_direction(_p0.x, _p0.y, _p1.x, _p1.y) + 90;
2023-02-14 02:48:33 +01:00
2025-01-16 04:39:54 +01:00
out.x += _p.x + lengthdir_x(curr_shift, dir);
out.y += _p.y + lengthdir_y(curr_shift, dir);
2025-01-15 07:55:00 +01:00
out.weight = _p.weight;
2023-02-14 02:48:33 +01:00
2025-01-15 07:55:00 +01:00
cached_pos[? _cKey] = new __vec2P(out.x, out.y, out.weight);
2023-10-29 06:29:10 +01:00
return out;
2024-08-13 13:17:45 +02:00
}
2023-02-14 02:48:33 +01:00
2023-10-29 06:29:10 +01:00
static getPointDistance = function(_dist, ind = 0, out = undefined) { return getPointRatio(_dist / getLength(), ind, out); }
2023-03-19 09:17:39 +01:00
2024-08-13 13:17:45 +02:00
static update = function() {
2025-01-16 04:39:54 +01:00
curr_path = getInputData(0);
is_path = curr_path != noone && struct_has(curr_path, "getPointRatio");
curr_shift = getInputData(1);
ds_map_clear(cached_pos);
2024-08-08 06:57:51 +02:00
outputs[0].setValue(self);
2024-08-13 13:17:45 +02:00
}
2023-02-14 02:48:33 +01:00
2024-08-13 13:17:45 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-02-14 02:48:33 +01:00
var bbox = drawGetBbox(xx, yy, _s);
2023-02-14 11:40:24 +01:00
draw_sprite_fit(s_node_path_shift, 0, bbox.xc, bbox.yc, bbox.w, bbox.h);
2024-08-13 13:17:45 +02:00
}
2023-02-14 02:48:33 +01:00
}