From 29d3da44838904c9f530441237966cbde826b012 Mon Sep 17 00:00:00 2001 From: Yan Pujante Date: Wed, 23 Aug 2023 08:29:05 -0700 Subject: [PATCH] Added a way to commit all effects everywhere --- src/cpp/re/edit/AppContext.cpp | 26 +++++++++++++++++++++++--- src/cpp/re/edit/AppContext.h | 3 ++- src/cpp/re/edit/FilmStrip.cpp | 20 ++++++++++---------- src/cpp/re/edit/FilmStrip.h | 2 +- src/cpp/re/edit/Graphics.cpp | 29 ++++++++++++++--------------- src/cpp/re/edit/Graphics.h | 3 ++- src/cpp/re/edit/Panel.h | 2 ++ src/cpp/re/edit/PanelActions.cpp | 25 +++++++++++++++++++++++++ src/cpp/re/edit/TextureManager.cpp | 10 ++++++---- src/cpp/re/edit/TextureManager.h | 4 ++++ src/cpp/re/edit/Widget.h | 1 + src/cpp/re/edit/WidgetActions.cpp | 16 +++++++++++++++- 12 files changed, 105 insertions(+), 36 deletions(-) diff --git a/src/cpp/re/edit/AppContext.cpp b/src/cpp/re/edit/AppContext.cpp index c27a052..98ed26c 100644 --- a/src/cpp/re/edit/AppContext.cpp +++ b/src/cpp/re/edit/AppContext.cpp @@ -848,6 +848,10 @@ void AppContext::renderMainMenu() { handleUnusedTextures(); } + if(ImGui::MenuItem(ReGui_Prefix(ICON_FAC_SparklesCircleCheck, "Commit All Effects"))) + { + commitTextureEffects(); + } ImGui::EndMenu(); } @@ -1030,7 +1034,7 @@ void AppContext::save() UserError errors{}; auto GUI2D = fRoot / "GUI2D"; importBuiltIns(&errors); // convert built ins into actual images first (so that cmake() can see them) - applyEffects(&errors); + applyTextureEffects(&errors); Application::saveFile(GUI2D / "device_2D.lua", device2D(), &errors); Application::saveFile(GUI2D / "hdgui_2D.lua", hdgui2D(), &errors); if(fs::exists(fRoot / "CMakeLists.txt")) @@ -1297,9 +1301,9 @@ void AppContext::importBuiltIns(UserError *oErrors) } //------------------------------------------------------------------------ -// AppContext::applyEffects +// AppContext::applyTextureEffects //------------------------------------------------------------------------ -void AppContext::applyEffects(UserError *oErrors) +void AppContext::applyTextureEffects(UserError *oErrors) { std::vector effects{}; fFrontPanel->fPanel.collectFilmStripEffects(effects); @@ -1314,6 +1318,22 @@ void AppContext::applyEffects(UserError *oErrors) fTextureManager->applyEffects(effects, oErrors); } +//------------------------------------------------------------------------ +// AppContext::commitTextureEffects +//------------------------------------------------------------------------ +void AppContext::commitTextureEffects() +{ + beginUndoTx("Commit image effects"); + fFrontPanel->fPanel.commitTextureEffects(*this); + fBackPanel->fPanel.commitTextureEffects(*this); + if(fHasFoldedPanels) + { + fFoldedFrontPanel->fPanel.commitTextureEffects(*this); + fFoldedBackPanel->fPanel.commitTextureEffects(*this); + } + commitUndoTx(); +} + //------------------------------------------------------------------------ // AppContext::renderZoomSelection //------------------------------------------------------------------------ diff --git a/src/cpp/re/edit/AppContext.h b/src/cpp/re/edit/AppContext.h index 062c0f9..9bd5a51 100644 --- a/src/cpp/re/edit/AppContext.h +++ b/src/cpp/re/edit/AppContext.h @@ -259,7 +259,8 @@ class AppContext void reloadDevice(); void save(); void importBuiltIns(UserError *oErrors = nullptr); - void applyEffects(UserError *oErrors = nullptr); + void applyTextureEffects(UserError *oErrors = nullptr); + void commitTextureEffects(); std::string hdgui2D() const; std::string device2D() const; std::string cmake() const; diff --git a/src/cpp/re/edit/FilmStrip.cpp b/src/cpp/re/edit/FilmStrip.cpp index 5a2e4df..3a477e6 100644 --- a/src/cpp/re/edit/FilmStrip.cpp +++ b/src/cpp/re/edit/FilmStrip.cpp @@ -640,23 +640,23 @@ std::set FilmStripMgr::importBuiltIns(std::set FilmStripMgr::applyEffects(FilmStrip::key_t const &iKey, - texture::FX const &iEffects, - UserError *oErrors) +std::pair FilmStripMgr::applyEffects(FilmStrip::key_t const &iKey, + texture::FX const &iEffects, + UserError *oErrors) { auto filmStrip = findFilmStrip(iKey); if(filmStrip && filmStrip->isValid()) { // no effects => return if(!iEffects.hasAny()) - return std::nullopt; + return {iKey, false}; auto keyFX = FilmStrip::computeKey(iKey, filmStrip->numFrames(), iEffects); // do we already know about this? auto filmStripFX = findFilmStrip(keyFX); if(filmStripFX && filmStripFX->isValid()) - return std::nullopt; + return {keyFX, false}; else { // no we don't so save and add to map @@ -664,7 +664,7 @@ std::optional FilmStripMgr::applyEffects(FilmStrip::key_t cons if(filmStripFX) { fFilmStrips[keyFX] = filmStripFX; - return keyFX; + return {keyFX, true}; } else { @@ -674,7 +674,7 @@ std::optional FilmStripMgr::applyEffects(FilmStrip::key_t cons } } - return std::nullopt; + return {iKey, false}; } //------------------------------------------------------------------------ @@ -686,9 +686,9 @@ std::set FilmStripMgr::applyEffects(std::vector c for(auto const &e: iEffects) { - auto keyFX = applyEffects(e.fKey, e.fEffects, oErrors); - if(keyFX) - modifiedKeys.emplace(*keyFX); + auto [keyFX, modified] = applyEffects(e.fKey, e.fEffects, oErrors); + if(modified) + modifiedKeys.emplace(keyFX); } return modifiedKeys; diff --git a/src/cpp/re/edit/FilmStrip.h b/src/cpp/re/edit/FilmStrip.h index db3b37e..5e3bcf4 100644 --- a/src/cpp/re/edit/FilmStrip.h +++ b/src/cpp/re/edit/FilmStrip.h @@ -369,7 +369,7 @@ class FilmStripMgr std::set importBuiltIns(std::set const &iKeys, UserError *oErrors = nullptr); bool remove(FilmStrip::key_t const &iKey); - std::optional applyEffects(FilmStrip::key_t const &iKey, texture::FX const &iEffects, UserError *oErrors = nullptr); + std::pair applyEffects(FilmStrip::key_t const &iKey, texture::FX const &iEffects, UserError *oErrors = nullptr); std::set applyEffects(std::vector const &iEffects, UserError *oErrors = nullptr); static std::vector scanDirectory(fs::path const &iDirectory); diff --git a/src/cpp/re/edit/Graphics.cpp b/src/cpp/re/edit/Graphics.cpp index 09f2241..6625dad 100644 --- a/src/cpp/re/edit/Graphics.cpp +++ b/src/cpp/re/edit/Graphics.cpp @@ -304,14 +304,7 @@ void Graphics::editView(AppContext &iCtx) if(ImGui::MenuItem(ReGui_Prefix(ReGui_Icon_ResetAllEffects, "Reset All Effects"))) fParent->setBackgroundEffect("all effects (reset)", texture::kDefaultFX, MergeKey::from(&fEffects)); if(ImGui::MenuItem(ReGui_Prefix(ICON_FAC_SparklesCircleCheck, "Commit All Effects"))) - { - if(hasTexture()) - { - auto newKey = iCtx.applyTextureEffects(getTextureKey(), fEffects); - if(newKey) - fParent->setBackgroundKey(*newKey); - } - } + fParent->commitBackgroundEffects(iCtx); ImGui::EndDisabled(); ImGui::EndPopup(); @@ -788,13 +781,7 @@ void Graphics::editView(AppContext &iCtx) { editView(iCtx, fFilter, - [this](auto &k) { - update([this, &k] { - setTextureKey(k); - fFrameNumber = 0; - }, - fmt::printf("Change %s graphics", getParent()->getName())); - }, + [this](auto &k) { updateTextureKey(k); }, [this](auto &s) { update([this, &s] { if(hasTexture()) @@ -1055,11 +1042,23 @@ bool Graphics::copyFromAction(Attribute const *iFromAttribute) void Graphics::setTextureKey(Texture::key_t const &iTextureKey) { fTexture = iTextureKey; + fFrameNumber = 0; fDNZTexture = AppContext::GetCurrent().getTexture(iTextureKey); fEffects = texture::kDefaultFX; fEdited = true; } +//------------------------------------------------------------------------ +// Graphics::updateTextureKey +//------------------------------------------------------------------------ +void Graphics::updateTextureKey(Texture::key_t const &iTextureKey) +{ + update([this, &iTextureKey] { + setTextureKey(iTextureKey); + }, + fmt::printf("Change %s graphics", getParent()->getName())); +} + //------------------------------------------------------------------------ // Graphics::initTextureKey //------------------------------------------------------------------------ diff --git a/src/cpp/re/edit/Graphics.h b/src/cpp/re/edit/Graphics.h index 35f036f..2eb2de5 100644 --- a/src/cpp/re/edit/Graphics.h +++ b/src/cpp/re/edit/Graphics.h @@ -160,7 +160,8 @@ class Graphics : public Attribute inline Texture const *getTexture() const { RE_EDIT_INTERNAL_ASSERT(fDNZTexture != nullptr); return fDNZTexture.get(); } inline Texture::key_t getTextureKey() const { return std::get(fTexture); } - void setTextureKey(Texture::key_t const &iTextureKey); + void setTextureKey(Texture::key_t const &iTextureKey); // action only + void updateTextureKey(Texture::key_t const &iTextureKey); // action with undo void initTextureKey(Texture::key_t const &iTextureKey, std::optional const &iOriginalTextureKey, texture::FX const &iEffects); diff --git a/src/cpp/re/edit/Panel.h b/src/cpp/re/edit/Panel.h index c283b3c..390728b 100644 --- a/src/cpp/re/edit/Panel.h +++ b/src/cpp/re/edit/Panel.h @@ -111,6 +111,8 @@ class Panel : public Editable bool checkForErrors(AppContext &iCtx) override; void setBackgroundKey(Texture::key_t const &iTextureKey); + void commitBackgroundEffects(AppContext &iCtx); + void commitTextureEffects(AppContext &iCtx); void setBackgroundEffect(char const *iName, texture::FX const &fx, MergeKey const &iMergeKey); void setOptions(std::vector const &iOptions); int addWidget(AppContext &iCtx, std::unique_ptr iWidget, bool iMakeSingleSelected, char const *iUndoActionName = "Add"); diff --git a/src/cpp/re/edit/PanelActions.cpp b/src/cpp/re/edit/PanelActions.cpp index caefd68..7ff0993 100644 --- a/src/cpp/re/edit/PanelActions.cpp +++ b/src/cpp/re/edit/PanelActions.cpp @@ -941,6 +941,31 @@ void Panel::setBackgroundKey(Texture::key_t const &iTextureKey) MergeKey::from(&fGraphics.fTextureKey)); } +//------------------------------------------------------------------------ +// Panel::commitBackgroundEffects +//------------------------------------------------------------------------ +void Panel::commitBackgroundEffects(AppContext &iCtx) +{ + if(fGraphics.hasTexture() && fGraphics.fEffects.hasAny()) + { + auto newKey = iCtx.applyTextureEffects(fGraphics.getTextureKey(), fGraphics.fEffects); + if(newKey) + setBackgroundKey(*newKey); + } +} + +//------------------------------------------------------------------------ +// Panel::commitTextureEffects +//------------------------------------------------------------------------ +void Panel::commitTextureEffects(AppContext &iCtx) +{ + iCtx.beginUndoTx(fmt::printf("Commit image effects (%s)", getName())); + commitBackgroundEffects(iCtx); + for(auto &[id, w]: fWidgets) + w->commitTextureEffects(iCtx); + iCtx.commitUndoTx(); +} + //------------------------------------------------------------------------ // Panel::setBackgroundEffect //------------------------------------------------------------------------ diff --git a/src/cpp/re/edit/TextureManager.cpp b/src/cpp/re/edit/TextureManager.cpp index e38fcda..fcc5836 100644 --- a/src/cpp/re/edit/TextureManager.cpp +++ b/src/cpp/re/edit/TextureManager.cpp @@ -151,10 +151,12 @@ std::optional TextureManager::applyEffects(FilmStrip::key_t co texture::FX const &iEffects, UserError *oErrors) { - auto key = fFilmStripMgr->applyEffects(iKey, iEffects, oErrors); - if(key) - updateTexture(*key); - return key; + auto [keyFX, needsUpdate] = fFilmStripMgr->applyEffects(iKey, iEffects, oErrors); + if(needsUpdate) + { + updateTexture(keyFX); + } + return keyFX != iKey ? std::optional{keyFX} : std::nullopt; } //------------------------------------------------------------------------ diff --git a/src/cpp/re/edit/TextureManager.h b/src/cpp/re/edit/TextureManager.h index ae066e0..81837dc 100644 --- a/src/cpp/re/edit/TextureManager.h +++ b/src/cpp/re/edit/TextureManager.h @@ -47,7 +47,11 @@ class TextureManager std::optional importTexture(fs::path const &iTexturePath); void importBuiltIns(std::set const &iKeys, UserError *oErrors = nullptr); void applyEffects(std::vector const &iEffects, UserError *oErrors = nullptr); + + /** + * If no effects or no filmstrip found for `iKey` returns `std::nullopt` otherwise returns the key of the new texture */ std::optional applyEffects(FilmStrip::key_t const &iKey, texture::FX const &iEffects, UserError *oErrors = nullptr); + bool remove(FilmStrip::key_t const &iKey); protected: diff --git a/src/cpp/re/edit/Widget.h b/src/cpp/re/edit/Widget.h index a9e609f..8aa028e 100644 --- a/src/cpp/re/edit/Widget.h +++ b/src/cpp/re/edit/Widget.h @@ -113,6 +113,7 @@ class Widget : public Editable void init(AppContext &iCtx); void draw(AppContext &iCtx, ReGui::Canvas &iCanvas); void editView(AppContext &iCtx); + void commitTextureEffects(AppContext &iCtx); bool checkForErrors(AppContext &iCtx) override; void markEdited() override; diff --git a/src/cpp/re/edit/WidgetActions.cpp b/src/cpp/re/edit/WidgetActions.cpp index bfbc2e9..c92a5ba 100644 --- a/src/cpp/re/edit/WidgetActions.cpp +++ b/src/cpp/re/edit/WidgetActions.cpp @@ -142,7 +142,6 @@ void Widget::setVisibility(widget::Visibility iVisibility) MergeKey::from(&fName)); } - //------------------------------------------------------------------------ // Widget::toggleVisibility //------------------------------------------------------------------------ @@ -151,5 +150,20 @@ void Widget::toggleVisibility() setVisibility(isHidden() ? widget::Visibility::kManualVisible : widget::Visibility::kManualHidden); } +//------------------------------------------------------------------------ +// Widget::commitTextureEffects +//------------------------------------------------------------------------ +void Widget::commitTextureEffects(AppContext &iCtx) +{ + if(fGraphics->hasTexture() && fGraphics->fEffects.hasAny()) + { + auto newKey = iCtx.applyTextureEffects(fGraphics->getTextureKey(), fGraphics->fEffects); + if(newKey) + { + fGraphics->updateTextureKey(*newKey); + fEdited = fGraphics->isEdited(); + } + } +} } \ No newline at end of file