Skip to content

Commit

Permalink
Merge pull request #32021 from Calinou/editor-audio-bus-snapping
Browse files Browse the repository at this point in the history
Implement snapping in the audio bus editor
  • Loading branch information
akien-mga authored Sep 23, 2019
2 parents 2f228cd + 8a81732 commit b8e56c5
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions editor/editor_audio_buses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "editor_audio_buses.h"

#include "core/io/resource_saver.h"
#include "core/os/input.h"
#include "core/os/keyboard.h"
#include "editor_node.h"
#include "filesystem_dock.h"
Expand Down Expand Up @@ -321,7 +322,13 @@ void EditorAudioBus::_volume_changed(float p_normalized) {

updating_bus = true;

float p_db = this->_normalized_volume_to_scaled_db(p_normalized);
const float p_db = this->_normalized_volume_to_scaled_db(p_normalized);

if (Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
// Snap the value when holding Ctrl for easier editing.
// To do so, it needs to be converted back to normalized volume (as the slider uses that unit).
slider->set_value(_scaled_db_to_normalized_volume(Math::round(p_db)));
}

UndoRedo *ur = EditorNode::get_undo_redo();
ur->create_action(TTR("Change Audio Bus Volume"), UndoRedo::MERGE_ENDS);
Expand Down Expand Up @@ -376,15 +383,24 @@ float EditorAudioBus::_scaled_db_to_normalized_volume(float db) {
}

void EditorAudioBus::_show_value(float slider_value) {
String text = vformat("%10.1f dB", _normalized_volume_to_scaled_db(slider_value));

float db;
if (Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
// Display the correct (snapped) value when holding Ctrl
db = Math::round(_normalized_volume_to_scaled_db(slider_value));
} else {
db = _normalized_volume_to_scaled_db(slider_value);
}

String text = vformat("%10.1f dB", db);

slider->set_tooltip(text);
audio_value_preview_label->set_text(text);
Vector2 slider_size = slider->get_size();
Vector2 slider_position = slider->get_global_position();
float left_padding = 5.0f;
float vert_padding = 10.0f;
Vector2 box_position = Vector2(slider_size.x + left_padding, (slider_size.y - vert_padding) * (1.0f - slider_value) - vert_padding);
Vector2 box_position = Vector2(slider_size.x + left_padding, (slider_size.y - vert_padding) * (1.0f - slider->get_value()) - vert_padding);
audio_value_preview_box->set_position(slider_position + box_position);
audio_value_preview_box->set_size(audio_value_preview_label->get_size());
if (slider->has_focus() && !audio_value_preview_box->is_visible()) {
Expand Down

0 comments on commit b8e56c5

Please sign in to comment.