WM: Add dynamic callback version of operator "depends on cursor" flag #118983

Merged
Hans Goudey merged 5 commits from HooglyBoogly/blender:op-type-depends-on-cursor-dynamic into main 2024-03-11 19:23:25 +01:00
4 changed files with 18 additions and 1 deletions

View File

@ -1190,6 +1190,9 @@ std::string WM_operatortype_description_or_name(bContext *C,
wmOperatorType *ot,
PointerRNA *properties);
/** Check the #OPTYPE_DEPENDS_ON_CURSOR flag and the callback. */
bool WM_operator_depends_on_cursor(bContext &C, wmOperatorType &ot, PointerRNA *properties);
/* `wm_operator_utils.cc` */
/**

View File

@ -1060,6 +1060,9 @@ struct wmOperatorType {
*/
std::string (*get_description)(bContext *C, wmOperatorType *ot, PointerRNA *ptr);
/** A dynamic version of #OPTYPE_DEPENDS_ON_CURSOR which can depend on operator properties. */
bool (*depends_on_cursor)(bContext &C, wmOperatorType &ot, PointerRNA *ptr);
/** RNA for properties. */
StructRNA *srna;

View File

@ -1978,7 +1978,7 @@ void WM_operator_name_call_ptr_with_depends_on_cursor(bContext *C,
}
}
if ((flag & OPTYPE_DEPENDS_ON_CURSOR) == 0) {
if (!WM_operator_depends_on_cursor(*C, *ot, properties)) {
WM_operator_name_call_ptr(C, ot, opcontext, properties, event);
return;
}

View File

@ -615,4 +615,15 @@ std::string WM_operatortype_description_or_name(bContext *C,
return text;
}
bool WM_operator_depends_on_cursor(bContext &C, wmOperatorType &ot, PointerRNA *properties)
{
if (ot.flag & OPTYPE_DEPENDS_ON_CURSOR) {
return true;
}
if (ot.depends_on_cursor) {

The check for properties seems like it would introduce unexpected behavior. It's valid to call WM_operator_name_call_ptr(..) with a null properties argument, so it seems odd that depending on the cursor would change behavior based on the existence of default properties.

Suggest to pass in a pointer which may be null. Since code which calls this accepts null properties.

An alternative could be to create default properties and pass them in.

The check for `properties` seems like it would introduce unexpected behavior. It's valid to call `WM_operator_name_call_ptr(..)` with a null `properties` argument, so it seems odd that depending on the cursor would change behavior based on the existence of default properties. Suggest to pass in a pointer which may be null. Since code which calls this accepts null properties. An alternative could be to create default properties and pass them in.
return ot.depends_on_cursor(C, ot, properties);
}
return false;
}
/** \} */