Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_geometry_attribute_search.cc « space_node « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e92449e3535457d019d5ee34b8397ef4eb1516c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "BLI_index_range.hh"
#include "BLI_listbase.h"
#include "BLI_map.hh"
#include "BLI_set.hh"
#include "BLI_string_ref.hh"
#include "BLI_string_search.h"

#include "DNA_modifier_types.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
#include "DNA_space_types.h"

#include "BKE_context.h"
#include "BKE_node_ui_storage.hh"
#include "BKE_object.h"

#include "RNA_access.h"
#include "RNA_enum_types.h"

#include "ED_undo.h"

#include "BLT_translation.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "node_intern.h"

using blender::IndexRange;
using blender::Map;
using blender::Set;
using blender::StringRef;

struct AttributeSearchData {
  AvailableAttributeInfo &dummy_info_for_search;
  const NodeUIStorage &ui_storage;
  bNodeSocket &socket;
};

/* This class must not have a destructor, since it is used by buttons and freed with #MEM_freeN. */
BLI_STATIC_ASSERT(std::is_trivially_destructible_v<AttributeSearchData>, "");

static StringRef attribute_data_type_string(const CustomDataType type)
{
  const char *name = nullptr;
  RNA_enum_name_from_value(rna_enum_attribute_type_items, type, &name);
  return StringRef(IFACE_(name));
}

static StringRef attribute_domain_string(const AttributeDomain domain)
{
  const char *name = nullptr;
  RNA_enum_name_from_value(rna_enum_attribute_domain_items, domain, &name);
  return StringRef(IFACE_(name));
}

/* Unicode arrow. */
#define MENU_SEP "\xe2\x96\xb6"

static bool attribute_search_item_add(uiSearchItems *items, const AvailableAttributeInfo &item)
{
  const StringRef data_type_name = attribute_data_type_string(item.data_type);
  const StringRef domain_name = attribute_domain_string(item.domain);
  std::string search_item_text = domain_name + " " + MENU_SEP + item.name + UI_SEP_CHAR +
                                 data_type_name;

  return UI_search_item_add(
      items, search_item_text.c_str(), (void *)&item, ICON_NONE, UI_BUT_HAS_SEP_CHAR, 0);
}

static void attribute_search_update_fn(const bContext *UNUSED(C),
                                       void *arg,
                                       const char *str,
                                       uiSearchItems *items,
                                       const bool is_first)
{
  AttributeSearchData *data = static_cast<AttributeSearchData *>(arg);

  const Set<AvailableAttributeInfo> &attribute_hints = data->ui_storage.attribute_hints;

  /* Any string may be valid, so add the current search string along with the hints. */
  if (str[0] != '\0') {
    /* Note that the attribute domain and data type are dummies, since
     * #AvailableAttributeInfo equality is only based on the string. */
    if (!attribute_hints.contains(AvailableAttributeInfo{str, ATTR_DOMAIN_AUTO, CD_PROP_BOOL})) {
      data->dummy_info_for_search.name = std::string(str);
      UI_search_item_add(items, str, &data->dummy_info_for_search, ICON_ADD, 0, 0);
    }
  }

  if (str[0] == '\0' && !is_first) {
    /* Allow clearing the text field when the string is empty, but not on the first pass,
     * or opening an attribute field for the first time would show this search item. */
    data->dummy_info_for_search.name = std::string(str);
    UI_search_item_add(items, str, &data->dummy_info_for_search, ICON_X, 0, 0);
  }

  /* Don't filter when the menu is first opened, but still run the search
   * so the items are in the same order they will appear in while searching. */
  const char *string = is_first ? "" : str;

  StringSearch *search = BLI_string_search_new();
  for (const AvailableAttributeInfo &item : attribute_hints) {
    BLI_string_search_add(search, item.name.c_str(), (void *)&item);
  }

  AvailableAttributeInfo **filtered_items;
  const int filtered_amount = BLI_string_search_query(search, string, (void ***)&filtered_items);

  for (const int i : IndexRange(filtered_amount)) {
    const AvailableAttributeInfo *item = filtered_items[i];
    if (!attribute_search_item_add(items, *item)) {
      break;
    }
  }

  MEM_freeN(filtered_items);
  BLI_string_search_free(search);
}

static void attribute_search_exec_fn(bContext *C, void *data_v, void *item_v)
{
  AttributeSearchData *data = static_cast<AttributeSearchData *>(data_v);
  AvailableAttributeInfo *item = static_cast<AvailableAttributeInfo *>(item_v);

  bNodeSocket &socket = data->socket;
  switch (socket.type) {
    case SOCK_STRING: {
      bNodeSocketValueString *value = static_cast<bNodeSocketValueString *>(socket.default_value);
      BLI_strncpy(value->value, item->name.c_str(), MAX_NAME);
      break;
    }
    case SOCK_ATTRIBUTE: {
      bNodeSocketValueAttribute *value = static_cast<bNodeSocketValueAttribute *>(
          socket.default_value);
      BLI_strncpy(value->attribute_name, item->name.c_str(), MAX_NAME);
      break;
    }
  }

  ED_undo_push(C, "Assign Attribute Name");
}

void node_geometry_add_attribute_search_button(const bContext *C,
                                               const bNodeTree *node_tree,
                                               const bNode *node,
                                               PointerRNA *socket_ptr,
                                               const char *propname,
                                               uiLayout *layout)
{
  const NodeUIStorage *ui_storage = BKE_node_tree_ui_storage_get_from_context(
      C, *node_tree, *node);

  if (ui_storage == nullptr) {
    uiItemR(layout, socket_ptr, propname, 0, "", 0);
    return;
  }

  const NodeTreeUIStorage *tree_ui_storage = node_tree->ui_storage;

  uiBlock *block = uiLayoutGetBlock(layout);
  uiBut *but = uiDefIconTextButR(block,
                                 UI_BTYPE_SEARCH_MENU,
                                 0,
                                 ICON_NONE,
                                 "",
                                 0,
                                 0,
                                 10 * UI_UNIT_X, /* Dummy value, replaced by layout system. */
                                 UI_UNIT_Y,
                                 socket_ptr,
                                 propname,
                                 0,
                                 0.0f,
                                 0.0f,
                                 0.0f,
                                 0.0f,
                                 "");

  AttributeSearchData *data = OBJECT_GUARDED_NEW(AttributeSearchData,
                                                 {tree_ui_storage->dummy_info_for_search,
                                                  *ui_storage,
                                                  *static_cast<bNodeSocket *>(socket_ptr->data)});

  UI_but_func_search_set_results_are_suggestions(but, true);
  UI_but_func_search_set_sep_string(but, MENU_SEP);
  UI_but_func_search_set(but,
                         nullptr,
                         attribute_search_update_fn,
                         static_cast<void *>(data),
                         true,
                         nullptr,
                         attribute_search_exec_fn,
                         nullptr);
}