Blender  V3.3
interface_template_attribute_search.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "BLI_string_ref.hh"
8 #include "BLI_string_search.h"
9 
10 #include "DNA_customdata_types.h"
11 
12 #include "RNA_access.h"
13 #include "RNA_enum_types.h"
14 
15 #include "BLT_translation.h"
16 
18 
19 #include "UI_interface.h"
20 #include "UI_interface.hh"
21 #include "UI_resources.h"
22 
24 
25 namespace blender::ui {
26 
28 {
29  const char *name = nullptr;
31  return StringRef(IFACE_(name));
32 }
33 
35 {
36  const char *name = nullptr;
38  return StringRef(IFACE_(name));
39 }
40 
42 {
43  const StringRef data_type_name = attribute_data_type_string(*item.data_type);
44  const StringRef domain_name = attribute_domain_string(*item.domain);
45  std::string search_item_text = domain_name + " " + UI_MENU_ARROW_SEP + item.name + UI_SEP_CHAR +
46  data_type_name;
47 
48  return UI_search_item_add(
49  items, search_item_text.c_str(), (void *)&item, ICON_NONE, UI_BUT_HAS_SEP_CHAR, 0);
50 }
51 
53  const bool can_create_attribute,
55  uiSearchItems *seach_items,
56  const bool is_first)
57 {
58  static GeometryAttributeInfo dummy_info;
59 
60  /* Any string may be valid, so add the current search string along with the hints. */
61  if (str[0] != '\0') {
62  bool contained = false;
63  for (const GeometryAttributeInfo *attribute_info : infos) {
64  if (attribute_info->name == str) {
65  contained = true;
66  break;
67  }
68  }
69  if (!contained) {
70  dummy_info.name = str;
71  UI_search_item_add(seach_items,
72  str.c_str(),
73  &dummy_info,
74  can_create_attribute ? ICON_ADD : ICON_NONE,
75  0,
76  0);
77  }
78  }
79 
80  if (str[0] == '\0' && !is_first) {
81  /* Allow clearing the text field when the string is empty, but not on the first pass,
82  * or opening an attribute field for the first time would show this search item. */
83  dummy_info.name = str;
84  UI_search_item_add(seach_items, str.c_str(), &dummy_info, ICON_X, 0, 0);
85  }
86 
87  /* Don't filter when the menu is first opened, but still run the search
88  * so the items are in the same order they will appear in while searching. */
89  const char *string = is_first ? "" : str.c_str();
90 
92  for (const GeometryAttributeInfo *item : infos) {
93 
94  /* Don't show the legacy "normal" attribute. */
95  if (item->name == "normal" && item->domain == ATTR_DOMAIN_FACE) {
96  continue;
97  }
98  if (!bke::allow_procedural_attribute_access(item->name)) {
99  continue;
100  }
101 
102  BLI_string_search_add(search, item->name.c_str(), (void *)item, 0);
103  }
104 
105  GeometryAttributeInfo **filtered_items;
106  const int filtered_amount = BLI_string_search_query(search, string, (void ***)&filtered_items);
107 
108  for (const int i : IndexRange(filtered_amount)) {
109  const GeometryAttributeInfo *item = filtered_items[i];
110  if (!attribute_search_item_add(seach_items, *item)) {
111  break;
112  }
113  }
114 
115  MEM_freeN(filtered_items);
116  BLI_string_search_free(search);
117 }
118 
119 } // namespace blender::ui
eAttrDomain
Definition: BKE_attribute.h:25
@ ATTR_DOMAIN_FACE
Definition: BKE_attribute.h:29
void BLI_string_search_add(StringSearch *search, const char *str, void *user_data, int weight)
void BLI_string_search_free(StringSearch *search)
StringSearch * BLI_string_search_new(void)
int BLI_string_search_query(StringSearch *search, const char *query, void ***r_data)
#define IFACE_(msgid)
eCustomDataType
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
@ UI_BUT_HAS_SEP_CHAR
Definition: UI_interface.h:222
#define UI_SEP_CHAR
Definition: UI_interface.h:83
bool UI_search_item_add(uiSearchItems *items, const char *name, void *poin, int iconid, int but_flag, uint8_t name_prefix_offset)
#define str(s)
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
bool allow_procedural_attribute_access(StringRef attribute_name)
static bool attribute_search_item_add(uiSearchItems *items, const GeometryAttributeInfo &item)
void attribute_search_add_items(StringRefNull str, bool can_create_attribute, Span< const nodes::geometry_nodes_eval_log::GeometryAttributeInfo * > infos, uiSearchItems *items, bool is_first)
static StringRef attribute_domain_string(const eAttrDomain domain)
static StringRef attribute_data_type_string(const eCustomDataType type)
bool RNA_enum_name_from_value(const EnumPropertyItem *item, int value, const char **r_name)
Definition: rna_access.c:5106
const EnumPropertyItem rna_enum_attribute_domain_items[]
Definition: rna_attribute.c:70
const EnumPropertyItem rna_enum_attribute_type_items[]
Definition: rna_attribute.c:26
#define UI_MENU_ARROW_SEP