Cleanup: Deduplicate code for finding context object

Instead of manually checking the pinned object, use the existing
ED_object_active_context function. This requires adding const
to the context in that function.
This commit is contained in:
Hans Goudey 2020-07-03 11:58:43 -04:00
parent 33a74941c5
commit a21cb22f8b
6 changed files with 28 additions and 76 deletions

View File

@ -58,9 +58,9 @@ struct wmWindowManager;
/* object_edit.c */
/* context.object */
struct Object *ED_object_context(struct bContext *C);
struct Object *ED_object_context(const struct bContext *C);
/* context.object or context.active_object */
struct Object *ED_object_active_context(struct bContext *C);
struct Object *ED_object_active_context(const struct bContext *C);
void ED_collection_hide_menu_draw(const struct bContext *C, struct uiLayout *layout);
/* object_utils.c */

View File

@ -1836,18 +1836,6 @@ void uiTemplatePathBuilder(uiLayout *layout,
* Template for building the panel layout for the active object's modifiers.
* \{ */
/**
* Get the active object or the property region's pinned object.
*/
static Object *get_context_object(const bContext *C)
{
SpaceProperties *sbuts = CTX_wm_space_properties(C);
if (sbuts != NULL && (sbuts->pinid != NULL) && GS(sbuts->pinid->name) == ID_OB) {
return (Object *)sbuts->pinid;
}
return CTX_data_active_object(C);
}
static void modifier_panel_id(void *md_link, char *r_name)
{
ModifierData *md = (ModifierData *)md_link;
@ -1859,7 +1847,7 @@ void uiTemplateModifiers(uiLayout *UNUSED(layout), bContext *C)
ScrArea *sa = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
ListBase *modifiers = &ob->modifiers;
bool panels_match = UI_panel_list_matches_data(region, modifiers, modifier_panel_id);
@ -1952,7 +1940,7 @@ static ListBase *get_constraints(const bContext *C, bool use_bone_constraints)
}
}
else {
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
if (ob != NULL) {
constraints = &ob->constraints;
}
@ -2095,7 +2083,7 @@ void uiTemplateGpencilModifiers(uiLayout *UNUSED(layout), bContext *C)
{
ScrArea *sa = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
ListBase *modifiers = &ob->greasepencil_modifiers;
bool panels_match = UI_panel_list_matches_data(region, modifiers, gpencil_modifier_panel_id);
@ -2183,7 +2171,7 @@ void uiTemplateShaderFx(uiLayout *UNUSED(layout), bContext *C)
{
ScrArea *sa = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
ListBase *shaderfx = &ob->shader_fx;
bool panels_match = UI_panel_list_matches_data(region, shaderfx, shaderfx_panel_id);

View File

@ -126,14 +126,14 @@ static ListBase selected_objects_get(bContext *C);
/** \name Internal Utilities
* \{ */
Object *ED_object_context(bContext *C)
Object *ED_object_context(const bContext *C)
{
return CTX_data_pointer_get_type(C, "object", &RNA_Object).data;
}
/* find the correct active object per context
* note: context can be NULL when called from a enum with PROP_ENUM_NO_CONTEXT */
Object *ED_object_active_context(bContext *C)
Object *ED_object_active_context(const bContext *C)
{
Object *ob = NULL;
if (C) {

View File

@ -34,7 +34,6 @@
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "ED_object.h"
@ -50,23 +49,12 @@
#include "MOD_gpencil_ui_common.h" /* Self include */
static Object *get_gpencilmodifier_object(const bContext *C)
{
SpaceProperties *sbuts = CTX_wm_space_properties(C);
if (sbuts != NULL && (sbuts->pinid != NULL) && GS(sbuts->pinid->name) == ID_OB) {
return (Object *)sbuts->pinid;
}
else {
return CTX_data_active_object(C);
}
}
/**
* Poll function so these modifier panels only show for grease pencil objects.
*/
static bool gpencil_modifier_ui_poll(const bContext *C, PanelType *UNUSED(pt))
{
Object *ob = get_gpencilmodifier_object(C);
Object *ob = ED_object_active_context(C);
return (ob != NULL) && (ob->type == OB_GPENCIL);
}
@ -80,7 +68,7 @@ static bool gpencil_modifier_ui_poll(const bContext *C, PanelType *UNUSED(pt))
*/
static void gpencil_modifier_reorder(bContext *C, Panel *panel, int new_index)
{
Object *ob = get_gpencilmodifier_object(C);
Object *ob = ED_object_active_context(C);
GpencilModifierData *md = BLI_findlink(&ob->greasepencil_modifiers, panel->runtime.list_index);
PointerRNA props_ptr;
@ -94,7 +82,7 @@ static void gpencil_modifier_reorder(bContext *C, Panel *panel, int new_index)
static short get_gpencil_modifier_expand_flag(const bContext *C, Panel *panel)
{
Object *ob = get_gpencilmodifier_object(C);
Object *ob = ED_object_active_context(C);
GpencilModifierData *md = BLI_findlink(&ob->greasepencil_modifiers, panel->runtime.list_index);
return md->ui_expand_flag;
return 0;
@ -102,7 +90,7 @@ static short get_gpencil_modifier_expand_flag(const bContext *C, Panel *panel)
static void set_gpencil_modifier_expand_flag(const bContext *C, Panel *panel, short expand_flag)
{
Object *ob = get_gpencilmodifier_object(C);
Object *ob = ED_object_active_context(C);
GpencilModifierData *md = BLI_findlink(&ob->greasepencil_modifiers, panel->runtime.list_index);
md->ui_expand_flag = expand_flag;
}
@ -245,7 +233,7 @@ void gpencil_modifier_panel_get_property_pointers(const bContext *C,
PointerRNA *r_ob_ptr,
PointerRNA *r_md_ptr)
{
Object *ob = get_gpencilmodifier_object(C);
Object *ob = ED_object_active_context(C);
GpencilModifierData *md = BLI_findlink(&ob->greasepencil_modifiers, panel->runtime.list_index);
RNA_pointer_create(&ob->id, &RNA_GpencilModifier, md, r_md_ptr);
@ -269,7 +257,7 @@ static void gpencil_modifier_ops_extra_draw(bContext *C, uiLayout *layout, void
const GpencilModifierTypeInfo *mti = BKE_gpencil_modifier_get_info(md->type);
PointerRNA ptr;
Object *ob = get_gpencilmodifier_object(C);
Object *ob = ED_object_active_context(C);
RNA_pointer_create(&ob->id, &RNA_GpencilModifier, md, &ptr);
uiLayoutSetContextPointer(layout, "modifier", &ptr);
uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
@ -328,7 +316,7 @@ static void gpencil_modifier_panel_header(const bContext *C, Panel *panel)
uiLayout *row, *sub;
uiLayout *layout = panel->layout;
Object *ob = get_gpencilmodifier_object(C);
Object *ob = ED_object_active_context(C);
GpencilModifierData *md = BLI_findlink(&ob->greasepencil_modifiers, panel->runtime.list_index);
PointerRNA ptr;
RNA_pointer_create(&ob->id, &RNA_GpencilModifier, md, &ptr);

View File

@ -33,7 +33,6 @@
#include "DNA_particle_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "ED_object.h"
@ -50,24 +49,13 @@
#include "MOD_modifiertypes.h"
#include "MOD_ui_common.h" /* Self include */
static Object *get_modifier_object(const bContext *C)
{
SpaceProperties *sbuts = CTX_wm_space_properties(C);
if (sbuts != NULL && (sbuts->pinid != NULL) && GS(sbuts->pinid->name) == ID_OB) {
return (Object *)sbuts->pinid;
}
else {
return CTX_data_active_object(C);
}
}
/**
* Poll function so these modifier panels don't show for other object types with modifiers (only
* grease pencil currently).
*/
static bool modifier_ui_poll(const bContext *C, PanelType *UNUSED(pt))
{
Object *ob = get_modifier_object(C);
Object *ob = ED_object_active_context(C);
return (ob != NULL) && (ob->type != OB_GPENCIL);
}
@ -81,7 +69,7 @@ static bool modifier_ui_poll(const bContext *C, PanelType *UNUSED(pt))
*/
static void modifier_reorder(bContext *C, Panel *panel, int new_index)
{
Object *ob = get_modifier_object(C);
Object *ob = ED_object_active_context(C);
ModifierData *md = BLI_findlink(&ob->modifiers, panel->runtime.list_index);
PointerRNA props_ptr;
@ -95,14 +83,14 @@ static void modifier_reorder(bContext *C, Panel *panel, int new_index)
static short get_modifier_expand_flag(const bContext *C, Panel *panel)
{
Object *ob = get_modifier_object(C);
Object *ob = ED_object_active_context(C);
ModifierData *md = BLI_findlink(&ob->modifiers, panel->runtime.list_index);
return md->ui_expand_flag;
}
static void set_modifier_expand_flag(const bContext *C, Panel *panel, short expand_flag)
{
Object *ob = get_modifier_object(C);
Object *ob = ED_object_active_context(C);
ModifierData *md = BLI_findlink(&ob->modifiers, panel->runtime.list_index);
md->ui_expand_flag = expand_flag;
}
@ -135,7 +123,7 @@ void modifier_panel_get_property_pointers(const bContext *C,
PointerRNA *r_ob_ptr,
PointerRNA *r_md_ptr)
{
Object *ob = get_modifier_object(C);
Object *ob = ED_object_active_context(C);
ModifierData *md = BLI_findlink(&ob->modifiers, panel->runtime.list_index);
@ -228,7 +216,7 @@ static void modifier_ops_extra_draw(bContext *C, uiLayout *layout, void *md_v)
ModifierData *md = (ModifierData *)md_v;
PointerRNA ptr;
Object *ob = get_modifier_object(C);
Object *ob = ED_object_active_context(C);
RNA_pointer_create(&ob->id, &RNA_Modifier, md, &ptr);
uiLayoutSetContextPointer(layout, "modifier", &ptr);
uiLayoutSetOperatorContext(layout, WM_OP_INVOKE_DEFAULT);
@ -303,7 +291,7 @@ static void modifier_panel_header(const bContext *C, Panel *panel)
uiLayout *layout = panel->layout;
PointerRNA ptr;
Object *ob = get_modifier_object(C);
Object *ob = ED_object_active_context(C);
/* Don't use #modifier_panel_get_property_pointers, we don't want to lock the header. */
ModifierData *md = BLI_findlink(&ob->modifiers, panel->runtime.list_index);

View File

@ -32,7 +32,6 @@
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_shader_fx_types.h"
#include "DNA_space_types.h"
#include "ED_object.h"
@ -48,17 +47,6 @@
#include "FX_ui_common.h" /* Self include */
static Object *get_context_object(const bContext *C)
{
SpaceProperties *sbuts = CTX_wm_space_properties(C);
if (sbuts != NULL && (sbuts->pinid != NULL) && GS(sbuts->pinid->name) == ID_OB) {
return (Object *)sbuts->pinid;
}
else {
return CTX_data_active_object(C);
}
}
/* -------------------------------------------------------------------- */
/** \name Panel Drag and Drop, Expansion Saving
* \{ */
@ -68,7 +56,7 @@ static Object *get_context_object(const bContext *C)
*/
static void shaderfx_reorder(bContext *C, Panel *panel, int new_index)
{
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
ShaderFxData *fx = BLI_findlink(&ob->shader_fx, panel->runtime.list_index);
PointerRNA props_ptr;
@ -85,7 +73,7 @@ static void shaderfx_reorder(bContext *C, Panel *panel, int new_index)
*/
static short get_shaderfx_expand_flag(const bContext *C, Panel *panel)
{
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
ShaderFxData *fx = BLI_findlink(&ob->shader_fx, panel->runtime.list_index);
return fx->ui_expand_flag;
}
@ -95,7 +83,7 @@ static short get_shaderfx_expand_flag(const bContext *C, Panel *panel)
*/
static void set_shaderfx_expand_flag(const bContext *C, Panel *panel, short expand_flag)
{
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
ShaderFxData *fx = BLI_findlink(&ob->shader_fx, panel->runtime.list_index);
fx->ui_expand_flag = expand_flag;
}
@ -126,7 +114,7 @@ void shaderfx_panel_get_property_pointers(const bContext *C,
PointerRNA *r_ob_ptr,
PointerRNA *r_md_ptr)
{
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
ShaderFxData *md = BLI_findlink(&ob->shader_fx, panel->runtime.list_index);
RNA_pointer_create(&ob->id, &RNA_ShaderFx, md, r_md_ptr);
@ -147,7 +135,7 @@ static void shaderfx_panel_header(const bContext *C, Panel *panel)
PointerRNA ptr;
shaderfx_panel_get_property_pointers(C, panel, NULL, &ptr);
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
ShaderFxData *fx = (ShaderFxData *)ptr.data;
const ShaderFxTypeInfo *fxti = BKE_shaderfx_get_info(fx->type);
@ -192,7 +180,7 @@ static void shaderfx_panel_header(const bContext *C, Panel *panel)
static bool shaderfx_ui_poll(const bContext *C, PanelType *UNUSED(pt))
{
Object *ob = get_context_object(C);
Object *ob = ED_object_active_context(C);
return (ob != NULL) && (ob->type == OB_GPENCIL);
}