Commit: ab72406dc347b428b5ef6981afd1ca45781f0ae6
Author: Sybren A. Stüvel
Date:   Fri Oct 2 11:56:06 2020 +0200
Branches: master
https://developer.blender.org/rBab72406dc347b428b5ef6981afd1ca45781f0ae6

Cleanup: split up `ed_screen_context()` into separate functions

Refactor `ed_screen_context()` to call separate functions, instead of
having the entire functionality in one function. Each function now only
retrieves the data it needs from the context. Furthermore, some string
comparisons are removed.

No functional changes.

Reviwed by: brecht

Differential Revision: https://developer.blender.org/D9090

===================================================================

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..84f16e8667d 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,669 +111,1016 @@ 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);
+  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);
+
+  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;
+}
+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;
+}
+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;
+}
+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;
+}
+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;
+}
+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);
+    }
+    FOREACH_OBJECT_IN_MODE_END;
+  }
+  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;
+}
+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_view_layer(win);
+  Object *obedit = OBEDIT_FROM_VIEW_LAYER(view_layer);
+
+  bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : 
NULL;
+  EditBone *flipbone = NULL;
+
+  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);
+          }
+
+          /* if we're filtering for editable too, use the check for that 
instead,
+           * as it has selection check too */
+          if (editable_bones) {
+            /* only selected + editable */
+            if (EBONE_EDITABLE(ebone)) {
+              CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
+
+              if ((flipbone) && !(flipbone->flag & BONE_SELECTED)) {
+                CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
+              }
+            }
+          }
+          else {
+            /* only include bones if visible */
+            CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
+
+            if ((flipbone) && EBONE_VISIBLE(arm, flipbone) == 0) {
+              CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
+            }
+          }
+        }
+      }
+    }
+    MEM_freeN(objects);
+
+    CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
+    return 1;
+  }
+  return -1; /* found but not available */
+}
+static int screen_ctx_visible_bones(const bContext *C, bContextDataResult 
*result)
+{
+  return screen_ctx_visible_or_editable_bones_(C, result, false);
+}
+static int screen_ctx_editable_bones(const bContext *C, bContextDataResult 
*result)
+{
+  return screen_ctx_visible_or_editable_bones_(C, result, true);
+}
+static int screen_ctx_selected_bones_(const bContext *C,
+                                      bContextDataResult *result,
+                                      const bool selected_editable_bones)
+{
+  wmWindow *win = CTX_wm_window(C);
+  ViewLayer *view_layer = WM_window_get_active_view_layer(win);
+  Object *obedit = OBEDIT_FROM_VIEW_LAYER(view_layer);
+  bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : 
NULL;
+  EditBone *flipbone = NULL;
+
+  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) && (ebone->flag & BONE_SELECTED)) {
+          /* 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);
+          }
+
+          /* if we're filtering for editable too, use the check for that 
instead,
+           * as it has selection check too */
+          if (selected_editable_bones) {
+            /* only selected + editable */
+            if (EBONE_EDITABLE(ebone)) {
+              CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
+
+              if ((flipbone) && !(flipbo

@@ 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

Reply via email to