UI/Unique ID name: add library ID name if present.
Also reshuffle a bit that whole code, did some renaming, `BKE_id_to_unique_string_key()` is now using same base code (instead of using whole library filepath...), etc.
This commit is contained in:
@@ -198,8 +198,10 @@ void BKE_main_id_clear_newpoins(struct Main *bmain);
|
||||
|
||||
void BKE_main_lib_objects_recalc_all(struct Main *bmain);
|
||||
|
||||
/* (MAX_ID_NAME - 2) + 3 */
|
||||
void BKE_id_ui_prefix(char name[66 + 1], const struct ID *id);
|
||||
#define MAX_ID_FULL_NAME (64 + 64 + 3 + 1) /* 64 is MAX_ID_NAME - 2 */
|
||||
#define MAX_ID_FULL_NAME_UI (MAX_ID_FULL_NAME + 3) /* Adds 'keycode' two letters at begining. */
|
||||
void BKE_id_full_name_get(char name[MAX_ID_FULL_NAME], const struct ID *id);
|
||||
void BKE_id_full_name_ui_prefix_get(char name[MAX_ID_FULL_NAME_UI], const struct ID *id);
|
||||
|
||||
char *BKE_id_to_unique_string_key(const struct ID *id);
|
||||
|
||||
|
||||
@@ -2526,35 +2526,58 @@ void BKE_libblock_rename(Main *bmain, ID *id, const char *name)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns in name the name of the block, with a 3-character prefix prepended
|
||||
* indicating whether it comes from a library, has a fake user, or no users.
|
||||
* Generate full name of the data-block (without ID code, but with library is any)
|
||||
*
|
||||
* \note Result is unique to a given ID type in a given Main database.
|
||||
*
|
||||
* \param name An allocated string of minimal length MAX_ID_FULL_NAME, will be filled with generated string.
|
||||
*/
|
||||
void BKE_id_ui_prefix(char name[MAX_ID_NAME + 1], const ID *id)
|
||||
void BKE_id_full_name_get(char name[MAX_ID_FULL_NAME], const ID *id)
|
||||
{
|
||||
strcpy(name, id->name + 2);
|
||||
|
||||
if (id->lib != NULL) {
|
||||
const size_t idname_len = strlen(id->name + 2);
|
||||
const size_t libname_len = strlen(id->lib->id.name + 2);
|
||||
|
||||
name[idname_len] = ' ';
|
||||
name[idname_len + 1] = '[';
|
||||
strcpy(name + idname_len + 2, id->lib->id.name + 2);
|
||||
name[idname_len + 2 + libname_len] = ']';
|
||||
name[idname_len + 2 + libname_len + 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate full name of the data-block (without ID code, but with library is any), with a 3-character prefix prepended
|
||||
* indicating whether it comes from a library, is overriding, has a fake or no user, etc.
|
||||
*
|
||||
* \note Result is unique to a given ID type in a given Main database.
|
||||
*
|
||||
* \param name An allocated string of minimal length MAX_ID_FULL_NAME_UI, will be filled with generated string.
|
||||
*/
|
||||
void BKE_id_full_name_ui_prefix_get(char name[MAX_ID_FULL_NAME_UI], const ID *id)
|
||||
{
|
||||
name[0] = id->lib ? (ID_MISSING(id) ? 'M' : 'L') : ID_IS_STATIC_OVERRIDE(id) ? 'O' : ' ';
|
||||
name[1] = (id->flag & LIB_FAKEUSER) ? 'F' : ((id->us == 0) ? '0' : ' ');
|
||||
name[2] = ' ';
|
||||
|
||||
strcpy(name + 3, id->name + 2);
|
||||
BKE_id_full_name_get(name + 3, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an allocated string concatenating ID name (including two-chars type code) and its lib name if any,
|
||||
* which is expected to be unique in a given Main database..
|
||||
* Generate a concatenation of ID name (including two-chars type code) and its lib name, if any.
|
||||
*
|
||||
* \return A unique allocated string key for any ID in the whole Main database.
|
||||
*/
|
||||
char *BKE_id_to_unique_string_key(const struct ID *id)
|
||||
{
|
||||
const size_t key_len_base = strlen(id->name) + 1;
|
||||
const size_t key_len_ext = ((id->lib != NULL) ? strlen(id->lib->name) : 0) + 1;
|
||||
const size_t key_len = key_len_base + key_len_ext - 1;
|
||||
char *key = MEM_mallocN(key_len, __func__);
|
||||
char name[MAX_ID_FULL_NAME + 2];
|
||||
name[0] = id->name[0];
|
||||
name[1] = id->name[1];
|
||||
BKE_id_full_name_get(name + 2, id);
|
||||
|
||||
BLI_strncpy(key, id->name, key_len_base);
|
||||
if (id->lib != NULL) {
|
||||
BLI_strncpy(key + key_len_base - 1, id->lib->name, key_len_ext);
|
||||
}
|
||||
|
||||
return key;
|
||||
return BLI_strdup(name);
|
||||
}
|
||||
|
||||
void BKE_library_filepath_set(Main *bmain, Library *lib, const char *filepath)
|
||||
|
||||
@@ -300,8 +300,8 @@ static bool id_search_add(
|
||||
/* +1 is needed because BKE_id_ui_prefix uses 3 letter prefix
|
||||
* followed by ID_NAME-2 characters from id->name.
|
||||
*/
|
||||
char name_ui[MAX_ID_NAME + 1];
|
||||
BKE_id_ui_prefix(name_ui, id);
|
||||
char name_ui[MAX_ID_FULL_NAME];
|
||||
BKE_id_full_name_ui_prefix_get(name_ui, id);
|
||||
|
||||
int iconid = ui_id_icon_get(C, id, template_ui->preview);
|
||||
|
||||
|
||||
@@ -273,8 +273,8 @@ void ui_rna_collection_search_cb(const struct bContext *C, void *arg, const char
|
||||
|
||||
iconid = 0;
|
||||
if (itemptr.type && RNA_struct_is_ID(itemptr.type)) {
|
||||
name = MEM_malloc_arrayN(MAX_ID_NAME + 1, sizeof(*name), __func__);
|
||||
BKE_id_ui_prefix(name, itemptr.data);
|
||||
name = MEM_malloc_arrayN(MAX_ID_FULL_NAME, sizeof(*name), __func__);
|
||||
BKE_id_full_name_ui_prefix_get(name, itemptr.data);
|
||||
iconid = ui_id_icon_get(C, itemptr.data, false);
|
||||
}
|
||||
else {
|
||||
|
||||
@@ -310,15 +310,14 @@ static int rna_ID_lookup_string(ListBase *lb, const char *key, PointerRNA *r_ptr
|
||||
{
|
||||
ID *id;
|
||||
for (id = lb->first; id != NULL; id = id->next) {
|
||||
printf("%s vs %s\n", id->name, key);
|
||||
if (STREQ(id->name + 2, key)) {
|
||||
break;
|
||||
}
|
||||
else if (strstr(key, id->name + 2) != NULL) {
|
||||
char uiname[MAX_ID_NAME * 3];
|
||||
BKE_id_ui_prefix(uiname, id);
|
||||
printf("second chance: %s vs %s\n", uiname, key);
|
||||
if (STREQ(uiname, key)) {
|
||||
char full_name_ui[MAX_ID_FULL_NAME_UI];
|
||||
BKE_id_full_name_ui_prefix_get(full_name_ui, id);
|
||||
/* Second check skips the three 'UI keycode letters' prefix. */
|
||||
if (STREQ(full_name_ui, key) || STREQ(full_name_ui + 3, key)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user