[Bf-blender-cvs] [2a3e4d8bcd3] master: Outliner: Add generic label element type

Julian Eisel noreply at git.blender.org
Thu Aug 4 15:57:16 CEST 2022


Commit: 2a3e4d8bcd3447ca307108c95b0eeb9ee8bf7323
Author: Julian Eisel
Date:   Thu Aug 4 15:18:29 2022 +0200
Branches: master
https://developer.blender.org/rB2a3e4d8bcd3447ca307108c95b0eeb9ee8bf7323

Outliner: Add generic label element type

No user visible changes expected.

We have a bunch of "base" element types, just to show a label element
for grouping together other elements. There is no reason to have these
tied to a case, just have a generic label type for this. It requires a
string to display, and can display an icon too. The new element type
isn't used yet, but will be in one of the following commits. Would be
nice if the existing base elements can be replaced by this.

Part of D15606.

===================================================================

M	source/blender/editors/space_outliner/CMakeLists.txt
M	source/blender/editors/space_outliner/outliner_draw.cc
M	source/blender/editors/space_outliner/outliner_tree.cc
M	source/blender/editors/space_outliner/tree/tree_element.cc
M	source/blender/editors/space_outliner/tree/tree_element.hh
A	source/blender/editors/space_outliner/tree/tree_element_label.cc
A	source/blender/editors/space_outliner/tree/tree_element_label.hh
M	source/blender/makesdna/DNA_outliner_types.h

===================================================================

diff --git a/source/blender/editors/space_outliner/CMakeLists.txt b/source/blender/editors/space_outliner/CMakeLists.txt
index 97d2957eed2..78ec057f921 100644
--- a/source/blender/editors/space_outliner/CMakeLists.txt
+++ b/source/blender/editors/space_outliner/CMakeLists.txt
@@ -48,6 +48,7 @@ set(SRC
   tree/tree_element_anim_data.cc
   tree/tree_element_collection.cc
   tree/tree_element_driver.cc
+  tree/tree_element_label.cc
   tree/tree_element_gpencil_layer.cc
   tree/tree_element_id.cc
   tree/tree_element_id_library.cc
@@ -67,6 +68,7 @@ set(SRC
   tree/tree_element_anim_data.hh
   tree/tree_element_collection.hh
   tree/tree_element_driver.hh
+  tree/tree_element_label.hh
   tree/tree_element_gpencil_layer.hh
   tree/tree_element_id.hh
   tree/tree_element_id_library.hh
diff --git a/source/blender/editors/space_outliner/outliner_draw.cc b/source/blender/editors/space_outliner/outliner_draw.cc
index 1828846811a..b74964060a8 100644
--- a/source/blender/editors/space_outliner/outliner_draw.cc
+++ b/source/blender/editors/space_outliner/outliner_draw.cc
@@ -2825,10 +2825,20 @@ TreeElementIcon tree_element_get_icon(TreeStoreElem *tselem, TreeElement *te)
     data.icon = tree_element_get_icon_from_id(tselem->id);
   }
 
+  if (!te->abstract_element) {
+    /* Pass */
+  }
+  else if (auto icon = te->abstract_element->getIcon()) {
+    data.icon = *icon;
+  }
+
   return data;
 }
 
-static void tselem_draw_icon(uiBlock *block,
+/**
+ * \return Return true if the element has an icon that was drawn, false if it doesn't have an icon.
+ */
+static bool tselem_draw_icon(uiBlock *block,
                              int xmax,
                              float x,
                              float y,
@@ -2839,7 +2849,7 @@ static void tselem_draw_icon(uiBlock *block,
 {
   TreeElementIcon data = tree_element_get_icon(tselem, te);
   if (data.icon == 0) {
-    return;
+    return false;
   }
 
   const bool is_collection = outliner_is_collection_tree_element(te);
@@ -2863,7 +2873,7 @@ static void tselem_draw_icon(uiBlock *block,
                         0.0f,
                         btheme->collection_color[collection->color_tag].color,
                         true);
-        return;
+        return true;
       }
     }
 
@@ -2895,6 +2905,8 @@ static void tselem_draw_icon(uiBlock *block,
                  alpha,
                  (data.drag_id && ID_IS_LINKED(data.drag_id)) ? data.drag_id->lib->filepath : "");
   }
+
+  return true;
 }
 
 /**
@@ -3314,15 +3326,15 @@ static void outliner_draw_tree_element(bContext *C,
     offsx += UI_UNIT_X;
 
     /* Data-type icon. */
