Skip to content

Commit

Permalink
Make MediaBrowser a globally accessibe module
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 3, 2017
1 parent 08c129f commit c604f3e
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 50 deletions.
57 changes: 57 additions & 0 deletions include/imediabrowser.h
@@ -0,0 +1,57 @@
#pragma once

#include "imodule.h"
#include <string>

namespace ui
{

/**
* Interface to the MediaBrowser, which is displaying the
* available materials, accessible as a page in the
* GroupDialog's notebook.
*/
class IMediaBrowser :
public RegisterableModule
{
public:
virtual ~IMediaBrowser()
{}

/**
* Returns the texture name of the currently selected item.
* Will return an empty string if a folder is selected or
* nothing is selected at all.
*/
virtual std::string getSelection() = 0;

/** Set the given path as the current selection, highlighting it
* in the tree view.
*
* @param selection
* The fullname of the item to select, or the empty string if there
* should be no selection.
*/
virtual void setSelection(const std::string& selection) = 0;

// The tab name as registred in the GroupDialog
const char* const getGroupDialogTabName() const
{
return "mediabrowser";
}
};

}

const char* const MODULE_MEDIABROWSER = "MediaBrowser";

inline ui::IMediaBrowser& GlobalMediaBrowser()
{
// Cache the reference locally
static ui::IMediaBrowser& _mediaBrowser(
*std::static_pointer_cast<ui::IMediaBrowser>(
module::GlobalModuleRegistry().getModule(MODULE_MEDIABROWSER)
)
);
return _mediaBrowser;
}
1 change: 1 addition & 0 deletions include/precompiled_interfaces.h
Expand Up @@ -50,6 +50,7 @@
#include "imapresource.h"
#include "imd5anim.h"
#include "imd5model.h"
#include "imediabrowser.h"
#include "imenu.h"
#include "imodel.h"
#include "imodelcache.h"
Expand Down
12 changes: 7 additions & 5 deletions radiant/selection/shaderclipboard/ShaderClipboard.cpp
Expand Up @@ -4,7 +4,7 @@
#include "iselectiontest.h"
#include "iscenegraph.h"
#include "iuimanager.h"
#include "ui/mediabrowser/MediaBrowser.h"
#include "imediabrowser.h"
#include "ui/texturebrowser/TextureBrowser.h"
#include "ClosestTexturableFinder.h"

Expand Down Expand Up @@ -69,22 +69,24 @@ Texturable ShaderClipboard::getTexturable(SelectionTest& test) {
return returnValue;
}

void ShaderClipboard::updateMediaBrowsers() {
void ShaderClipboard::updateMediaBrowsers()
{
// Avoid nasty loopbacks
_updatesDisabled = true;

// Set the active shader in the Texture window as well
GlobalTextureBrowser().setSelectedShader(_source.getShader());

std::string sourceShader = _source.getShader();
ui::MediaBrowser::getInstance().setSelection(sourceShader);
GlobalMediaBrowser().setSelection(sourceShader);

_updatesDisabled = false;

updateStatusText();
}

void ShaderClipboard::updateStatusText() {

void ShaderClipboard::updateStatusText()
{
std::string statusText;

if (!_source.empty()) {
Expand Down
61 changes: 31 additions & 30 deletions radiant/ui/mediabrowser/MediaBrowser.cpp
Expand Up @@ -325,8 +325,6 @@ class MediaBrowser::Populator :
}
};

std::string MediaBrowser::GROUPDIALOG_TAB_NAME = "mediabrowser";

// Constructor
MediaBrowser::MediaBrowser() :
_tempParent(nullptr),
Expand Down Expand Up @@ -427,25 +425,12 @@ bool MediaBrowser::isDirectorySelected()
return row[_columns.isFolder].getBool();
}

std::string MediaBrowser::getSelectedName()
{
// Get the selected value
wxDataViewItem item = _treeView->GetSelection();

if (!item.IsOk()) return ""; // nothing selected

// Cast to TreeModel::Row and get the full name
wxutil::TreeModel::Row row(item, *_treeView->GetModel());

return row[_columns.fullName];
}

void MediaBrowser::onRadiantStartup()
{
// Add the Media Browser page
IGroupDialog::PagePtr mediaBrowserPage(new IGroupDialog::Page);

mediaBrowserPage->name = GROUPDIALOG_TAB_NAME;
mediaBrowserPage->name = getGroupDialogTabName();
mediaBrowserPage->windowLabel = _("Media");
mediaBrowserPage->page = _mainWidget;
mediaBrowserPage->tabIcon = "folder16.png";
Expand All @@ -461,6 +446,27 @@ void MediaBrowser::onRadiantStartup()
}
}

