Commit: b81aad9064d626d1d75873672774594fd9bb70a5 Author: Sybren A. Stüvel Date: Fri Oct 2 11:56:06 2020 +0200 Branches: temp-cleanup-screen-context https://developer.blender.org/rBb81aad9064d626d1d75873672774594fd9bb70a5
Cleanup: refactor `ed_screen_context()` to use hash lookups Refactor `ed_screen_context()` to use `GHash` lookups instead of a sequence of string comparisons, and move each context member into its own function. =================================================================== M source/blender/editors/screen/screen_context.c =================================================================== diff --git a/source/blender/editors/screen/screen_context.c b/source/blender/editors/screen/screen_context.c index 89d6befbb25..8ca10b7b493 100644 --- a/source/blender/editors/screen/screen_context.c +++ b/source/blender/editors/screen/screen_context.c @@ -37,6 +37,7 @@ #include "DNA_space_types.h" #include "DNA_windowmanager_types.h" +#include "BLI_ghash.h" #include "BLI_listbase.h" #include "BLI_utildefines.h" @@ -110,197 +111,242 @@ const char *screen_context_dir[] = { NULL, }; -int ed_screen_context(const bContext *C, const char *member, bContextDataResult *result) +/* Each function `screen_ctx_XXX()` will be called when the screen context "XXX" is requested. + * ensure_ed_screen_context_functions() is responsible for creating the hash map from context + * member name to function. + * + * Each function returns: + * 1 for "the member name was found and returned data is valid" + * 0 for "the member name was not found" + * -1 for "the member name was found but data is not available" + * + * */ + +static int screen_ctx_scene(const bContext *C, bContextDataResult *result) { wmWindow *win = CTX_wm_window(C); - View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */ - bScreen *screen = CTX_wm_screen(C); - ScrArea *area = CTX_wm_area(C); Scene *scene = WM_window_get_active_scene(win); + CTX_data_id_pointer_set(result, &scene->id); + return 1; +} +static int screen_ctx_visible_objects(const bContext *C, bContextDataResult *result) +{ + wmWindow *win = CTX_wm_window(C); + View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */ ViewLayer *view_layer = WM_window_get_active_view_layer(win); - Object *obact = view_layer->basact ? view_layer->basact->object : NULL; - Object *obedit = OBEDIT_FROM_VIEW_LAYER(view_layer); - if (CTX_data_dir(member)) { - CTX_data_dir_set(result, screen_context_dir); - return 1; - } - if (CTX_data_equals(member, "scene")) { - CTX_data_id_pointer_set(result, &scene->id); - return 1; - } - if (CTX_data_equals(member, "visible_objects")) { - LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { - if (BASE_VISIBLE(v3d, base)) { - CTX_data_id_list_add(result, &base->object->id); - } + LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { + if (BASE_VISIBLE(v3d, base)) { + CTX_data_id_list_add(result, &base->object->id); } - CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); - return 1; } - if (CTX_data_equals(member, "selectable_objects")) { - LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { - if (BASE_SELECTABLE(v3d, base)) { - CTX_data_id_list_add(result, &base->object->id); - } + CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); + return 1; +} +static int screen_ctx_selectable_objects(const bContext *C, bContextDataResult *result) +{ + wmWindow *win = CTX_wm_window(C); + View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */ + ViewLayer *view_layer = WM_window_get_active_view_layer(win); + + LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { + if (BASE_SELECTABLE(v3d, base)) { + CTX_data_id_list_add(result, &base->object->id); } - CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); - return 1; } - if (CTX_data_equals(member, "selected_objects")) { - LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { - if (BASE_SELECTED(v3d, base)) { - CTX_data_id_list_add(result, &base->object->id); - } + CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); + return 1; +} +static int screen_ctx_selected_objects(const bContext *C, bContextDataResult *result) +{ + wmWindow *win = CTX_wm_window(C); + View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */ + ViewLayer *view_layer = WM_window_get_active_view_layer(win); + + LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { + if (BASE_SELECTED(v3d, base)) { + CTX_data_id_list_add(result, &base->object->id); } - CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); - return 1; } - if (CTX_data_equals(member, "selected_editable_objects")) { - LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { - if (BASE_SELECTED_EDITABLE(v3d, base)) { - CTX_data_id_list_add(result, &base->object->id); - } + CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); + return 1; +} +static int screen_ctx_selected_editable_objects(const bContext *C, bContextDataResult *result) +{ + wmWindow *win = CTX_wm_window(C); + View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */ + ViewLayer *view_layer = WM_window_get_active_view_layer(win); + + LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { + if (BASE_SELECTED_EDITABLE(v3d, base)) { + CTX_data_id_list_add(result, &base->object->id); } - CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); - return 1; } - if (CTX_data_equals(member, "editable_objects")) { - /* Visible + Editable, but not necessarily selected */ - LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { - if (BASE_EDITABLE(v3d, base)) { - CTX_data_id_list_add(result, &base->object->id); - } + CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); + return 1; +} +static int screen_ctx_editable_objects(const bContext *C, bContextDataResult *result) +{ + wmWindow *win = CTX_wm_window(C); + View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */ + ViewLayer *view_layer = WM_window_get_active_view_layer(win); + + /* Visible + Editable, but not necessarily selected */ + LISTBASE_FOREACH (Base *, base, &view_layer->object_bases) { + if (BASE_EDITABLE(v3d, base)) { + CTX_data_id_list_add(result, &base->object->id); } - CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); - return 1; } - if (CTX_data_equals(member, "objects_in_mode")) { - if (obact && (obact->mode != OB_MODE_OBJECT)) { - FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) { - CTX_data_id_list_add(result, &ob_iter->id); - } - FOREACH_OBJECT_IN_MODE_END; + CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); + return 1; +} +static int screen_ctx_objects_in_mode(const bContext *C, bContextDataResult *result) +{ + wmWindow *win = CTX_wm_window(C); + View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */ + ViewLayer *view_layer = WM_window_get_active_view_layer(win); + Object *obact = view_layer->basact ? view_layer->basact->object : NULL; + + if (obact && (obact->mode != OB_MODE_OBJECT)) { + FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) { + CTX_data_id_list_add(result, &ob_iter->id); } - CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); - return 1; + FOREACH_OBJECT_IN_MODE_END; } - if (CTX_data_equals(member, "objects_in_mode_unique_data")) { - if (obact && (obact->mode != OB_MODE_OBJECT)) { - FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) { - ob_iter->id.tag |= LIB_TAG_DOIT; - } - FOREACH_OBJECT_IN_MODE_END; - FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) { - if (ob_iter->id.tag & LIB_TAG_DOIT) { - ob_iter->id.tag &= ~LIB_TAG_DOIT; - CTX_data_id_list_add(result, &ob_iter->id); - } + CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); + return 1; +} +static int screen_ctx_objects_in_mode_unique_data(const bContext *C, bContextDataResult *result) +{ + wmWindow *win = CTX_wm_window(C); + View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */ + ViewLayer *view_layer = WM_window_get_active_view_layer(win); + Object *obact = view_layer->basact ? view_layer->basact->object : NULL; + + if (obact && (obact->mode != OB_MODE_OBJECT)) { + FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) { + ob_iter->id.tag |= LIB_TAG_DOIT; + } + FOREACH_OBJECT_IN_MODE_END; + FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) { + if (ob_iter->id.tag & LIB_TAG_DOIT) { + ob_iter->id.tag &= ~LIB_TAG_DOIT; + CTX_data_id_list_add(result, &ob_iter->id); } - FOREACH_OBJECT_IN_MODE_END; } - CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); - return 1; + FOREACH_OBJECT_IN_MODE_END; } - if (CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) { - bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : NULL; - EditBone *flipbone = NULL; - const bool editable_bones = CTX_data_equals(member, "editable_bones"); - - if (arm && arm->edbo) { - uint objects_len; - Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data( - view_layer, CTX_wm_view3d(C), &objects_len); - for (uint i = 0; i < objects_len; i++) { - Object *ob = objects[i]; - arm = ob->data; - - /* Attention: X-Axis Mirroring is also handled here... */ - LISTBASE_FOREACH (EditBone *, ebone, arm->edbo) { - /* first and foremost, bone must be visible and selected */ - if (EBONE_VISIBLE(arm, ebone)) { - /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled - * so that most users of this data don't need to explicitly check for it themselves. - * - * We need to make sure that these mirrored copies are not selected, otherwise some - * bones will be operated on twice. - */ - if (arm->flag & ARM_MIRROR_EDIT) { - flipbone = ED_armature_ebone_get_mirrored(arm->edbo, ebone); - } + CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION); + return 1; +} +static int screen_ctx_visible_or_editable_bones_(const bContext *C, + bContextDataResult *result, + const bool editable_bones) +{ + wmWindow *win = CTX_wm_window(C); + ViewLayer *view_layer = WM_window_get_active @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org https://lists.blender.org/mailman/listinfo/bf-blender-cvs