Geometry Nodes: show correct type in menu socket tooltips

Previously, menu sockets were sometimes shown as integers or strings
in socket tooltips. Now, they are always shown as "Menu" type. This also
changes how these values are logged. Previously, they were logged as
strings. Now, only the integer identifier is logged and the name is looked
up when drawing the tooltip.

Pull Request: https://projects.blender.org/blender/blender/pulls/121236
This commit is contained in:
Jacques Lucke
2024-05-05 09:30:02 +02:00
parent a7cae51cf7
commit 18600e430c
4 changed files with 41 additions and 25 deletions

View File

@@ -38,6 +38,8 @@ struct RuntimeNodeEnumItem {
struct RuntimeNodeEnumItems : ImplicitSharingMixin {
Vector<RuntimeNodeEnumItem> items;
const RuntimeNodeEnumItem *find_item_by_identifier(int identifier) const;
void delete_self() override
{
delete this;

View File

@@ -26,3 +26,18 @@ blender::MutableSpan<NodeEnumItem> NodeEnumDefinition::items()
{
return {this->items_array, this->items_num};
}
namespace blender::bke {
const RuntimeNodeEnumItem *RuntimeNodeEnumItems::find_item_by_identifier(
const int identifier) const
{
for (const RuntimeNodeEnumItem &item : this->items) {
if (item.identifier == identifier) {
return &item;
}
}
return nullptr;
}
} // namespace blender::bke

View File

@@ -42,6 +42,7 @@
#include "BKE_lib_id.hh"
#include "BKE_main.hh"
#include "BKE_node.hh"
#include "BKE_node_enum.hh"
#include "BKE_node_runtime.hh"
#include "BKE_node_tree_update.hh"
#include "BKE_node_tree_zones.hh"
@@ -1335,6 +1336,28 @@ static void create_inspection_string_for_generic_value(const bNodeSocket &socket
}
const CPPType &socket_type = *socket.typeinfo->base_cpp_type;
if (socket.type == SOCK_MENU) {
if (!value_type.is<int>()) {
return;
}
const int item_identifier = *static_cast<const int *>(buffer);
const auto *socket_storage = socket.default_value_typed<bNodeSocketValueMenu>();
if (!socket_storage->enum_items) {
return;
}
if (socket_storage->has_conflict()) {
return;
}
const bke::RuntimeNodeEnumItem *enum_item =
socket_storage->enum_items->find_item_by_identifier(item_identifier);
if (!enum_item) {
return;
}
ss << fmt::format(TIP_("{} (Menu)"), enum_item->name);
return;
}
const bke::DataTypeConversions &convert = bke::get_implicit_type_conversions();
if (value_type != socket_type) {
if (!convert.is_convertible(value_type, socket_type)) {

View File

@@ -7,7 +7,6 @@
#include "BKE_compute_contexts.hh"
#include "BKE_curves.hh"
#include "BKE_node_enum.hh"
#include "BKE_node_runtime.hh"
#include "BKE_node_socket_value.hh"
@@ -178,16 +177,6 @@ void GeoTreeLogger::log_value(const bNode &node, const bNodeSocket &socket, cons
store_logged_value(this->allocator->construct<GenericValueLog>(GMutablePointer{type, buffer}));
};
auto log_menu_value = [&](Span<bke::RuntimeNodeEnumItem> enum_items, const int identifier) {
for (const bke::RuntimeNodeEnumItem &item : enum_items) {
if (item.identifier == identifier) {
log_generic_value(CPPType::get<std::string>(), &item.name);
return;
}
}
log_generic_value(CPPType::get<int>(), &identifier);
};
if (type.is<bke::GeometrySet>()) {
const bke::GeometrySet &geometry = *value.get<bke::GeometrySet>();
store_logged_value(this->allocator->construct<GeometryInfoLog>(geometry));
@@ -201,20 +190,7 @@ void GeoTreeLogger::log_value(const bNode &node, const bNodeSocket &socket, cons
else {
value_variant.convert_to_single();
const GPointer value = value_variant.get_single_ptr();
if (socket.type == SOCK_MENU) {
const bNodeSocketValueMenu &default_value =
*socket.default_value_typed<bNodeSocketValueMenu>();
if (default_value.enum_items) {
const int identifier = *value.get<int>();
log_menu_value(default_value.enum_items->items, identifier);
}
else {
log_generic_value(*value.type(), value.get());
}
}
else {
log_generic_value(*value.type(), value.get());
}
log_generic_value(*value.type(), value.get());
}
}
else {