Pixel-Composer/scripts/node_sprite_stack/node_sprite_stack.gml

295 lines
8.6 KiB
Plaintext
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_Sprite_Stack(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2022-01-13 05:24:03 +01:00
name = "Sprite Stack";
2022-12-27 13:30:02 +01:00
dimension_index = 1;
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Surface("Base shape", self));
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Dimension(self));
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Int("Stack amount", self, 4));
2023-01-25 06:49:00 +01:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Vec2("Stack shift", self, [ 0, 1 ] ));
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(4, nodeValue_Vec2("Position", self, [ 0, 0 ] ))
2022-12-27 13:30:02 +01:00
.setUnitRef(function(index) { return getDimension(index); });
2022-12-27 04:00:50 +01:00
2024-08-18 06:16:20 +02:00
newInput(5, nodeValue_Rotation("Rotation", self, 0));
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(6, nodeValue_Color("Stack blend", self, c_white ));
2022-01-13 05:24:03 +01:00
2024-08-18 09:13:41 +02:00
newInput(7, nodeValue_Float("Alpha end", self, 1, "Alpha value for the last copy." ))
.setDisplay(VALUE_DISPLAY.slider);
2022-01-13 05:24:03 +01:00
2024-08-18 06:16:20 +02:00
newInput(8, nodeValue_Bool("Move base", self, false, "Make each copy move the original image." ));
2022-09-21 06:09:40 +02:00
2024-08-18 06:16:20 +02:00
newInput(9, nodeValue_Enum_Scroll("Highlight", self, 0, [ "None", "Color", "Inner pixel" ]));
2023-07-21 12:40:20 +02:00
2024-08-18 06:16:20 +02:00
newInput(10, nodeValue_Color("Highlight color", self, c_white));
2023-07-21 12:40:20 +02:00
2024-08-18 09:13:41 +02:00
newInput(11, nodeValue_Float("Highlight alpha", self, 1))
.setDisplay(VALUE_DISPLAY.slider);
2023-07-21 12:40:20 +02:00
2024-08-18 09:13:41 +02:00
newInput(12, nodeValue_Float("Array process", self, 1))
2023-10-18 14:58:55 +02:00
.setDisplay(VALUE_DISPLAY.enum_scroll, [ "Individual", "Combined" ]);
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone));
2022-09-21 06:09:40 +02:00
input_display_list = [
2023-10-18 14:58:55 +02:00
["Surface", false], 0, 1, 12,
2022-09-21 06:09:40 +02:00
["Stack", false], 2, 3, 8, 4, 5,
2024-01-08 08:10:50 +01:00
["Render", false], 6, 7, 9, 10,
2022-09-21 06:09:40 +02:00
];
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-10-18 14:58:55 +02:00
preview_custom = false;
preview_custom_surface = -1;
preview_custom_index = noone;
preview_custom_x = 0;
preview_custom_x_to = 0;
preview_custom_x_max = 0;
2024-08-06 14:04:41 +02:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
PROCESSOR_OVERLAY_CHECK
var pos = current_data[4];
2024-06-26 12:52:24 +02:00
var sck = current_data[3];
2022-01-13 05:24:03 +01:00
var px = _x + pos[0] * _s;
var py = _y + pos[1] * _s;
2024-06-26 12:52:24 +02:00
var sx = px + sck[0] * _s * 4;
var sy = py + sck[1] * _s * 4;
draw_set_color(COLORS._main_accent);
draw_line(px, py, sx, sy);
2024-07-02 12:18:32 +02:00
var _hov = false;
2024-08-08 06:57:51 +02:00
var hv = inputs[3].drawOverlay(hover, active, px, py, _s * 4, _mx, _my, _snx, _sny, 1); active &= hv; _hov |= hv;
var hv = inputs[4].drawOverlay(hover, active, _x, _y, _s, _mx, _my, _snx, _sny); active &= hv; _hov |= hv;
var hv = inputs[5].drawOverlay(hover, active, px, py, _s, _mx, _my, _snx, _sny); active &= hv; _hov |= hv;
2022-01-13 05:24:03 +01:00
2024-07-02 12:18:32 +02:00
return _hov;
2024-08-06 14:04:41 +02:00
}
2023-10-18 14:58:55 +02:00
2024-08-06 14:04:41 +02:00
static drawPreviewToolOverlay = function(hover, active, _mx, _my, _panel) {
2023-10-18 14:58:55 +02:00
var _surf = getInputData(0);
if(!is_array(_surf)) return false;
var _arry = getInputData(12);
if(_arry == 0) return false;
var prev_size = ui(48);
var sx = preview_custom_x + ui(8);
var sy = _panel.y1 - ui(8) - prev_size;
var hov = false;
preview_custom_index = noone;
preview_custom_x_max = 0;
for( var i = 0, n = array_length(_surf); i < n; i++ ) {
var _s = _surf[i];
var _sw = surface_get_width_safe(_s);
var _sh = surface_get_height_safe(_s);
2024-03-14 14:35:19 +01:00
var _ss = prev_size / max(_sw, _sh);
2023-10-18 14:58:55 +02:00
var _sx = sx + (prev_size / 2 - _sw * _ss / 2);
var _sy = sy + (prev_size / 2 - _sh * _ss / 2);
draw_surface_ext_safe(_s, _sx, _sy, _ss, _ss);
2024-03-14 14:35:19 +01:00
if(hover && point_in_rectangle(_mx, _my, _sx - ui(4), _sy, _sx + _sw * _ss + ui(4), _sy + _sh * _ss)) {
2023-10-18 14:58:55 +02:00
preview_custom_index = i;
2024-03-14 14:35:19 +01:00
draw_set_color(COLORS._main_accent);
} else
draw_set_color(COLORS.panel_preview_surface_outline);
draw_rectangle(_sx, _sy, _sx + _sw * _ss, _sy + _sh * _ss, true);
2023-10-18 14:58:55 +02:00
sx += _sw * _ss + ui(8);
preview_custom_x_max += _sw * _ss + ui(8);
2023-10-18 14:58:55 +02:00
}
preview_custom_x_max = max(preview_custom_x_max - _panel.w + ui(64), 0);
2024-06-26 12:52:24 +02:00
var hov = hover && point_in_rectangle(_mx, _my, 0, sy, _panel.x1, _panel.y1);
2023-10-18 14:58:55 +02:00
if(hov) {
if(mouse_wheel_down()) preview_custom_x_to -= ui(128);
if(mouse_wheel_up()) preview_custom_x_to += ui(128);
}
preview_custom_x_to = clamp(preview_custom_x_to, -preview_custom_x_max, 0);
preview_custom_x = lerp_float(preview_custom_x, preview_custom_x_to, 5);
return hov;
2024-08-06 14:04:41 +02:00
}
2022-01-13 05:24:03 +01:00
2024-08-06 14:04:41 +02:00
static preGetInputs = function() {
2024-08-08 06:57:51 +02:00
var _surf = inputs[ 0].getValue();
var _arry = inputs[12].getValue();
2023-10-18 14:58:55 +02:00
2024-08-08 06:57:51 +02:00
inputs[0].setArrayDepth(is_array(_surf) && _arry);
2024-08-06 14:04:41 +02:00
}
2023-10-18 14:58:55 +02:00
2024-08-06 14:04:41 +02:00
static step = function() {
var _high = getInputData(9);
2023-10-18 14:58:55 +02:00
var _surf = getInputData(0);
var _arry = getInputData(12);
2024-08-08 06:57:51 +02:00
inputs[ 2].setVisible(_arry == 0 || !is_array(_surf));
2023-07-21 12:40:20 +02:00
2024-08-08 06:57:51 +02:00
inputs[10].setVisible(_high);
inputs[11].setVisible(_high);
2023-10-18 14:58:55 +02:00
2024-08-08 06:57:51 +02:00
inputs[12].setVisible(is_array(_surf));
2023-10-18 14:58:55 +02:00
2024-08-06 14:04:41 +02:00
// custom preview
preview_custom = preview_custom_index != noone && is_array(_surf) && _arry;
if(preview_custom) drawPreviewCustom();
}
2023-07-21 12:40:20 +02:00
2024-08-06 14:04:41 +02:00
static drawPreviewCustom = function() {
2023-10-18 14:58:55 +02:00
var _in = getSingleValue(0);
var _dim = getSingleValue(1);
var _shf = getSingleValue(3);
var _pos = getSingleValue(4);
var _rot = getSingleValue(5);
var _col = getSingleValue(6);
var _alp = getSingleValue(7);
var _mov = getSingleValue(8);
_pos = [ _pos[0], _pos[1] ];
if(_mov) {
_pos[0] -= _shf[0] * _amo;
_pos[1] -= _shf[1] * _amo;
}
var _prev_s = noone;
var _prev_x = noone;
var _prev_y = noone;
preview_custom_surface = surface_verify(preview_custom_surface, _dim[0], _dim[1]);
surface_set_target(preview_custom_surface);
DRAW_CLEAR
for(var i = 0; i < array_length(_in); i++) {
var index = clamp(i, 0, array_length(_in) - 1);
var _surf = _in[index];
if(!is_surface(_surf)) continue;
var _ww = surface_get_width_safe(_surf);
var _hh = surface_get_height_safe(_surf);
var _po = point_rotate(0, 0, _ww / 2, _hh / 2, _rot);
var _aa = i == preview_custom_index? 1 : 0.2;
if(i == preview_custom_index) {
_prev_s = _surf;
_prev_x = _po[0] + _pos[0];
_prev_y = _po[1] + _pos[1];
}
draw_surface_ext_safe(_surf, _po[0] + _pos[0], _po[1] + _pos[1], 1, 1, _rot, _col, 0.2);
_pos[0] += _shf[0];
_pos[1] += _shf[1];
}
if(is_surface(_prev_s))
draw_surface_ext_safe(_prev_s, _prev_x, _prev_y, 1, 1, _rot, _col, 1);
surface_reset_target();
2024-08-06 14:04:41 +02:00
}
2023-10-18 14:58:55 +02:00
2024-08-06 14:04:41 +02:00
static processData = function(_outSurf, _data, _output_index, _array_index) {
2023-07-21 12:40:20 +02:00
var _in = _data[0];
var _dim = _data[1];
var _amo = _data[2];
var _shf = _data[3];
var _pos = _data[4];
var _rot = _data[5];
var _col = _data[6];
var _alp = _data[7];
var _mov = _data[8];
2022-01-13 05:24:03 +01:00
2023-07-21 12:40:20 +02:00
var _hig = _data[ 9];
var _hiC = _data[10];
2024-03-14 14:35:19 +01:00
var _hiA = _data[11];
2023-10-18 14:58:55 +02:00
var _arr = _data[12];
2022-01-13 05:24:03 +01:00
2023-10-18 14:58:55 +02:00
_pos = [ _pos[0], _pos[1] ];
2023-03-19 09:17:39 +01:00
_outSurf = surface_verify(_outSurf, _dim[0], _dim[1], attrDepth());
2022-01-13 05:24:03 +01:00
2023-10-18 14:58:55 +02:00
if(is_array(_in) && _arr) _amo = array_length(_in);
2022-09-21 06:09:40 +02:00
if(_mov) {
_pos[0] -= _shf[0] * _amo;
_pos[1] -= _shf[1] * _amo;
}
2022-01-13 05:24:03 +01:00
surface_set_target(_outSurf);
2023-03-19 09:17:39 +01:00
DRAW_CLEAR
2022-09-21 06:09:40 +02:00
if(is_surface(_in)) {
2023-09-08 21:37:36 +02:00
var _ww = surface_get_width_safe(_in);
var _hh = surface_get_height_safe(_in);
2022-09-21 06:09:40 +02:00
var _po = point_rotate(0, 0, _ww / 2, _hh / 2, _rot);
var aa = _alp;
var aa_delta = (1 - aa) / _amo;
2023-10-18 14:58:55 +02:00
2022-09-21 06:09:40 +02:00
_pos[0] += _shf[0] * _amo;
_pos[1] += _shf[1] * _amo;
2022-01-13 05:24:03 +01:00
2023-07-21 12:40:20 +02:00
for( var i = 0; i < _amo; i++ ) {
if(_hig && i == _amo - 1) {
shader_set(sh_replace_color);
2024-06-24 13:49:54 +02:00
shader_set_i("type", _hig);
2023-07-21 12:40:20 +02:00
shader_set_f("dimension", _ww, _hh);
2024-06-24 13:49:54 +02:00
shader_set_f("shift", _shf[0] / _ww, _shf[1] / _hh);
shader_set_f("angle", degtorad(_rot));
2024-01-08 08:10:50 +01:00
draw_surface_ext_safe(_in, _po[0] + _pos[0], _po[1] + _pos[1], 1, 1, _rot, _hiC, _color_get_alpha(_hiC));
2023-07-21 12:40:20 +02:00
shader_reset();
} else
2024-01-08 08:10:50 +01:00
draw_surface_ext_safe(_in, _po[0] + _pos[0], _po[1] + _pos[1], 1, 1, _rot, _col, _color_get_alpha(_col) * aa);
2022-09-21 06:09:40 +02:00
_pos[0] -= _shf[0];
_pos[1] -= _shf[1];
2022-01-13 05:24:03 +01:00
2022-09-21 06:09:40 +02:00
aa += aa_delta;
2022-01-13 05:24:03 +01:00
}
2023-07-21 12:40:20 +02:00
2022-09-21 06:09:40 +02:00
draw_surface_ext_safe(_in, _po[0] + _pos[0], _po[1] + _pos[1], 1, 1, _rot, c_white, aa);
} else if(is_array(_in)) {
2022-01-13 05:24:03 +01:00
for(var i = 0; i < _amo; i++) {
var index = clamp(i, 0, array_length(_in) - 1);
2023-10-18 14:58:55 +02:00
var _surf = _in[index];
if(!is_surface(_surf)) continue;
var _ww = surface_get_width_safe(_surf);
var _hh = surface_get_height_safe(_surf);
var _po = point_rotate(0, 0, _ww / 2, _hh / 2, _rot);
2024-01-08 08:10:50 +01:00
draw_surface_ext_safe(_surf, _po[0] + _pos[0], _po[1] + _pos[1], 1, 1, _rot, _col, _color_get_alpha(_col));
2023-10-18 14:58:55 +02:00
_pos[0] += _shf[0];
_pos[1] += _shf[1];
2022-01-13 05:24:03 +01:00
}
}
surface_reset_target();
2022-12-27 04:00:50 +01:00
return _outSurf;
2024-08-06 14:04:41 +02:00
}
2023-10-18 14:58:55 +02:00
2024-08-06 14:04:41 +02:00
static getPreviewValues = function() {
2023-10-18 14:58:55 +02:00
if(preview_custom && is_surface(preview_custom_surface)) return preview_custom_surface;
2024-08-08 06:57:51 +02:00
if(preview_channel >= array_length(outputs)) return noone;
2023-10-18 14:58:55 +02:00
2024-08-08 06:57:51 +02:00
switch(outputs[preview_channel].type) {
2023-10-18 14:58:55 +02:00
case VALUE_TYPE.surface :
case VALUE_TYPE.dynaSurface :
break;
default :
return;
}
2024-08-08 06:57:51 +02:00
return outputs[preview_channel].getValue();
2024-08-06 14:04:41 +02:00
}
2022-01-13 05:24:03 +01:00
}