Skip to content

Commit

Permalink
Add AutoVolume option to AudioPlaybackTrack
Browse files Browse the repository at this point in the history
  • Loading branch information
TokageItLab committed Aug 17, 2022
1 parent dbd1524 commit dfe4fe4
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 15 deletions.
15 changes: 15 additions & 0 deletions doc/classes/Animation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@
Sets the key identified by [param key_idx] to value [param animation]. The [param track_idx] must be the index of an Animation Track.
</description>
</method>
<method name="audio_track_get_auto_volume" qualifiers="const">
<return type="bool" />
<param index="0" name="track_idx" type="int" />
<description>
Returns [code]true[/code] if the track at [code]idx[/code] changes the volume in [AudioStreamPlayer].
</description>
</method>
<method name="audio_track_get_key_end_offset" qualifiers="const">
<return type="float" />
<param index="0" name="track_idx" type="int" />
Expand Down Expand Up @@ -103,6 +110,14 @@
[param stream] is the [AudioStream] resource to play. [param start_offset] is the number of seconds cut off at the beginning of the audio stream, while [param end_offset] is at the ending.
</description>
</method>
<method name="audio_track_set_auto_volume">
<return type="void" />
<param index="0" name="track_idx" type="int" />
<param index="1" name="enable" type="bool" />
<description>
If [code]true[/code], the track at [code]idx[/code] changes the volume in [AudioStreamPlayer].
</description>
</method>
<method name="audio_track_set_key_end_offset">
<return type="void" />
<param index="0" name="track_idx" type="int" />
Expand Down
50 changes: 42 additions & 8 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,6 @@ void AnimationTrackEdit::_notification(int p_what) {
get_theme_icon(SNAME("InterpWrapClamp"), SNAME("EditorIcons")),
get_theme_icon(SNAME("InterpWrapLoop"), SNAME("EditorIcons")),
};

