diff --git a/src/ControlManager.cpp b/src/ControlManager.cpp index 9a6bbc5..3e2b277 100644 --- a/src/ControlManager.cpp +++ b/src/ControlManager.cpp @@ -1172,6 +1172,9 @@ bool Control::receiveSourceAttribute(Source *target, const std::string &attribut arguments >> str >> osc::EndMessage; if (str && strlen(str) > 0) { target->call(new SetCode(std::string(str)), true); + // show and refresh source editor if source is currently selected + if (target == Mixer::manager().currentSource()) + UserInterface::manager().showSourceEditor(target); } } /// e.g. '/vimix/current/filter sf blur 0.5' diff --git a/src/Source/SourceCallback.cpp b/src/Source/SourceCallback.cpp index 5001181..404a6d3 100644 --- a/src/Source/SourceCallback.cpp +++ b/src/Source/SourceCallback.cpp @@ -1304,27 +1304,18 @@ void SetFilter::update(Source *s, float dt) Log::Info("Filter Image: failed to get image filter"); break; } - // Open the file + FilteringProgram prog; + prog.setName( __f->program().name() ); + // try to open a file std::ifstream file(target_method_); // Check if the file is opened successfully if (file.is_open()) { - // Read the content of the file into an std::string - std::string fileContent((std::istreambuf_iterator(file)), - std::istreambuf_iterator()); - FilteringProgram prog; - prog.setName(target_method_); - prog.setCode({fileContent, ""}); - // get alpha filter - __f->setProgram(prog); + prog.setFilename(target_method_); + __f->setProgram(prog); + file.close(); } - else { - FilteringProgram prog; - prog.setName( __f->program().name() ); - prog.setCode({target_method_, ""}); - __f->setProgram(prog); - } - // Close the file - file.close(); + else + Log::Info("Filter Image: failed to open file '%s'", target_method_.c_str()); } break; default: break; @@ -1501,7 +1492,7 @@ void SetUniform::accept(Visitor& v) } SetCode::SetCode(const std::string &code) : SourceCallback(), - code_(code) + code_(code), compilation_(nullptr) { } @@ -1514,6 +1505,25 @@ void SetCode::update(Source *s, float dt) if (s->locked() || ( !clonesrc && !shadersrc ) ) status_ = FINISHED; + // update if there is an ongoing compilation + if (status_ == ACTIVE && compilation_ != nullptr ) { + + static std::chrono::milliseconds timeout = std::chrono::milliseconds(4); + if (compilation_return_.wait_for(timeout) == std::future_status::ready ) + { + // get message returned from compilation + std::string status_ = compilation_return_.get(); + Log::Info("setCode: %s", status_.c_str()); + + // end compilation promise + delete compilation_; + compilation_ = nullptr; + + // done + status_ = FINISHED; + } + } + // apply when ready if (status_ == READY) { // if there is an image filter in the source @@ -1523,16 +1533,37 @@ void SetCode::update(Source *s, float dt) else __f = dynamic_cast(shadersrc->filter()); if (__f) { + + // set code to the image filter and start compilation FilteringProgram prog; prog.setName( __f->program().name() ); - prog.setCode({code_, ""}); - __f->setProgram(prog); - } - else - Log::Info("setCode: failed to get image filter"); - status_ = FINISHED; + // try to open a file + std::ifstream file(code_); + // Check if the file is opened successfully + if (file.is_open()) { + prog.setFilename(code_); + } + else { + prog.resetFilename(); + prog.setCode({code_, ""}); + } + file.close(); // close anyways + + // compile new code + compilation_ = new std::promise(); + __f->setProgram(prog, compilation_); + compilation_return_ = compilation_->get_future(); + + // building + status_ = ACTIVE; + } + else { + Log::Info("setCode: failed to get image filter"); + status_ = FINISHED; + } } + } SourceCallback *SetCode::clone() const diff --git a/src/Source/SourceCallback.h b/src/Source/SourceCallback.h index 0f1c37a..c2bf5f2 100644 --- a/src/Source/SourceCallback.h +++ b/src/Source/SourceCallback.h @@ -3,6 +3,7 @@ #include #include +#include #include class Visitor; @@ -536,6 +537,8 @@ public: class SetCode : public SourceCallback { std::string code_; + std::promise *compilation_; + std::future compilation_return_; public: SetCode (const std::string &code = std::string()); diff --git a/src/Window/ShaderEditWindow.cpp b/src/Window/ShaderEditWindow.cpp index cd15764..7e50260 100644 --- a/src/Window/ShaderEditWindow.cpp +++ b/src/Window/ShaderEditWindow.cpp @@ -70,7 +70,7 @@ void saveEditorText(const std::string &filename) /// SHADER EDITOR /// /// -ShaderEditWindow::ShaderEditWindow() : WorkspaceWindow("Shader"), _cs_id(0), current_(nullptr), show_shader_inputs_(false) +ShaderEditWindow::ShaderEditWindow() : WorkspaceWindow("Shader"), _cs_id(0), current_(nullptr), show_shader_inputs_(false), compilation_(nullptr) { auto lang = TextEditor::LanguageDefinition::GLSL();