From a17ea1a6691920cc307f3e44dd285b2d8e8bdd35 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Mon, 26 Apr 2021 09:09:50 +0200 Subject: Spreadsheet: combine vector/color spreadsheet columns Differential Revision: https://developer.blender.org/D11056 --- .../editors/space_spreadsheet/space_spreadsheet.cc | 7 +- .../space_spreadsheet/spreadsheet_cell_value.hh | 7 ++ .../space_spreadsheet/spreadsheet_column.cc | 1 - .../space_spreadsheet/spreadsheet_column.hh | 4 +- .../space_spreadsheet/spreadsheet_column_values.hh | 14 ++- .../spreadsheet_data_source_geometry.cc | 116 +++++++-------------- .../space_spreadsheet/spreadsheet_layout.cc | 42 ++++++++ source/blender/makesdna/DNA_space_types.h | 2 - 8 files changed, 104 insertions(+), 89 deletions(-) diff --git a/source/blender/editors/space_spreadsheet/space_spreadsheet.cc b/source/blender/editors/space_spreadsheet/space_spreadsheet.cc index 190f50ed443..fc9606b1249 100644 --- a/source/blender/editors/space_spreadsheet/space_spreadsheet.cc +++ b/source/blender/editors/space_spreadsheet/space_spreadsheet.cc @@ -304,7 +304,12 @@ static void update_visible_columns(ListBase &columns, DataSource &data_source) continue; } - used_ids.add(*column->id); + if (!used_ids.add(*column->id)) { + /* Remove duplicate columns for now. */ + BLI_remlink(&columns, column); + spreadsheet_column_free(column); + continue; + } } data_source.foreach_default_column_ids([&](const SpreadsheetColumnID &column_id) { diff --git a/source/blender/editors/space_spreadsheet/spreadsheet_cell_value.hh b/source/blender/editors/space_spreadsheet/spreadsheet_cell_value.hh index 0627cff7abc..d1e80f1d87e 100644 --- a/source/blender/editors/space_spreadsheet/spreadsheet_cell_value.hh +++ b/source/blender/editors/space_spreadsheet/spreadsheet_cell_value.hh @@ -18,6 +18,10 @@ #include +#include "BLI_color.hh" +#include "BLI_float2.hh" +#include "BLI_float3.hh" + struct Object; struct Collection; @@ -44,6 +48,9 @@ class CellValue { std::optional value_int; std::optional value_float; std::optional value_bool; + std::optional value_float2; + std::optional value_float3; + std::optional value_color; std::optional value_object; std::optional value_collection; }; diff --git a/source/blender/editors/space_spreadsheet/spreadsheet_column.cc b/source/blender/editors/space_spreadsheet/spreadsheet_column.cc index 6c573ce7238..de40545fdae 100644 --- a/source/blender/editors/space_spreadsheet/spreadsheet_column.cc +++ b/source/blender/editors/space_spreadsheet/spreadsheet_column.cc @@ -37,7 +37,6 @@ SpreadsheetColumnID *spreadsheet_column_id_copy(const SpreadsheetColumnID *src_c { SpreadsheetColumnID *new_column_id = spreadsheet_column_id_new(); new_column_id->name = BLI_strdup(src_column_id->name); - new_column_id->index = src_column_id->index; return new_column_id; } diff --git a/source/blender/editors/space_spreadsheet/spreadsheet_column.hh b/source/blender/editors/space_spreadsheet/spreadsheet_column.hh index 0f74ee0141a..bb245851d55 100644 --- a/source/blender/editors/space_spreadsheet/spreadsheet_column.hh +++ b/source/blender/editors/space_spreadsheet/spreadsheet_column.hh @@ -24,7 +24,7 @@ namespace blender { template<> struct DefaultHash { uint64_t operator()(const SpreadsheetColumnID &column_id) const { - return get_default_hash_2(StringRef(column_id.name), column_id.index); + return get_default_hash(StringRef(column_id.name)); } }; } // namespace blender @@ -32,7 +32,7 @@ template<> struct DefaultHash { inline bool operator==(const SpreadsheetColumnID &a, const SpreadsheetColumnID &b) { using blender::StringRef; - return StringRef(a.name) == StringRef(b.name) && a.index == b.index; + return StringRef(a.name) == StringRef(b.name); } namespace blender::ed::spreadsheet { diff --git a/source/blender/editors/space_spreadsheet/spreadsheet_column_values.hh b/source/blender/editors/space_spreadsheet/spreadsheet_column_values.hh index 58a2776e0fc..373c988a41c 100644 --- a/source/blender/editors/space_spreadsheet/spreadsheet_column_values.hh +++ b/source/blender/editors/space_spreadsheet/spreadsheet_column_values.hh @@ -74,11 +74,19 @@ template class LambdaColumnValues : public ColumnValues { /* Utility function that simplifies creating a spreadsheet column from a lambda function. */ template std::unique_ptr column_values_from_function(std::string name, - int size, - GetValueF get_value) + const int size, + GetValueF get_value, + const float default_width = 0.0f) { - return std::make_unique>( + std::unique_ptr column_values = std::make_unique>( std::move(name), size, std::move(get_value)); + column_values->default_width = default_width; + return column_values; } +static constexpr float default_float_column_width = 3; +static constexpr float default_float2_column_width = 2 * default_float_column_width; +static constexpr float default_float3_column_width = 3 * default_float_column_width; +static constexpr float default_color_column_width = 4 * default_float_column_width; + } // namespace blender::ed::spreadsheet diff --git a/source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc b/source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc index 520d29ce306..018431225e5 100644 --- a/source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc +++ b/source/blender/editors/space_spreadsheet/spreadsheet_data_source_geometry.cc @@ -47,28 +47,7 @@ void GeometryDataSource::foreach_default_column_ids( } SpreadsheetColumnID column_id; column_id.name = (char *)name.c_str(); - if (meta_data.data_type == CD_PROP_FLOAT3) { - for (const int i : {0, 1, 2}) { - column_id.index = i; - fn(column_id); - } - } - else if (meta_data.data_type == CD_PROP_FLOAT2) { - for (const int i : {0, 1}) { - column_id.index = i; - fn(column_id); - } - } - else if (meta_data.data_type == CD_PROP_COLOR) { - for (const int i : {0, 1, 2, 3}) { - column_id.index = i; - fn(column_id); - } - } - else { - column_id.index = -1; - fn(column_id); - } + fn(column_id); return true; }); } @@ -89,9 +68,6 @@ std::unique_ptr GeometryDataSource::get_column_values( int domain_size = attribute->size(); switch (attribute->custom_data_type()) { case CD_PROP_FLOAT: - if (column_id.index != -1) { - return {}; - } return column_values_from_function( column_id.name, domain_size, [attribute](int index, CellValue &r_cell_value) { float value; @@ -99,9 +75,6 @@ std::unique_ptr GeometryDataSource::get_column_values( r_cell_value.value_float = value; }); case CD_PROP_INT32: - if (column_id.index != -1) { - return {}; - } return column_values_from_function( column_id.name, domain_size, [attribute](int index, CellValue &r_cell_value) { int value; @@ -109,9 +82,6 @@ std::unique_ptr GeometryDataSource::get_column_values( r_cell_value.value_int = value; }); case CD_PROP_BOOL: - if (column_id.index != -1) { - return {}; - } return column_values_from_function( column_id.name, domain_size, [attribute](int index, CellValue &r_cell_value) { bool value; @@ -119,49 +89,37 @@ std::unique_ptr GeometryDataSource::get_column_values( r_cell_value.value_bool = value; }); case CD_PROP_FLOAT2: { - if (column_id.index < 0 || column_id.index > 1) { - return {}; - } - const std::array suffixes = {" X", " Y"}; - const std::string name = StringRef(column_id.name) + suffixes[column_id.index]; return column_values_from_function( - name, + column_id.name, domain_size, - [attribute, axis = column_id.index](int index, CellValue &r_cell_value) { + [attribute](int index, CellValue &r_cell_value) { float2 value; attribute->get(index, &value); - r_cell_value.value_float = value[axis]; - }); + r_cell_value.value_float2 = value; + }, + default_float2_column_width); } case CD_PROP_FLOAT3: { - if (column_id.index < 0 || column_id.index > 2) { - return {}; - } - const std::array suffixes = {" X", " Y", " Z"}; - const std::string name = StringRef(column_id.name) + suffixes[column_id.index]; return column_values_from_function( - name, + column_id.name, domain_size, - [attribute, axis = column_id.index](int index, CellValue &r_cell_value) { + [attribute](int index, CellValue &r_cell_value) { float3 value; attribute->get(index, &value); - r_cell_value.value_float = value[axis]; - }); + r_cell_value.value_float3 = value; + }, + default_float3_column_width); } case CD_PROP_COLOR: { - if (column_id.index < 0 || column_id.index > 3) { - return {}; - } - const std::array suffixes = {" R", " G", " B", " A"}; - const std::string name = StringRef(column_id.name) + suffixes[column_id.index]; return column_values_from_function( - name, + column_id.name, domain_size, - [attribute, axis = column_id.index](int index, CellValue &r_cell_value) { + [attribute](int index, CellValue &r_cell_value) { Color4f value; attribute->get(index, &value); - r_cell_value.value_float = value[axis]; - }); + r_cell_value.value_color = value; + }, + default_color_column_width); } default: break; @@ -300,15 +258,11 @@ void InstancesDataSource::foreach_default_column_ids( } SpreadsheetColumnID column_id; - column_id.index = -1; column_id.name = (char *)"Name"; fn(column_id); for (const char *name : {"Position", "Rotation", "Scale"}) { - for (const int i : {0, 1, 2}) { - column_id.name = (char *)name; - column_id.index = i; - fn(column_id); - } + column_id.name = (char *)name; + fn(column_id); } } @@ -319,7 +273,6 @@ std::unique_ptr InstancesDataSource::get_column_values( return {}; } - const std::array suffixes = {" X", " Y", " Z"}; const int size = this->tot_rows(); if (STREQ(column_id.name, "Name")) { Span instance_data = component_->instanced_data(); @@ -340,30 +293,33 @@ std::unique_ptr InstancesDataSource::get_column_values( values->default_width = 8.0f; return values; } - if (column_id.index < 0 || column_id.index > 2) { - return {}; - } Span transforms = component_->transforms(); if (STREQ(column_id.name, "Position")) { - std::string name = StringRef("Position") + suffixes[column_id.index]; return column_values_from_function( - name, size, [transforms, axis = column_id.index](int index, CellValue &r_cell_value) { - r_cell_value.value_float = transforms[index].translation()[axis]; - }); + column_id.name, + size, + [transforms](int index, CellValue &r_cell_value) { + r_cell_value.value_float3 = transforms[index].translation(); + }, + default_float3_column_width); } if (STREQ(column_id.name, "Rotation")) { - std::string name = StringRef("Rotation") + suffixes[column_id.index]; return column_values_from_function( - name, size, [transforms, axis = column_id.index](int index, CellValue &r_cell_value) { - r_cell_value.value_float = transforms[index].to_euler()[axis]; - }); + column_id.name, + size, + [transforms](int index, CellValue &r_cell_value) { + r_cell_value.value_float3 = transforms[index].to_euler(); + }, + default_float3_column_width); } if (STREQ(column_id.name, "Scale")) { - std::string name = StringRef("Scale") + suffixes[column_id.index]; return column_values_from_function( - name, size, [transforms, axis = column_id.index](int index, CellValue &r_cell_value) { - r_cell_value.value_float = transforms[index].scale()[axis]; - }); + column_id.name, + size, + [transforms](int index, CellValue &r_cell_value) { + r_cell_value.value_float3 = transforms[index].scale(); + }, + default_float3_column_width); } return {}; } diff --git a/source/blender/editors/space_spreadsheet/spreadsheet_layout.cc b/source/blender/editors/space_spreadsheet/spreadsheet_layout.cc index bca80cff773..f1ca65817f6 100644 --- a/source/blender/editors/space_spreadsheet/spreadsheet_layout.cc +++ b/source/blender/editors/space_spreadsheet/spreadsheet_layout.cc @@ -161,6 +161,18 @@ class SpreadsheetLayoutDrawer : public SpreadsheetDrawer { nullptr); UI_but_drawflag_disable(but, UI_BUT_ICON_LEFT); } + else if (cell_value.value_float2.has_value()) { + const float2 value = *cell_value.value_float2; + this->draw_float_vector(params, Span(&value.x, 2)); + } + else if (cell_value.value_float3.has_value()) { + const float3 value = *cell_value.value_float3; + this->draw_float_vector(params, Span(&value.x, 3)); + } + else if (cell_value.value_color.has_value()) { + const Color4f value = *cell_value.value_color; + this->draw_float_vector(params, Span(&value.r, 4)); + } else if (cell_value.value_object.has_value()) { const ObjectCellValue value = *cell_value.value_object; uiDefIconTextBut(params.block, @@ -199,6 +211,36 @@ class SpreadsheetLayoutDrawer : public SpreadsheetDrawer { } } + void draw_float_vector(const CellDrawParams ¶ms, const Span values) const + { + BLI_assert(!values.is_empty()); + const float segment_width = (float)params.width / values.size(); + for (const int i : values.index_range()) { + std::stringstream ss; + const float value = values[i]; + ss << std::fixed << std::setprecision(3) << value; + const std::string value_str = ss.str(); + uiBut *but = uiDefIconTextBut(params.block, + UI_BTYPE_LABEL, + 0, + ICON_NONE, + value_str.c_str(), + params.xmin + i * segment_width, + params.ymin, + segment_width, + params.height, + nullptr, + 0, + 0, + 0, + 0, + nullptr); + /* Right-align Floats. */ + UI_but_drawflag_disable(but, UI_BUT_TEXT_LEFT); + UI_but_drawflag_enable(but, UI_BUT_TEXT_RIGHT); + } + } + int column_width(int column_index) const final { return spreadsheet_layout_.columns[column_index].width; diff --git a/source/blender/makesdna/DNA_space_types.h b/source/blender/makesdna/DNA_space_types.h index ab74282eb64..ce3875dfbc7 100644 --- a/source/blender/makesdna/DNA_space_types.h +++ b/source/blender/makesdna/DNA_space_types.h @@ -1856,8 +1856,6 @@ typedef struct SpaceStatusBar { typedef struct SpreadsheetColumnID { char *name; - int index; - char _pad[4]; } SpreadsheetColumnID; typedef struct SpreadsheetColumn { -- cgit v1.2.3