Pixel-Composer/scripts/node_armature_pose/node_armature_pose.gml

362 lines
10 KiB
Text
Raw Normal View History

function Node_Armature_Pose(_x, _y, _group = noone) : Node(_x, _y, _group) constructor {
name = "Armature Pose";
2024-03-28 14:18:02 +01:00
setDimension(96, 72);
2023-06-25 20:12:17 +02:00
2024-08-18 09:13:41 +02:00
newInput(0, nodeValue_Armature("Armature", self, noone))
2023-06-23 15:39:24 +02:00
.setVisible(true, true);
input_display_list = [ 0 ]
2023-06-21 20:36:53 +02:00
2024-09-04 03:57:11 +02:00
newOutput(0, nodeValue_Output("Armature", self, VALUE_TYPE.armature, noone));
2024-11-27 05:32:43 +01:00
boneHash = "";
bonePose = noone;
boneMap = {};
2023-06-21 20:36:53 +02:00
attributes.display_name = true;
2023-07-05 15:09:52 +02:00
attributes.display_bone = 0;
array_push(attributeEditors, "Display");
2024-11-26 11:28:04 +01:00
array_push(attributeEditors, ["Display name", function() /*=>*/ {return attributes.display_name}, new checkBox(function() /*=>*/ { attributes.display_name = !attributes.display_name; })]);
array_push(attributeEditors, ["Display bone", function() /*=>*/ {return attributes.display_bone}, new scrollBox(["Octahedral", "Stick"], function(ind) /*=>*/ { attributes.display_bone = ind; })]);
2023-06-21 20:36:53 +02:00
2024-05-23 10:59:39 +02:00
static createNewInput = function(bone = noone) {
2024-08-08 06:57:51 +02:00
var index = array_length(inputs);
2023-06-21 20:36:53 +02:00
2024-08-20 10:15:53 +02:00
newInput(index, nodeValue(bone != noone? bone.name : "bone", self, CONNECT_TYPE.input, VALUE_TYPE.float, [ 0, 0, 0, 1 ] ))
2023-06-21 20:36:53 +02:00
.setDisplay(VALUE_DISPLAY.transform);
2024-11-23 12:08:44 +01:00
inputs[index].attributes.bone_id = bone != noone? bone.ID : noone;
2023-06-21 20:36:53 +02:00
2024-11-12 01:41:42 +01:00
if(bone != noone) boneMap[$ bone.ID] = inputs[index];
2023-06-21 20:36:53 +02:00
array_push(input_display_list, index);
2023-06-23 15:39:24 +02:00
2024-08-08 06:57:51 +02:00
return inputs[index];
2024-11-26 11:28:04 +01:00
}
setDynamicInput(1, false);
2023-06-21 20:36:53 +02:00
2024-08-06 14:04:41 +02:00
static setBone = function() {
2024-11-27 05:32:43 +01:00
// print("Setting dem bones...");
var _b = getInputData(0);
2023-06-21 20:36:53 +02:00
if(_b == noone) return;
2024-11-27 05:32:43 +01:00
bonePose = _b.clone().connect();
var _bArr = _b.toArray();
2024-08-08 06:57:51 +02:00
var _inputs = [ inputs[0] ];
2023-06-21 20:36:53 +02:00
var _input_display_list = [
input_display_list[0],
input_display_list[1]
];
2024-11-27 05:32:43 +01:00
for( var i = 0, n = array_length(_bArr); i < n; i++ ) {
var bone = _bArr[i];
2024-08-08 06:57:51 +02:00
var _idx = array_length(_inputs);
2024-11-26 11:28:04 +01:00
var _inp;
2023-06-23 15:39:24 +02:00
array_push(_input_display_list, _idx);
2024-11-12 01:41:42 +01:00
if(struct_exists(boneMap, bone.ID)) {
2024-11-26 11:28:04 +01:00
_inp = boneMap[$ bone.ID];
2023-06-21 20:36:53 +02:00
_inp.index = _idx;
2024-11-26 11:28:04 +01:00
} else
_inp = createNewInput(bone);
array_push(_inputs, _inp);
2023-06-21 20:36:53 +02:00
}
inputs = _inputs;
input_display_list = _input_display_list;
2024-08-06 14:04:41 +02:00
}
2023-06-21 20:36:53 +02:00
tools = [];
2023-06-21 20:36:53 +02:00
anchor_selecting = noone;
2024-11-26 11:28:04 +01:00
posing_bone = noone;
posing_input = 0;
posing_type = 0;
2024-11-27 05:32:43 +01:00
2024-11-26 11:28:04 +01:00
posing_sx = 0;
posing_sy = 0;
posing_mx = 0;
posing_my = 0;
2023-06-21 20:36:53 +02:00
2024-08-06 14:04:41 +02:00
static drawOverlay = function(hover, active, _x, _y, _s, _mx, _my, _snx, _sny) {
2024-11-27 05:32:43 +01:00
var _b = inputs[0].getValue();
2023-06-21 20:36:53 +02:00
if(_b == noone) return;
2024-11-27 05:32:43 +01:00
if(bonePose == noone) return;
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
anchor_selecting = bonePose.draw(attributes, active * 0b111, _x, _y, _s, _mx, _my, anchor_selecting, posing_bone);
bonePose.drawControl(attributes);
2023-06-21 20:36:53 +02:00
var mx = (_mx - _x) / _s;
var my = (_my - _y) / _s;
2023-08-01 19:21:51 +02:00
var smx = value_snap(mx, _snx);
var smy = value_snap(my, _sny);
2024-08-06 14:04:41 +02:00
if(posing_bone) {
2024-11-27 05:32:43 +01:00
gpu_set_texfilter(true);
var val = array_clone(posing_input.getValue());
if(posing_type == 0) { //move
var ang = posing_bone.pose_local_rotate;
2023-08-01 19:21:51 +02:00
var pp = point_rotate(smx - posing_mx, smy - posing_my, 0, 0, -ang);
var bx = posing_sx + pp[0];
var by = posing_sy + pp[1];
2023-06-21 20:36:53 +02:00
val[TRANSFORM.pos_x] = bx;
val[TRANSFORM.pos_y] = by;
2024-11-27 05:32:43 +01:00
orig = posing_bone.getHead();
var _rx = _x + _s * orig.x;
var _ry = _y + _s * orig.y;
draw_sprite_ext(s_bone_move, 0, _rx, _ry, 1, 1, 0, COLORS._main_value_positive, 1);
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
} else if(posing_type == 1) { //free move
var _direction = point_direction(posing_sx, posing_sy, smx, smy);
var _distance = point_distance(posing_sx, posing_sy, smx, smy);
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
var _scale = _distance / (posing_bone.length * posing_bone.pose_local_scale);
var _rotation = _direction - (posing_bone.angle + posing_bone.pose_local_rotate);
2024-11-27 05:32:43 +01:00
val[TRANSFORM.sca_x] = _scale;
val[TRANSFORM.rot] = _rotation;
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
orig = posing_bone.getTail();
var _rx = _x + _s * orig.x;
var _ry = _y + _s * orig.y;
draw_sprite_ext(s_bone_move, 0, _rx, _ry, 1, 1, 0, COLORS._main_value_positive, 1);
2023-06-23 15:39:24 +02:00
} else if(posing_type == 2) { //rotate
var ori = posing_bone.getHead();
2023-06-23 15:39:24 +02:00
var ang = point_direction(ori.x, ori.y, mx, my);
var rot = angle_difference(ang, posing_sy);
posing_sy = ang;
posing_sx += rot;
2023-06-21 20:36:53 +02:00
2023-06-23 15:39:24 +02:00
val[TRANSFORM.rot] = posing_sx;
2023-08-08 18:45:00 +02:00
2024-11-27 05:32:43 +01:00
orig = posing_bone.getHead();
var _rx = _x + _s * orig.x;
var _ry = _y + _s * orig.y;
draw_sprite_ext(s_bone_rotate, 0, _rx, _ry, 1, 1, posing_bone.pose_angle, COLORS._main_value_positive, 1);
} else if(posing_type == 3) { //scale
var ss = point_distance(posing_mx, posing_my, smx, smy) / posing_sx;
val[TRANSFORM.sca_x] = ss;
orig = posing_bone.getPoint(0.8);
var _rx = _x + _s * orig.x;
var _ry = _y + _s * orig.y;
draw_sprite_ext(s_bone_scale, 0, _rx, _ry, 1, 1, posing_bone.pose_angle, COLORS._main_value_positive, 1);
}
gpu_set_texfilter(false);
2024-11-27 09:03:12 +01:00
if(posing_input.value_from == noone) {
if(posing_input.setValue(val)) UNDO_HOLDING = true;
} else if(is(posing_input.value_from.node, Node_Vector4)) {
var _nod = posing_input.value_from.node;
for( var i = 0; i < 4; i++ )
if(_nod.inputs[i].setValue(val[i])) UNDO_HOLDING = true;
}
2023-06-21 20:36:53 +02:00
if(mouse_release(mb_left)) {
posing_bone = noone;
2023-06-23 15:39:24 +02:00
posing_type = noone;
2023-06-21 20:36:53 +02:00
UNDO_HOLDING = false;
}
2024-11-27 05:32:43 +01:00
} else if(anchor_selecting != noone) {
var _bne = anchor_selecting[0];
var _typ = anchor_selecting[1];
if(_bne.IKlength) _typ = 0;
gpu_set_texfilter(true);
if(_typ == 0) { // free move
var orig = _bne.getHead();
draw_sprite_ext(s_bone_move, 0, _x + _s * orig.x, _y + _s * orig.y, 1, 1, 0, COLORS._main_accent, 1);
2024-11-27 05:32:43 +01:00
} else if(_typ == 1) { // bone move
var orig = _bne.getTail();
draw_sprite_ext(s_bone_move, 0, _x + _s * orig.x, _y + _s * orig.y, 1, 1, 0, COLORS._main_accent, 1);
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
} else if(_typ == 2) { // bone rotate
var orig = _bne.getHead();
var _rx = _x + _s * orig.x;
var _ry = _y + _s * orig.y;
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
var orig = _bne.getPoint(0.8);
var _sx = _x + _s * orig.x;
var _sy = _y + _s * orig.y;
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
if(point_in_circle(_mx, _my, _sx, _sy, 12)) {
draw_sprite_ext(s_bone_scale, 0, _sx, _sy, 1, 1, _bne.pose_angle, COLORS._main_accent, 1);
draw_sprite_ext(s_bone_rotate, 0, _rx, _ry, 1, 1, _bne.pose_angle, c_white, 1);
_typ = 3;
} else {
draw_sprite_ext(s_bone_scale, 0, _sx, _sy, 1, 1, _bne.pose_angle, c_white, 1);
draw_sprite_ext(s_bone_rotate, 0, _rx, _ry, 1, 1, _bne.pose_angle, COLORS._main_accent, 1);
_typ = 2;
}
}
gpu_set_texfilter(false);
2024-11-27 05:32:43 +01:00
if(mouse_press(mb_left, active)) {
posing_bone = _bne;
posing_type = _typ;
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
if(!struct_exists(boneMap, _bne.ID)) setBone();
posing_input = boneMap[$ _bne.ID];
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
if(_typ == 0) { // move
var val = posing_input.getValue();
var _p = anchor_selecting[2];
posing_sx = val[TRANSFORM.pos_x];
posing_sy = val[TRANSFORM.pos_y];
posing_mx = _p.x;
posing_my = _p.y;
} else if(_typ == 1) { // free move
var orig = _bne.getHead();
posing_sx = orig.x;
posing_sy = orig.y;
} else if(_typ == 2) { // rotate
var orig = _bne.getHead();
var val = posing_input.getValue();
posing_sx = val[TRANSFORM.rot];
posing_sy = point_direction(orig.x, orig.y, mx, my);
posing_mx = mx;
posing_my = my;
} else if(_typ == 3) { // scale
var orig = _bne.getHead();
var _sca = point_distance(orig.x, orig.y, mx, my) / _bne.pose_length;
posing_sx = _bne.length * _bne.pose_local_scale * _sca;
posing_mx = orig.x;
posing_my = orig.y;
}
2023-06-21 20:36:53 +02:00
}
2024-08-06 14:04:41 +02:00
}
}
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
static update = function(frame = CURRENT_FRAME) {
var _b = getInputData(0);
2024-11-27 05:32:43 +01:00
if(_b == noone) {
boneHash = "";
2023-06-23 15:39:24 +02:00
return;
}
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
var _h = _b.getHash();
if(boneHash != _h) {
boneHash = _h;
setBone();
}
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
bonePose.resetPose()
.setPosition();
2024-11-27 05:32:43 +01:00
var _bArr = _b.toArray();
var bArr = bonePose.toArray();
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
for( var i = 0, n = array_length(bArr); i < n; i++ ) {
var _bone = _bArr[i];
var bone = bArr[i];
var _id = bone.ID;
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
if(!struct_exists(boneMap, _id)) continue;
var _inp = boneMap[$ _id];
_inp.updateName(bone.name);
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
var _trn = _inp.getValue();
bone.angle = _bone.angle;
bone.length = _bone.length;
bone.direction = _bone.direction;
bone.distance = _bone.distance;
bone.pose_posit = [ _trn[TRANSFORM.pos_x], _trn[TRANSFORM.pos_y] ];
bone.pose_rotate = _trn[TRANSFORM.rot];
bone.pose_scale = _trn[TRANSFORM.sca_x];
2023-06-21 20:36:53 +02:00
}
2024-11-27 05:32:43 +01:00
bonePose.setPose();
2023-06-21 20:36:53 +02:00
2024-11-27 05:32:43 +01:00
outputs[0].setValue(bonePose);
2024-08-06 14:04:41 +02:00
}
2024-08-06 14:04:41 +02:00
static getPreviewBoundingBox = function() {
2023-07-10 20:14:10 +02:00
var minx = 9999999;
var miny = 9999999;
var maxx = -9999999;
var maxy = -9999999;
2024-08-08 06:57:51 +02:00
var _b = outputs[0].getValue();
2023-08-01 19:21:51 +02:00
if(_b == noone) return BBOX().fromPoints(0, 0, 1, 1);
2023-07-10 20:14:10 +02:00
var _bst = ds_stack_create();
ds_stack_push(_bst, _b);
while(!ds_stack_empty(_bst)) {
var __b = ds_stack_pop(_bst);
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(__b.childs); i < n; i++ ) {
var p0 = __b.childs[i].getHead();
var p1 = __b.childs[i].getTail();
2023-07-10 20:14:10 +02:00
minx = min(minx, p0.x); miny = min(miny, p0.y);
maxx = max(maxx, p0.x); maxy = max(maxy, p0.y);
minx = min(minx, p1.x); miny = min(miny, p1.y);
maxx = max(maxx, p1.x); maxy = max(maxy, p1.y);
ds_stack_push(_bst, __b.childs[i]);
}
}
ds_stack_destroy(_bst);
if(minx == 9999999) return noone;
return BBOX().fromPoints(minx, miny, maxx, maxy);
2024-08-06 14:04:41 +02:00
}
2023-07-10 20:14:10 +02:00
static postApplyDeserialize = function() {
2024-08-08 06:57:51 +02:00
for( var i = input_fix_len; i < array_length(inputs); i += data_length ) {
var inp = inputs[i];
2024-11-23 12:08:44 +01:00
var idx = LOADING_VERSION < 1_18_04_0? struct_try_get(inp.display_data, "bone_id", 0) :
struct_try_get(inp.attributes, "bone_id", 0);
2023-06-21 20:36:53 +02:00
2024-11-12 01:41:42 +01:00
boneMap[$ idx] = inp;
2024-11-23 12:08:44 +01:00
inp.attributes.bone_id = idx;
2023-06-21 20:36:53 +02:00
}
2023-06-25 20:12:17 +02:00
setBone();
2024-08-06 14:04:41 +02:00
}
2023-06-25 20:12:17 +02:00
2024-08-06 14:04:41 +02:00
static onDrawNode = function(xx, yy, _mx, _my, _s, _hover, _focus) {
2023-06-25 20:12:17 +02:00
var bbox = drawGetBbox(xx, yy, _s);
draw_sprite_fit(s_node_armature_pose, 0, bbox.xc, bbox.yc, bbox.w, bbox.h);
2024-08-06 14:04:41 +02:00
}
}