Pixel-Composer/scripts/node_align_functions/node_align_functions.gml

111 lines
2.4 KiB
Text
Raw Normal View History

2023-05-08 10:50:42 +02:00
function node_halign(nodeList, alignment = fa_center) {
var amo = array_length(nodeList);
2023-05-08 10:50:42 +02:00
switch(alignment) {
case fa_left:
var x0 = 999999;
for( var i = 0; i < amo; i++ )
x0 = min(x0, nodeList[i].x);
2023-05-08 10:50:42 +02:00
for( var i = 0; i < amo; i++ )
nodeList[i].x = x0;
2023-05-08 10:50:42 +02:00
break;
case fa_center:
var xc = 0;
for( var i = 0; i < amo; i++ )
xc += nodeList[i].x + nodeList[i].w / 2;
2023-05-08 10:50:42 +02:00
xc /= amo;
for( var i = 0; i < amo; i++ )
nodeList[i].x = xc - nodeList[i].w / 2;
2023-05-08 10:50:42 +02:00
break;
case fa_right:
var x0 = -999999;
for( var i = 0; i < amo; i++ )
x0 = max(x0, nodeList[i].x + nodeList[i].w);
2023-05-08 10:50:42 +02:00
for( var i = 0; i < amo; i++ )
nodeList[i].x = x0 - nodeList[i].w;
2023-05-08 10:50:42 +02:00
break;
}
}
function node_valign(nodeList, alignment = fa_middle) {
var amo = array_length(nodeList);
2023-05-08 10:50:42 +02:00
switch(alignment) {
case fa_top:
var y0 = 999999;
for( var i = 0; i < amo; i++ )
y0 = min(y0, nodeList[i].y);
2023-05-08 10:50:42 +02:00
for( var i = 0; i < amo; i++ )
nodeList[i].y = y0;
2023-05-08 10:50:42 +02:00
break;
case fa_middle:
var yc = 0;
for( var i = 0; i < amo; i++ )
yc += nodeList[i].y + nodeList[i].h / 2;
2023-05-08 10:50:42 +02:00
yc /= amo;
for( var i = 0; i < amo; i++ )
nodeList[i].y = yc - nodeList[i].h / 2;
2023-05-08 10:50:42 +02:00
break;
case fa_bottom:
var y0 = -999999;
for( var i = 0; i < amo; i++ )
y0 = max(y0, nodeList[i].y + nodeList[i].h);
2023-05-08 10:50:42 +02:00
for( var i = 0; i < amo; i++ )
nodeList[i].y = y0 - nodeList[i].h;
2023-05-08 10:50:42 +02:00
break;
}
}
function node_hdistribute(nodeList) {
var amo = array_length(nodeList);
2023-05-08 10:50:42 +02:00
var nodes = ds_priority_create();
var x0 = 999999;
var x1 = -999999;
for( var i = 0; i < amo; i++ ) {
var _x = nodeList[i].x + nodeList[i].w / 2;
2023-05-08 10:50:42 +02:00
x0 = min(x0, _x);
x1 = max(x1, _x);
ds_priority_add(nodes, nodeList[i], _x);
2023-05-08 10:50:42 +02:00
}
var sp = (x1 - x0) / (amo - 1);
for( var i = 0; i < amo; i++ ) {
2023-07-25 20:12:40 +02:00
var _node = ds_priority_delete_min(nodes);
_node.x = x0 + sp * i - _node.w / 2;
2023-05-08 10:50:42 +02:00
}
ds_priority_destroy(nodes);
}
function node_vdistribute(nodeList) {
var amo = ds_list_size(nodeList);
var nodes = ds_priority_create();
var y0 = 999999;
var y1 = -999999;
for( var i = 0; i < amo; i++ ) {
var _y = nodeList[i].y + nodeList[i].h / 2;
2023-05-08 10:50:42 +02:00
y0 = min(y0, _y);
y1 = max(y1, _y);
ds_priority_add(nodes, nodeList[i], _y);
2023-05-08 10:50:42 +02:00
}
var sp = (y1 - y0) / (amo - 1);
for( var i = 0; i < amo; i++ ) {
2023-07-25 20:12:40 +02:00
var _node = ds_priority_delete_min(nodes);
_node.y = y0 + sp * i - _node.h / 2;
2023-05-08 10:50:42 +02:00
}
ds_priority_destroy(nodes);
}