std::string MediaBrowser::getSelection()
{
if (!_isPopulated)
{
return std::string();
}

// Get the selected value
wxDataViewItem item = _treeView->GetSelection();

if (!item.IsOk())
{
return std::string(); // nothing selected
}

// Cast to TreeModel::Row and get the full name
wxutil::TreeModel::Row row(item, *_treeView->GetModel());

return row[_columns.fullName];
}

// Set the selection in the treeview
void MediaBrowser::setSelection(const std::string& selection)
{
Expand Down Expand Up @@ -547,7 +553,7 @@ void MediaBrowser::_onLoadInTexView()
{
// Use a TextureDirectoryLoader functor to search the directory. This
// may throw an exception if cancelled by user.
TextureDirectoryLoader loader(getSelectedName());
TextureDirectoryLoader loader(getSelection());

try
{
Expand All @@ -571,30 +577,30 @@ bool MediaBrowser::_testLoadInTexView()
void MediaBrowser::_onApplyToSel()
{
// Pass shader name to the selection system
selection::algorithm::applyShaderToSelection(getSelectedName());
selection::algorithm::applyShaderToSelection(getSelection());
}

// Check if a single non-directory texture is selected (used by multiple menu
// options).
bool MediaBrowser::_testSingleTexSel()
{
if (!isDirectorySelected() && !getSelectedName().empty())
if (!isDirectorySelected() && !getSelection().empty())
return true;
else
return false;
}

void MediaBrowser::_onShowShaderDefinition()
{
std::string shaderName = getSelectedName();
std::string shaderName = getSelection();

// Construct a shader view and pass the shader name
ShaderDefinitionView::ShowDialog(shaderName);
}

void MediaBrowser::_onSelectItems(bool select)
{
std::string shaderName = getSelectedName();
std::string shaderName = getSelection();

if (select)
{
Expand Down Expand Up @@ -627,8 +633,8 @@ void MediaBrowser::handleSelectionChange()
// Update the preview if a texture is selected
if (!isDirectorySelected())
{
_preview->SetTexture(getSelectedName());
GlobalShaderClipboard().setSource(getSelectedName());
_preview->SetTexture(getSelection());
GlobalShaderClipboard().setSource(getSelection());
}
else
{
Expand All @@ -645,7 +651,7 @@ void MediaBrowser::_onSelectionChanged(wxTreeEvent& ev)

void MediaBrowser::togglePage(const cmd::ArgumentList& args)
{
GlobalGroupDialog().togglePage(GROUPDIALOG_TAB_NAME);
GlobalGroupDialog().togglePage(getGroupDialogTabName());
}

const std::string& MediaBrowser::getName() const
Expand Down Expand Up @@ -678,7 +684,7 @@ void MediaBrowser::initialiseModule(const ApplicationContext& ctx)

// We need to create the liststore and widgets before attaching ourselves
// to the material manager as observer, as the attach() call below
// will trigger a realise() callback, which trigger population
// will invoke a realise() callback, which triggers a population
construct();

// The startup event will add this page to the group dialog tab
Expand All @@ -699,9 +705,4 @@ void MediaBrowser::shutdownModule()
// Static module
module::StaticModule<MediaBrowser> mediaBrowserModule;

MediaBrowser& MediaBrowser::getInstance()
{
return *mediaBrowserModule.getModule();
}

} // namespace
20 changes: 8 additions & 12 deletions radiant/ui/mediabrowser/MediaBrowser.h
@@ -1,5 +1,6 @@
#pragma once

#include "imediabrowser.h"
#include "iradiant.h"
#include "imodule.h"
#include "icommandsystem.h"
Expand Down Expand Up @@ -27,7 +28,7 @@ class TexturePreviewCombo;
* into the texture window or applying directly to map geometry.
*/
class MediaBrowser :
public RegisterableModule,
public IMediaBrowser,
public wxEvtHandler,
public ModuleObserver // to monitor the MaterialManager module
{
Expand All @@ -52,8 +53,6 @@ class MediaBrowser :

class PopulatorFinishedEvent; // wxEvent type

static std::string GROUPDIALOG_TAB_NAME;

private:
wxFrame* _tempParent;

Expand Down Expand Up @@ -96,36 +95,33 @@ class MediaBrowser :

/* Tree selection query functions */
bool isDirectorySelected(); // is a directory selected
std::string getSelectedName(); // return name of selection

// Populates the treeview
void populate();

void onTreeStorePopulationFinished(wxutil::TreeModel::PopulationFinishedEvent& ev);

public:

/** Return the singleton instance.
*/
static MediaBrowser& getInstance();

/** Constructor creates widgets.
*/
MediaBrowser();

// Returns the currently selected item, or an empty string if nothing is selected
std::string getSelection() override;

/** Set the given path as the current selection, highlighting it
* in the tree view.
*
* @param selection
* The fullname of the item to select, or the empty string if there
* should be no selection.
*/
void setSelection(const std::string& selection);
void setSelection(const std::string& selection) override;

// ModuleObserver implementation, these are called when the MaterialManager
// is emitting realise signals
void unrealise();
void realise();
void unrealise() override;
void realise() override;

const std::string& getName() const override;
const StringSet& getDependencies() const override;
Expand Down
6 changes: 3 additions & 3 deletions radiant/ui/texturebrowser/TextureBrowser.cpp
Expand Up @@ -7,6 +7,7 @@
#include "igroupdialog.h"
#include "iradiant.h"
#include "ipreferencesystem.h"
#include "imediabrowser.h"

#include "wxutil/menu/IconTextMenuItem.h"
#include "wxutil/GLWidget.h"
Expand All @@ -18,7 +19,6 @@
#include "shaderlib.h"
#include "selection/algorithm/Shader.h"
#include "selection/shaderclipboard/ShaderClipboard.h"
#include "ui/mediabrowser/MediaBrowser.h"

#include <boost/algorithm/string/predicate.hpp>
#include <functional>
Expand Down Expand Up @@ -833,8 +833,8 @@ void TextureBrowser::onSeekInMediaBrowser()
if (shader != NULL)
{
// Focus the MediaBrowser selection to the given shader
GlobalGroupDialog().setPage(MediaBrowser::GROUPDIALOG_TAB_NAME);
MediaBrowser::getInstance().setSelection(shader->getName());
GlobalGroupDialog().setPage(GlobalMediaBrowser().getGroupDialogTabName());
GlobalMediaBrowser().setSelection(shader->getName());
}
}

Expand Down
1 change: 1 addition & 0 deletions tools/msvc2015/include.vcxproj
Expand Up @@ -150,6 +150,7 @@
<ClInclude Include="..\..\include\imapresource.h" />
<ClInclude Include="..\..\include\imd5anim.h" />
<ClInclude Include="..\..\include\imd5model.h" />
<ClInclude Include="..\..\include\imediabrowser.h" />
<ClInclude Include="..\..\include\imenu.h" />
<ClInclude Include="..\..\include\imodel.h" />
<ClInclude Include="..\..\include\imodelcache.h" />
Expand Down

0 comments on commit c604f3e

Please sign in to comment.