Skip to content

Commit

Permalink
#5537: Migrate SoundChooser dialog to derive from DeclarationSelector…
Browse files Browse the repository at this point in the history
…Dialog
  • Loading branch information
codereader committed Sep 23, 2022
1 parent f8c5083 commit 0c13973
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 114 deletions.
1 change: 1 addition & 0 deletions install/user.xml
Expand Up @@ -342,6 +342,7 @@
<EntityClassChooser defaultWidthFraction="0.7" defaultHeightFraction="0.8" />
<SkinChooser defaultWidthFraction="0.7" defaultHeightFraction="0.8" />
<ParticleChooser defaultWidthFraction="0.7" defaultHeightFraction="0.8" />
<SoundChooser defaultWidthFraction="0.5" defaultHeightFraction="0.7" />
</windowStates>
</ui>
</user>
6 changes: 4 additions & 2 deletions libs/wxutil/decl/DeclarationSelector.cpp
Expand Up @@ -198,8 +198,10 @@ void DeclarationSelector::onTreeViewSelectionChanged(wxDataViewEvent& ev)
void DeclarationSelector::onTreeViewItemActivated(wxDataViewEvent& ev)
{
// Invoke the virtual method
onTreeViewItemActivated();
ev.Skip();
if (!onTreeViewItemActivated())
{
ev.Skip();
}
}

}
7 changes: 5 additions & 2 deletions libs/wxutil/decl/DeclarationSelector.h
Expand Up @@ -98,8 +98,11 @@ class DeclarationSelector :
{}

// Event method invoked when the tree view selection has been activated (Enter key or double-click)
virtual void onTreeViewItemActivated()
{}
// Returns true to indicate that the event has been handled and should not be further processed
virtual bool onTreeViewItemActivated()
{
return false;
}

// Default tree view columns. Subclasses can use a different set of columns if needed
static const DeclarationTreeView::Columns& CreateDefaultColumns();
Expand Down
3 changes: 3 additions & 0 deletions libs/wxutil/decl/DeclarationSelectorDialog.h
Expand Up @@ -19,6 +19,9 @@ class DeclarationSelector;
*
* Provides a tree view of available declaration items plus optional previews
* of the active selection.
*
* It will attempt to restore the last selected item from the registry when shown,
* unless the SetSelectedDeclName() method is called, which always takes precedence.
*/
class DeclarationSelectorDialog :
public DialogBase,
Expand Down
106 changes: 12 additions & 94 deletions radiant/ui/common/SoundChooser.cpp
@@ -1,105 +1,33 @@
#include "SoundChooser.h"

#include "i18n.h"
#include "isound.h"
#include "ideclmanager.h"
#include "registry/registry.h"

#include "wxutil/dataview/VFSTreePopulator.h"
#include "ui/UserInterfaceModule.h"

#include <wx/sizer.h>
#include <wx/button.h>
#include <wx/stattext.h>

#include <sigc++/functors/mem_fun.h>

namespace ui
{

namespace
{
constexpr const char* const RKEY_WINDOW_STATE = "user/ui/soundChooser/window";
constexpr const char* const RKEY_LAST_SELECTED_SHADER = "user/ui/soundChooser/lastSelectedShader";
}

// Constructor
SoundChooser::SoundChooser(wxWindow* parent) :
DialogBase(_("Choose sound"), parent),
DeclarationSelectorDialog(decl::Type::SoundShader, _("Choose sound"), "SoundChooser", parent),
_selector(nullptr)
{
SetSizer(new wxBoxSizer(wxVERTICAL));

auto* buttonSizer = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
auto* reloadButton = new wxButton(this, wxID_ANY, _("Reload Sounds"));
reloadButton->Bind(wxEVT_BUTTON, &SoundChooser::_onReloadSounds, this);

buttonSizer->Prepend(reloadButton, 0, wxRIGHT, 32);
auto* dblClickHint = new wxStaticText(this, wxID_ANY,
_( "Ctrl + Double Click on treeview items for quick play" ), wxDefaultPosition, wxDefaultSize, wxALIGN_RIGHT);
auto* grid = new wxFlexGridSizer( 2 );
grid->AddGrowableCol( 1 );
grid->Add( dblClickHint, 0, wxALIGN_CENTER_VERTICAL );
grid->Add( buttonSizer, 0, wxALIGN_RIGHT );

_selector = new SoundShaderSelector(this);
_selector->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &SoundChooser::_onItemActivated, this);

GetSizer()->Add(_selector, 1, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 12);
GetSizer()->Add(grid, 0, wxEXPAND | wxALL, 12);

_windowPosition.initialise(this, RKEY_WINDOW_STATE, 0.5f, 0.7f);
SetSelector(_selector);

// Load the shaders
loadSoundShaders();
auto dblClickHint = new wxStaticText(this, wxID_ANY, _("Ctrl + Double Click on treeview items for quick play"));
AddItemToBottomRow(dblClickHint);
}

