Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/asset/intern/asset_ops.cc
- This file was moved from source/blender/editors/asset/asset_ops.cc.
| Show First 20 Lines • Show All 163 Lines • ▼ Show 20 Lines | |||||
| } | } | ||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| class AssetClearHelper { | class AssetClearHelper { | ||||
| public: | public: | ||||
| void operator()(PointerRNAVec &ids); | void operator()(PointerRNAVec &ids); | ||||
| void reportResults(ReportList &reports) const; | void reportResults(const bContext *C, ReportList &reports) const; | ||||
| bool wasSuccessful() const; | bool wasSuccessful() const; | ||||
| private: | private: | ||||
| struct Stats { | struct Stats { | ||||
| int tot_cleared = 0; | int tot_cleared = 0; | ||||
| ID *last_id = nullptr; | ID *last_id = nullptr; | ||||
| }; | }; | ||||
| Show All 12 Lines | for (PointerRNA &ptr : ids) { | ||||
| if (ED_asset_clear_id(id)) { | if (ED_asset_clear_id(id)) { | ||||
| stats.tot_cleared++; | stats.tot_cleared++; | ||||
| stats.last_id = id; | stats.last_id = id; | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| void AssetClearHelper::reportResults(ReportList &reports) const | void AssetClearHelper::reportResults(const bContext *C, ReportList &reports) const | ||||
| { | { | ||||
| if (!wasSuccessful()) { | if (!wasSuccessful()) { | ||||
| bool is_valid; | |||||
| /* Dedicated error message for when there is an active asset detected, but it's not an ID local | |||||
| * to this file. Helps users better understanding what's going on. */ | |||||
| if (AssetHandle active_asset = CTX_wm_asset_handle(C, &is_valid); | |||||
| is_valid && !ED_asset_handle_get_local_id(&active_asset)) { | |||||
| BKE_report(&reports, | |||||
| RPT_ERROR, | |||||
| "No asset data-blocks from the current file selected (assets must be stored in " | |||||
| "the current file to be able to edit or clear them)"); | |||||
| } | |||||
| else { | |||||
| BKE_report(&reports, RPT_ERROR, "No asset data-blocks selected/focused"); | BKE_report(&reports, RPT_ERROR, "No asset data-blocks selected/focused"); | ||||
| } | } | ||||
| } | |||||
| else if (stats.tot_cleared == 1) { | else if (stats.tot_cleared == 1) { | ||||
| /* If only one data-block: Give more useful message by printing asset name. */ | /* If only one data-block: Give more useful message by printing asset name. */ | ||||
| BKE_reportf( | BKE_reportf( | ||||
| &reports, RPT_INFO, "Data-block '%s' is no asset anymore", stats.last_id->name + 2); | &reports, RPT_INFO, "Data-block '%s' is no asset anymore", stats.last_id->name + 2); | ||||
| } | } | ||||
| else { | else { | ||||
| BKE_reportf(&reports, RPT_INFO, "%i data-blocks are no assets anymore", stats.tot_cleared); | BKE_reportf(&reports, RPT_INFO, "%i data-blocks are no assets anymore", stats.tot_cleared); | ||||
| } | } | ||||
| } | } | ||||
| bool AssetClearHelper::wasSuccessful() const | bool AssetClearHelper::wasSuccessful() const | ||||
| { | { | ||||
| return stats.tot_cleared > 0; | return stats.tot_cleared > 0; | ||||
| } | } | ||||
| static int asset_clear_exec(bContext *C, wmOperator *op) | static int asset_clear_exec(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| PointerRNAVec ids = asset_operation_get_ids_from_context(C); | PointerRNAVec ids = asset_operation_get_ids_from_context(C); | ||||
| AssetClearHelper clear_helper; | AssetClearHelper clear_helper; | ||||
| clear_helper(ids); | clear_helper(ids); | ||||
| clear_helper.reportResults(*op->reports); | clear_helper.reportResults(C, *op->reports); | ||||
| if (!clear_helper.wasSuccessful()) { | if (!clear_helper.wasSuccessful()) { | ||||
| return OPERATOR_CANCELLED; | return OPERATOR_CANCELLED; | ||||
| } | } | ||||
| WM_main_add_notifier(NC_ID | NA_EDITED, nullptr); | WM_main_add_notifier(NC_ID | NA_EDITED, nullptr); | ||||
| WM_main_add_notifier(NC_ASSET | NA_REMOVED, nullptr); | WM_main_add_notifier(NC_ASSET | NA_REMOVED, nullptr); | ||||
| Show All 11 Lines | static void ASSET_OT_clear(wmOperatorType *ot) | ||||
| ot->exec = asset_clear_exec; | ot->exec = asset_clear_exec; | ||||
| ot->poll = asset_operation_poll; | ot->poll = asset_operation_poll; | ||||
| ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; | ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; | ||||
| } | } | ||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| static bool asset_list_refresh_poll(bContext *C) | |||||
| { | |||||
| const AssetLibraryReference *library = CTX_wm_asset_library(C); | |||||
| if (!library) { | |||||
| return false; | |||||
| } | |||||
| return ED_assetlist_storage_has_list_for_library(library); | |||||
| } | |||||
| static int asset_list_refresh_exec(bContext *C, wmOperator *UNUSED(unused)) | |||||
| { | |||||
| const AssetLibraryReference *library = CTX_wm_asset_library(C); | |||||
| ED_assetlist_clear(library, C); | |||||
| return OPERATOR_FINISHED; | |||||
| } | |||||
| static void ASSET_OT_list_refresh(struct wmOperatorType *ot) | |||||
| { | |||||
| /* identifiers */ | |||||
| ot->name = "Refresh Asset List"; | |||||
| ot->description = "Trigger a reread of the assets"; | |||||
| ot->idname = "ASSET_OT_list_refresh"; | |||||
| /* api callbacks */ | |||||
| ot->exec = asset_list_refresh_exec; | |||||
| ot->poll = asset_list_refresh_poll; | |||||
| } | |||||
| /* -------------------------------------------------------------------- */ | |||||
| void ED_operatortypes_asset(void) | void ED_operatortypes_asset(void) | ||||
| { | { | ||||
| WM_operatortype_append(ASSET_OT_mark); | WM_operatortype_append(ASSET_OT_mark); | ||||
| WM_operatortype_append(ASSET_OT_clear); | WM_operatortype_append(ASSET_OT_clear); | ||||
| WM_operatortype_append(ASSET_OT_list_refresh); | |||||
| } | } | ||||