Asset Catalogs: expose undo/redo operators to UI

Ensure that catalog operations create an undo snapshot, and show
undo/redo operators in the asset browser.

A hidden operator `ASSET_OT_catalog_undo_push` is also added such that
add-ons can also set undo snapshots if they need.
This commit is contained in:
Sybren A. Stüvel 2021-10-12 14:45:11 +02:00
parent f1d97a308d
commit 74ea21ec9d
5 changed files with 119 additions and 0 deletions

View File

@ -604,6 +604,7 @@ class ASSETBROWSER_MT_editor_menus(AssetBrowserMenu, Menu):
layout.menu("ASSETBROWSER_MT_view")
layout.menu("ASSETBROWSER_MT_select")
layout.menu("ASSETBROWSER_MT_edit")
class ASSETBROWSER_MT_view(AssetBrowserMenu, Menu):
@ -642,6 +643,16 @@ class ASSETBROWSER_MT_select(AssetBrowserMenu, Menu):
layout.operator("file.select_box")
class ASSETBROWSER_MT_edit(AssetBrowserMenu, Menu):
bl_label = "Edit"
def draw(self, _context):
layout = self.layout
layout.operator("asset.catalog_undo", text="Undo")
layout.operator("asset.catalog_redo", text="Redo")
class ASSETBROWSER_PT_metadata(asset_utils.AssetBrowserPanel, Panel):
bl_region_type = 'TOOL_PROPS'
bl_label = "Asset Metadata"
@ -795,6 +806,7 @@ classes = (
ASSETBROWSER_MT_editor_menus,
ASSETBROWSER_MT_view,
ASSETBROWSER_MT_select,
ASSETBROWSER_MT_edit,
ASSETBROWSER_PT_metadata,
ASSETBROWSER_PT_metadata_preview,
ASSETBROWSER_PT_metadata_details,

View File

@ -485,6 +485,7 @@ void AssetCatalogService::undo()
redo_snapshots_.append(std::move(catalog_collection_));
catalog_collection_ = std::move(undo_snapshots_.pop_last());
rebuild_tree();
}
void AssetCatalogService::redo()
@ -493,6 +494,7 @@ void AssetCatalogService::redo()
undo_snapshots_.append(std::move(catalog_collection_));
catalog_collection_ = std::move(redo_snapshots_.pop_last());
rebuild_tree();
}
void AssetCatalogService::undo_push()

View File

@ -66,6 +66,7 @@ AssetCatalog *ED_asset_catalog_add(::AssetLibrary *library,
std::string unique_name = catalog_name_ensure_unique(*catalog_service, name, parent_path);
AssetCatalogPath fullpath = AssetCatalogPath(parent_path) / unique_name;
catalog_service->undo_push();
return catalog_service->create_catalog(fullpath);
}
@ -77,5 +78,6 @@ void ED_asset_catalog_remove(::AssetLibrary *library, const CatalogID &catalog_i
return;
}
catalog_service->undo_push();
catalog_service->prune_catalogs_by_id(catalog_id);
}

View File

@ -19,6 +19,7 @@
*/
#include "BKE_asset_catalog.hh"
#include "BKE_asset_library.hh"
#include "BKE_context.h"
#include "BKE_lib_id.h"
#include "BKE_report.h"
@ -455,6 +456,103 @@ static void ASSET_OT_catalog_delete(struct wmOperatorType *ot)
RNA_def_string(ot->srna, "catalog_id", nullptr, 0, "Catalog ID", "ID of the catalog to delete");
}
static bke::AssetCatalogService *get_catalog_service(bContext *C)
{
const SpaceFile *sfile = CTX_wm_space_file(C);
if (!asset_operation_poll(C) || !sfile) {
return nullptr;
}
AssetLibrary *asset_lib = ED_fileselect_active_asset_library_get(sfile);
return BKE_asset_library_get_catalog_service(asset_lib);
}
static int asset_catalog_undo_exec(bContext *C, wmOperator * /*op*/)
{
bke::AssetCatalogService *catalog_service = get_catalog_service(C);
if (!catalog_service) {
return OPERATOR_CANCELLED;
}
catalog_service->undo();
return OPERATOR_FINISHED;
}
static bool asset_catalog_undo_poll(bContext *C)
{
const bke::AssetCatalogService *catalog_service = get_catalog_service(C);
return catalog_service && catalog_service->is_undo_possbile();
}
static void ASSET_OT_catalog_undo(struct wmOperatorType *ot)
{
/* identifiers */
ot->name = "Undo Catalog Edits";
ot->description = "Undo the last edit to the asset catalogs";
ot->idname = "ASSET_OT_catalog_undo";
/* api callbacks */
ot->exec = asset_catalog_undo_exec;
ot->poll = asset_catalog_undo_poll;
}
static int asset_catalog_redo_exec(bContext *C, wmOperator * /*op*/)
{
bke::AssetCatalogService *catalog_service = get_catalog_service(C);
if (!catalog_service) {
return OPERATOR_CANCELLED;
}
catalog_service->redo();
return OPERATOR_FINISHED;
}
static bool asset_catalog_redo_poll(bContext *C)
{
const bke::AssetCatalogService *catalog_service = get_catalog_service(C);
return catalog_service && catalog_service->is_redo_possbile();
}
static void ASSET_OT_catalog_redo(struct wmOperatorType *ot)
{
/* identifiers */
ot->name = "redo Catalog Edits";
ot->description = "Redo the last undone edit to the asset catalogs";
ot->idname = "ASSET_OT_catalog_redo";
/* api callbacks */
ot->exec = asset_catalog_redo_exec;
ot->poll = asset_catalog_redo_poll;
}
static int asset_catalog_undo_push_exec(bContext *C, wmOperator * /*op*/)
{
bke::AssetCatalogService *catalog_service = get_catalog_service(C);
if (!catalog_service) {
return OPERATOR_CANCELLED;
}
catalog_service->undo_push();
return OPERATOR_FINISHED;
}
static bool asset_catalog_undo_push_poll(bContext *C)
{
return get_catalog_service(C) != nullptr;
}
static void ASSET_OT_catalog_undo_push(struct wmOperatorType *ot)
{
/* identifiers */
ot->name = "Store undo snapshot for asset catalog edits";
ot->description = "Store the current state of the asset catalogs in the undo buffer";
ot->idname = "ASSET_OT_catalog_undo_push";
/* api callbacks */
ot->exec = asset_catalog_undo_push_exec;
ot->poll = asset_catalog_undo_push_poll;
}
/* -------------------------------------------------------------------- */
void ED_operatortypes_asset(void)
@ -464,6 +562,9 @@ void ED_operatortypes_asset(void)
WM_operatortype_append(ASSET_OT_catalog_new);
WM_operatortype_append(ASSET_OT_catalog_delete);
WM_operatortype_append(ASSET_OT_catalog_undo);
WM_operatortype_append(ASSET_OT_catalog_redo);
WM_operatortype_append(ASSET_OT_catalog_undo_push);
WM_operatortype_append(ASSET_OT_list_refresh);
}

View File

@ -356,6 +356,8 @@ bool AssetCatalogTreeViewItem::rename(StringRefNull new_name)
AssetCatalogPath new_path = catalog_item_.catalog_path().parent();
new_path = new_path / StringRef(new_name);
tree_view.catalog_service_->undo_push();
tree_view.catalog_service_->update_catalog_path(catalog_item_.get_catalog_id(), new_path);
return true;
}