Pixel-Composer/scripts/node_9slice/node_9slice.gml

185 lines
6.1 KiB
Text
Raw Normal View History

2023-02-28 09:43:01 +01:00
function Node_9Slice(_x, _y, _group = noone) : Node_Processor(_x, _y, _group) constructor {
2023-01-25 06:49:00 +01:00
name = "Nine Slice";
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 0] = nodeValue("Surface in", self, JUNCTION_CONNECT.input, VALUE_TYPE.surface, 0);
2022-12-27 04:00:50 +01:00
2023-07-21 12:40:20 +02:00
inputs[| 1] = nodeValue("Dimension", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, DEF_SURF)
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.vector);
2023-02-14 05:32:32 +01:00
inputs[| 2] = nodeValue("Splice", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, [ 0, 0, 0, 0 ])
2022-12-27 04:00:50 +01:00
.setDisplay(VALUE_DISPLAY.padding)
2022-12-27 13:30:02 +01:00
.setUnitRef(function(index) { return getDimension(index); });
2022-01-13 05:24:03 +01:00
2023-02-14 05:32:32 +01:00
inputs[| 3] = nodeValue("Filling modes", self, JUNCTION_CONNECT.input, VALUE_TYPE.integer, 0)
2022-01-13 05:24:03 +01:00
.setDisplay(VALUE_DISPLAY.enum_scroll, [ "Scale", "Repeat" ]);
2023-02-14 05:32:32 +01:00
outputs[| 0] = nodeValue("Surface out", self, JUNCTION_CONNECT.output, VALUE_TYPE.surface, noone);
2022-01-13 05:24:03 +01:00
2023-03-19 09:17:39 +01:00
attribute_surface_depth();
2023-03-21 03:01:53 +01:00
attribute_interpolation();
2023-03-19 09:17:39 +01:00
2022-01-13 05:24:03 +01:00
drag_side = -1;
drag_mx = 0;
drag_my = 0;
drag_sv = 0;
2023-01-09 03:14:20 +01:00
static onValueFromUpdate = function(index) {
2022-01-13 05:24:03 +01:00
if(index == 0) {
var s = inputs[| 0].getValue();
if(is_array(s)) s = s[0];
inputs[| 1].setValue([surface_get_width(s), surface_get_height(s)]);
}
}
2022-12-19 13:35:30 +01:00
static drawOverlay = function(active, _x, _y, _s, _mx, _my, _snx, _sny) {
2022-12-18 03:20:38 +01:00
if(array_length(current_data) < 1) return;
var _dim = current_data[1];
var _splice = current_data[2];
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(_splice); i < n; i++ )
2022-12-27 04:00:50 +01:00
_splice[i] = round(_splice[i]);
2022-01-13 05:24:03 +01:00
var sp_r = _x + (_dim[0] - _splice[0]) * _s;
var sp_l = _x + _splice[2] * _s;
var sp_t = _y + _splice[1] * _s;
var sp_b = _y + (_dim[1] - _splice[3]) * _s;
var ww = WIN_W;
var hh = WIN_H;
2022-11-18 03:20:31 +01:00
draw_set_color(COLORS._main_accent);
2022-01-13 05:24:03 +01:00
draw_line(sp_r, -hh, sp_r, hh);
draw_line(sp_l, -hh, sp_l, hh);
draw_line(-ww, sp_t, ww, sp_t);
draw_line(-ww, sp_b, ww, sp_b);
if(drag_side > -1) {
var vv;
if(drag_side == 0) vv = drag_sv - (_mx - drag_mx) / _s;
else if(drag_side == 2) vv = drag_sv + (_mx - drag_mx) / _s;
else if(drag_side == 1) vv = drag_sv + (_my - drag_my) / _s;
else vv = drag_sv - (_my - drag_my) / _s;
_splice[drag_side] = vv;
if(inputs[| 2].setValue(_splice))
UNDO_HOLDING = true;
2022-12-10 05:06:01 +01:00
if(mouse_release(mb_left)) {
2022-01-13 05:24:03 +01:00
drag_side = -1;
UNDO_HOLDING = false;
}
}
2023-01-01 02:06:02 +01:00
if(inputs[| 1].drawOverlay(active, _x, _y, _s, _mx, _my, _snx, _sny))
return;
2022-12-27 04:00:50 +01:00
if(distance_to_line_infinite(_mx, _my, sp_r, -hh, sp_r, hh) < 12) {
draw_line_width(sp_r, -hh, sp_r, hh, 3);
if(mouse_press(mb_left, active)) {
drag_side = 0;
drag_mx = _mx;
drag_my = _my;
drag_sv = _splice[0];
}
} else if(distance_to_line_infinite(_mx, _my, -ww, sp_t, ww, sp_t) < 12) {
draw_line_width(-ww, sp_t, ww, sp_t, 3);
if(mouse_press(mb_left, active)) {
drag_side = 1;
drag_mx = _mx;
drag_my = _my;
drag_sv = _splice[1];
}
} else if(distance_to_line_infinite(_mx, _my, sp_l, -hh, sp_l, hh) < 12) {
draw_line_width(sp_l, -hh, sp_l, hh, 3);
if(mouse_press(mb_left, active)) {
drag_side = 2;
drag_mx = _mx;
drag_my = _my;
drag_sv = _splice[2];
}
} else if(distance_to_line_infinite(_mx, _my, -ww, sp_b, ww, sp_b) < 12) {
draw_line_width(-ww, sp_b, ww, sp_b, 3);
if(mouse_press(mb_left, active)) {
drag_side = 3;
drag_mx = _mx;
drag_my = _my;
drag_sv = _splice[3];
2022-01-13 05:24:03 +01:00
}
}
}
2023-08-17 16:56:54 +02:00
static processData = function(_outSurf, _data, _output_index, _array_index) {
2022-01-13 05:24:03 +01:00
var _inSurf = _data[0];
var _dim = _data[1];
var _splice = _data[2];
var _fill = _data[3];
if(!surface_exists(_inSurf)) return;
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-03-21 03:01:53 +01:00
surface_set_shader(_outSurf);
shader_set_interpolation(_inSurf);
2022-01-13 05:24:03 +01:00
var ww = _dim[0];
var hh = _dim[1];
var in_w = surface_get_width(_inSurf);
var in_h = surface_get_height(_inSurf);
var sp_r = _splice[0];
var sp_t = _splice[1];
var sp_l = _splice[2];
var sp_b = _splice[3];
var cw = max(in_w - sp_l - sp_r, 1);
var ch = max(in_h - sp_t - sp_b, 1);
var sw = (ww - sp_l - sp_r) / cw;
var sh = (hh - sp_t - sp_b) / ch;
2023-04-05 20:13:27 +02:00
draw_surface_part_ext_safe(_inSurf, 0, 0, sp_l, sp_t, 0, 0, 1, 1, 0, c_white, 1);
draw_surface_part_ext_safe(_inSurf, in_w - sp_r, 0, sp_r, sp_t, ww - sp_r, 0, 1, 1, 0, c_white, 1);
draw_surface_part_ext_safe(_inSurf, 0, in_h - sp_b, sp_l, sp_b, 0, hh - sp_b, 1, 1, 0, c_white, 1);
draw_surface_part_ext_safe(_inSurf, in_w - sp_r, in_h - sp_b, sp_r, sp_b, ww - sp_r, hh - sp_b, 1, 1, 0, c_white, 1);
2022-01-13 05:24:03 +01:00
if(_fill == 0) {
2023-04-05 20:13:27 +02:00
draw_surface_part_ext_safe(_inSurf, sp_l, 0, cw, sp_t, sp_l, 0, sw, 1, 0, c_white, 1);
draw_surface_part_ext_safe(_inSurf, sp_l, in_h - sp_b, cw, sp_b, sp_l, hh - sp_b, sw, 1, 0, c_white, 1);
2022-01-13 05:24:03 +01:00
2023-04-05 20:13:27 +02:00
draw_surface_part_ext_safe(_inSurf, 0, sp_t, sp_l, ch, 0, sp_t, 1, sh, 0, c_white, 1);
draw_surface_part_ext_safe(_inSurf, in_w - sp_r, sp_t, sp_r, ch, ww - sp_r, sp_t, 1, sh, 0, c_white, 1);
2022-01-13 05:24:03 +01:00
2023-04-14 12:23:25 +02:00
draw_surface_part_ext_safe(_inSurf, sp_l, sp_t, cw, ch, sp_l, sp_t, sw, sh, 0, c_white, 1);
2022-01-13 05:24:03 +01:00
} else if(_fill == 1) {
var _cw_max = ww - sp_r;
var _ch_max = hh - sp_b;
var _x = sp_l;
while(_x < _cw_max) {
2023-04-05 20:13:27 +02:00
draw_surface_part_ext_safe(_inSurf, sp_l, 0, min(cw, _cw_max - _x), sp_t, _x, 0, 1, 1, 0, c_white, 1);
draw_surface_part_ext_safe(_inSurf, sp_l, in_h - sp_b, min(cw, _cw_max - _x), sp_b, _x, hh - sp_b, 1, 1, 0, c_white, 1);
2022-01-13 05:24:03 +01:00
_x += cw;
}
var _y = sp_t;
while(_y < _ch_max) {
2023-04-05 20:13:27 +02:00
draw_surface_part_ext_safe(_inSurf, 0, sp_t, sp_l, min(ch, _ch_max - _y), 0, _y, 1, 1, 0, c_white, 1);
draw_surface_part_ext_safe(_inSurf, in_w - sp_r, sp_t, sp_r, min(ch, _ch_max - _y), ww - sp_r, _y, 1, 1, 0, c_white, 1);
2022-01-13 05:24:03 +01:00
_y += ch;
}
_x = sp_l;
_y = sp_t;
while(_x < _cw_max) {
_y = sp_t;
while(_y < _ch_max) {
2023-04-05 20:13:27 +02:00
draw_surface_part_ext_safe(_inSurf, sp_l, sp_t, min(cw, _cw_max - _x), min(ch, _ch_max - _y), _x, _y, 1, 1, 0, c_white, 1);
2022-01-13 05:24:03 +01:00
_y += ch;
}
_x += cw;
}
}
2023-03-21 03:01:53 +01:00
surface_reset_shader();
2022-12-27 04:00:50 +01:00
return _outSurf;
2022-01-13 05:24:03 +01:00
}
}