void SoundChooser::loadSoundShaders()
{
_selector->Populate();
}

std::string SoundChooser::getSelectedShader() const
{
return _selector->GetSelectedDeclName();
}

void SoundChooser::setSelectedShader(const std::string& shader)
{
_selector->SetSelectedDeclName(shader);
}

void SoundChooser::_onItemActivated(wxDataViewEvent& ev)
{
auto selectedItem = _selector->GetSelectedDeclName();

if (!selectedItem.empty() && !wxGetKeyState(WXK_CONTROL))
{
// simple double click closes modal, ctrl+dblclk plays sound
EndModal(wxID_OK);
}

ev.Skip();
}

int SoundChooser::ShowModal()
{
_windowPosition.applyPosition();

_shadersReloaded = GlobalDeclarationManager().signal_DeclsReloaded(decl::Type::SoundShader)
.connect(sigc::mem_fun(this, &SoundChooser::onShadersReloaded));

int returnCode = DialogBase::ShowModal();

_windowPosition.saveToPath(RKEY_WINDOW_STATE);
_shadersReloaded.disconnect();

return returnCode;
}

void SoundChooser::_onReloadSounds(wxCommandEvent& ev)
{
_selector->SetSelectedDeclName({});
SetSelectedDeclName({});

// Send the command to the SoundManager
// After parsing it will fire the sounds reloaded signal => onShadersReloaded()
Expand All @@ -117,27 +45,17 @@ void SoundChooser::onShadersReloaded()

std::string SoundChooser::chooseResource(const std::string& shaderToPreselect)
{
auto preselected = !shaderToPreselect.empty() ? shaderToPreselect :
registry::getValue<std::string>(RKEY_LAST_SELECTED_SHADER);

if (!preselected.empty())
if (!shaderToPreselect.empty())
{
setSelectedShader(preselected);
SetSelectedDeclName(shaderToPreselect);
}

std::string selectedShader;

if (ShowModal() == wxID_OK)
{
selectedShader = getSelectedShader();

if (!selectedShader.empty())
{
registry::setValue(RKEY_LAST_SELECTED_SHADER, selectedShader);
}
}
if (ShowModal() == wxID_OK)
{
return GetSelectedDeclName();
}

return selectedShader;
return ""; // Empty selection on cancel
}

void SoundChooser::destroyDialog()
Expand Down
17 changes: 2 additions & 15 deletions radiant/ui/common/SoundChooser.h
@@ -1,11 +1,9 @@
#pragma once

#include "ui/iresourcechooser.h"
#include "wxutil/dialog/DialogBase.h"
#include "wxutil/WindowPosition.h"
#include "wxutil/decl/DeclarationSelectorDialog.h"
#include "ui/common/SoundShaderSelector.h"

#include "SoundShaderPreview.h"
#include <string>
#include <sigc++/connection.h>

Expand All @@ -16,36 +14,25 @@ namespace ui
* Dialog for listing and selection of sound shaders.
*/
class SoundChooser :
public wxutil::DialogBase,
public wxutil::DeclarationSelectorDialog,
public IResourceChooser
{
private:
SoundShaderSelector* _selector;

sigc::connection _shadersReloaded;

wxutil::WindowPosition _windowPosition;

private:
void loadSoundShaders();

// callbacks
void _onItemActivated(wxDataViewEvent& ev);
void _onReloadSounds(wxCommandEvent& ev);

void onShadersReloaded();

public:
SoundChooser(wxWindow* parent = nullptr);

// Retrieve the selected sound shader
std::string getSelectedShader() const;

// Set the selected sound shader, and focuses the treeview to the new selection
void setSelectedShader(const std::string& shader);

int ShowModal() override;

// Run the dialog and return the selected shader - this will be empty if the user clicks cancel
std::string chooseResource(const std::string& preselected = std::string()) override;

Expand Down
5 changes: 4 additions & 1 deletion radiant/ui/common/SoundShaderSelector.h
Expand Up @@ -87,15 +87,18 @@ class SoundShaderSelector :
}

protected:
void onTreeViewItemActivated() override
bool onTreeViewItemActivated() override
{
auto selectedItem = GetSelectedDeclName();

// Ctrl-Double-Click plays back a random file
if (wxGetKeyState(WXK_CONTROL))
{
_preview->playRandomSoundFile();
return true;
}

return false; // not processed
}
};

Expand Down

0 comments on commit 0c13973

Please sign in to comment.