Skip to content

Commit

Permalink
#5634: Check the system clipboard to detect any material names that h…
Browse files Browse the repository at this point in the history
…ave been copied there
  • Loading branch information
codereader committed Jun 10, 2021
1 parent 04e988e commit ca74f71
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
49 changes: 43 additions & 6 deletions radiantcore/selection/shaderclipboard/ShaderClipboard.cpp
Expand Up @@ -4,7 +4,9 @@
#include "imap.h"
#include "iselectiontest.h"
#include "iscenegraph.h"
#include "ishaders.h"
#include "iclipboard.h"
#include "string/trim.h"
#include "ClosestTexturableFinder.h"

#include "util/ScopedBoolLock.h"
Expand Down Expand Up @@ -52,6 +54,12 @@ void ShaderClipboard::sourceChanged()
util::ScopedBoolLock lock(_updatesDisabled); // prevent loopbacks

_signalSourceChanged.emit();

// Sync the material name to the system clipboard
if (!_source.empty() && module::GlobalModuleRegistry().moduleExists(MODULE_CLIPBOARD))
{
GlobalClipboard().setString(_source.getShader());
}
}

void ShaderClipboard::clear()
Expand Down Expand Up @@ -94,12 +102,6 @@ void ShaderClipboard::pickFromSelectionTest(SelectionTest& test)

_source = getTexturable(test);

// Copy the material name to the system clipboard when picking
if (!_source.empty() && module::GlobalModuleRegistry().moduleExists(MODULE_CLIPBOARD))
{
GlobalClipboard().setString(_source.getShader());
}

sourceChanged();
}

Expand Down Expand Up @@ -234,13 +236,48 @@ void ShaderClipboard::initialiseModule(const IApplicationContext& ctx)
sigc::mem_fun(this, &ShaderClipboard::onMapEvent));

clear();

module::GlobalModuleRegistry().signal_allModulesInitialised().connect(
sigc::mem_fun(this, &ShaderClipboard::postModuleInitialisation)
);
}

void ShaderClipboard::shutdownModule()
{
_postUndoConn.disconnect();
_postRedoConn.disconnect();
_mapEventConn.disconnect();
_clipboardContentsChangedConn.disconnect();
}

void ShaderClipboard::postModuleInitialisation()
{
if (module::GlobalModuleRegistry().moduleExists(MODULE_CLIPBOARD))
{
// Subscribe to clipboard changes to check for copied material names
_clipboardContentsChangedConn = GlobalClipboard().signal_clipboardContentChanged().connect(
sigc::mem_fun(this, &ShaderClipboard::onSystemClipboardContentsChanged)
);
}
}

void ShaderClipboard::onSystemClipboardContentsChanged()
{
if (_updatesDisabled) return;

auto candidate = GlobalClipboard().getString();

// If we get a single line, check if the contents point to a material we know
if (!candidate.empty() && candidate.find('\n') == std::string::npos)
{
string::trim(candidate);

if (GlobalMaterialManager().materialExists(candidate))
{
rMessage() << "Found a valid material name in the system clipboard: " << candidate << std::endl;
setSourceShader(candidate);
}
}
}

// Define the static module
Expand Down
4 changes: 4 additions & 0 deletions radiantcore/selection/shaderclipboard/ShaderClipboard.h
Expand Up @@ -24,6 +24,7 @@ class ShaderClipboard :
sigc::connection _postUndoConn;
sigc::connection _postRedoConn;
sigc::connection _mapEventConn;
sigc::connection _clipboardContentsChangedConn;

public:
ShaderClipboard();
Expand Down Expand Up @@ -82,6 +83,9 @@ class ShaderClipboard :
* given SelectionTest.
*/
Texturable getTexturable(SelectionTest& test);

void postModuleInitialisation();
void onSystemClipboardContentsChanged();
};

} // namespace selection

0 comments on commit ca74f71

Please sign in to comment.