Changeset View
Changeset View
Standalone View
Standalone View
source/blender/makesrna/intern/rna_asset.c
| Show All 34 Lines | |||||
| # include "BKE_idprop.h" | # include "BKE_idprop.h" | ||||
| # include "BLI_listbase.h" | # include "BLI_listbase.h" | ||||
| # include "ED_asset.h" | # include "ED_asset.h" | ||||
| # include "RNA_access.h" | # include "RNA_access.h" | ||||
| static AssetTag *rna_AssetMetaData_tag_new(AssetMetaData *asset_data, | static bool rna_AssetMetaData_editable_from_owner_id(const ID *owner_id, | ||||
| ReportList *reports, | const AssetMetaData *asset_data, | ||||
| const char *name, | const char **r_info) | ||||
| bool skip_if_exists) | { | ||||
| if (owner_id && asset_data && (owner_id->asset_data == asset_data)) { | |||||
sybren: I think it's better to reduce the cognitive complexity by inverting the condition and removing… | |||||
| return true; | |||||
| } | |||||
| if (r_info) { | |||||
| *r_info = | |||||
| "Asset metadata from external asset libraries can't be edited, only assets stored in the " | |||||
| "current file can"; | |||||
| } | |||||
| return false; | |||||
| } | |||||
| int rna_AssetMetaData_editable(PointerRNA *ptr, const char **r_info) | |||||
| { | |||||
| AssetMetaData *asset_data = ptr->data; | |||||
| return rna_AssetMetaData_editable_from_owner_id(ptr->owner_id, asset_data, r_info) ? | |||||
| PROP_EDITABLE : | |||||
| 0; | |||||
| } | |||||
| static int rna_AssetTag_editable(PointerRNA *ptr, const char **r_info) | |||||
| { | { | ||||
| AssetTag *asset_tag = ptr->data; | |||||
| ID *owner_id = ptr->owner_id; | |||||
| if (owner_id && owner_id->asset_data) { | |||||
| BLI_assert_msg(BLI_findindex(&owner_id->asset_data->tags, asset_tag) != -1, | |||||
| "The owner of the asset tag pointer is not the asset ID containing the tag"); | |||||
| } | |||||
| return rna_AssetMetaData_editable_from_owner_id(ptr->owner_id, owner_id->asset_data, r_info) ? | |||||
| PROP_EDITABLE : | |||||
| 0; | |||||
| } | |||||
| static AssetTag *rna_AssetMetaData_tag_new( | |||||
| ID *id, AssetMetaData *asset_data, ReportList *reports, const char *name, bool skip_if_exists) | |||||
| { | |||||
| const char *disabled_info = NULL; | |||||
| if (!rna_AssetMetaData_editable_from_owner_id(id, asset_data, &disabled_info)) { | |||||
| BKE_report(reports, RPT_WARNING, disabled_info); | |||||
| return NULL; | |||||
| } | |||||
| AssetTag *tag = NULL; | AssetTag *tag = NULL; | ||||
| if (skip_if_exists) { | if (skip_if_exists) { | ||||
| struct AssetTagEnsureResult result = BKE_asset_metadata_tag_ensure(asset_data, name); | struct AssetTagEnsureResult result = BKE_asset_metadata_tag_ensure(asset_data, name); | ||||
| if (!result.is_new) { | if (!result.is_new) { | ||||
| BKE_reportf( | BKE_reportf( | ||||
| reports, RPT_WARNING, "Tag '%s' already present for given asset", result.tag->name); | reports, RPT_WARNING, "Tag '%s' already present for given asset", result.tag->name); | ||||
| /* Report, but still return valid item. */ | /* Report, but still return valid item. */ | ||||
| } | } | ||||
| tag = result.tag; | tag = result.tag; | ||||
| } | } | ||||
| else { | else { | ||||
| tag = BKE_asset_metadata_tag_add(asset_data, name); | tag = BKE_asset_metadata_tag_add(asset_data, name); | ||||
| } | } | ||||
| return tag; | return tag; | ||||
| } | } | ||||
| static void rna_AssetMetaData_tag_remove(AssetMetaData *asset_data, | static void rna_AssetMetaData_tag_remove(ID *id, | ||||
| AssetMetaData *asset_data, | |||||
| ReportList *reports, | ReportList *reports, | ||||
| PointerRNA *tag_ptr) | PointerRNA *tag_ptr) | ||||
| { | { | ||||
| const char *disabled_info = NULL; | |||||
| if (!rna_AssetMetaData_editable_from_owner_id(id, asset_data, &disabled_info)) { | |||||
| BKE_report(reports, RPT_WARNING, disabled_info); | |||||
| return; | |||||
| } | |||||
| AssetTag *tag = tag_ptr->data; | AssetTag *tag = tag_ptr->data; | ||||
| if (BLI_findindex(&asset_data->tags, tag) == -1) { | if (BLI_findindex(&asset_data->tags, tag) == -1) { | ||||
| BKE_reportf(reports, RPT_ERROR, "Tag '%s' not found in given asset", tag->name); | BKE_reportf(reports, RPT_ERROR, "Tag '%s' not found in given asset", tag->name); | ||||
| return; | return; | ||||
| } | } | ||||
| BKE_asset_metadata_tag_remove(asset_data, tag); | BKE_asset_metadata_tag_remove(asset_data, tag); | ||||
| RNA_POINTER_INVALIDATE(tag_ptr); | RNA_POINTER_INVALIDATE(tag_ptr); | ||||
| ▲ Show 20 Lines • Show All 101 Lines • ▼ Show 20 Lines | |||||
| { | { | ||||
| StructRNA *srna; | StructRNA *srna; | ||||
| PropertyRNA *prop; | PropertyRNA *prop; | ||||
| srna = RNA_def_struct(brna, "AssetTag", NULL); | srna = RNA_def_struct(brna, "AssetTag", NULL); | ||||
| RNA_def_struct_ui_text(srna, "Asset Tag", "User defined tag (name token)"); | RNA_def_struct_ui_text(srna, "Asset Tag", "User defined tag (name token)"); | ||||
| prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE); | prop = RNA_def_property(srna, "name", PROP_STRING, PROP_NONE); | ||||
| RNA_def_property_editable_func(prop, "rna_AssetTag_editable"); | |||||
| RNA_def_property_string_maxlength(prop, MAX_NAME); | RNA_def_property_string_maxlength(prop, MAX_NAME); | ||||
| RNA_def_property_ui_text(prop, "Name", "The identifier that makes up this tag"); | RNA_def_property_ui_text(prop, "Name", "The identifier that makes up this tag"); | ||||
| RNA_def_struct_name_property(srna, prop); | RNA_def_struct_name_property(srna, prop); | ||||
| } | } | ||||
| static void rna_def_asset_tags_api(BlenderRNA *brna, PropertyRNA *cprop) | static void rna_def_asset_tags_api(BlenderRNA *brna, PropertyRNA *cprop) | ||||
| { | { | ||||
| StructRNA *srna; | StructRNA *srna; | ||||
| FunctionRNA *func; | FunctionRNA *func; | ||||
| PropertyRNA *parm; | PropertyRNA *parm; | ||||
| RNA_def_property_srna(cprop, "AssetTags"); | RNA_def_property_srna(cprop, "AssetTags"); | ||||
| srna = RNA_def_struct(brna, "AssetTags", NULL); | srna = RNA_def_struct(brna, "AssetTags", NULL); | ||||
| RNA_def_struct_sdna(srna, "AssetMetaData"); | RNA_def_struct_sdna(srna, "AssetMetaData"); | ||||
| RNA_def_struct_ui_text(srna, "Asset Tags", "Collection of custom asset tags"); | RNA_def_struct_ui_text(srna, "Asset Tags", "Collection of custom asset tags"); | ||||
| /* Tag collection */ | /* Tag collection */ | ||||
| func = RNA_def_function(srna, "new", "rna_AssetMetaData_tag_new"); | func = RNA_def_function(srna, "new", "rna_AssetMetaData_tag_new"); | ||||
| RNA_def_function_ui_description(func, "Add a new tag to this asset"); | RNA_def_function_ui_description(func, "Add a new tag to this asset"); | ||||
| RNA_def_function_flag(func, FUNC_USE_REPORTS); | RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_USE_REPORTS); | ||||
| parm = RNA_def_string(func, "name", NULL, MAX_NAME, "Name", ""); | parm = RNA_def_string(func, "name", NULL, MAX_NAME, "Name", ""); | ||||
| RNA_def_parameter_flags(parm, 0, PARM_REQUIRED); | RNA_def_parameter_flags(parm, 0, PARM_REQUIRED); | ||||
| parm = RNA_def_boolean(func, | parm = RNA_def_boolean(func, | ||||
| "skip_if_exists", | "skip_if_exists", | ||||
| false, | false, | ||||
| "Skip if Exists", | "Skip if Exists", | ||||
| "Do not add a new tag if one of the same type already exists"); | "Do not add a new tag if one of the same type already exists"); | ||||
| /* return type */ | /* return type */ | ||||
| parm = RNA_def_pointer(func, "tag", "AssetTag", "", "New tag"); | parm = RNA_def_pointer(func, "tag", "AssetTag", "", "New tag"); | ||||
| RNA_def_function_return(func, parm); | RNA_def_function_return(func, parm); | ||||
| func = RNA_def_function(srna, "remove", "rna_AssetMetaData_tag_remove"); | func = RNA_def_function(srna, "remove", "rna_AssetMetaData_tag_remove"); | ||||
| RNA_def_function_ui_description(func, "Remove an existing tag from this asset"); | RNA_def_function_ui_description(func, "Remove an existing tag from this asset"); | ||||
| RNA_def_function_flag(func, FUNC_USE_REPORTS); | RNA_def_function_flag(func, FUNC_USE_SELF_ID | FUNC_USE_REPORTS); | ||||
| /* tag to remove */ | /* tag to remove */ | ||||
| parm = RNA_def_pointer(func, "tag", "AssetTag", "", "Removed tag"); | parm = RNA_def_pointer(func, "tag", "AssetTag", "", "Removed tag"); | ||||
| RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR); | RNA_def_parameter_flags(parm, PROP_NEVER_NULL, PARM_REQUIRED | PARM_RNAPTR); | ||||
| RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0); | RNA_def_parameter_clear_flags(parm, PROP_THICK_WRAP, 0); | ||||
| } | } | ||||
| static void rna_def_asset_data(BlenderRNA *brna) | static void rna_def_asset_data(BlenderRNA *brna) | ||||
| { | { | ||||
| StructRNA *srna; | StructRNA *srna; | ||||
| PropertyRNA *prop; | PropertyRNA *prop; | ||||
| srna = RNA_def_struct(brna, "AssetMetaData", NULL); | srna = RNA_def_struct(brna, "AssetMetaData", NULL); | ||||
| RNA_def_struct_ui_text(srna, "Asset Data", "Additional data stored for an asset data-block"); | RNA_def_struct_ui_text(srna, "Asset Data", "Additional data stored for an asset data-block"); | ||||
| // RNA_def_struct_ui_icon(srna, ICON_ASSET); /* TODO: Icon doesn't exist! */ | // RNA_def_struct_ui_icon(srna, ICON_ASSET); /* TODO: Icon doesn't exist! */ | ||||
| /* The struct has custom properties, but no pointer properties to other IDs! */ | /* The struct has custom properties, but no pointer properties to other IDs! */ | ||||
| RNA_def_struct_idprops_func(srna, "rna_AssetMetaData_idprops"); | RNA_def_struct_idprops_func(srna, "rna_AssetMetaData_idprops"); | ||||
| RNA_def_struct_flag(srna, STRUCT_NO_DATABLOCK_IDPROPERTIES); /* Mandatory! */ | RNA_def_struct_flag(srna, STRUCT_NO_DATABLOCK_IDPROPERTIES); /* Mandatory! */ | ||||
| prop = RNA_def_property(srna, "description", PROP_STRING, PROP_NONE); | prop = RNA_def_property(srna, "description", PROP_STRING, PROP_NONE); | ||||
| RNA_def_property_editable_func(prop, "rna_AssetMetaData_editable"); | |||||
| RNA_def_property_string_funcs(prop, | RNA_def_property_string_funcs(prop, | ||||
| "rna_AssetMetaData_description_get", | "rna_AssetMetaData_description_get", | ||||
| "rna_AssetMetaData_description_length", | "rna_AssetMetaData_description_length", | ||||
| "rna_AssetMetaData_description_set"); | "rna_AssetMetaData_description_set"); | ||||
| RNA_def_property_ui_text( | RNA_def_property_ui_text( | ||||
| prop, "Description", "A description of the asset to be displayed for the user"); | prop, "Description", "A description of the asset to be displayed for the user"); | ||||
| prop = RNA_def_property(srna, "tags", PROP_COLLECTION, PROP_NONE); | prop = RNA_def_property(srna, "tags", PROP_COLLECTION, PROP_NONE); | ||||
| RNA_def_property_struct_type(prop, "AssetTag"); | RNA_def_property_struct_type(prop, "AssetTag"); | ||||
| RNA_def_property_flag(prop, PROP_EDITABLE); | RNA_def_property_editable_func(prop, "rna_AssetMetaData_editable"); | ||||
| RNA_def_property_ui_text(prop, | RNA_def_property_ui_text(prop, | ||||
| "Tags", | "Tags", | ||||
| "Custom tags (name tokens) for the asset, used for filtering and " | "Custom tags (name tokens) for the asset, used for filtering and " | ||||
| "general asset management"); | "general asset management"); | ||||
| rna_def_asset_tags_api(brna, prop); | rna_def_asset_tags_api(brna, prop); | ||||
| prop = RNA_def_property(srna, "active_tag", PROP_INT, PROP_NONE); | prop = RNA_def_property(srna, "active_tag", PROP_INT, PROP_NONE); | ||||
| RNA_def_property_int_funcs(prop, NULL, NULL, "rna_AssetMetaData_active_tag_range"); | RNA_def_property_int_funcs(prop, NULL, NULL, "rna_AssetMetaData_active_tag_range"); | ||||
| ▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines | |||||
I think it's better to reduce the cognitive complexity by inverting the condition and removing the nesting: