Pixel-Composer/scripts/node_keyframe/node_keyframe.gml

418 lines
12 KiB
Plaintext
Raw Normal View History

2022-01-13 05:24:03 +01:00
enum CURVE_TYPE {
2023-01-09 03:14:20 +01:00
none,
2022-01-13 05:24:03 +01:00
bezier,
2023-01-25 06:49:00 +01:00
cut,
2022-01-13 05:24:03 +01:00
}
2023-01-09 03:14:20 +01:00
function valueKey(_time, _value, _anim = noone, _in = 0, _ot = 0) constructor {
2022-09-23 13:28:42 +02:00
time = _time;
2023-01-01 02:06:02 +01:00
ratio = time / (ANIMATOR.frames_total - 1);
2022-09-23 13:28:42 +02:00
value = _value;
anim = _anim;
2022-01-13 05:24:03 +01:00
2023-01-09 03:14:20 +01:00
ease_in = is_array(_in)? _in : [_in, 1];
ease_out = is_array(_ot)? _ot : [_ot, 0];
2022-09-23 13:28:42 +02:00
2023-01-09 03:14:20 +01:00
ease_in_type = CURVE_TYPE.none;
ease_out_type = CURVE_TYPE.none;
2022-12-10 05:06:01 +01:00
2023-03-05 07:16:44 +01:00
dopesheet_x = 0;
2022-12-10 05:06:01 +01:00
static setTime = function(time) {
self.time = time;
2023-01-01 02:06:02 +01:00
ratio = time / (ANIMATOR.frames_total - 1);
2022-12-10 05:06:01 +01:00
}
2023-01-25 06:49:00 +01:00
2023-03-02 07:59:14 +01:00
static clone = function(target = noone) {
2023-01-25 06:49:00 +01:00
var key = new valueKey(time, value, target);
key.ease_in = ease_in;
key.ease_out = ease_out;
key.ease_in_type = ease_in_type;
key.ease_out_type = ease_out_type;
return key;
}
2023-03-02 07:59:14 +01:00
2023-03-05 07:16:44 +01:00
static cloneAnimator = function(shift = 0, anim = noone, removeDup = true) {
if(anim != noone) { //check value compat between animator
if(value_bit(self.anim.prop.type) & value_bit(anim.prop.type) == 0) {
noti_warning("Type incompatible");
return noone;
}
if(typeArray(self.anim.prop.display_type) != typeArray(anim.prop.display_type)) {
noti_warning("Type incompatible");
return noone;
}
}
if(anim == noone) anim = self.anim;
2023-03-02 07:59:14 +01:00
var key = new valueKey(time + shift, value, anim);
key.ease_in = ease_in;
key.ease_out = ease_out;
key.ease_in_type = ease_in_type;
key.ease_out_type = ease_out_type;
ds_list_add(anim.values, key);
2023-03-05 07:16:44 +01:00
anim.setKeyTime(key, time + shift, removeDup);
2023-03-02 07:59:14 +01:00
return key;
}
2022-01-13 05:24:03 +01:00
}
2023-03-21 03:01:53 +01:00
function valueAnimator(_val, _prop, _sep_axis = false) constructor {
suffix = "";
values = ds_list_create();
sep_axis = _sep_axis;
2022-09-23 13:28:42 +02:00
ds_list_add(values, new valueKey(0, _val, self));
2023-03-21 03:01:53 +01:00
//print(_prop.name + ": " + string(_val));
2022-01-13 05:24:03 +01:00
2023-03-21 03:01:53 +01:00
index = 0;
2023-01-09 03:14:20 +01:00
prop = _prop;
2023-03-05 07:16:44 +01:00
dopesheet_y = 0;
2022-01-13 05:24:03 +01:00
static interpolate = function(from, to, rat) {
2023-01-25 06:49:00 +01:00
if(prop.type == VALUE_TYPE.boolean)
return 0;
2023-01-09 03:14:20 +01:00
if(to.ease_in_type == CURVE_TYPE.none && from.ease_out_type == CURVE_TYPE.none)
return rat;
2023-01-25 06:49:00 +01:00
if(to.ease_in_type == CURVE_TYPE.cut)
return 0;
if(from.ease_out_type == CURVE_TYPE.cut)
return 1;
2023-01-09 03:14:20 +01:00
if(rat == 0 || rat == 1)
2022-09-23 13:28:42 +02:00
return rat;
2023-01-09 03:14:20 +01:00
var eox = clamp(from.ease_out[0], 0, 0.9);
var eix = clamp(to.ease_in[0], 0, 0.9);
var eoy = from.ease_out[1];
var eiy = to.ease_in[1];
2022-09-23 13:28:42 +02:00
2023-01-09 03:14:20 +01:00
var bz = [0, eox, eoy, 1. - eix, eiy, 1];
2023-02-14 02:51:14 +01:00
return eval_curve_segment_x(bz, rat);
2023-01-09 03:14:20 +01:00
}
static lerpValue = function(from, to, _lrp) {
if(prop.type == VALUE_TYPE.color) {
var _f = from.value;
var _t = to.value;
if(is_array(_f)) {
var amo = max(array_length(_f), array_length(_t));
var res = array_create(amo);
for( var i = 0; i < amo; i++ )
res[i] = merge_color(array_safe_get(_f, i, 0), array_safe_get(_t, i, 0), _lrp);
return res;
}
return processType(merge_color(_f, _t, _lrp));
}
if(typeArray(prop.display_type) && is_array(from.value)) {
var _vec = array_create(array_length(from.value));
for(var i = 0; i < array_length(_vec); i++)
_vec[i] = processType(lerp(from.value[i], to.value[i], _lrp));
return _vec;
}
if(prop.type == VALUE_TYPE.text)
return processType(from.value);
return processType(lerp(from.value, to.value, _lrp));
2022-01-13 05:24:03 +01:00
}
2023-03-21 03:01:53 +01:00
static getName = function() { return prop.name + suffix; }
2022-12-10 05:06:01 +01:00
static getValue = function(_time = ANIMATOR.current_frame) {
2023-02-14 02:51:14 +01:00
if(ds_list_size(values) == 0) return processTypeDefault();
2023-03-21 03:01:53 +01:00
if(ds_list_size(values) == 1) {
//if(prop.name == "Position") print(values[| 0].value);
return processType(values[| 0].value);
}
2023-02-14 02:51:14 +01:00
if(prop.display_type == VALUE_DISPLAY.gradient) return values[| 0].value;
if(prop.type == VALUE_TYPE.path) return processType(values[| 0].value);
2023-03-21 03:01:53 +01:00
if(!prop.is_anim) return processType(values[| 0].value);
2022-01-13 05:24:03 +01:00
2023-01-09 03:14:20 +01:00
var _time_first = values[| 0].time;
var _time_last = values[| ds_list_size(values) - 1].time;
var _time_dura = _time_last - _time_first;
2022-01-13 05:24:03 +01:00
2023-03-21 03:01:53 +01:00
if(_time > _time_last) { //loop
2023-01-09 03:14:20 +01:00
switch(prop.on_end) {
case KEYFRAME_END.loop :
_time = _time_first + safe_mod(_time - _time_last, _time_dura + 1);
break;
case KEYFRAME_END.ping :
var time_in_loop = safe_mod(_time - _time_first, _time_dura * 2);
if(time_in_loop < _time_dura)
_time = _time_first + time_in_loop;
else
_time = _time_first + _time_dura * 2 - time_in_loop;
break;
2022-01-13 05:24:03 +01:00
}
}
2023-01-09 03:14:20 +01:00
if(_time < values[| 0].time) { //Wrap begin
if(prop.on_end == KEYFRAME_END.wrap) {
var from = values[| ds_list_size(values) - 1];
var to = values[| 0];
var prog = ANIMATOR.frames_total - from.time + _time;
var totl = ANIMATOR.frames_total - from.time + to.time;
var rat = prog / totl;
var _lrp = interpolate(from, to, rat);
return lerpValue(from, to, _lrp);
2022-01-13 05:24:03 +01:00
}
2023-01-09 03:14:20 +01:00
return processType(values[| 0].value); //First frame
2022-01-13 05:24:03 +01:00
}
2023-01-09 03:14:20 +01:00
for(var i = 0; i < ds_list_size(values); i++) { //In between
var _key = values[| i];
if(_key.time <= _time) continue;
var rat = (_time - values[| i - 1].time) / (values[| i].time - values[| i - 1].time);
var from = values[| i - 1];
var to = values[| i];
var _lrp = interpolate(from, to, rat);
return lerpValue(from, to, _lrp);
}
if(prop.on_end == KEYFRAME_END.wrap) { //Wrap end
var from = values[| ds_list_size(values) - 1];
var to = values[| 0];
var prog = _time - from.time;
var totl = ANIMATOR.frames_total - from.time + to.time;
var rat = prog / totl;
var _lrp = interpolate(from, to, rat);
return lerpValue(from, to, _lrp);
}
return processType(values[| ds_list_size(values) - 1].value); //Last frame
2022-01-13 05:24:03 +01:00
}
2023-02-14 02:51:14 +01:00
static processTypeDefault = function() {
2023-03-21 03:01:53 +01:00
if(!sep_axis && typeArray(prop.display_type)) return [];
2023-02-14 02:51:14 +01:00
return 0;
}
2022-01-13 05:24:03 +01:00
static processType = function(_val) {
2023-03-21 03:01:53 +01:00
if(!sep_axis && typeArray(prop.display_type) && is_array(_val)) {
2022-01-13 05:24:03 +01:00
for(var i = 0; i < array_length(_val); i++)
_val[i] = processValue(_val[i]);
return _val;
}
return processValue(_val);
}
2023-01-17 08:11:55 +01:00
2022-01-13 05:24:03 +01:00
static processValue = function(_val) {
if(is_array(_val)) return _val;
2022-12-27 04:00:50 +01:00
if(prop.type == VALUE_TYPE.integer && prop.unit.mode == VALUE_UNIT.constant)
return round(toNumber(_val));
2023-02-14 02:51:14 +01:00
2022-09-23 13:28:42 +02:00
switch(prop.type) {
2022-12-27 04:00:50 +01:00
case VALUE_TYPE.integer :
2022-09-21 06:09:40 +02:00
case VALUE_TYPE.float : return toNumber(_val);
2023-02-17 04:48:54 +01:00
case VALUE_TYPE.text : return string_real(_val);
2022-09-27 06:37:28 +02:00
case VALUE_TYPE.surface :
if(is_string(_val))
return get_asset(_val);
return is_surface(_val)? _val : DEF_SURFACE;
2022-01-13 05:24:03 +01:00
}
return _val;
}
2022-09-23 13:28:42 +02:00
static setKeyTime = function(_key, _time, _replace = true) {
if(!ds_list_exist(values, _key)) return 0;
2023-02-23 07:02:19 +01:00
if(!LOADING) MODIFIED = true;
2022-09-23 13:28:42 +02:00
2023-01-25 06:49:00 +01:00
_time = max(_time, 0);
2022-12-10 05:06:01 +01:00
_key.setTime(_time);
2022-09-23 13:28:42 +02:00
ds_list_remove(values, _key);
if(_replace) {
for( var i = 0; i < ds_list_size(values); i++ ) {
if(values[| i].time == _time) {
values[| i] = _key;
return 2;
}
}
}
for( var i = 0; i < ds_list_size(values); i++ ) {
if(values[| i].time > _time) {
ds_list_insert(values, i, _key);
return 1;
}
}
ds_list_add(values, _key);
return 1;
}
2022-12-27 04:00:50 +01:00
static setValue = function(_val = 0, _record = true, _time = ANIMATOR.current_frame, ease_in = 0, ease_out = 0) {
2023-03-21 03:01:53 +01:00
if(!prop.is_anim) {
2022-11-22 14:25:39 +01:00
if(isEqual(values[| 0].value, _val))
return false;
2023-02-14 02:51:14 +01:00
if(_record) recordAction(ACTION_TYPE.var_modify, values[| 0], [ values[| 0].value, "value", prop.name ]);
2022-11-22 14:25:39 +01:00
values[| 0].value = _val;
return true;
2022-01-13 05:24:03 +01:00
}
if(ds_list_size(values) == 0) {
2022-09-23 13:28:42 +02:00
var k = new valueKey(_time, _val, self, ease_in, ease_out);
2022-01-13 05:24:03 +01:00
ds_list_add(values, k);
2023-02-14 02:51:14 +01:00
if(_record) recordAction(ACTION_TYPE.list_insert, values, [ k, ds_list_size(values) - 1, "add " + string(prop.name) + " keyframe" ]);
2022-01-13 05:24:03 +01:00
return true;
}
for(var i = 0; i < ds_list_size(values); i++) {
var _key = values[| i];
if(_key.time == _time) {
if(_key.value != _val) {
2023-02-14 02:51:14 +01:00
if(_record) recordAction(ACTION_TYPE.var_modify, _key, [ _key.value, "value", prop.name ]);
2022-01-13 05:24:03 +01:00
_key.value = _val;
return true;
}
return false;
} else if(_key.time > _time) {
2022-09-23 13:28:42 +02:00
var k = new valueKey(_time, _val, self, ease_in, ease_out);
2022-01-13 05:24:03 +01:00
ds_list_insert(values, i, k);
2023-02-14 02:51:14 +01:00
if(_record) recordAction(ACTION_TYPE.list_insert, values, [k, i, "add " + string(prop.name) + " keyframe" ]);
2022-01-13 05:24:03 +01:00
return true;
}
}
2022-09-23 13:28:42 +02:00
var k = new valueKey(_time, _val, self, ease_in, ease_out);
2023-02-14 02:51:14 +01:00
if(_record) recordAction(ACTION_TYPE.list_insert, values, [ k, ds_list_size(values), "add " + string(prop.name) + " keyframe" ]);
2022-01-13 05:24:03 +01:00
ds_list_add(values, k);
return true;
}
2022-09-23 13:28:42 +02:00
static removeKey = function(key) {
if(ds_list_size(values) > 1)
ds_list_remove(values, key);
else
2023-03-21 03:01:53 +01:00
prop.is_anim = false;
2022-09-23 13:28:42 +02:00
}
2022-01-13 05:24:03 +01:00
static serialize = function(scale = false) {
var _list = ds_list_create();
for(var i = 0; i < ds_list_size(values); i++) {
var _value_list = ds_list_create();
2023-02-14 02:51:14 +01:00
if(scale)
2022-12-10 05:06:01 +01:00
_value_list[| 0] = values[| i].time / (ANIMATOR.frames_total - 1);
2022-01-13 05:24:03 +01:00
else
_value_list[| 0] = values[| i].time;
2023-02-14 02:51:14 +01:00
var val = values[| i].value;
2023-03-11 01:40:17 +01:00
if(prop.type == VALUE_TYPE.struct)
_value_list[| 1] = json_stringify(val);
else if(is_struct(val))
2023-03-02 07:59:14 +01:00
_value_list[| 1] = val.serialize();
2023-03-21 03:01:53 +01:00
else if(!sep_axis && typeArray(prop.display_type) && is_array(val)) {
2022-01-13 05:24:03 +01:00
var __v = ds_list_create();
2023-02-14 02:51:14 +01:00
for(var j = 0; j < array_length(val); j++) {
2023-02-19 13:49:20 +01:00
if(is_struct(val[j]) && struct_has(val[j], "serialize"))
2023-02-14 02:51:14 +01:00
ds_list_add_map(__v, val[j].serialize());
else
ds_list_add(__v, val[j]);
}
2022-01-13 05:24:03 +01:00
_value_list[| 1] = __v;
ds_list_mark_as_list(_value_list, 1);
2023-03-11 01:40:17 +01:00
} else
2022-01-13 05:24:03 +01:00
_value_list[| 1] = values[| i].value;
2023-01-09 03:14:20 +01:00
_value_list[| 2] = ds_list_create_from_array(values[| i].ease_in);
ds_list_mark_as_list(_value_list, 2);
_value_list[| 3] = ds_list_create_from_array(values[| i].ease_out);
ds_list_mark_as_list(_value_list, 3);
2022-09-23 13:28:42 +02:00
_value_list[| 4] = values[| i].ease_in_type;
_value_list[| 5] = values[| i].ease_out_type;
2022-01-13 05:24:03 +01:00
2023-01-09 03:14:20 +01:00
ds_list_add_list(_list, _value_list);
2022-01-13 05:24:03 +01:00
}
return _list;
}
static deserialize = function(_list, scale = false) {
ds_list_clear(values);
2022-12-12 09:08:03 +01:00
2023-03-07 14:29:47 +01:00
if(prop.type == VALUE_TYPE.color && prop.display_type == VALUE_DISPLAY.gradient && LOADING_VERSION < 1340 && !CLONING) { //backward compat: Gradient
2023-02-14 02:51:14 +01:00
var _val = [];
2023-03-02 07:59:14 +01:00
var value = _list[| 0][| 1];
2023-02-14 02:51:14 +01:00
2023-03-02 07:59:14 +01:00
if(ds_exists(value, ds_type_list))
for(var i = 0; i < ds_list_size(value); i++) {
var _keyframe = value[| i];
var _t = ds_map_try_get(_keyframe, "time");
var _v = ds_map_try_get(_keyframe, "value");
2023-02-14 02:51:14 +01:00
2023-03-02 07:59:14 +01:00
array_push(_val, new gradientKey(_t, _v));
2023-02-14 02:51:14 +01:00
}
2023-03-02 07:59:14 +01:00
var grad = new gradientObject();
grad.keys = _val;
ds_list_add(values, new valueKey(0, grad, self));
2023-02-14 02:51:14 +01:00
return;
}
var base = getValue();
2022-01-13 05:24:03 +01:00
for(var i = 0; i < ds_list_size(_list); i++) {
2023-02-14 02:51:14 +01:00
var _keyframe = _list[| i];
var _time = _keyframe[| 0];
if(scale && _time <= 1)
_time = round(_time * (ANIMATOR.frames_total - 1));
2022-01-13 05:24:03 +01:00
2023-02-14 02:51:14 +01:00
var value = ds_list_get(_keyframe, 1);
2023-03-21 03:01:53 +01:00
var ease_in = array_create_from_list(ds_list_get(_keyframe, 2));
var ease_out = array_create_from_list(ds_list_get(_keyframe, 3));
2023-01-09 03:14:20 +01:00
2023-02-14 02:51:14 +01:00
var ease_in_type = ds_list_get(_keyframe, 4, CURVE_TYPE.bezier);
var ease_out_type = ds_list_get(_keyframe, 5, CURVE_TYPE.bezier);
var _val = value;
2022-01-13 05:24:03 +01:00
2023-03-11 01:40:17 +01:00
if(prop.type == VALUE_TYPE.struct)
_val = json_parse(value);
else if(prop.type == VALUE_TYPE.path && prop.display_type == VALUE_DISPLAY.path_array) {
2022-12-27 13:30:02 +01:00
for(var j = 0; j < ds_list_size(value); j++)
_val[j] = value[| j];
2023-02-14 02:51:14 +01:00
} else if(prop.type == VALUE_TYPE.color && prop.display_type == VALUE_DISPLAY.gradient) {
2023-03-02 07:59:14 +01:00
var grad = new gradientObject();
_val = grad.deserialize(value);
2023-03-21 03:01:53 +01:00
} else if(!sep_axis && typeArray(prop.display_type)) {
2023-02-14 02:51:14 +01:00
_val = [];
if(ds_exists(value, ds_type_list)) {
for(var j = 0; j < ds_list_size(value); j++)
2022-12-27 13:30:02 +01:00
_val[j] = processValue(value[| j]);
2022-01-13 05:24:03 +01:00
}
2022-12-27 13:30:02 +01:00
}
2022-01-13 05:24:03 +01:00
2022-09-23 13:28:42 +02:00
var vk = new valueKey(_time, _val, self, ease_in, ease_out);
vk.ease_in_type = ease_in_type;
vk.ease_out_type = ease_out_type;
ds_list_add(values, vk);
2022-01-13 05:24:03 +01:00
}
}
2022-01-19 06:11:17 +01:00
static cleanUp = function() {
ds_list_destroy(values);
}
2022-01-13 05:24:03 +01:00
}