Blender V5.3
interface_template_shape_key_tree.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Foundation
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <fmt/format.h>
10
11#include "BKE_context.hh"
12#include "BKE_key.hh"
13#include "BKE_object.hh"
14
15#include "BLI_listbase.hh"
16#include "BLT_translation.hh"
17
19#include "UI_tree_view.hh"
20
21#include "RNA_access.hh"
22#include "RNA_prototypes.hh"
23
24#include "DEG_depsgraph.hh"
25
27#include "DNA_key_types.h"
28
29#include "WM_api.hh"
30#include "WM_types.hh"
31
32#include "ED_object.hh"
33#include "ED_undo.hh"
34
36
38 protected:
40
41 public:
43 {
44 is_flat_ = true;
45 };
46
47 void build_tree() override;
48};
49
56
58 private:
59 ShapeKey drag_key_;
60
61 public:
63 : AbstractViewItemDragController(view), drag_key_(drag_key)
64 {
65 }
66
67 std::optional<eWM_DragDataType> get_drag_type() const override
68 {
69 return WM_DRAG_SHAPE_KEY;
70 }
71
72 void *create_drag_data() const override
73 {
74 int selected_count = [&]() -> int {
75 int count = 0;
76 for (KeyBlock &kb : drag_key_.key->block) {
77 count += (kb.flag & KEYBLOCK_SEL) != 0;
78 }
79 return count;
80 }();
81
82 /* Allocate one extra element, to use it as null-delimiter. */
83 KeyBlock **selected_keys_ = MEM_new_array_zeroed<KeyBlock *>(selected_count + 1,
84 "Selected Key Blocks");
85
86 selected_count = 0;
87 for (const auto [index, kb] : drag_key_.key->block.enumerate()) {
88 if (index == 0) {
89 /* Prevent basis shape key from dragging. */
90 continue;
91 }
92
93 if (kb.flag & KEYBLOCK_SEL) {
94 selected_keys_[selected_count] = &kb;
95 selected_count++;
96 }
97 }
98 BLI_assert_msg(selected_keys_[selected_count] == nullptr,
99 "Expected last element to be null (null-delimiter)");
100 return selected_keys_;
101 }
102};
103
105 private:
106 KeyBlock &drop_kb_;
107 int drop_index_;
108
109 public:
111 ui::DropBehavior behavior,
112 KeyBlock &drop_kb,
113 int index)
114 : TreeViewItemDropTarget(item, behavior), drop_kb_(drop_kb), drop_index_(index)
115 {
116 }
117
118 bool can_drop(const wmDrag &drag, const char ** /*r_disabled_hint*/) const override
119 {
120 if (drag.type != WM_DRAG_SHAPE_KEY) {
121 return false;
122 }
123
124 const KeyBlock **drag_shapekey = static_cast<const KeyBlock **>(drag.poin);
125 if (!drag_shapekey || !drag_shapekey[0]) {
126 return false;
127 }
128
129 return true;
130 }
131
132 std::string drop_tooltip(const ui::DragInfo &drag_info) const override
133 {
134 const StringRef drag_name = TIP_("Selected Keys");
135 const StringRef drop_name = drop_kb_.name;
136
137 switch (drag_info.drop_location) {
140 break;
142 if (drop_index_ == 0) {
143 return TIP_("Cannot move above basis shape key");
144 }
145 return fmt::format(fmt::runtime(TIP_("Move {} above {}")), drag_name, drop_name);
147 return fmt::format(fmt::runtime(TIP_("Move {} below {}")), drag_name, drop_name);
148 default:
150 break;
151 }
152
153 return "";
154 }
155
156 bool on_drop(bContext *C, const ui::DragInfo &drag_info) const override
157 {
159 Key *key = BKE_key_from_object(ob);
160 const KeyBlock **drag_shapekey = static_cast<const KeyBlock **>(drag_info.drag_data.poin);
161
162 const int first_drag_index = BLI_findindex(&key->block, drag_shapekey[0]);
163 int drop_index = BLI_findindex(&key->block, &drop_kb_);
164 switch (drag_info.drop_location) {
167 break;
169 if (drop_index == 0) {
170 return false;
171 }
172 drop_index -= int(first_drag_index < drop_index);
173 break;
175 drop_index += int(first_drag_index > drop_index);
176 break;
177 }
178
179 for (int8_t i = 0; drag_shapekey[i] != nullptr; i++) {
180 const int drag_index = BLI_findindex(&key->block, drag_shapekey[i]);
181 if (drag_index == -1) {
182 continue;
183 }
184 if (i > 0) {
185 /* Place subsequent items directly after the previously moved item. */
186 drop_index += int(drag_index > drop_index);
187 }
188 BKE_keyblock_move(ob, drag_index, drop_index);
189 }
190
193 ED_undo_push(C, "Drop Active Shape Key");
194
195 return true;
196 }
197};
198
200 private:
201 ShapeKey shape_key_;
202
203 public:
204 ShapeKeyItem(Object *object, Key *key, KeyBlock *kb, int index)
205 {
206 label_ = kb->name;
207 shape_key_.object = object;
208 shape_key_.key = key;
209 shape_key_.kb = kb;
210 shape_key_.index = index;
211 };
212
213 void build_row(ui::Layout &row) override
214 {
215 uiItemL_ex(&row, this->label_, ICON_SHAPEKEY_DATA, false, false);
216 ui::Layout &sub = row.row(true);
218 sub.use_property_decorate_set(false);
220 &shape_key_.key->id, RNA_ShapeKey, shape_key_.kb);
221
222 if (shape_key_.key->type == KEY_NORMAL) {
223 sub.prop(&shapekey_ptr, "frame", ui::ITEM_R_ICON_ONLY, std::nullopt, ICON_NONE);
224 }
225 else {
226 if (shape_key_.index > 0) {
227 sub.prop(&shapekey_ptr, "value", ui::ITEM_R_ICON_ONLY, std::nullopt, ICON_NONE);
228 }
229 }
230
231 sub.prop(&shapekey_ptr, "mute", ui::ITEM_R_ICON_ONLY, std::nullopt, ICON_NONE);
232 sub.prop(&shapekey_ptr, "lock_shape", ui::ITEM_R_ICON_ONLY, std::nullopt, ICON_NONE);
233 if (shape_key_.kb->flag & KEYBLOCK_MUTE) {
234 row.active_set(false);
235 }
236 }
237
238 std::optional<bool> should_be_active() const override
239 {
240 return shape_key_.object->shapenr == shape_key_.index + 1;
241 }
242
243 void on_activate(bContext &C) override
244 {
246 &shape_key_.object->id, RNA_Object, shape_key_.object);
247 PropertyRNA *prop = RNA_struct_find_property(&object_ptr, "active_shape_key_index");
248 RNA_property_int_set(&object_ptr, prop, shape_key_.index);
249 RNA_property_update(&C, &object_ptr, prop);
250
251 ED_undo_push(&C, "Set Active Shape Key");
252 }
253
254 std::optional<bool> should_be_selected() const override
255 {
256 return shape_key_.kb->flag & KEYBLOCK_SEL;
257 }
258
259 void set_selected(const bool select) override
260 {
261 AbstractViewItem::set_selected(select);
262 SET_FLAG_FROM_TEST(shape_key_.kb->flag, select, KEYBLOCK_SEL);
263 }
264
265 bool supports_renaming() const override
266 {
267 return true;
268 }
269
270 bool rename(const bContext &C, StringRefNull new_name) override
271 {
273 &shape_key_.key->id, RNA_ShapeKey, shape_key_.kb);
274 PropertyRNA *prop = RNA_struct_find_property(&shapekey_ptr, "name");
275 RNA_property_string_set(&shapekey_ptr, prop, new_name.c_str());
276 RNA_property_update(&const_cast<bContext &>(C), &shapekey_ptr, prop);
277 ED_undo_push(const_cast<bContext *>(&C), "Rename shape key");
278 return true;
279 }
280
282 {
283 return label_;
284 }
285
286 void delete_item(bContext *C) override
287 {
288 Main *bmain = CTX_data_main(C);
289 BKE_object_shapekey_remove(bmain, shape_key_.object, shape_key_.kb);
290 DEG_id_tag_update(&shape_key_.object->id, ID_RECALC_GEOMETRY);
293 ED_undo_grouped_push(C, "Delete Shape Key");
294 }
295
296 void build_context_menu(bContext &C, ui::Layout &layout) const override
297 {
298 MenuType *mt = WM_menutype_find("MESH_MT_shape_key_tree_context_menu", true);
299 if (!mt) {
300 return;
301 }
302 ui::menutype_draw(&C, mt, &layout);
303 }
304
305 std::unique_ptr<ui::AbstractViewItemDragController> create_drag_controller() const override
306 {
307 return std::make_unique<ShapeKeyDragController>(
308 static_cast<ShapeKeyTreeView &>(get_tree_view()), shape_key_);
309 }
310
311 std::unique_ptr<ui::TreeViewItemDropTarget> create_drop_target() override
312 {
313 return std::make_unique<ShapeKeyDropTarget>(
314 *this, ui::DropBehavior::Reorder, *shape_key_.kb, shape_key_.index);
315 }
316};
317
319{
321 if (key == nullptr) {
322 return;
323 }
324
325 for (const auto [index, kb] : key->block.enumerate()) {
326 this->add_tree_item<ShapeKeyItem>(&object_, key, &kb, index);
327 }
328}
329
331{
333 if (ob == nullptr) {
334 return;
335 }
336
337 ui::Block *block = layout->block();
338
339 ui::AbstractTreeView *tree_view = block_add_view(
340 *block,
341 "Shape Key Tree View",
342 std::make_unique<ed::object::shapekey::ShapeKeyTreeView>(*ob));
343 tree_view->set_context_menu_title("Shape Key");
344 tree_view->set_default_rows(4);
345 tree_view->allow_multiselect_items();
346
347 ui::TreeViewBuilder::build_tree_view(*C, *tree_view, *layout);
348}
349} // namespace blender::ed::object::shapekey
General operations, lookup, etc. for blender objects.
#define TIP_(msgid)
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:502
#define NC_OBJECT
Definition WM_types.hh:392
#define BLI_assert_unreachable()
#define SET_FLAG_FROM_TEST(value, test, flag)
#define BLI_assert_msg(a, msg)
constexpr const char * c_str() const
std::optional< eWM_DragDataType > get_drag_type() const override
std::string drop_tooltip(const ui::DragInfo &drag_info) const override
bool on_drop(bContext *C, const ui::DragInfo &drag_info) const override
bool can_drop(const wmDrag &drag, const char **) const override
ShapeKeyDropTarget(ui::AbstractTreeViewItem &item, ui::DropBehavior behavior, KeyBlock &drop_kb, int index)
std::unique_ptr< ui::AbstractViewItemDragController > create_drag_controller() const override
bool rename(const bContext &C, StringRefNull new_name) override
std::unique_ptr< ui::TreeViewItemDropTarget > create_drop_target() override
void build_context_menu(bContext &C, ui::Layout &layout) const override
ShapeKeyItem(Object *object, Key *key, KeyBlock *kb, int index)
Abstract base class for defining a customizable tree-view item.
AbstractTreeView & get_tree_view() const
Definition tree_view.cc:775
void set_default_rows(int default_rows)
Definition tree_view.cc:234
void set_context_menu_title(const std::string &title)
static void build_tree_view(const bContext &C, AbstractTreeView &tree_view, Layout &layout, bool add_box=true)
ItemT & add_tree_item(Args &&...args)
TreeViewItemDropTarget(AbstractTreeViewItem &view_item, DropBehavior behavior=DropBehavior::Insert)
Definition tree_view.cc:557
void template_tree(ui::Layout *layout, bContext *C)
Object * context_active_object(const bContext *C)
void menutype_draw(bContext *C, MenuType *mt, Layout *layout)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void ED_undo_grouped_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:357
void RNA_property_update(bContext *C, PointerRNA *ptr, PropertyRNA *prop)
bool BKE_keyblock_move(Object *ob, int org_index, int new_index)
Definition key.cc:1778
MenuType * WM_menutype_find(const StringRef idname, bool quiet)
@ WM_DRAG_SHAPE_KEY
Definition WM_types.hh:1319
void RNA_property_int_set(PointerRNA *ptr, PropertyRNA *prop, int value)
int BLI_findindex(const ListBase *listbase, const void *vlink) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:593
void RNA_property_string_set(PointerRNA *ptr, PropertyRNA *prop, const char *value)
void DEG_id_tag_update(ID *id, unsigned int flags)
void ED_undo_push(bContext *C, const char *str, UndoEncodeHints hints=UndoEncodeHints(0))
Definition ed_undo.cc:103
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool BKE_object_shapekey_remove(Main *bmain, Object *ob, KeyBlock *kb)
Definition object.cc:4819
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_GEOMETRY
Definition DNA_ID.h:1074
void DEG_relations_tag_update(Main *bmain)
Key * BKE_key_from_object(Object *ob)
Definition key.cc:1311
signed char int8_t
Definition BKE_text.h:76
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
KeyBlockFlag flag
ListBaseT< KeyBlock > block
const wmDrag & drag_data
const DropLocation drop_location
void alignment_set(LayoutAlign alignment)
void use_property_decorate_set(bool is_decorate)
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)
void active_set(bool active)
Layout & row(bool align)
eWM_DragDataType type
Definition WM_types.hh:1424