-    if (!(ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM, TSE_ID_BASE))) {
-      tselem_draw_icon(block,
-                       xmax,
-                       (float)startx + offsx,
-                       (float)*starty,
-                       tselem,
-                       te,
-                       (tselem->flag & TSE_HIGHLIGHTED_ICON) ? alpha_fac + 0.5f : alpha_fac,
-                       true);
+    if (!(ELEM(tselem->type, TSE_RNA_PROPERTY, TSE_RNA_ARRAY_ELEM, TSE_ID_BASE)) &&
+        tselem_draw_icon(block,
+                         xmax,
+                         (float)startx + offsx,
+                         (float)*starty,
+                         tselem,
+                         te,
+                         (tselem->flag & TSE_HIGHLIGHTED_ICON) ? alpha_fac + 0.5f : alpha_fac,
+                         true)) {
       offsx += UI_UNIT_X + 4 * ufac;
     }
     else {
diff --git a/source/blender/editors/space_outliner/outliner_tree.cc b/source/blender/editors/space_outliner/outliner_tree.cc
index 49220762b65..98c1d0836cd 100644
--- a/source/blender/editors/space_outliner/outliner_tree.cc
+++ b/source/blender/editors/space_outliner/outliner_tree.cc
@@ -817,9 +817,12 @@ TreeElement *outliner_add_element(SpaceOutliner *space_outliner,
     /* idv is the layer itself */
     id = TREESTORE(parent)->id;
   }
+  else if (ELEM(type, TSE_GENERIC_LABEL)) {
+    id = nullptr;
+  }
 
   /* exceptions */
-  if (type == TSE_ID_BASE) {
+  if (ELEM(type, TSE_ID_BASE, TSE_GENERIC_LABEL)) {
     /* pass */
   }
   else if (id == nullptr) {
@@ -869,7 +872,7 @@ TreeElement *outliner_add_element(SpaceOutliner *space_outliner,
   else if (ELEM(type, TSE_LAYER_COLLECTION, TSE_SCENE_COLLECTION_BASE, TSE_VIEW_COLLECTION_BASE)) {
     /* pass */
   }
-  else if (type == TSE_ID_BASE) {
+  else if (ELEM(type, TSE_ID_BASE, TSE_GENERIC_LABEL)) {
     /* pass */
   }
   else if (type == TSE_SOME_ID) {
@@ -895,10 +898,13 @@ TreeElement *outliner_add_element(SpaceOutliner *space_outliner,
     te->idcode = GS(id->name);
   }
 
-  if (expand && te->abstract_element && te->abstract_element->isExpandValid()) {
+  if (!expand) {
+    /* Pass */
+  }
+  else if (te->abstract_element && te->abstract_element->isExpandValid()) {
     tree_element_expand(*te->abstract_element, *space_outliner);
   }
-  else if (expand && (type == TSE_SOME_ID)) {
+  else if (type == TSE_SOME_ID) {
     /* ID types not (fully) ported to new design yet. */
     if (te->abstract_element->expandPoll(*space_outliner)) {
       outliner_add_id_contents(space_outliner, te, tselem, id);
@@ -916,7 +922,8 @@ TreeElement *outliner_add_element(SpaceOutliner *space_outliner,
                 TSE_RNA_ARRAY_ELEM,
                 TSE_SEQUENCE,
                 TSE_SEQ_STRIP,
-                TSE_SEQUENCE_DUP)) {
+                TSE_SEQUENCE_DUP,
+                TSE_GENERIC_LABEL)) {
     BLI_assert_msg(false, "Element type should already use new AbstractTreeElement design");
   }
 
diff --git a/source/blender/editors/space_outliner/tree/tree_element.cc b/source/blender/editors/space_outliner/tree/tree_element.cc
index 7808c4a3c0f..5ad8ae0220a 100644
--- a/source/blender/editors/space_outliner/tree/tree_element.cc
+++ b/source/blender/editors/space_outliner/tree/tree_element.cc
@@ -17,6 +17,7 @@
 #include "tree_element_driver.hh"
 #include "tree_element_gpencil_layer.hh"
 #include "tree_element_id.hh"
+#include "tree_element_label.hh"
 #include "tree_element_nla.hh"
 #include "tree_element_overrides.hh"
 #include "tree_element_rna.hh"
@@ -52,6 +53,8 @@ std::unique_ptr<AbstractTreeElement> AbstractTreeElement::createFromType(const i
   switch (type) {
     case TSE_SOME_ID:
       return TreeElementID::createFromID(legacy_te, *static_cast<ID *>(idv));
+    case TSE_GENERIC_LABEL:
+      return std::make_unique<TreeElementLabel>(legacy_te, static_cast<const char *>(idv));
     case TSE_ANIM_DATA:
       return std::make_unique<TreeElementAnimData>(legacy_te,
                                                    *static_cast<IdAdtTemplate *>(idv)->adt);
@@ -103,6 +106,11 @@ StringRefNull AbstractTreeElement::getWarning() const
   return "";
 }
 
+std::optional<BIFIconID> AbstractTreeElement::getIcon() const
+{
+  return {};
+}
+
 void AbstractTreeElement::uncollapse_by_default(TreeElement *legacy_te)
 {
   if (!TREESTORE(legacy_te)->used) {
diff --git a/source/blender/editors/space_outliner/tree/tree_element.hh b/source/blender/editors/space_outliner/tree/tree_element.hh
index 1098068d628..a3598e7740b 100644
--- a/source/blender/editors/space_outliner/tree/tree_element.hh
+++ b/source/blender/editors/space_outliner/tree/tree_element.hh
@@ -7,8 +7,10 @@
 #pragma once
 
 #include <memory>
+#include <optional>
 
 #include "BLI_string_ref.hh"
+#include "UI_resources.h"
 
 struct ListBase;
 struct SpaceOutliner;
@@ -63,6 +65,15 @@ class AbstractTreeElement {
    */
   virtual StringRefNull getWarning() const;
 
+  /**
+   * Define the icon to be displayed for this element. If this returns an icon, this will be
+   * displayed. Otherwise, #tree_element_get_icon() may still determine an icon. By default no
+   * value is returned (#std::nullopt).
+   *
+   * All elements should be ported to use this over #tree_element_get_icon().
+   */
+  virtual std::optional<BIFIconID> getIcon() const;
+
   /**
    * Expand this tree element if it is displayed for the first time (as identified by its
    * tree-store element).
diff --git a/source/blender/editors/space_outliner/tree/tree_element_label.cc b/source/blender/editors/space_outliner/tree/tree_element_label.cc
new file mode 100644
index 00000000000..32fa62c5f5e
--- /dev/null
+++ b/source/blender/editors/space_outliner/tree/tree_element_label.cc
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup spoutliner
+ */
+
+#include "DNA_listBase.h"
+
+#include "DNA_outliner_types.h"
+
+#include "../outliner_intern.hh"
+
+#include "tree_element_label.hh"
+
+namespace blender::ed::outliner {
+
+TreeElementLabel::TreeElementLabel(TreeElement &legacy_te, const char *label)
+    : AbstractTreeElement(legacy_te), label_(label)
+{
+  BLI_assert(legacy_te_.store_elem->type == TSE_GENERIC_LABEL);
+  /* The draw string is actually accessed via #TreeElement.name, so make sure this always points to
+   * our string. */
+  legacy_te_.name = label_.c_str();
+}
+
+void TreeElementLabel::setIcon(const BIFIconID icon)
+{
+  icon_ = icon;
+}
+
+std::optional<BIFIconID> TreeElementLabel::getIcon() const
+{
+  return icon_;
+}
+
+}  // namespace blender::ed::outliner
diff --git a/source/blender/editors/space_outliner/tree/tree_element_label.hh b/source/blender/editors/space_outliner/tree/tree_element_label.hh
new file mode 100644
index 00000000000..fc730c7b8f4
--- /dev/null
+++ b/source/blender/editors/space_outliner/tree/tree_element_label.hh
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup spoutliner
+ */
+
+#pragma once
+
+#include <string>
+
+#include "UI_resources.h"
+
+#include "tree_element.hh"
+
+namespace blender::ed::outliner {
+
+/**
+ * A basic, general purpose tree element to just display a label and an icon. Can be used to group
+ * together items underneath as well of course.
+ *
+ * Make sure to give this a unique index, so the element can be identified uniquely. Otherwise
+ * glitches like multiple highlighted elements happen, that share all state (e.g. collapsed,
+ * selected, etc.).
+ */
+class TreeElementLabel final : public AbstractTreeElement {
+  const std::string label_;
+  BIFIconID icon_ = ICON_NONE;
+
+ publi

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list