Pixel-Composer/scripts/__bone/__bone.gml

183 lines
4.9 KiB
Plaintext
Raw Normal View History

2023-06-21 20:36:53 +02:00
function __Bone(parent = noone, distance = 0, direction = 0, angle = 0, length = 0, attributes = {}) constructor {
id = UUID_generate();
self.name = "New bone";
2023-06-17 14:30:49 +02:00
self.distance = distance;
self.direction = direction;
self.angle = angle;
self.length = length;
self.is_main = false;
self.parent_anchor = true;
self.childs = [];
2023-06-21 20:36:53 +02:00
tb_name = new textBox(TEXTBOX_INPUT.text, function(_name) { name = _name; });
tb_name.font = f_p2;
tb_name.hide = true;
self.attributes = attributes;
updated = false;
2023-06-17 14:30:49 +02:00
self.parent = parent;
if(parent != noone) {
distance = parent.length;
direction = parent.angle;
}
static addChild = function(bone) {
array_push(childs, bone);
2023-06-19 20:28:30 +02:00
bone.parent = self;
2023-06-17 14:30:49 +02:00
return self;
}
2023-06-21 20:36:53 +02:00
static childCount = function() {
var amo = array_length(childs);
for( var i = 0; i < array_length(childs); i++ )
amo += childs[i].childCount();
return amo;
}
2023-06-17 14:30:49 +02:00
static getPoint = function(distance, direction) {
if(parent == noone)
return new Point(lengthdir_x(self.distance, self.direction), lengthdir_y(self.distance, self.direction))
.add(lengthdir_x( distance, direction), lengthdir_y( distance, direction));
if(parent_anchor) {
var p = parent.getPoint(parent.length, parent.angle);
return p.add(lengthdir_x(distance, direction), lengthdir_y(distance, direction));
}
var p = parent.getPoint(self.distance, self.direction);
return p.add(lengthdir_x(distance, direction), lengthdir_y(distance, direction));
}
static draw = function(edit = false, _x = 0, _y = 0, _s = 1, _mx = 0, _my = 0, child = true, hovering = noone) {
var hover = noone;
var p0 = getPoint(0, 0);
var p1 = getPoint(length, angle);
p0.x = _x + p0.x * _s;
p0.y = _y + p0.y * _s;
p1.x = _x + p1.x * _s;
p1.y = _y + p1.y * _s;
if(parent != noone) {
var aa = (hovering != noone && hovering[0] == self && hovering[1] == 2)? 1 : 0.75;
draw_set_color(COLORS._main_accent);
2023-06-19 20:28:30 +02:00
if(!parent_anchor && parent.parent != noone) {
var _p = parent.getPoint(0, 0);
_p.x = _x + _p.x * _s;
_p.y = _y + _p.y * _s;
draw_line_dashed(_p.x, _p.y, p0.x, p0.y, 1);
}
2023-06-17 14:30:49 +02:00
draw_set_alpha(aa);
draw_line_width2(p0.x, p0.y, p1.x, p1.y, 6, 2);
draw_set_alpha(1.00);
2023-06-21 20:36:53 +02:00
if(attributes.display_name) {
draw_set_text(f_p2, fa_left, fa_center, COLORS._main_accent);
draw_text((p0.x + p1.x) / 2 + 4, (p0.y + p1.y) / 2, name);
}
if(edit && distance_to_line(_mx, _my, p0.x, p0.y, p1.x, p1.y) <= 12) //drag bone
2023-06-17 14:30:49 +02:00
hover = [ self, 2 ];
if(!parent_anchor) {
2023-06-21 20:36:53 +02:00
if(edit && point_in_circle(_mx, _my, p0.x, p0.y, ui(20))) { //drag head
2023-06-19 20:28:30 +02:00
draw_sprite_colored(THEME.anchor_selector, 0, p0.x, p0.y);
2023-06-17 14:30:49 +02:00
hover = [ self, 0 ];
} else
draw_sprite_colored(THEME.anchor_selector, 2, p0.x, p0.y);
}
2023-06-21 20:36:53 +02:00
if(edit && point_in_circle(_mx, _my, p1.x, p1.y, ui(20))) { //drag tail
2023-06-17 14:30:49 +02:00
draw_sprite_colored(THEME.anchor_selector, 0, p1.x, p1.y);
hover = [ self, 1 ];
} else
draw_sprite_colored(THEME.anchor_selector, 2, p1.x, p1.y);
}
if(child)
for( var i = 0; i < array_length(childs); i++ ) {
2023-06-21 20:36:53 +02:00
var h = childs[i].draw(edit, _x, _y, _s, _mx, _my, true, hovering);
2023-06-17 14:30:49 +02:00
if(hover == noone && h != noone)
hover = h;
}
return hover;
}
2023-06-21 20:36:53 +02:00
static drawInspector = function(_x, _y, _w, _m, _hover, _focus) {
var _h = ui(28);
//draw_sprite_stretched(THEME.node_bg, 0, _x, _y, _w, _h);
draw_sprite_ui(THEME.bone, 0, _x + 12, _y + 12,,,, COLORS._main_icon);
tb_name.setFocusHover(_focus, _hover);
tb_name.draw(_x + 24, _y + 2, _w - 24 - 8, _h - 4, name, _m);
_y += _h;
draw_set_color(COLORS.node_composite_separator);
draw_line(_x + 16, _y, _x + _w - 16, _y);
for( var i = 0; i < array_length(childs); i++ ) {
_y = childs[i].drawInspector(_x + ui(16), _y, _w - ui(16), _m, _hover, _focus);
}
return _y;
}
static serialize = function() {
var bone = {};
2023-06-21 20:36:53 +02:00
bone.id = id;
bone.name = name;
bone.distance = distance;
bone.direction = direction;
bone.angle = angle;
bone.length = length;
bone.is_main = is_main;
bone.parent_anchor = parent_anchor;
bone.childs = [];
for( var i = 0; i < array_length(childs); i++ )
bone.childs[i] = childs[i].serialize();
return bone;
}
2023-06-21 20:36:53 +02:00
static deserialize = function(bone, attributes) {
id = bone.id;
name = bone.name;
distance = bone.distance;
direction = bone.direction;
angle = bone.angle;
length = bone.length;
is_main = bone.is_main;
parent_anchor = bone.parent_anchor;
2023-06-21 20:36:53 +02:00
self.attributes = attributes;
childs = [];
for( var i = 0; i < array_length(bone.childs); i++ ) {
2023-06-21 20:36:53 +02:00
var _b = new __Bone().deserialize(bone.childs[i], attributes);
addChild(_b);
}
return self;
}
2023-06-21 20:36:53 +02:00
static clone = function(attributes) {
var _b = new __Bone(parent, distance, direction, angle, length, attributes);
_b.is_main = is_main;
_b.parent_anchor = parent_anchor;
for( var i = 0; i < array_length(childs); i++ )
_b.addChild(childs[i].clone(attributes));
return _b;
}
2023-06-17 14:30:49 +02:00
}