Blender V5.3
interface_template_light_linking.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstdio>
10#include <memory>
11
12#include <fmt/format.h>
13
14#include "BLI_assert.hh"
15#include "BLI_listbase.hh"
16
17#include "BLT_translation.hh"
18
20#include "DNA_object_types.h"
21
22#include "BKE_collection.hh"
23#include "BKE_context.hh"
24#include "BKE_light_linking.h"
25#include "BKE_scene.hh"
26
27#include "RNA_access.hh"
28#include "RNA_prototypes.hh"
29
30#include "UI_interface.hh"
32#include "UI_resources.hh"
33#include "UI_tree_view.hh"
34
35#include "WM_api.hh"
36
37#include "ED_undo.hh"
38
39namespace blender::ui {
40namespace light_linking {
41
42namespace {
43
44class CollectionDropTarget {
45 Collection &collection_;
46
47 public:
48 bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const
49 {
50 if (drag.type != WM_DRAG_ID) {
51 return false;
52 }
53
54 const wmDragID *drag_id = drag.ids.first();
55 if (!drag_id) {
56 return false;
57 }
58
59 /* The dragged IDs are guaranteed to be the same type, so only check the type of the first one.
60 */
61 const ID_Type id_type = GS(drag_id->id->name);
62 if (!ELEM(id_type, ID_OB, ID_GR)) {
63 *r_disabled_hint = "Can only add objects and collections to the light linking collection";
64 return false;
65 }
66
67 return true;
68 }
69
70 CollectionDropTarget(Collection &collection) : collection_(collection) {}
71
72 Collection &get_collection() const
73 {
74 return collection_;
75 }
76};
77
78class CollectionInsertDropTarget : public CollectionDropTarget {
79 Main &bmain_;
80
81 public:
82 bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const
83 {
84 if (!CollectionDropTarget::can_drop(drag, r_disabled_hint)) {
85 return false;
86 }
87
88 bool have_ids_outside_collection = false;
89 for (const wmDragID &drag_id : drag.ids) {
90 const ID *id = drag_id.id;
91
92 /* Allow insertion if the ID is already in the target collection.
93 * This is to support reordering within the target collection. */
94 const ID_Type id_type = GS(id->name);
95 switch (id_type) {
96 case ID_OB: {
97 const Object *drag_object = id_cast<const Object *>(id);
98 if (!BKE_collection_has_object(&get_collection(), drag_object)) {
99 have_ids_outside_collection = true;
100 }
101 break;
102 }
103 case ID_GR: {
104 const Collection *drag_collection = id_cast<const Collection *>(id);
105 if (!BKE_collection_has_collection(&get_collection(), drag_collection)) {
106 have_ids_outside_collection = true;
107 }
108 break;
109 }
110 default:
111 break;
112 }
113
114 if (have_ids_outside_collection) {
115 break;
116 }
117 }
118
119 if (have_ids_outside_collection) {
120 if (BKE_scene_find_from_collection(&bmain_, &get_collection()) != nullptr) {
121 *r_disabled_hint = "Can not modify collection that is used by a scene";
122 return false;
123 }
124 }
125
126 return true;
127 }
128
129 CollectionInsertDropTarget(Main &bmain, Collection &collection)
130 : CollectionDropTarget(collection), bmain_(bmain)
131 {
132 }
133};
134
138class InsertCollectionDropTarget : public DropTargetInterface {
139 CollectionInsertDropTarget collection_target_;
140
141 public:
142 InsertCollectionDropTarget(Main &bmain, Collection &collection)
143 : collection_target_(bmain, collection)
144 {
145 }
146
147 bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override
148 {
149 return collection_target_.can_drop(drag, r_disabled_hint);
150 }
151
152 std::string drop_tooltip(const DragInfo & /*drag*/) const override
153 {
154 return TIP_("Add to linking collection");
155 }
156
157 bool on_drop(bContext *C, const DragInfo &drag) const override
158 {
159 Main *bmain = CTX_data_main(C);
160 Scene *scene = CTX_data_scene(C);
161
162 for (wmDragID &drag_id : drag.drag_data.ids) {
164 &collection_target_.get_collection(),
165 drag_id.id,
167 }
168
169 /* It is possible that the light linking collection is also used by the view layer.
170 * For this case send a notifier so that the UI is updated for the changes in the collection
171 * content. */
173
174 ED_undo_push(C, "Add to linking collection");
175
176 return true;
177 }
178};
179
180class ReorderCollectionDropTarget : public TreeViewItemDropTarget {
181 CollectionDropTarget collection_target_;
182 const ID &drop_id_;
183
184 public:
185 ReorderCollectionDropTarget(AbstractTreeViewItem &item,
186 Collection &collection,
187 const ID &drop_id)
189 collection_target_(collection),
190 drop_id_(drop_id)
191 {
192 }
193
194 bool can_drop(const wmDrag &drag, const char **r_disabled_hint) const override
195 {
196 return collection_target_.can_drop(drag, r_disabled_hint);
197 }
198
199 std::string drop_tooltip(const DragInfo &drag) const override
200 {
201 const StringRef drop_name = drop_id_.name + 2;
202
203 switch (drag.drop_location) {
205 return "Add to linking collection";
207 return fmt::format(fmt::runtime(TIP_("Add to linking collection before {}")), drop_name);
209 return fmt::format(fmt::runtime(TIP_("Add to linking collection after {}")), drop_name);
210 }
211
212 return "";
213 }
214
215 bool on_drop(bContext *C, const DragInfo &drag) const override
216 {
217 Main *bmain = CTX_data_main(C);
218 Scene *scene = CTX_data_scene(C);
219
220 Collection &collection = collection_target_.get_collection();
222
223 for (wmDragID &drag_id : drag.drag_data.ids) {
224 if (drag_id.id == &drop_id_) {
225 continue;
226 }
227
228 BKE_light_linking_unlink_id_from_collection(bmain, &collection, drag_id.id, nullptr);
229
230 switch (drag.drop_location) {
232 BKE_light_linking_add_receiver_to_collection(bmain, &collection, drag_id.id, link_state);
233 break;
236 bmain, &collection, drag_id.id, &drop_id_, link_state);
237 break;
240 bmain, &collection, drag_id.id, &drop_id_, link_state);
241 break;
242 }
243 }
244
245 /* It is possible that the light linking collection is also used by the view layer.
246 * For this case send a notifier so that the UI is updated for the changes in the collection
247 * content. */
249
250 ED_undo_push(C, "Add to linking collection");
251
252 return true;
253 }
254};
255
256class ItemDragController : public AbstractViewItemDragController {
257 ID &id_;
258
259 public:
260 ItemDragController(AbstractView &view, ID &id) : AbstractViewItemDragController(view), id_(id) {}
261
262 std::optional<eWM_DragDataType> get_drag_type() const override
263 {
264 return WM_DRAG_ID;
265 }
266
267 void *create_drag_data() const override
268 {
269 return static_cast<void *>(&id_);
270 }
271};
272
273class CollectionViewItem : public BasicTreeViewItem {
274 Layout &context_layout_;
275 Collection &collection_;
276
277 ID &id_;
278 CollectionLightLinking &collection_light_linking_;
279
280 public:
281 CollectionViewItem(Layout &context_layout,
282 Collection &collection,
283 ID &id,
284 CollectionLightLinking &collection_light_linking,
285 const BIFIconID icon)
286 : BasicTreeViewItem(id.name + 2, icon),
287 context_layout_(context_layout),
288 collection_(collection),
289 id_(id),
290 collection_light_linking_(collection_light_linking)
291 {
292 }
293
294 void build_row(Layout &row) override
295 {
296 if (is_active()) {
297 PointerRNA id_ptr = RNA_id_pointer_create(&id_);
298 PointerRNA collection_ptr = RNA_id_pointer_create(&collection_.id);
299
300 context_layout_.context_ptr_set("id", &id_ptr);
301 context_layout_.context_ptr_set("collection", &collection_ptr);
302 }
303
304 add_label(row);
305
306 Layout &sub = row.row(true);
307 sub.use_property_decorate_set(false);
308
309 build_state_button(sub);
310 }
311
312 std::unique_ptr<AbstractViewItemDragController> create_drag_controller() const override
313 {
314 return std::make_unique<ItemDragController>(get_tree_view(), id_);
315 }
316
317 std::unique_ptr<TreeViewItemDropTarget> create_drop_target() override
318 {
319 return std::make_unique<ReorderCollectionDropTarget>(*this, collection_, id_);
320 }
321
322 private:
323 int get_state_icon() const
324 {
325 switch (collection_light_linking_.link_state) {
327 return ICON_CHECKBOX_HLT;
329 return ICON_CHECKBOX_DEHLT;
330 }
332 return ICON_NONE;
333 }
334
335 static void link_state_toggle(CollectionLightLinking &collection_light_linking)
336 {
337 switch (collection_light_linking.link_state) {
339 collection_light_linking.link_state = COLLECTION_LIGHT_LINKING_STATE_EXCLUDE;
340 return;
342 collection_light_linking.link_state = COLLECTION_LIGHT_LINKING_STATE_INCLUDE;
343 return;
344 }
345
347 }
348
349 void build_state_button(Layout &row)
350 {
351 Block *block = row.block();
352 const int icon = get_state_icon();
353
354 PointerRNA collection_light_linking_ptr = RNA_pointer_create_discrete(
355 &collection_.id, RNA_CollectionLightLinking, &collection_light_linking_);
356
357 Button *button = uiDefIconButR(block,
359 icon,
360 0,
361 0,
362 UI_UNIT_X,
363 UI_UNIT_Y,
364 &collection_light_linking_ptr,
365 "link_state",
366 0,
367 0.0f,
368 0.0f,
369 std::nullopt);
370
371 button_func_set(button, [&collection_light_linking = collection_light_linking_](bContext &) {
372 link_state_toggle(collection_light_linking);
373 });
374 }
375};
376
377class CollectionView : public AbstractTreeView {
378 Layout &context_layout_;
379 Main &bmain_;
380 Collection &collection_;
381
382 public:
383 CollectionView(Layout &context_layout, Main &bmain, Collection &collection)
384 : context_layout_(context_layout), bmain_(bmain), collection_(collection)
385 {
386 }
387
388 void build_tree() override
389 {
390 for (CollectionChild &collection_child : collection_.children) {
391 Collection *child_collection = collection_child.collection;
392 add_tree_item<CollectionViewItem>(context_layout_,
393 collection_,
394 child_collection->id,
395 collection_child.light_linking,
396 ICON_OUTLINER_COLLECTION);
397 }
398
399 for (CollectionObject &collection_object : collection_.gobject) {
400 Object *child_object = collection_object.ob;
401 add_tree_item<CollectionViewItem>(context_layout_,
402 collection_,
403 child_object->id,
404 collection_object.light_linking,
405 ICON_OBJECT_DATA);
406 }
407 }
408
409 std::unique_ptr<DropTargetInterface> create_drop_target() override
410 {
411 return std::make_unique<InsertCollectionDropTarget>(bmain_, collection_);
412 }
413};
414
415} // namespace
416} // namespace light_linking
417
419 bContext *C,
420 Layout *context_layout,
421 PointerRNA *ptr,
422 const StringRefNull propname)
423{
424 if (!*ptr) {
425 return;
426 }
427
428 PropertyRNA *prop = RNA_struct_find_property(ptr, propname.c_str());
429 if (!prop) {
430 printf("%s: property not found: %s.%s\n",
431 __func__,
433 propname.c_str());
434 return;
435 }
436
437 if (RNA_property_type(prop) != PROP_POINTER) {
438 printf("%s: expected pointer property for %s.%s\n",
439 __func__,
441 propname.c_str());
442 return;
443 }
444
445 const PointerRNA collection_ptr = RNA_property_pointer_get(ptr, prop);
446 if (!collection_ptr) {
447 return;
448 }
449 if (collection_ptr.type != RNA_Collection) {
450 printf("%s: expected collection pointer property for %s.%s\n",
451 __func__,
453 propname.c_str());
454 return;
455 }
456
457 Collection *collection = static_cast<Collection *>(collection_ptr.data);
458
459 Block *block = layout->block();
460
461 AbstractTreeView *tree_view = block_add_view(
462 *block,
463 "Light Linking Collection Tree View",
464 std::make_unique<light_linking::CollectionView>(
465 *context_layout, *CTX_data_main(C), *collection));
466 tree_view->set_context_menu_title("Light Linking");
467 tree_view->set_default_rows(5);
468
469 TreeViewBuilder::build_tree_view(*C, *tree_view, *layout);
470}
471
472} // namespace blender::ui
#define TIP_(msgid)
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define UI_UNIT_Y
#define UI_UNIT_X
#define NC_SCENE
Definition WM_types.hh:391
#define ND_LAYER_CONTENT
Definition WM_types.hh:472
#define BLI_assert_unreachable()
#define ELEM(...)
constexpr const char * c_str() const
Abstract base class for defining a customizable tree-view item.
void set_default_rows(int default_rows)
Definition tree_view.cc:234
void set_context_menu_title(const std::string &title)
BasicTreeViewItem(StringRef label, BIFIconID icon=ICON_NONE)
static void build_tree_view(const bContext &C, AbstractTreeView &tree_view, Layout &layout, bool add_box=true)
TreeViewItemDropTarget(AbstractTreeViewItem &view_item, DropBehavior behavior=DropBehavior::Insert)
Definition tree_view.cc:557
Button * uiDefIconButR(Block *block, ButtonType type, int icon, int x, int y, short width, short height, PointerRNA *ptr, StringRefNull propname, int index, float min, float max, std::optional< StringRef > tip)
AbstractGridView * block_add_view(Block &block, StringRef idname, std::unique_ptr< AbstractGridView > grid_view)
void button_func_set(Button *but, std::function< void(bContext &)> func)
void template_light_linking_collection(Layout *layout, bContext *C, Layout *context_layout, PointerRNA *ptr, StringRefNull propname)
PointerRNA RNA_property_pointer_get(PointerRNA *ptr, PropertyRNA *prop)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
void BKE_light_linking_add_receiver_to_collection_after(struct Main *bmain, struct Collection *collection, struct ID *receiver, const struct ID *after, const eCollectionLightLinkingState link_state)
PointerRNA RNA_id_pointer_create(ID *id)
bool BKE_collection_has_object(Collection *collection, const Object *ob)
@ COLLECTION_LIGHT_LINKING_STATE_INCLUDE
@ COLLECTION_LIGHT_LINKING_STATE_EXCLUDE
PropertyType RNA_property_type(PropertyRNA *prop)
@ PROP_POINTER
Definition RNA_types.hh:215
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)
bool BKE_collection_has_collection(const Collection *parent, const Collection *collection)
Scene * BKE_scene_find_from_collection(const Main *bmain, const Collection *collection)
Definition scene.cc:2427
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
int BIFIconID
Definition ED_asset.hh:30
const char * RNA_struct_identifier(const StructRNA *type)
bool BKE_light_linking_unlink_id_from_collection(struct Main *bmain, struct Collection *collection, struct ID *id, struct ReportList *reports)
void BKE_light_linking_add_receiver_to_collection_before(struct Main *bmain, struct Collection *collection, struct ID *receiver, const struct ID *before, const eCollectionLightLinkingState link_state)
Main * CTX_data_main(const bContext *C)
ID_Type GS(const char *name)
Definition DNA_ID.h:410
void BKE_light_linking_add_receiver_to_collection(struct Main *bmain, struct Collection *collection, struct ID *receiver, const eCollectionLightLinkingState link_state)
Scene * CTX_data_scene(const bContext *C)
CollectionLightLinking light_linking
eCollectionLightLinkingState link_state
CollectionLightLinking light_linking
ListBaseT< CollectionChild > children
ListBaseT< CollectionObject > gobject
char name[258]
Definition DNA_ID.h:433
StructRNA * type
Definition RNA_types.hh:63
const wmDrag & drag_data
const DropLocation drop_location
void use_property_decorate_set(bool is_decorate)
Layout & row(bool align)
void context_ptr_set(StringRef name, const PointerRNA *ptr)
eWM_DragDataType type
Definition WM_types.hh:1424
ListBaseT< wmDragID > ids
Definition WM_types.hh:1438