Pixel-Composer/scripts/node_pack_sprites/node_pack_sprites.gml

128 lines
3.3 KiB
Text
Raw Normal View History

2023-03-19 09:17:39 +01:00
function Node_Pack_Sprites(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
2023-07-30 19:56:53 +02:00
name = "Pack Sprites";
2023-03-19 09:17:39 +01:00
2024-08-18 06:16:20 +02:00
newInput(0, nodeValue_Surface("Sprites", self));
2023-03-19 09:17:39 +01:00
2024-08-18 06:16:20 +02:00
newInput(1, nodeValue_Enum_Scroll("Algorithm", self, 0, { data: [ "Skyline", "Shelf", "Top left", "Best fit" ], update_hover: false }));
2023-03-19 09:17:39 +01:00
2024-08-18 06:16:20 +02:00
newInput(2, nodeValue_Int("Max width", self, 128));
2023-03-19 09:17:39 +01:00
2024-08-18 06:16:20 +02:00
newInput(3, nodeValue_Int("Max height", self, 128));
2023-03-23 13:38:50 +01:00
2024-08-18 06:16:20 +02:00
newInput(4, nodeValue_Int("Spacing", self, 0));
2023-03-19 09:17:39 +01:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Packed image", self, VALUE_TYPE.surface, noone));
2023-03-19 09:17:39 +01:00
2024-11-15 09:39:23 +01:00
newOutput(1, nodeValue_Output("Atlas data", self, VALUE_TYPE.atlas, []));
2023-03-19 09:17:39 +01:00
2023-10-18 14:58:55 +02:00
input_display_list = [ 0, 4, 1, 2, 3 ];
2023-03-23 13:38:50 +01:00
2024-03-14 14:35:19 +01:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) { #region
2024-08-08 06:57:51 +02:00
var rect = outputs[1].getValue();
var spac = getInputData(4);
2023-03-19 09:17:39 +01:00
draw_set_color(COLORS._main_accent);
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(rect); i < n; i++ ) {
2023-03-19 09:17:39 +01:00
var r = rect[i];
2023-07-30 19:56:53 +02:00
2023-10-06 11:51:11 +02:00
var _surf = r.getSurface();
var _sx = r.x;
var _sy = r.y;
2023-07-30 19:56:53 +02:00
if(!is_surface(_surf)) continue;
2023-09-08 21:37:36 +02:00
var _sw = surface_get_width_safe(_surf);
var _sh = surface_get_height_safe(_surf);
2023-07-30 19:56:53 +02:00
2023-03-19 09:17:39 +01:00
draw_rectangle(
2024-04-30 10:49:12 +02:00
_x + _s * (_sx),
_y + _s * (_sy),
_x + _s * (_sx + _sw),
_y + _s * (_sy + _sh), true);
2023-03-19 09:17:39 +01:00
}
2023-10-18 14:58:55 +02:00
} #endregion
2023-03-19 09:17:39 +01:00
2023-10-18 14:58:55 +02:00
static step = function() { #region
var algo = getInputData(1);
2023-03-19 09:17:39 +01:00
2024-08-08 06:57:51 +02:00
inputs[2].setVisible(algo == 1 || algo == 0);
inputs[3].setVisible(algo == 2 || algo == 0);
2023-10-18 14:58:55 +02:00
} #endregion
2023-03-19 09:17:39 +01:00
2023-10-18 14:58:55 +02:00
static update = function() { #region
var _inpt = getInputData(0);
var _algo = getInputData(1);
var _spac = getInputData(4);
2023-03-19 09:17:39 +01:00
if(!is_array(_inpt) || array_length(_inpt) == 0) return;
var _rects = [], _ind = 0;
2023-03-19 09:17:39 +01:00
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(_inpt); i < n; i++ ) {
2023-03-19 09:17:39 +01:00
var s = _inpt[i];
if(!is_surface(s)) continue;
_rects[_ind] = new SurfaceAtlas(s);
_rects[_ind].w = surface_get_width_safe(s) + _spac * 2;
_rects[_ind].h = surface_get_height_safe(s) + _spac * 2;
_ind++;
2023-03-19 09:17:39 +01:00
}
array_resize(_rects, _ind);
2023-03-19 09:17:39 +01:00
var pack;
switch(_algo) {
case 0 :
var _wid = getInputData(2);
var _hei = getInputData(3);
2023-03-19 09:17:39 +01:00
pack = sprite_pack_skyline(_rects, _wid, _hei);
break;
2024-04-30 10:49:12 +02:00
2023-03-19 09:17:39 +01:00
case 1 :
var _wid = getInputData(2);
2023-03-19 09:17:39 +01:00
pack = sprite_pack_shelf(_rects, _wid);
break;
2024-04-30 10:49:12 +02:00
2023-03-19 09:17:39 +01:00
case 2 :
var _hei = getInputData(3);
2023-03-19 09:17:39 +01:00
pack = sprite_pack_bottom_left(_rects, _hei);
break;
2024-04-30 10:49:12 +02:00
2023-03-19 09:17:39 +01:00
case 3 :
pack = sprite_pack_best_fit(_rects);
break;
}
2023-04-16 15:26:52 +02:00
var area = pack[0];
var rect = pack[1];
var atlas = [];
2023-03-19 09:17:39 +01:00
2024-06-26 06:38:57 +02:00
if(array_length(rect) < array_length(_rects)) {
var _txt = $"Not enought space, packed {array_length(rect)} out of {array_length(_rects)} images.";
logNode(_txt); noti_warning(_txt);
}
2023-10-18 14:58:55 +02:00
2024-08-08 06:57:51 +02:00
var _surf = outputs[0].getValue();
2023-03-19 09:17:39 +01:00
_surf = surface_verify(_surf, area.w, area.h, surface_get_format(_inpt[0]));
2024-08-08 06:57:51 +02:00
outputs[0].setValue(_surf);
2023-03-19 09:17:39 +01:00
surface_set_target(_surf);
DRAW_CLEAR
BLEND_OVERRIDE
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(rect); i < n; i++ ) {
2023-03-19 09:17:39 +01:00
var r = rect[i];
2023-04-16 15:26:52 +02:00
2023-10-18 14:58:55 +02:00
array_push(atlas, new SurfaceAtlas(r.surface.surface, r.x + _spac, r.y + _spac));
draw_surface_safe(r.surface.surface, r.x + _spac, r.y + _spac);
2023-03-19 09:17:39 +01:00
}
BLEND_NORMAL
surface_reset_target();
2024-08-08 06:57:51 +02:00
outputs[1].setValue(atlas);
2023-10-18 14:58:55 +02:00
} #endregion
2023-03-19 09:17:39 +01:00
}