Ref<Texture2D> interp_icon[3] = {
get_theme_icon(SNAME("InterpRaw"), SNAME("EditorIcons")),
get_theme_icon(SNAME("InterpLinear"), SNAME("EditorIcons")),
Expand All @@ -2126,6 +2125,10 @@ void AnimationTrackEdit::_notification(int p_what) {
get_theme_icon(SNAME("TrackTrigger"), SNAME("EditorIcons")),
get_theme_icon(SNAME("TrackCapture"), SNAME("EditorIcons"))
};
Ref<Texture2D> volume_icon[2] = {
get_theme_icon(SNAME("AutoVolumeDisable"), SNAME("EditorIcons")),
get_theme_icon(SNAME("AutoVolumeEnable"), SNAME("EditorIcons")),
};

int ofs = get_size().width - timeline->get_buttons_width();

Expand Down Expand Up @@ -2154,6 +2157,11 @@ void AnimationTrackEdit::_notification(int p_what) {
if (!animation->track_is_compressed(track) && animation->track_get_type(track) == Animation::TYPE_VALUE) {
draw_texture(update_icon, update_mode_rect.position);
}
if (animation->track_get_type(track) == Animation::TYPE_AUDIO) {
Ref<Texture2D> auto_volume_icon = volume_icon[animation->audio_track_get_auto_volume(track) ? 1 : 0];
Vector2 auto_volume_icon_pos = update_mode_rect.position + (update_mode_rect.size - auto_volume_icon->get_size()) / 2;
draw_texture(auto_volume_icon, auto_volume_icon_pos);
}
// Make it easier to click.
update_mode_rect.position.y = 0;
update_mode_rect.size.y = get_size().height;
Expand All @@ -2162,13 +2170,12 @@ void AnimationTrackEdit::_notification(int p_what) {
update_mode_rect.size.x += hsep / 2;

if (!read_only) {
if (animation->track_get_type(track) == Animation::TYPE_VALUE) {
if (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_AUDIO) {
draw_texture(down_icon, Vector2(ofs, int(get_size().height - down_icon->get_height()) / 2));
update_mode_rect.size.x += down_icon->get_width();
} else if (animation->track_get_type(track) == Animation::TYPE_BEZIER) {
Ref<Texture2D> bezier_icon = get_theme_icon(SNAME("EditBezier"), SNAME("EditorIcons"));
update_mode_rect.size.x += down_icon->get_width();

update_mode_rect = Rect2();
} else {
update_mode_rect = Rect2();
Expand Down Expand Up @@ -2614,7 +2621,11 @@ String AnimationTrackEdit::get_tooltip(const Point2 &p_pos) const {
}

if (update_mode_rect.has_point(p_pos)) {
return TTR("Update Mode (How this property is set)");
if (animation->track_get_type(track) == Animation::TYPE_AUDIO) {
return TTR("Auto Volume (Allow animation tree to change stream player volume)");
} else {
return TTR("Update Mode (How this property is set)");
}
}

if (interp_mode_rect.has_point(p_pos)) {
Expand Down Expand Up @@ -2809,10 +2820,15 @@ void AnimationTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
menu->connect("id_pressed", callable_mp(this, &AnimationTrackEdit::_menu_selected));
}
menu->clear();
menu->add_icon_item(get_theme_icon(SNAME("TrackContinuous"), SNAME("EditorIcons")), TTR("Continuous"), MENU_CALL_MODE_CONTINUOUS);
menu->add_icon_item(get_theme_icon(SNAME("TrackDiscrete"), SNAME("EditorIcons")), TTR("Discrete"), MENU_CALL_MODE_DISCRETE);
menu->add_icon_item(get_theme_icon(SNAME("TrackTrigger"), SNAME("EditorIcons")), TTR("Trigger"), MENU_CALL_MODE_TRIGGER);
menu->add_icon_item(get_theme_icon(SNAME("TrackCapture"), SNAME("EditorIcons")), TTR("Capture"), MENU_CALL_MODE_CAPTURE);
if (animation->track_get_type(track) == Animation::TYPE_AUDIO) {
menu->add_icon_item(get_theme_icon(SNAME("AutoVolumeDisable"), SNAME("EditorIcons")), TTR("Disable"), MENU_AUTO_VOLUME_DISABLE);
menu->add_icon_item(get_theme_icon(SNAME("AutoVolumeEnable"), SNAME("EditorIcons")), TTR("Enable"), MENU_AUTO_VOLUME_ENABLE);
} else {
menu->add_icon_item(get_theme_icon(SNAME("TrackContinuous"), SNAME("EditorIcons")), TTR("Continuous"), MENU_CALL_MODE_CONTINUOUS);
menu->add_icon_item(get_theme_icon(SNAME("TrackDiscrete"), SNAME("EditorIcons")), TTR("Discrete"), MENU_CALL_MODE_DISCRETE);
menu->add_icon_item(get_theme_icon(SNAME("TrackTrigger"), SNAME("EditorIcons")), TTR("Trigger"), MENU_CALL_MODE_TRIGGER);
menu->add_icon_item(get_theme_icon(SNAME("TrackCapture"), SNAME("EditorIcons")), TTR("Capture"), MENU_CALL_MODE_CAPTURE);
}
menu->reset_size();

Vector2 popup_pos = get_screen_position() + update_mode_rect.position + Vector2(0, update_mode_rect.size.height);
Expand Down Expand Up @@ -3203,6 +3219,15 @@ void AnimationTrackEdit::_menu_selected(int p_index) {
emit_signal(SNAME("delete_request"));

} break;
case MENU_AUTO_VOLUME_DISABLE:
case MENU_AUTO_VOLUME_ENABLE: {
bool auto_volume = p_index == MENU_AUTO_VOLUME_ENABLE;
undo_redo->create_action(TTR("Change Animation Auto Volume"));
undo_redo->add_do_method(animation.ptr(), "audio_track_set_auto_volume", track, auto_volume);
undo_redo->add_undo_method(animation.ptr(), "audio_track_set_auto_volume", track, animation->audio_track_get_auto_volume(track));
undo_redo->commit_action();
update();
} break;
}
}

Expand Down Expand Up @@ -3641,6 +3666,9 @@ void AnimationTrackEditor::_animation_track_remove_request(int p_track, Ref<Anim
if (p_from_animation->track_get_type(idx) == Animation::TYPE_VALUE) {
undo_redo->add_undo_method(p_from_animation.ptr(), "value_track_set_update_mode", idx, p_from_animation->value_track_get_update_mode(idx));
}
if (animation->track_get_type(idx) == Animation::TYPE_AUDIO) {
undo_redo->add_undo_method(animation.ptr(), "audio_track_set_auto_volume", idx, animation->audio_track_get_auto_volume(idx));
}

undo_redo->commit_action();
}
Expand Down Expand Up @@ -5799,6 +5827,9 @@ void AnimationTrackEditor::_edit_menu_pressed(int p_option) {
if (tc.track_type == Animation::TYPE_VALUE) {
tc.update_mode = animation->value_track_get_update_mode(idx);
}
if (tc.track_type == Animation::TYPE_AUDIO) {
tc.auto_volume = animation->audio_track_get_auto_volume(idx);
}
tc.loop_wrap = animation->track_get_interpolation_loop_wrap(idx);
tc.enabled = animation->track_is_enabled(idx);
for (int i = 0; i < animation->track_get_key_count(idx); i++) {
Expand Down Expand Up @@ -5842,6 +5873,9 @@ void AnimationTrackEditor::_edit_menu_pressed(int p_option) {
if (track_clipboard[i].track_type == Animation::TYPE_VALUE) {
undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", base_track, track_clipboard[i].update_mode);
}
if (track_clipboard[i].track_type == Animation::TYPE_AUDIO) {
undo_redo->add_do_method(animation.ptr(), "audio_track_set_auto_volume", base_track, track_clipboard[i].auto_volume);
}

for (int j = 0; j < track_clipboard[i].keys.size(); j++) {
undo_redo->add_do_method(animation.ptr(), "track_insert_key", base_track, track_clipboard[i].keys[j].time, track_clipboard[i].keys[j].value, track_clipboard[i].keys[j].transition);
Expand Down
5 changes: 4 additions & 1 deletion editor/animation_track_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ class AnimationTrackEdit : public Control {
MENU_KEY_INSERT,
MENU_KEY_DUPLICATE,
MENU_KEY_ADD_RESET,
MENU_KEY_DELETE
MENU_KEY_DELETE,
MENU_AUTO_VOLUME_DISABLE,
MENU_AUTO_VOLUME_ENABLE,
};

AnimationTimelineEdit *timeline = nullptr;
Expand Down Expand Up @@ -491,6 +493,7 @@ class AnimationTrackEditor : public VBoxContainer {
Animation::LoopMode loop_mode = Animation::LOOP_LINEAR;
bool loop_wrap = false;
bool enabled = false;
bool auto_volume = false;

struct Key {
float time = 0;
Expand Down
1 change: 1 addition & 0 deletions editor/icons/AutoVolumeDisable.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions editor/icons/AutoVolumeEnable.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions scene/animation/animation_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1528,11 +1528,13 @@ void AnimationTree::_process_graph(double p_delta) {
}
}

real_t db = Math::linear2db(MAX(blend, 0.00001));
if (t->object->has_method(SNAME("set_unit_db"))) {
t->object->call(SNAME("set_unit_db"), db);
} else {
t->object->call(SNAME("set_volume_db"), db);
if (a->audio_track_get_auto_volume(i)) {
real_t db = Math::linear2db(MAX(blend, 0.00001));
if (t->object->has_method(SNAME("set_unit_db"))) {
t->object->call(SNAME("set_unit_db"), db);
} else {
t->object->call(SNAME("set_volume_db"), db);
}
}
} break;
case Animation::TYPE_ANIMATION: {
Expand Down
35 changes: 34 additions & 1 deletion scene/resources/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ bool Animation::_set(const StringName &p_name, const Variant &p_value) {
}
}
return true;
} else if (what == "auto_volume") {
if (track_get_type(track) == TYPE_AUDIO) {
audio_track_set_auto_volume(track, p_value);
}
} else if (what == "interp") {
track_set_interpolation_type(track, InterpolationType(p_value.operator int()));
} else if (what == "loop_wrap") {
Expand Down Expand Up @@ -520,7 +524,10 @@ bool Animation::_get(const StringName &p_name, Variant &r_ret) const {
}

return true;

} else if (what == "auto_volume") {
if (track_get_type(track) == TYPE_AUDIO) {
r_ret = audio_track_get_auto_volume(track);
}
} else if (what == "interp") {
r_ret = track_get_interpolation_type(track);
} else if (what == "loop_wrap") {
Expand Down Expand Up @@ -815,6 +822,9 @@ void Animation::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/loop_wrap", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
p_list->push_back(PropertyInfo(Variant::ARRAY, "tracks/" + itos(i) + "/keys", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
}
if (track_get_type(i) == TYPE_AUDIO) {
p_list->push_back(PropertyInfo(Variant::BOOL, "tracks/" + itos(i) + "/auto_volume", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
}
}
}

Expand Down Expand Up @@ -3560,6 +3570,27 @@ real_t Animation::audio_track_get_key_end_offset(int p_track, int p_key) const {
return at->values[p_key].value.end_offset;
}

void Animation::audio_track_set_auto_volume(int p_track, bool p_enable) {
ERR_FAIL_INDEX(p_track, tracks.size());
Track *t = tracks[p_track];
ERR_FAIL_COND(t->type != TYPE_AUDIO);

AudioTrack *at = static_cast<AudioTrack *>(t);

at->auto_volume = p_enable;
emit_changed();
}

bool Animation::audio_track_get_auto_volume(int p_track) const {
ERR_FAIL_INDEX_V(p_track, tracks.size(), false);
Track *t = tracks[p_track];
ERR_FAIL_COND_V(t->type != TYPE_AUDIO, false);

AudioTrack *at = static_cast<AudioTrack *>(t);

return at->auto_volume;
}

//

int Animation::animation_track_insert_key(int p_track, double p_time, const StringName &p_animation) {
Expand Down Expand Up @@ -3798,6 +3829,8 @@ void Animation::_bind_methods() {
ClassDB::bind_method(D_METHOD("audio_track_get_key_stream", "track_idx", "key_idx"), &Animation::audio_track_get_key_stream);
ClassDB::bind_method(D_METHOD("audio_track_get_key_start_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_start_offset);
ClassDB::bind_method(D_METHOD("audio_track_get_key_end_offset", "track_idx", "key_idx"), &Animation::audio_track_get_key_end_offset);
ClassDB::bind_method(D_METHOD("audio_track_set_auto_volume", "track_idx", "enable"), &Animation::audio_track_set_auto_volume);
ClassDB::bind_method(D_METHOD("audio_track_get_auto_volume", "track_idx"), &Animation::audio_track_get_auto_volume);

ClassDB::bind_method(D_METHOD("bezier_track_set_key_handle_mode", "track_idx", "key_idx", "key_handle_mode", "balanced_value_time_ratio"), &Animation::bezier_track_set_key_handle_mode, DEFVAL(1.0));
ClassDB::bind_method(D_METHOD("bezier_track_get_key_handle_mode", "track_idx", "key_idx"), &Animation::bezier_track_get_key_handle_mode);
Expand Down
3 changes: 3 additions & 0 deletions scene/resources/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class Animation : public Resource {

struct AudioTrack : public Track {
Vector<TKey<AudioKey>> values;
bool auto_volume = false;

AudioTrack() {
type = TYPE_AUDIO;
Expand Down Expand Up @@ -443,6 +444,8 @@ class Animation : public Resource {
Ref<Resource> audio_track_get_key_stream(int p_track, int p_key) const;
real_t audio_track_get_key_start_offset(int p_track, int p_key) const;
real_t audio_track_get_key_end_offset(int p_track, int p_key) const;
void audio_track_set_auto_volume(int p_track, bool p_enable);
bool audio_track_get_auto_volume(int p_track) const;

int animation_track_insert_key(int p_track, double p_time, const StringName &p_animation);
void animation_track_set_key_animation(int p_track, int p_key, const StringName &p_animation);
Expand Down

0 comments on commit dfe4fe4

Please sign in to comment.