Pixel-Composer/scripts/node_align_functions/node_align_functions.gml

268 lines
6.9 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
2024-12-02 05:31:44 +01:00
for( var i = 0; i < amo; i++ ) {
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].x, "x", "node x position" ]);
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].y, "y", "node y position" ]);
}
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;
2024-12-02 05:31:44 +01:00
2023-05-08 10:50:42 +02:00
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;
2024-12-02 05:31:44 +01:00
2023-05-08 10:50:42 +02:00
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
2024-12-02 05:31:44 +01:00
for( var i = 0; i < amo; i++ ) {
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].x, "x", "node x position" ]);
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].y, "y", "node y position" ]);
}
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;
2024-12-02 05:31:44 +01:00
2023-05-08 10:50:42 +02:00
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;
2024-12-02 05:31:44 +01:00
2023-05-08 10:50:42 +02:00
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) {
2024-05-23 02:17:13 +02:00
var amo = array_length(nodeList);
2023-05-08 10:50:42 +02:00
var nodes = ds_priority_create();
2024-12-02 05:31:44 +01:00
for( var i = 0; i < amo; i++ ) {
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].x, "x", "node x position" ]);
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].y, "y", "node y position" ]);
}
2023-05-08 10:50:42 +02:00
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) {
2024-05-23 02:17:13 +02:00
var amo = array_length(nodeList);
2023-05-08 10:50:42 +02:00
var nodes = ds_priority_create();
2024-12-02 05:31:44 +01:00
for( var i = 0; i < amo; i++ ) {
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].x, "x", "node x position" ]);
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].y, "y", "node y position" ]);
}
2023-05-08 10:50:42 +02:00
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);
}
2024-07-05 04:50:07 +02:00
function node_hdistribute_dist(nodeList, anchor = noone, distance = 0) {
var amo = array_length(nodeList);
var nodes = ds_priority_create();
2024-12-02 05:31:44 +01:00
for( var i = 0; i < amo; i++ ) {
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].x, "x", "node x position" ]);
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].y, "y", "node y position" ]);
}
var x0 = 999999;
var x1 = -999999;
for( var i = 0; i < amo; i++ ) {
var _x = nodeList[i].x + nodeList[i].w / 2;
x0 = min(x0, _x);
x1 = max(x1, _x);
ds_priority_add(nodes, nodeList[i], _x);
}
var ar = array_create(ds_priority_size(nodes));
for( var i = 0; i < amo; i++ )
ar[i] = ds_priority_delete_min(nodes);
ds_priority_destroy(nodes);
2024-07-05 04:50:07 +02:00
if(anchor == noone) anchor = ar[0];
var an_ind = array_find(ar, anchor);
var an_ind_x = anchor.x + anchor.w + distance;
for (var i = an_ind + 1, n = array_length(ar); i < n; i++) {
ar[i].x = an_ind_x;
an_ind_x += ar[i].w + distance;
}
var an_ind_x = anchor.x - distance;
for (var i = an_ind - 1; i >= 0; i--) {
ar[i].x = an_ind_x - ar[i].w;
an_ind_x -= ar[i].w + distance;
}
}
2024-07-05 04:50:07 +02:00
function node_vdistribute_dist(nodeList, anchor = noone, distance = 0) {
var amo = array_length(nodeList);
var nodes = ds_priority_create();
2024-12-02 05:31:44 +01:00
for( var i = 0; i < amo; i++ ) {
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].x, "x", "node x position" ]);
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].y, "y", "node y position" ]);
}
var y0 = 999999;
var y1 = -999999;
for( var i = 0; i < amo; i++ ) {
var _y = nodeList[i].y + nodeList[i].h / 2;
y0 = min(y0, _y);
y1 = max(y1, _y);
ds_priority_add(nodes, nodeList[i], _y);
}
var ar = array_create(ds_priority_size(nodes));
for( var i = 0; i < amo; i++ )
ar[i] = ds_priority_delete_min(nodes);
ds_priority_destroy(nodes);
2024-07-05 04:50:07 +02:00
if(anchor == noone) anchor = ar[0];
var an_ind = array_find(ar, anchor);
var an_ind_y = anchor.y + anchor.h + distance;
for (var i = an_ind + 1, n = array_length(ar); i < n; i++) {
ar[i].y = an_ind_y;
an_ind_y += ar[i].h + distance;
}
var an_ind_y = anchor.y - distance;
for (var i = an_ind - 1; i >= 0; i--) {
ar[i].y = an_ind_y - ar[i].h;
an_ind_y -= ar[i].h + distance;
}
}
2024-07-05 04:50:07 +02:00
function node_auto_align(nodeList) {
var h_avg = 0, h_var = 0;
var v_avg = 0, v_var = 0;
var amo = array_length(nodeList);
2024-12-02 05:31:44 +01:00
for( var i = 0; i < amo; i++ ) {
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].x, "x", "node x position" ]);
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].y, "y", "node y position" ]);
}
2024-07-05 04:50:07 +02:00
for( var i = 0; i < amo; i++ ) {
var _n = nodeList[i];
var _x = _n.x;
var _y = _n.y;
h_avg += _x;
v_avg += _y;
}
h_avg /= amo;
v_avg /= amo;
for( var i = 0; i < amo; i++ ) {
var _n = nodeList[i];
h_var += sqr(_n.x - h_avg);
v_var += sqr(_n.y - v_avg);
}
if(h_var < v_var) { node_halign(nodeList); node_vdistribute(nodeList); }
else if(v_var < h_var) { node_valign(nodeList); node_hdistribute(nodeList); }
2024-08-14 05:28:46 +02:00
}
function node_snap_grid(nodeList, spacing = 16) {
2024-12-02 05:31:44 +01:00
var amo = array_length(nodeList);
for( var i = 0; i < amo; i++ ) {
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].x, "x", "node x position" ]);
recordAction(ACTION_TYPE.var_modify, nodeList[i], [ nodeList[i].y, "y", "node y position" ]);
}
2024-08-14 05:28:46 +02:00
for (var i = 0, n = array_length(nodeList); i < n; i++) {
var _node = nodeList[i];
_node.x = value_snap(_node.x, spacing);
_node.y = value_snap(_node.y, spacing);
}
2024-07-05 04:50:07 +02:00
}