From 52d585b0d5733c7a781132de8c7dc09c5d97ecef Mon Sep 17 00:00:00 2001 From: TrickyLeifa Date: Fri, 8 Nov 2024 03:36:30 +0100 Subject: [PATCH] Tweaked sfx selection --- src/aoapplication.h | 2 + src/courtroom.cpp | 88 ++++++++++++++++--------------------- src/courtroom.h | 3 +- src/datatypes.h | 9 ++++ src/text_file_functions.cpp | 29 ++++++++++++ 5 files changed, 81 insertions(+), 50 deletions(-) diff --git a/src/aoapplication.h b/src/aoapplication.h index bc39d80a7..095daf60a 100644 --- a/src/aoapplication.h +++ b/src/aoapplication.h @@ -201,6 +201,8 @@ class AOApplication : public QObject // Figure out if we can opus this or if we should fall back to wav QString get_sfx_suffix(VPath sound_to_check); + QList get_sfx_list(QString file_name); + // Can we use APNG for this? If not, WEBP? If not, GIF? If not, fall back to // PNG. QString get_image_suffix(VPath path_to_check, bool static_image = false); diff --git a/src/courtroom.cpp b/src/courtroom.cpp index b089d6c92..fcdedf0a6 100644 --- a/src/courtroom.cpp +++ b/src/courtroom.cpp @@ -440,6 +440,7 @@ Courtroom::Courtroom(AOApplication *p_ao_app) connect(ui_iniswap_remove, &AOButton::clicked, this, &Courtroom::on_iniswap_remove_clicked); connect(ui_sfx_dropdown, QOverload::of(&QComboBox::currentIndexChanged), this, &Courtroom::on_sfx_dropdown_changed); + connect(ui_sfx_dropdown, &QComboBox::editTextChanged, this, &Courtroom::update_custom_sfx); connect(ui_sfx_dropdown, &QComboBox::customContextMenuRequested, this, &Courtroom::on_sfx_context_menu_requested); connect(ui_sfx_remove, &AOButton::clicked, this, &Courtroom::on_sfx_reset_selection); @@ -927,17 +928,14 @@ void Courtroom::set_widgets() ui_iniswap_remove->hide(); set_size_and_pos(ui_sfx_dropdown, "sfx_dropdown"); - ui_sfx_dropdown->setEditable(true); ui_sfx_dropdown->setInsertPolicy(QComboBox::NoInsert); ui_sfx_dropdown->setToolTip(tr("Set a sound effect to play on your next 'Preanim'. Leaving it on " - "Default will use the emote-defined sound (if any).\n" - "Edit by typing and pressing Enter, [X] to remove. This saves to your " - "base/characters//soundlist.ini")); + "Default will use the emote-defined sound (if any).")); set_size_and_pos(ui_sfx_remove, "sfx_remove"); ui_sfx_remove->setText("X"); ui_sfx_remove->setImage("evidencex"); - ui_sfx_remove->setToolTip(tr("Remove the currently selected sound effect.")); + ui_sfx_remove->setToolTip(tr("Reset the selected sound effect to default.")); ui_sfx_remove->hide(); set_iniswap_dropdown(); @@ -5106,91 +5104,87 @@ void Courtroom::set_sfx_dropdown() ui_sfx_remove->hide(); return; } - // Initialzie character sound list first. Will be empty if not found. - sound_list = ao_app->get_list_file(ao_app->get_character_path(current_char, "soundlist.ini")); - // If AO2 sound list is empty, try to find the DRO one. - if (sound_list.isEmpty()) - { - sound_list = ao_app->get_list_file(ao_app->get_character_path(current_char, "sounds.ini")); - } + sfx_list.clear(); + sfx_list.append(SfxItem{"Default"}); + sfx_list.append(SfxItem{"None"}); + sfx_list.append(SfxItem{"Editable"}); + + // Initialzie character sound list first. Will be empty if not found. + sfx_list.append(ao_app->get_sfx_list(ao_app->get_real_path(ao_app->get_character_path(current_char, "soundlist.ini")))); // Append default sound list after the character sound list. - sound_list += ao_app->get_list_file(VPath("soundlist.ini")); + sfx_list.append(ao_app->get_sfx_list(ao_app->get_real_path(VPath("soundlist.ini")))); QStringList display_sounds; - display_sounds.append("Default"); - display_sounds.append("None"); - display_sounds.append(QString()); - for (const QString &sound : qAsConst(sound_list)) + for (const SfxItem &sfx : qAsConst(sfx_list)) { - QStringList unpacked = sound.split("="); - QString display = unpacked[0].trimmed(); - if (unpacked.size() > 1) - { - display = unpacked[1].trimmed(); - } - - display_sounds.append(display); + ui_sfx_dropdown->addItem(sfx.nameOrFilename()); } ui_sfx_dropdown->show(); - ui_sfx_dropdown->addItems(display_sounds); ui_sfx_dropdown->setCurrentIndex(SFX_DEFAULT); ui_sfx_dropdown->blockSignals(false); } -void Courtroom::on_sfx_dropdown_changed(int p_index) +void Courtroom::update_custom_sfx(const QString &filename) +{ + const int index = ui_sfx_dropdown->currentIndex(); + if (index == SFX_EDITABLE) + { + sfx_list[index].filename = filename; + } +} + +void Courtroom::on_sfx_dropdown_changed(int index) { - ui_sfx_remove->setHidden(p_index == SFX_DEFAULT); - ui_sfx_dropdown->setEditable(p_index == SFX_EDITABLE); + ui_sfx_remove->setHidden(index == SFX_DEFAULT); + ui_sfx_dropdown->setEditable(index == SFX_EDITABLE); + + SfxItem &sfx = sfx_list[index]; if (ui_sfx_dropdown->isEditable()) { - ui_sfx_dropdown->setPlaceholderText("Editable custom sfx"); + ui_sfx_dropdown->setCurrentText(sfx.filename); + ui_sfx_dropdown->lineEdit()->setPlaceholderText(sfx.name); } else { - ui_sfx_dropdown->setPlaceholderText(QString()); + ui_sfx_dropdown->setCurrentText(sfx_list.at(index).nameOrFilename()); } + focus_ic_input(); } void Courtroom::on_sfx_context_menu_requested(const QPoint &pos) { - QMenu *menu = ui_sfx_dropdown->lineEdit()->createStandardContextMenu(); - + QMenu *menu = new QMenu; menu->setAttribute(Qt::WA_DeleteOnClose); - menu->addSeparator(); const QString sfx = get_current_sfx(); if (!sfx.isEmpty()) { - menu->addAction(QString("Play " + sfx), this, &Courtroom::on_sfx_play_clicked); + menu->addAction("Play " + sfx, this, &Courtroom::on_sfx_play_clicked); } if (file_exists(ao_app->get_real_path(ao_app->get_character_path(current_char, "soundlist.ini")))) { - menu->addAction(QString("Edit " + current_char + "/soundlist.ini"), this, &Courtroom::on_sfx_edit_requested); + menu->addAction("Edit " + current_char + "/soundlist.ini", this, &Courtroom::on_sfx_edit_requested); } else { - menu->addAction(QString("Edit base soundlist.ini"), this, &Courtroom::on_sfx_edit_requested); - } - - if (ui_sfx_dropdown->isEditable()) - { - menu->addAction(QString("Clear Edit Text"), this, &Courtroom::on_sfx_reset_selection); + menu->addAction("Edit base soundlist.ini", this, &Courtroom::on_sfx_edit_requested); } menu->addSeparator(); - menu->addAction(QString("Open base sounds folder"), this, [=] { - QString p_path = get_base_path() + "sounds/general/"; + menu->addAction("Open base sounds folder", this, [this] { + QString p_path = get_base_path() + "sounds/" + current_char + "/"; if (!dir_exists(p_path)) { return; } QDesktopServices::openUrl(QUrl::fromLocalFile(p_path)); }); + menu->popup(ui_sfx_dropdown->mapToGlobal(pos)); } @@ -5221,10 +5215,6 @@ void Courtroom::on_sfx_edit_requested() void Courtroom::on_sfx_reset_selection() { - if (ui_sfx_dropdown->isEditable()) - { - ui_sfx_dropdown->setCurrentText(QString()); - } ui_sfx_dropdown->setCurrentIndex(SFX_DEFAULT); } @@ -5335,7 +5325,7 @@ QString Courtroom::get_current_sfx() return ui_sfx_dropdown->currentText(); default: - return sound_list.value(index - (SFX_EDITABLE + 1)).split("=")[0].trimmed(); + return sfx_list.at(index).filename; } return QString(); diff --git a/src/courtroom.h b/src/courtroom.h index 01fa227bd..41092a4b8 100644 --- a/src/courtroom.h +++ b/src/courtroom.h @@ -530,7 +530,7 @@ class Courtroom : public QMainWindow // Text Color-related optimization END // Current list file sorted line by line - QStringList sound_list; + QList sfx_list; // is the message we're about to send supposed to present evidence? bool is_presenting_evidence = false; @@ -867,6 +867,7 @@ private Q_SLOTS: void on_sfx_dropdown_changed(int p_index); void set_sfx_dropdown(); + void update_custom_sfx(const QString &filename); void on_sfx_context_menu_requested(const QPoint &pos); void on_sfx_play_clicked(); void on_sfx_edit_requested(); diff --git a/src/datatypes.h b/src/datatypes.h index 46d824966..240e98214 100644 --- a/src/datatypes.h +++ b/src/datatypes.h @@ -36,6 +36,15 @@ struct pos_size_type int height = 0; }; +class SfxItem +{ +public: + QString name; + QString filename; + + inline QString nameOrFilename() const { return name.isEmpty() ? filename : name; } +}; + enum CHAT_MESSAGE { DESK_MOD = 0, diff --git a/src/text_file_functions.cpp b/src/text_file_functions.cpp index 4cf770b0e..1a3ec6443 100644 --- a/src/text_file_functions.cpp +++ b/src/text_file_functions.cpp @@ -378,6 +378,35 @@ QString AOApplication::get_sfx_suffix(VPath sound_to_check) return get_real_path(sound_to_check, suffixes); } +QList AOApplication::get_sfx_list(QString file_name) +{ + QList list; + + QFile file(file_name); + if (!file.open(QIODevice::ReadOnly)) + { + qWarning() << "Failed to open sfx file" << file_name << ":" << file.errorString(); + return list; + } + + QTextStream in(&file); + while (!in.atEnd()) + { + QStringList raw = in.readLine().split("="); + if (raw.isEmpty()) + { + continue; + } + + SfxItem item; + item.filename = raw.value(0); + list.append(item); + } + file.close(); + + return list; +} + QString AOApplication::get_image_suffix(VPath path_to_check, bool static_image) { QStringList suffixes{};