Pixel-Composer/scripts/node_image_grid/node_image_grid.gml

148 lines
3.9 KiB
Plaintext
Raw Normal View History

2024-01-21 03:55:31 +01:00
function Node_Image_Grid(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Image Grid";
2024-08-20 10:15:53 +02:00
newInput(0, nodeValue_Enum_Scroll("Main Axis", self, 0, [ new scrollItem("Horizontal", s_node_alignment, 0),
new scrollItem("Vertical", s_node_alignment, 1) ]))
2024-01-21 03:55:31 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(1, nodeValue_Int("Column", self, 4))
.setValidator(VV_min(1))
2024-01-21 03:55:31 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(2, nodeValue_Vec2("Spacing", self, [ 0, 0 ]))
2024-01-21 03:55:31 +01:00
.rejectArray();
2024-08-18 09:13:41 +02:00
newInput(3, nodeValue_Padding("Padding", self, [ 0, 0, 0, 0 ]))
2024-01-21 03:55:31 +01:00
.rejectArray();
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Surface out", self, VALUE_TYPE.surface, noone));
2024-01-21 03:55:31 +01:00
2024-09-04 03:57:11 +02:00
newOutput(1, nodeValue_Output("Atlas data", self, VALUE_TYPE.surface, []));
2024-01-21 03:55:31 +01:00
temp_surface = [ noone, noone ];
2024-05-23 10:59:39 +02:00
static createNewInput = function() {
2024-08-08 06:57:51 +02:00
var index = array_length(inputs);
2024-08-18 09:13:41 +02:00
newInput(index, nodeValue_Surface("Input", self))
2024-05-23 10:59:39 +02:00
.setVisible(true, true);
2024-08-08 06:57:51 +02:00
return inputs[index];
2024-05-23 10:59:39 +02:00
} setDynamicInput(1, true, VALUE_TYPE.surface);
2024-01-21 03:55:31 +01:00
2024-05-23 10:59:39 +02:00
attribute_surface_depth();
2024-01-21 03:55:31 +01:00
static update = function(frame = CURRENT_FRAME) { #region
var _axis = getInputData(0);
var _col = getInputData(1);
var _spac = getInputData(2);
var _padd = getInputData(3);
var ww = 0;
var hh = 0;
var surfs = [];
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i++ ) {
2024-01-21 03:55:31 +01:00
var _surf = getInputData(i);
if(!is_array(_surf)) _surf = [ _surf ];
array_append(surfs, _surf);
}
var _coli = 0;
var _mainw = 0;
var _subw = 0;
var _mains = 0;
var _subs = 0;
for( var j = 0; j < array_length(surfs); j++ ) {
var _s = surfs[j];
if(!is_surface(_s)) continue;
var sw = surface_get_width_safe(_s);
var sh = surface_get_height_safe(_s);
if(_axis == 0) { _mains += sw + _spac[0]; _subs = max(_subs, sh + _spac[1]); }
else { _mains += sh + _spac[1]; _subs = max(_subs, sw + _spac[0]); }
_coli++;
if(_coli >= _col) {
_coli = 0;
_mainw = max(_mainw, _mains);
_subw += _subs;
_mains = 0;
_subs = 0;
}
}
_mainw = max(_mainw, _mains);
_subw += _subs;
if(_axis == 0) { ww = _mainw - _spac[0]; hh = _subw - _spac[1]; }
else { hh = _mainw - _spac[1]; ww = _subw - _spac[0]; }
ww += _padd[PADDING.left] + _padd[PADDING.right];
hh += _padd[PADDING.top] + _padd[PADDING.bottom];
2024-08-08 06:57:51 +02:00
var _outSurf = outputs[0].getValue();
2024-01-21 03:55:31 +01:00
_outSurf = surface_verify(_outSurf, ww, hh, attrDepth());
temp_surface[0] = surface_verify(temp_surface[0], ww, hh, attrDepth());
temp_surface[1] = surface_verify(temp_surface[1], ww, hh, attrDepth());
surface_clear(temp_surface[0]);
surface_clear(temp_surface[1]);
var atlas = [];
var ppind = 0;
var sx = 0;
var sy = 0;
var _mains = 0;
var _subs = 0;
var _coli = 0;
for( var j = 0; j < array_length(surfs); j++ ) {
var _s = surfs[j];
if(!is_surface(_s)) continue;
var sw = surface_get_width_safe(_s);
var sh = surface_get_height_safe(_s);
2024-04-13 07:32:04 +02:00
array_push(atlas, new SurfaceAtlas(_s, sx, sy));
2024-01-21 03:55:31 +01:00
surface_set_shader(temp_surface[!ppind], sh_draw_surface);
shader_set_f("dimension", ww, hh);
2024-04-13 07:32:04 +02:00
shader_set_surface("fore", _s);
2024-01-21 03:55:31 +01:00
shader_set_f("fdimension", sw, sh);
shader_set_f("position", sx + _padd[PADDING.left], sy + _padd[PADDING.top]);
draw_surface_safe(temp_surface[ppind]);
2024-01-21 03:55:31 +01:00
surface_reset_shader();
ppind = !ppind;
if(_axis == 0) { sx += sw + _spac[0]; _subs = max(_subs, sh); }
else { sy += sh + _spac[1]; _subs = max(_subs, sw); }
_coli++;
if(_coli >= _col) {
_coli = 0;
if(_axis == 0) { sy += _subs + _spac[1]; sx = 0; }
else { sx += _subs + _spac[0]; sy = 0; }
}
}
surface_set_shader(_outSurf, noone);
draw_surface_safe(temp_surface[ppind]);
2024-01-21 03:55:31 +01:00
surface_reset_shader();
2024-08-08 06:57:51 +02:00
outputs[0].setValue(_outSurf);
outputs[1].setValue(atlas);
2024-01-21 03:55:31 +01:00
} #endregion
}