Merge branch 'master' into blender2.8
[blender.git] / source / blender / editors / transform / transform_orientations.c
index f3078eb7824beaf4f3615c8e17fb9890a16c74a7..f8b11a0bcae4ec83705144f1dbb2a6c7c894e2cf 100644 (file)
@@ -38,6 +38,7 @@
 #include "DNA_screen_types.h"
 #include "DNA_space_types.h"
 #include "DNA_view3d_types.h"
+#include "DNA_workspace_types.h"
 
 #include "BLI_math.h"
 #include "BLI_listbase.h"
@@ -52,6 +53,7 @@
 #include "BKE_report.h"
 #include "BKE_main.h"
 #include "BKE_screen.h"
+#include "BKE_workspace.h"
 
 #include "BLT_translation.h"
 
 
 void BIF_clearTransformOrientation(bContext *C)
 {
+       WorkSpace *workspace = CTX_wm_workspace(C);
+       ListBase *transform_orientations = BKE_workspace_transform_orientations_get(workspace);
        View3D *v3d = CTX_wm_view3d(C);
 
-       ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
-       BLI_freelistN(transform_spaces);
+       BLI_freelistN(transform_orientations);
        
        // Need to loop over all view3d
-       if (v3d && v3d->twmode >= V3D_MANIP_CUSTOM) {
-               v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global   */
+       if (v3d && v3d->twmode == V3D_MANIP_CUSTOM) {
+               v3d->twmode = V3D_MANIP_GLOBAL; /* fallback to global */
+               v3d->custom_orientation_index = -1;
        }
 }
 
@@ -318,23 +322,24 @@ void BIF_createTransformOrientation(bContext *C, ReportList *reports,
 TransformOrientation *addMatrixSpace(bContext *C, float mat[3][3],
                                      const char *name, const bool overwrite)
 {
-       ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
        TransformOrientation *ts = NULL;
+       WorkSpace *workspace = CTX_wm_workspace(C);
+       ListBase *transform_orientations = BKE_workspace_transform_orientations_get(workspace);
        char name_unique[sizeof(ts->name)];
 
        if (overwrite) {
-               ts = findOrientationName(transform_spaces, name);
+               ts = findOrientationName(transform_orientations, name);
        }
        else {
                BLI_strncpy(name_unique, name, sizeof(name_unique));
-               uniqueOrientationName(transform_spaces, name_unique);
+               uniqueOrientationName(transform_orientations, name_unique);
                name = name_unique;
        }
 
        /* if not, create a new one */
        if (ts == NULL) {
                ts = MEM_callocN(sizeof(TransformOrientation), "UserTransSpace from matrix");
-               BLI_addtail(transform_spaces, ts);
+               BLI_addtail(transform_orientations, ts);
                BLI_strncpy(ts->name, name, sizeof(ts->name));
        }
 
@@ -346,70 +351,55 @@ TransformOrientation *addMatrixSpace(bContext *C, float mat[3][3],
 
 void BIF_removeTransformOrientation(bContext *C, TransformOrientation *target)
 {
-       Scene *scene = CTX_data_scene(C);
-       ListBase *transform_spaces = &scene->transform_spaces;
-       const int i = BLI_findindex(transform_spaces, target);
-
-       if (i != -1) {
-               Main *bmain = CTX_data_main(C);
-               BKE_screen_view3d_main_twmode_remove(&bmain->screen, scene, i);
-               BLI_freelinkN(transform_spaces, target);
-       }
+       BKE_workspace_transform_orientation_remove(CTX_wm_workspace(C), target);
 }
 
 void BIF_removeTransformOrientationIndex(bContext *C, int index)
 {
-       ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
-       TransformOrientation *ts = BLI_findlink(transform_spaces, index);
-
-       if (ts) {
-               BIF_removeTransformOrientation(C, ts);
-       }
+       TransformOrientation *target = BKE_workspace_transform_orientation_find(CTX_wm_workspace(C), index);
+       BIF_removeTransformOrientation(C, target);
 }
 
 void BIF_selectTransformOrientation(bContext *C, TransformOrientation *target)
 {
-       ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
-       const int i = BLI_findindex(transform_spaces, target);
+       int index = BKE_workspace_transform_orientation_get_index(CTX_wm_workspace(C), target);
+       View3D *v3d = CTX_wm_view3d(C);
 
-       if (i != -1) {
-               View3D *v3d = CTX_wm_view3d(C);
-               v3d->twmode = V3D_MANIP_CUSTOM + i;
-       }
+       BLI_assert(index != -1);
+
+       v3d->twmode = V3D_MANIP_CUSTOM;
+       v3d->custom_orientation_index = index;
 }
 
-void BIF_selectTransformOrientationValue(bContext *C, int orientation)
+/**
+ * Activate a transform orientation in a 3D view based on an enum value.
+ *
+ * \param orientation: If this is #V3D_MANIP_CUSTOM or greater, the custom transform orientation
+ *                     with index \a orientation - #V3D_MANIP_CUSTOM gets activated.
+ */
+void BIF_selectTransformOrientationValue(View3D *v3d, int orientation)
 {
-       View3D *v3d = CTX_wm_view3d(C);
-       if (v3d) /* currently using generic poll */
-               v3d->twmode = orientation;
+       const bool is_custom = orientation >= V3D_MANIP_CUSTOM;
+
+       v3d->twmode = is_custom ? V3D_MANIP_CUSTOM : orientation;
+       v3d->custom_orientation_index = is_custom ? (orientation - V3D_MANIP_CUSTOM) : -1;
 }
 
 int BIF_countTransformOrientation(const bContext *C)
 {
-       ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
-       return BLI_listbase_count(transform_spaces);
+       WorkSpace *workspace = CTX_wm_workspace(C);
+       ListBase *transform_orientations = BKE_workspace_transform_orientations_get(workspace);
+       return BLI_listbase_count(transform_orientations);
 }
 
-bool applyTransformOrientation(const bContext *C, float mat[3][3], char *r_name, int index)
+bool applyTransformOrientation(const TransformOrientation *ts, float r_mat[3][3], char *r_name)
 {
-       ListBase *transform_spaces = &CTX_data_scene(C)->transform_spaces;
-       TransformOrientation *ts = BLI_findlink(transform_spaces, index);
-
-       BLI_assert(index >= 0);
-
-       if (ts) {
-               if (r_name) {
-                       BLI_strncpy(r_name, ts->name, MAX_NAME);
-               }
-
-               copy_m3_m3(mat, ts->mat);
-               return true;
-       }
-       else {
-               /* invalid index, can happen sometimes */
-               return false;
+       if (r_name) {
+               BLI_strncpy(r_name, ts->name, MAX_NAME);
        }
+       copy_m3_m3(r_mat, ts->mat);
+
+       return true;
 }
 
 static int count_bone_select(bArmature *arm, ListBase *lb, const bool do_it)
@@ -492,8 +482,10 @@ void initTransformOrientation(bContext *C, TransInfo *t)
                                unit_m3(t->spacemtx);
                        }
                        break;
-               default: /* V3D_MANIP_CUSTOM */
-                       if (applyTransformOrientation(C, t->spacemtx, t->spacename, t->current_orientation - V3D_MANIP_CUSTOM)) {
+               case V3D_MANIP_CUSTOM:
+                       BLI_strncpy(t->spacename, t->custom_orientation->name, sizeof(t->spacename));
+
+                       if (applyTransformOrientation(t->custom_orientation, t->spacemtx, t->spacename)) {
                                /* pass */
                        }
                        else {
@@ -586,10 +578,10 @@ static unsigned int bm_mesh_faces_select_get_n(BMesh *bm, BMVert **elems, const
 
 int getTransformOrientation_ex(const bContext *C, float normal[3], float plane[3], const short around)
 {
-       Scene *scene = CTX_data_scene(C);
+       ViewLayer *view_layer = CTX_data_view_layer(C);
        Object *obedit = CTX_data_edit_object(C);
        Base *base;
-       Object *ob = OBACT;
+       Object *ob = OBACT(view_layer);
        int result = ORIENTATION_NONE;
        const bool activeOnly = (around == V3D_AROUND_ACTIVE);
 
@@ -1061,16 +1053,16 @@ int getTransformOrientation_ex(const bContext *C, float normal[3], float plane[3
        }
        else {
                /* we need the one selected object, if its not active */
-               View3D *v3d = CTX_wm_view3d(C);
-               ob = OBACT;
-               if (ob && (ob->flag & SELECT)) {
+               base = BASACT(view_layer);
+               ob = OBACT(view_layer);
+               if (base && ((base->flag & BASE_SELECTED) != 0)) {
                        /* pass */
                }
                else {
                        /* first selected */
                        ob = NULL;
-                       for (base = scene->base.first; base; base = base->next) {
-                               if (TESTBASELIB(v3d, base)) {
+                       for (base = view_layer->object_bases.first; base; base = base->next) {
+                               if (TESTBASELIB(base)) {
                                        ob = base->object;
                                        break;
                                }