Pixel-Composer/scripts/node_keyframe/node_keyframe.gml

734 lines
20 KiB
Text
Raw Normal View History

2023-12-18 04:40:21 +01:00
enum KEY_TYPE { normal, adder }
enum CURVE_TYPE { linear, bezier, cut }
enum DRIVER_TYPE { none, linear, wiggle, sine }
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 {
2023-08-19 12:42:50 +02:00
#region ---- main ----
time = _time;
2023-10-09 16:07:33 +02:00
ratio = time / (TOTAL_FRAMES - 1);
2023-08-19 12:42:50 +02:00
value = _value;
anim = _anim;
2022-09-23 13:28:42 +02:00
2023-08-19 12:42:50 +02:00
ease_y_lock = true;
ease_in = is_array(_in)? _in : [_in, 1];
ease_out = is_array(_ot)? _ot : [_ot, 0];
var _int = anim? anim.prop.key_inter : CURVE_TYPE.linear;
ease_in_type = _int;
ease_out_type = _int;
2023-12-18 04:40:21 +01:00
2023-08-19 12:42:50 +02:00
dopesheet_x = 0;
2023-12-18 04:40:21 +01:00
drivers = {
seed : irandom_range(100000, 999999),
type : DRIVER_TYPE.none,
speed : 1,
octave : 2,
frequency : 4,
amplitude : 1,
axis_sync : false,
phase : 0,
};
2023-08-19 12:42:50 +02:00
#endregion
2023-03-05 07:16:44 +01:00
static setTime = function(time) {
2022-12-10 05:06:01 +01:00
self.time = time;
2023-11-27 11:40:28 +01:00
ratio = time / (TOTAL_FRAMES - 1);
}
2023-01-25 06:49:00 +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
static cloneAnimator = function(shift = 0, anim = noone, removeDup = true) {
2023-03-05 07:16:44 +01:00
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) != typeArray(anim.prop)) {
2023-03-05 07:16:44 +01:00
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;
array_push(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;
}
2023-10-03 07:14:28 +02:00
static getDrawIndex = function() {
2023-12-18 04:40:21 +01:00
if(anim.prop.type == VALUE_TYPE.trigger)
return 1;
if(drivers.type)
return 2;
if(ease_in_type == CURVE_TYPE.cut)
return 1;
return 0;
}
2023-12-18 04:40:21 +01:00
2023-10-03 07:14:28 +02:00
static toString = function() { return $"[Keyframe] {time}: {value}"; }
2022-01-13 05:24:03 +01:00
}
2023-03-21 03:01:53 +01:00
function valueAnimator(_val, _prop, _sep_axis = false) constructor {
2023-08-19 12:42:50 +02:00
#region ---- main ----
2024-04-01 11:10:01 +02:00
suffix = "";
values = [];
2024-04-01 11:10:01 +02:00
//staticValue = 0;
length = 1;
2023-08-19 12:42:50 +02:00
sep_axis = _sep_axis;
2023-11-29 03:04:28 +01:00
index = 0;
prop = _prop;
y = 0;
key_map = array_create(TOTAL_FRAMES);
2023-12-02 14:30:27 +01:00
key_map_mode = KEYFRAME_END.hold;
2023-08-19 12:42:50 +02:00
2023-11-17 06:24:31 +01:00
animate_frames = [];
2023-08-19 12:42:50 +02:00
if(_prop.type != VALUE_TYPE.trigger)
array_push(values, new valueKey(0, _val, self));
2023-08-19 12:42:50 +02:00
#endregion
2022-01-13 05:24:03 +01:00
static refreshAnimation = function() {
2023-11-17 06:24:31 +01:00
animate_frames = array_verify(animate_frames, TOTAL_FRAMES);
var _anim = false;
var _fr = noone;
for( var i = 0, n = array_length(values); i < n; i++ ) {
var _key = values[i];
2023-11-17 06:24:31 +01:00
if(_fr == noone)
2023-11-17 06:24:31 +01:00
array_fill(animate_frames, 0, _key.time, 0);
else {
var frInd = array_equals(_fr.ease_out, [0, 0]) && array_equals(_fr.ease_in, [0, 1]) && isEqual(_fr.value, _key.value);
array_fill(animate_frames, _fr.time, _key.time, !frInd);
// if(frInd) array_fill(animate_frames, _fr.time, _key.time, 0);
// else array_fill(animate_frames, _fr.time, _key.time, 1);
2023-11-17 06:24:31 +01:00
}
_fr = _key;
}
if(_fr) array_fill(animate_frames, _fr.time, TOTAL_FRAMES, 0);
}
2023-11-17 06:24:31 +01:00
static updateKeyMap = function() {
length = array_length(values);
2023-11-29 03:04:28 +01:00
if(!prop.is_anim && !LOADING && !APPENDING) return;
if(array_empty(values)) {
2024-04-08 07:13:46 +02:00
array_resize(key_map, TOTAL_FRAMES);
return;
2023-11-30 13:07:08 +01:00
}
var _len = max(TOTAL_FRAMES, values[array_length(values) - 1].time);
2023-12-02 14:30:27 +01:00
key_map_mode = prop.on_end;
2023-11-30 13:07:08 +01:00
if(array_length(key_map) != _len)
array_resize(key_map, _len);
2023-11-29 03:04:28 +01:00
2024-04-08 07:13:46 +02:00
if(prop.type == VALUE_TYPE.trigger) {
array_fill(key_map, 0, _len, 0);
for( var i = 0, n = array_length(values); i < n; i++ )
key_map[values[i].time] = true;
2024-04-08 07:13:46 +02:00
return;
}
if(array_length(values) < 2) {
2023-11-30 13:07:08 +01:00
array_fill(key_map, 0, _len, 0);
2023-11-29 03:04:28 +01:00
return;
}
var _firstKey = values[0].time;
2023-11-29 03:04:28 +01:00
array_fill(key_map, 0, _firstKey, -1);
var _keyIndex = _firstKey;
for( var i = 1, n = array_length(values); i < n; i++ ) {
var _k1 = values[i].time;
2023-11-29 03:04:28 +01:00
array_fill(key_map, _keyIndex, _k1, i - 1);
_keyIndex = _k1;
}
2023-11-30 13:07:08 +01:00
array_fill(key_map, _keyIndex, _len, 999_999);
}
2023-11-29 03:04:28 +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-08-19 12:42:50 +02:00
if(to.ease_in_type == CURVE_TYPE.linear && from.ease_out_type == CURVE_TYPE.linear)
2023-01-09 03:14:20 +01:00
return rat;
2023-01-25 06:49:00 +01:00
if(to.ease_in_type == CURVE_TYPE.cut)
return 0;
2023-01-25 06:49:00 +01:00
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) {
2023-06-17 14:30:49 +02:00
var _f = from.value;
var _t = to.value;
2023-07-11 14:18:23 +02:00
if(is_struct(_f)) {
if(!struct_has(_f, "lerpTo")) return _f;
return _f.lerpTo(_t, _lrp);
}
2023-08-30 16:40:45 +02:00
2024-11-23 12:08:44 +01:00
if(prop.display_type == VALUE_DISPLAY.d3quarternion && prop.attributes.angle_display == QUARTERNION_DISPLAY.quarterion)
return quarternionArraySlerp(_f, _t, _lrp);
2023-01-09 03:14:20 +01:00
2023-06-17 14:30:49 +02:00
if(prop.type == VALUE_TYPE.color) {
2023-07-11 14:18:23 +02:00
if(is_array(_f) && is_array(_t)) {
var _len = ceil(lerp(array_length(_f), array_length(_t), _lrp));
var res = array_create(_len);
for( var i = 0; i < _len; i++ ) {
var rat = i / (_len - 1);
var rf = rat * (array_length(_f) - 1);
var rt = rat * (array_length(_t) - 1);
var cf = array_get_decimal(_f, rf, true);
var ct = array_get_decimal(_t, rt, true);
res[i] = merge_color(cf, ct, _lrp);
}
2023-01-09 03:14:20 +01:00
return res;
}
return processType(merge_color(_f, _t, _lrp));
}
2023-06-17 14:30:49 +02:00
2023-11-20 05:10:55 +01:00
if(is_array(_f) || is_array(_t)) {
var _len = max(array_safe_length(_f), array_safe_length(_t));
var _vec = array_create(_len);
for(var i = 0; i < _len; i++)
_vec[i] = processType(
lerp(
2024-03-31 05:36:11 +02:00
is_array(_f)? array_safe_get_fast(_f, i, 0) : _f,
is_array(_t)? array_safe_get_fast(_t, i, 0) : _t,
2023-11-20 05:10:55 +01:00
_lrp)
);
2023-01-09 03:14:20 +01:00
return _vec;
}
if(prop.type == VALUE_TYPE.text)
2023-06-17 14:30:49 +02:00
return processType(_f);
return processType(lerp(_f, _t, _lrp));
}
2022-01-13 05:24:03 +01:00
2023-03-21 03:01:53 +01:00
static getName = function() { return prop.name + suffix; }
static getValue = function(_time = CURRENT_FRAME) {
2024-04-01 11:10:01 +02:00
//if(!prop.is_anim) return staticValue;
length = array_length(values);
///////////////////////////////////////////////////////////// TRIGGER TYPE /////////////////////////////////////////////////////////////
2023-03-28 06:58:28 +02:00
if(prop.type == VALUE_TYPE.trigger) {
2024-04-08 07:13:46 +02:00
if(length == 0 || !prop.is_anim) return false;
2023-06-13 14:42:06 +02:00
2023-11-29 03:04:28 +01:00
if(array_length(key_map) != TOTAL_FRAMES) updateKeyMap();
2024-04-08 07:13:46 +02:00
return key_map[_time];
2023-03-21 03:01:53 +01:00
}
2023-02-14 02:51:14 +01:00
///////////////////////////////////////////////////////////// OPTIMIZATION /////////////////////////////////////////////////////////////
if(length == 0) return processTypeDefault();
if(length == 1) {
var _key = values[0];
2023-12-18 04:40:21 +01:00
if(_key.drivers.type && _time >= _key.time)
return processType(processDriver(_time, _key));
return processType(_key.value);
}
2023-03-28 06:58:28 +02:00
if(prop.type == VALUE_TYPE.path) return processType(values[0].value);
if(!prop.is_anim) return processType(values[0].value);
var _len = max(TOTAL_FRAMES, values[length - 1].time);
2023-11-30 13:07:08 +01:00
if(array_length(key_map) != _len) updateKeyMap();
2022-01-13 05:24:03 +01:00
var _time_first = prop.loop_range == -1? values[0].time : values[length - 1 - prop.loop_range].time;
var _time_last = values[length - 1].time;
2023-01-09 03:14:20 +01:00
var _time_dura = _time_last - _time_first;
2022-01-13 05:24:03 +01:00
////////////////////////////////////////////////////////////// LOOP TIME ///////////////////////////////////////////////////////////////
if(_time > _time_last) {
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-11-29 03:04:28 +01:00
2023-12-02 14:30:27 +01:00
var _keyIndex;
if(_time >= _len) _keyIndex = 999_999;
2023-12-15 12:56:36 +01:00
else if(_time <= 0) _keyIndex = -1;
2024-03-31 05:36:11 +02:00
else _keyIndex = array_safe_get_fast(key_map, _time);
2022-01-13 05:24:03 +01:00
//////////////////////////////////////////////////////////// BEFORE FIRST //////////////////////////////////////////////////////////////
if(_keyIndex == -1) {
2023-01-09 03:14:20 +01:00
if(prop.on_end == KEYFRAME_END.wrap) {
var from = values[length - 1];
var to = values[0];
2023-08-16 20:16:31 +02:00
var fTime = from.time;
var tTime = to.time;
2023-10-09 16:07:33 +02:00
var prog = TOTAL_FRAMES - fTime + _time;
var totl = TOTAL_FRAMES - fTime + tTime;
2023-01-09 03:14:20 +01:00
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
}
2023-01-09 03:14:20 +01:00
///////////////////////////////////////////////////////////// AFTER LAST ///////////////////////////////////////////////////////////////
if(_keyIndex == 999_999) {
var _lstKey = values[length - 1];
2023-12-18 04:40:21 +01:00
if(_lstKey.drivers.type)
return processType(processDriver(_time, _lstKey));
2023-12-02 14:30:27 +01:00
if(prop.on_end == KEYFRAME_END.wrap) {
2023-12-18 04:40:21 +01:00
var from = _lstKey;
var to = values[0];
2023-11-29 03:04:28 +01:00
var prog = _time - from.time;
var totl = TOTAL_FRAMES - from.time + to.time;
2023-01-09 03:14:20 +01:00
2023-11-29 03:04:28 +01:00
var rat = prog / totl;
var _lrp = interpolate(from, to, rat);
2023-01-09 03:14:20 +01:00
2023-11-29 03:04:28 +01:00
return lerpValue(from, to, _lrp);
}
2023-12-18 04:40:21 +01:00
return processType(_lstKey.value); //Last frame
}
///////////////////////////////////////////////////////////// INBETWEEN ////////////////////////////////////////////////////////////////
2023-01-09 03:14:20 +01:00
var from = values[_keyIndex];
var to = values[_keyIndex + 1];
2023-12-18 04:40:21 +01:00
var rat = (_time - from.time) / (to.time - from.time);
var _lrp = interpolate(from, to, rat);
2023-11-29 03:04:28 +01:00
if(from.drivers.type)
return processDriver(_time, from, lerpValue(from, to, _lrp), rat);
2023-12-18 04:40:21 +01:00
return lerpValue(from, to, _lrp);
}
2022-01-13 05:24:03 +01:00
static processTypeDefault = function() {
if(!sep_axis && typeArray(prop)) return [];
2023-02-14 02:51:14 +01:00
return 0;
}
2023-11-29 03:04:28 +01:00
static processDriver = function(_time, _key, _val = undefined, _intp = 0) {
2023-12-18 04:40:21 +01:00
static _processDriver = function(val, drivers, _t, _index = 0, _intp = 0) {
switch(drivers.type) {
case DRIVER_TYPE.linear :
return val + _t * drivers.speed;
case DRIVER_TYPE.wiggle :
var w = perlin1D(_t, drivers.seed + _index, drivers.frequency / 10, drivers.octave, -1, 1) * drivers.amplitude;
return val + w;
case DRIVER_TYPE.sine :
var w = sin((drivers.phase * (_index + 1) + _t * drivers.frequency / TOTAL_FRAMES) * pi * 2) * drivers.amplitude;
return val + w;
}
return 0;
}
var _dt = _time - _key.time;
_val = _val == undefined? _key.value : _val;
var _res = _val;
if(prop.type == VALUE_TYPE.integer || prop.type == VALUE_TYPE.float) {
if(is_array(_val)) {
_res = array_create(array_length(_val));
for( var i = 0, n = array_length(_val); i < n; i++ )
_res[i] = is_numeric(_val[i])? _processDriver(_val[i], _key.drivers, _dt, _key.drivers.axis_sync? 0 : i, _intp) : _val[i];
} else
_res = _processDriver(_val, _key.drivers, _dt, 0, _intp);
}
return _res;
}
2023-12-18 04:40:21 +01:00
static processType = function(_val) {
INLINE
2024-04-23 14:29:26 +02:00
if(PROJECT.attributes.strict) return processValue(_val);
2023-11-29 03:04:28 +01:00
var _res = _val;
if(!sep_axis && typeArray(prop) && is_array(_val)) {
2022-01-13 05:24:03 +01:00
for(var i = 0; i < array_length(_val); i++)
2023-11-29 03:04:28 +01:00
_res[i] = processValue(_val[i]);
} else
_res = processValue(_val);
2023-11-29 03:04:28 +01:00
return _res;
}
2023-01-17 08:11:55 +01:00
static processValue = function(_val) {
INLINE
2023-09-14 16:29:39 +02:00
if(is_array(_val)) return _val;
2023-05-16 21:28:16 +02:00
if(is_struct(_val)) return _val;
2023-02-14 02:51:14 +01:00
2022-09-23 13:28:42 +02:00
switch(prop.type) {
case VALUE_TYPE.integer : return prop.unit.mode == VALUE_UNIT.constant? round(_val) : _val;
case VALUE_TYPE.float : return _val;
2024-04-20 05:00:28 +02:00
case VALUE_TYPE.text : return is_string(_val)? _val : string_real(_val);
case VALUE_TYPE.color : return is_real(_val)? cola(_val) : _val;
2024-08-27 13:42:29 +02:00
case VALUE_TYPE.surface : return is_string(_val)? get_asset(_val) : _val;
2022-01-13 05:24:03 +01:00
}
return _val;
}
2022-01-13 05:24:03 +01:00
static insertKey = function(_key, _index) { array_push(values, _index, _key); }
2023-11-10 11:32:46 +01:00
2024-05-23 14:40:30 +02:00
function onUndo() { updateKeyMap(); prop.triggerSetFrom(); }
static setKeyTime = function(_key, _time, _replace = true, record = false) {
if(!array_exists(values, _key)) return 0;
2023-11-10 11:32:46 +01:00
if(_key.time == _time && !_replace) return 0;
2023-07-06 19:49:16 +02:00
if(!LOADING) PROJECT.modified = true;
2022-09-23 13:28:42 +02:00
2023-11-10 11:32:46 +01:00
var _prevTime = _key.time;
2023-01-25 06:49:00 +01:00
_time = max(_time, 0);
2022-12-10 05:06:01 +01:00
_key.setTime(_time);
array_remove(values, _key);
2022-09-23 13:28:42 +02:00
if(_replace)
for( var i = 0; i < array_length(values); i++ ) {
if(values[i].time != _time) continue;
2023-11-10 11:32:46 +01:00
if(record) {
var act = new Action(ACTION_TYPE.custom, function(data) {
if(data.undo) insertKey(data.overKey, data.index);
2023-12-18 04:40:21 +01:00
updateKeyMap();
data.undo = !data.undo;
2024-09-03 11:49:51 +02:00
}, { overKey : values[i], index : i, undo : true, tooltip : $"Set {prop.name} value" });
2023-11-10 11:32:46 +01:00
mergeAction(act);
}
values[i] = _key;
2023-11-29 03:04:28 +01:00
updateKeyMap();
return 2;
2022-09-23 13:28:42 +02:00
}
for( var i = 0; i < array_length(values); i++ ) { //insert key before the last key
if(values[i].time < _time) continue;
2023-11-10 11:32:46 +01:00
if(record) recordAction(ACTION_TYPE.custom, function(data) {
var _prevTime = data.key.time;
setKeyTime(data.key, data.time, false);
2023-12-18 04:40:21 +01:00
data.time = _prevTime;
2024-09-03 11:49:51 +02:00
}, { key : _key, time : _prevTime, tooltip : $"Set {prop.name} value" }, onUndo);
2023-11-10 11:32:46 +01:00
array_insert(values, i, _key);
2023-11-29 03:04:28 +01:00
if(_replace) updateKeyMap();
return 1;
2022-09-23 13:28:42 +02:00
}
2023-12-18 04:40:21 +01:00
if(record) recordAction(ACTION_TYPE.custom, function(data) { // insert key after the last key
2023-11-10 11:32:46 +01:00
var _prevTime = data.key.time;
setKeyTime(data.key, data.time, false);
2023-12-18 04:40:21 +01:00
data.time = _prevTime;
2024-09-03 11:49:51 +02:00
}, { key : _key, time : _prevTime, tooltip : $"Set {prop.name} value" }, onUndo);
2023-11-10 11:32:46 +01:00
array_push(values, _key);
2023-11-29 03:04:28 +01:00
if(_replace) updateKeyMap();
2022-09-23 13:28:42 +02:00
return 1;
}
2022-09-23 13:28:42 +02:00
static setValue = function(_val = 0, _record = true, _time = CURRENT_FRAME, ease_in = 0, ease_out = 0) {
2024-04-01 11:10:01 +02:00
//staticValue = _val;
2023-03-28 06:58:28 +02:00
if(prop.type == VALUE_TYPE.trigger) {
2023-06-13 14:42:06 +02:00
if(!prop.is_anim) {
values[0] = new valueKey(0, _val, self);
2023-11-29 03:04:28 +01:00
updateKeyMap();
2023-06-13 14:42:06 +02:00
return true;
}
for(var i = 0; i < array_length(values); i++) { //Find trigger
var _key = values[i];
2023-03-28 06:58:28 +02:00
if(_key.time == _time) {
2023-05-22 20:31:55 +02:00
if(!global.FLAG.keyframe_override) return false;
2023-03-28 06:58:28 +02:00
_key.value = _val;
return false;
2024-04-08 07:13:46 +02:00
2023-03-28 06:58:28 +02:00
} else if(_key.time > _time) {
array_insert(values, i, new valueKey(_time, _val, self));
2023-11-29 03:04:28 +01:00
updateKeyMap();
2023-03-28 06:58:28 +02:00
return true;
}
}
2024-04-08 07:13:46 +02:00
//print($"{_time}: {_val} | Insert last");
array_push(values, new valueKey(_time, _val, self));
2023-11-29 03:04:28 +01:00
updateKeyMap();
2023-03-28 06:58:28 +02:00
return true;
}
2023-03-21 03:01:53 +01:00
if(!prop.is_anim) {
if(isEqual(values[0].value, _val))
2022-11-22 14:25:39 +01:00
return false;
2024-09-03 11:49:51 +02:00
if(_record) recordAction_variable_change(values[0], "value", values[0].value, prop.name, onUndo);
2023-08-08 18:45:00 +02:00
values[0].value = _val;
2022-11-22 14:25:39 +01:00
return true;
2022-01-13 05:24:03 +01:00
}
if(array_length(values) == 0) { // Should not be called normally
2022-09-23 13:28:42 +02:00
var k = new valueKey(_time, _val, self, ease_in, ease_out);
array_push(values, k);
2024-09-03 11:49:51 +02:00
if(_record) recordAction(ACTION_TYPE.array_insert, values, [ k, array_length(values) - 1, $"add {prop.name} keyframe" ], onUndo);
2022-01-13 05:24:03 +01:00
return true;
}
for(var i = 0; i < array_length(values); i++) {
var _key = values[i];
2022-01-13 05:24:03 +01:00
if(_key.time == _time) {
2023-05-22 20:31:55 +02:00
if(!global.FLAG.keyframe_override) return false;
2022-01-13 05:24:03 +01:00
if(_key.value != _val) {
2024-09-03 11:49:51 +02:00
if(_record) recordAction_variable_change(_key, "value", _key.value, prop.name, onUndo);
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);
array_insert(values, i, k);
2024-09-03 11:49:51 +02:00
if(_record) recordAction(ACTION_TYPE.array_insert, values, [k, i, $"add {prop.name} keyframe" ], onUndo);
2023-11-29 03:04:28 +01:00
updateKeyMap();
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);
2024-09-03 11:49:51 +02:00
if(_record) recordAction(ACTION_TYPE.array_insert, values, [ k, array_length(values), $"add {prop.name} keyframe" ], onUndo);
array_push(values, k);
2023-11-29 03:04:28 +01:00
updateKeyMap();
2022-01-13 05:24:03 +01:00
return true;
}
2022-01-13 05:24:03 +01:00
static removeKey = function(key) {
if(array_length(values) > 1) array_remove(values, key);
else prop.is_anim = false;
2023-11-29 03:04:28 +01:00
updateKeyMap();
}
2022-09-23 13:28:42 +02:00
static serialize = function(scale = false) {
2023-06-13 14:42:06 +02:00
var _data = [];
2022-01-13 05:24:03 +01:00
for(var i = 0; i < array_length(values); i++) {
2023-06-13 14:42:06 +02:00
var _value_list = [];
_value_list[0] = scale? values[i].time / (TOTAL_FRAMES - 1) : values[i].time;
2022-01-13 05:24:03 +01:00
2024-11-23 12:08:44 +01:00
var _v = values[i];
var val = _v.value;
2023-03-11 01:40:17 +01:00
2024-04-08 07:13:46 +02:00
if(prop.type == VALUE_TYPE.struct) {
val = json_stringify(val);
2024-06-15 08:02:10 +02:00
} else if(is_struct(val) && struct_has(val, "serialize")) {
2024-04-08 07:13:46 +02:00
val = val.serialize();
} else if(!sep_axis && typeArray(prop) && is_array(val)) {
2023-06-13 14:42:06 +02:00
var __v = [];
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-06-13 14:42:06 +02:00
array_push(__v, val[j].serialize());
2023-02-14 02:51:14 +01:00
else
2023-06-13 14:42:06 +02:00
array_push(__v, val[j]);
2023-02-14 02:51:14 +01:00
}
2024-04-08 07:13:46 +02:00
val = __v;
}
2022-01-13 05:24:03 +01:00
2024-04-08 07:13:46 +02:00
_value_list[1] = val;
2024-11-23 12:08:44 +01:00
_value_list[2] = _v.ease_in;
_value_list[3] = _v.ease_out;
_value_list[4] = _v.ease_in_type;
_value_list[5] = _v.ease_out_type;
_value_list[6] = _v.ease_y_lock;
_value_list[7] = _v.drivers.type == DRIVER_TYPE.none? 0 : _v.drivers;
2022-01-13 05:24:03 +01:00
2023-06-13 14:42:06 +02:00
array_push(_data, _value_list);
2022-01-13 05:24:03 +01:00
}
2024-11-26 05:52:57 +01:00
if(array_length(values) == 1) return { d: _data[0][1] };
2023-06-13 14:42:06 +02:00
return _data;
}
2022-01-13 05:24:03 +01:00
static deserialize = function(_data, scale = false) {
values = [];
2022-12-12 09:08:03 +01:00
2024-11-23 12:08:44 +01:00
if(is_struct(_data)) _data = [ [ 0, _data.d ] ];
if(prop.type == VALUE_TYPE.gradient && LOADING_VERSION < 1340 && !CLONING) { //backward compat: Gradient
2023-02-14 02:51:14 +01:00
var _val = [];
2023-06-13 14:42:06 +02:00
var value = _data[0][1];
2023-02-14 02:51:14 +01:00
2023-06-13 14:42:06 +02:00
if(is_array(value))
for(var i = 0; i < array_length(value); i++) {
var _keyframe = value[i];
var _t = struct_try_get(_keyframe, "time");
var _v = struct_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;
array_push(values, new valueKey(0, grad, self));
2023-11-29 03:04:28 +01:00
updateKeyMap();
2023-02-14 02:51:14 +01:00
return;
}
2023-02-14 02:51:14 +01:00
var base = prop.def_val;
2023-02-14 02:51:14 +01:00
2023-06-13 14:42:06 +02:00
for(var i = 0; i < array_length(_data); i++) {
var _keyframe = _data[i];
2024-03-31 05:36:11 +02:00
var _time = array_safe_get_fast(_keyframe, 0);
2023-02-14 02:51:14 +01:00
if(scale && _time <= 1)
2023-10-09 16:07:33 +02:00
_time = round(_time * (TOTAL_FRAMES - 1));
2022-01-13 05:24:03 +01:00
2024-03-31 05:36:11 +02:00
var value = array_safe_get_fast(_keyframe, 1);
2024-11-23 12:08:44 +01:00
var ease_in = array_safe_get_fast(_keyframe, 2, [0, 1]);
var ease_out = array_safe_get_fast(_keyframe, 3, [0, 0]);
var ease_in_type = array_safe_get_fast(_keyframe, 4, 0);
var ease_out_type = array_safe_get_fast(_keyframe, 5, 0);
var ease_y_lock = array_safe_get_fast(_keyframe, 6, 1);
var driver = array_safe_get_fast(_keyframe, 7, 0);
2023-01-09 03:14:20 +01:00
2023-02-14 02:51:14 +01:00
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)
2023-06-17 18:59:20 +02:00
_val = json_try_parse(value);
2024-03-27 11:51:14 +01:00
2023-03-11 01:40:17 +01:00
else if(prop.type == VALUE_TYPE.path && prop.display_type == VALUE_DISPLAY.path_array) {
2023-06-13 14:42:06 +02:00
for(var j = 0; j < array_length(value); j++)
_val[j] = value[j];
2024-03-27 11:51:14 +01:00
2023-05-28 20:00:51 +02:00
} else if(prop.type == VALUE_TYPE.gradient) {
2023-03-02 07:59:14 +01:00
var grad = new gradientObject();
_val = grad.deserialize(value);
2024-03-27 11:51:14 +01:00
2024-04-08 07:13:46 +02:00
} else if(prop.type == VALUE_TYPE.d3Material) {
var mat = new __d3dMaterial();
_val = mat.deserialize(value);
} else if(prop.type == VALUE_TYPE.color) {
if(is_array(_val)) {
for( var i = 0, n = array_length(_val); i < n; i++ )
2024-03-27 11:51:14 +01:00
_val[i] = LOADING_VERSION < 11640 && !is_int64(_val[i])? cola(_val[i]) : int64(_val[i]);
} else
2024-03-27 11:51:14 +01:00
_val = LOADING_VERSION < 11640 && !is_int64(_val)? cola(_val) : int64(_val);
} else if(!sep_axis && typeArray(prop)) {
2023-02-14 02:51:14 +01:00
_val = [];
2023-10-03 07:14:28 +02:00
if(is_array(value)) {
for(var j = 0; j < array_length(value); j++)
_val[j] = processValue(value[j]);
} else if(is_array(base)) {
2023-10-03 07:14:28 +02:00
for(var j = 0; j < array_length(base); j++)
_val[j] = processValue(value);
}
if(prop.type == VALUE_TYPE.curve && array_length(value) % 6 == 0) {
array_insert(_val, 0, 0);
array_insert(_val, 1, 1);
}
2024-04-08 07:13:46 +02:00
}
2022-01-13 05:24:03 +01:00
2023-06-10 13:59:45 +02:00
//print($"Deserialize {prop.node.name}:{prop.name} = {_val} ");
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;
2023-07-14 20:34:35 +02:00
vk.ease_y_lock = ease_y_lock;
2024-11-23 12:08:44 +01:00
if(is_struct(driver)) struct_override(vk.drivers, driver);
2023-12-18 04:40:21 +01:00
array_push(values, vk);
2022-01-13 05:24:03 +01:00
}
2023-11-29 03:04:28 +01:00
updateKeyMap();
}
2022-01-19 06:11:17 +01:00
static cleanUp = function() {}
2022-01-13 05:24:03 +01:00
}