From 4b6a716ad9f1bb7a742b5c752490dc47e4119a68 Mon Sep 17 00:00:00 2001 From: codereader Date: Sat, 17 Sep 2022 07:49:05 +0200 Subject: [PATCH] #5537: Refactor SoundShaderSelector to manage the preview on its own --- radiant/ui/common/SoundChooser.cpp | 22 ++++++---------------- radiant/ui/common/SoundChooser.h | 5 +---- radiant/ui/common/SoundShaderSelector.h | 22 +++++++++++++++++++++- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/radiant/ui/common/SoundChooser.cpp b/radiant/ui/common/SoundChooser.cpp index b2817613b7..3fb6c9211e 100644 --- a/radiant/ui/common/SoundChooser.cpp +++ b/radiant/ui/common/SoundChooser.cpp @@ -26,8 +26,7 @@ namespace // Constructor SoundChooser::SoundChooser(wxWindow* parent) : DialogBase(_("Choose sound"), parent), - _selector(nullptr), - _preview(new SoundShaderPreview(this)) + _selector(nullptr) { SetSizer(new wxBoxSizer(wxVERTICAL)); @@ -47,8 +46,7 @@ SoundChooser::SoundChooser(wxWindow* parent) : _selector->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &SoundChooser::_onItemActivated, this); GetSizer()->Add(_selector, 1, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 12); - GetSizer()->Add(_preview, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 12); - GetSizer()->Add(grid, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 12); + GetSizer()->Add(grid, 0, wxEXPAND | wxALL, 12); _windowPosition.initialise(this, RKEY_WINDOW_STATE, 0.5f, 0.7f); @@ -73,23 +71,15 @@ void SoundChooser::setSelectedShader(const std::string& shader) void SoundChooser::_onItemActivated(wxDataViewEvent& ev) { - auto selectedItem = _selector->GetSelectedDeclName();; + auto selectedItem = _selector->GetSelectedDeclName(); - if (selectedItem.empty()) - { - ev.Skip(); - return; - } - - if (!wxGetKeyState(WXK_CONTROL)) + if (!selectedItem.empty() && !wxGetKeyState(WXK_CONTROL)) { // simple double click closes modal, ctrl+dblclk plays sound EndModal(wxID_OK); - return; } - // It's a regular item, try to play it back - _preview->playRandomSoundFile(); + ev.Skip(); } int SoundChooser::ShowModal() @@ -109,7 +99,7 @@ int SoundChooser::ShowModal() void SoundChooser::_onReloadSounds(wxCommandEvent& ev) { - _preview->ClearPreview(); + _selector->SetSelectedDeclName({}); // Send the command to the SoundManager // After parsing it will fire the sounds reloaded signal => onShadersReloaded() diff --git a/radiant/ui/common/SoundChooser.h b/radiant/ui/common/SoundChooser.h index 9b4fb87bb0..72bc40c425 100644 --- a/radiant/ui/common/SoundChooser.h +++ b/radiant/ui/common/SoundChooser.h @@ -22,10 +22,7 @@ class SoundChooser : private: SoundShaderSelector* _selector; - // The preview widget group - SoundShaderPreview* _preview; - - sigc::connection _shadersReloaded; + sigc::connection _shadersReloaded; wxutil::WindowPosition _windowPosition; diff --git a/radiant/ui/common/SoundShaderSelector.h b/radiant/ui/common/SoundShaderSelector.h index 6b5dd1aca1..d92e5e8beb 100644 --- a/radiant/ui/common/SoundShaderSelector.h +++ b/radiant/ui/common/SoundShaderSelector.h @@ -1,6 +1,7 @@ #pragma once #include "DeclarationSelector.h" +#include "SoundShaderPreview.h" #include "wxutil/dataview/ThreadedDeclarationTreePopulator.h" #include "wxutil/dataview/VFSTreePopulator.h" @@ -64,19 +65,38 @@ class ThreadedSoundShaderLoader : class SoundShaderSelector : public DeclarationSelector { +private: + // The preview widget group + SoundShaderPreview* _preview; + public: SoundShaderSelector(wxWindow* parent) : - DeclarationSelector(parent, decl::Type::SoundShader) + DeclarationSelector(parent, decl::Type::SoundShader), + _preview(new SoundShaderPreview(this)) { GetTreeView()->SetExpandTopLevelItemsAfterPopulation(true); LoadSoundShaders(); + + AddPreviewToBottom(_preview); } void LoadSoundShaders() { PopulateTreeView(std::make_shared(GetColumns())); } + +protected: + void onTreeViewItemActivated() override + { + auto selectedItem = GetSelectedDeclName(); + + // Ctrl-Double-Click plays back a random file + if (wxGetKeyState(WXK_CONTROL)) + { + _preview->playRandomSoundFile(); + } + } }; }