BKE_screen: add BKE_screen_find_area_xy
authorCampbell Barton <ideasman42@gmail.com>
Wed, 21 Jan 2015 02:43:46 +0000 (13:43 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 21 Jan 2015 02:43:46 +0000 (13:43 +1100)
Use from eyedropper & screen operators
also define SPACE_TYPE_ANY for readability.

source/blender/blenkernel/BKE_screen.h
source/blender/blenkernel/intern/screen.c
source/blender/editors/interface/interface_eyedropper.c
source/blender/editors/render/render_view.c
source/blender/editors/screen/screen_ops.c
source/blender/makesdna/DNA_space_types.h

index 4c11ec9923d51dd978a6d754ec88e3f237787490..3eb6ba7b6faf677c155d6ed9b40946a5c4d6965c 100644 (file)
@@ -265,7 +265,7 @@ struct SpaceType *BKE_spacetype_from_id(int spaceid);
 struct ARegionType *BKE_regiontype_from_id(struct SpaceType *st, int regionid);
 const struct ListBase *BKE_spacetypes_list(void);
 void BKE_spacetype_register(struct SpaceType *st);
-int BKE_spacetype_exists(int spaceid);
+bool BKE_spacetype_exists(int spaceid);
 void BKE_spacetypes_free(void); /* only for quitting blender */
 
 /* spacedata */
@@ -282,6 +282,7 @@ struct ARegion *BKE_area_find_region_type(struct ScrArea *sa, int type);
 struct ARegion *BKE_area_find_region_active_win(struct ScrArea *sa);
 struct ScrArea *BKE_screen_find_area_from_space(struct bScreen *sc, struct SpaceLink *sl) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1, 2);
 struct ScrArea *BKE_screen_find_big_area(struct bScreen *sc, const int spacetype, const short min);
+struct ScrArea *BKE_screen_find_area_xy(struct bScreen *sc, const int spacetype, int x, int y);
 
 unsigned int BKE_screen_view3d_layer_active_ex(
         const struct View3D *v3d, const struct Scene *scene, bool use_localvd) ATTR_NONNULL(2);
index ad4ed5a0b99c4c57013041938929ed3b415488e5..c9dba38b713e238655f893a3913bc67eacdf9d67 100644 (file)
@@ -46,6 +46,7 @@
 
 #include "BLI_listbase.h"
 #include "BLI_utildefines.h"
+#include "BLI_rect.h"
 
 #include "BKE_idprop.h"
 #include "BKE_screen.h"
@@ -137,7 +138,7 @@ void BKE_spacetype_register(SpaceType *st)
        BLI_addtail(&spacetypes, st);
 }
 
-int BKE_spacetype_exists(int spaceid)
+bool BKE_spacetype_exists(int spaceid)
 {
        return BKE_spacetype_from_id(spaceid) != NULL;
 }
@@ -419,16 +420,17 @@ ScrArea *BKE_screen_find_area_from_space(struct bScreen *sc, SpaceLink *sl)
        return sa;
 }
 
-/* note, using this function is generally a last resort, you really want to be
+/**
+ * \note Using this function is generally a last resort, you really want to be
  * using the context when you can - campbell
- * -1 for any type */
+ */
 ScrArea *BKE_screen_find_big_area(bScreen *sc, const int spacetype, const short min)
 {
        ScrArea *sa, *big = NULL;
        int size, maxsize = 0;
 
        for (sa = sc->areabase.first; sa; sa = sa->next) {
-               if ((spacetype == -1) || sa->spacetype == spacetype) {
+               if ((spacetype == SPACE_TYPE_ANY) || (sa->spacetype == spacetype)) {
                        if (min <= sa->winx && min <= sa->winy) {
                                size = sa->winx * sa->winy;
                                if (size > maxsize) {
@@ -442,6 +444,22 @@ ScrArea *BKE_screen_find_big_area(bScreen *sc, const int spacetype, const short
        return big;
 }
 
+ScrArea *BKE_screen_find_area_xy(bScreen *sc, const int spacetype, int x, int y)
+{
+       ScrArea *sa, *sa_found = NULL;
+
+       for (sa = sc->areabase.first; sa; sa = sa->next) {
+               if (BLI_rcti_isect_pt(&sa->totrct, x, y)) {
+                       if ((spacetype == SPACE_TYPE_ANY) || (sa->spacetype == spacetype)) {
+                               sa_found = sa;
+                       }
+                       break;
+               }
+       }
+       return sa_found;
+}
+
+
 /**
  * Utility function to get the active layer to use when adding new objects.
  */
index ec1234a82bed1e46d334c8ce3e099a8575b13fcb..f5b24f49f989a3a46d61fa302a291759d0782e77 100644 (file)
@@ -172,43 +172,42 @@ static void eyedropper_color_sample_fl(bContext *C, Eyedropper *UNUSED(eye), int
 
        /* we could use some clever */
        wmWindow *win = CTX_wm_window(C);
-       ScrArea *sa;
-       for (sa = win->screen->areabase.first; sa; sa = sa->next) {
-               if (BLI_rcti_isect_pt(&sa->totrct, mx, my)) {
-                       if (sa->spacetype == SPACE_IMAGE) {
-                               ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
-                               if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
-                                       SpaceImage *sima = sa->spacedata.first;
-                                       int mval[2] = {mx - ar->winrct.xmin,
-                                                      my - ar->winrct.ymin};
-
-                                       if (ED_space_image_color_sample(CTX_data_scene(C), sima, ar, mval, r_col)) {
-                                               return;
-                                       }
+       ScrArea *sa = BKE_screen_find_area_xy(win->screen, SPACE_TYPE_ANY, mx, my);
+
+       if (sa) {
+               if (sa->spacetype == SPACE_IMAGE) {
+                       ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
+                       if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
+                               SpaceImage *sima = sa->spacedata.first;
+                               int mval[2] = {mx - ar->winrct.xmin,
+                                              my - ar->winrct.ymin};
+
+                               if (ED_space_image_color_sample(CTX_data_scene(C), sima, ar, mval, r_col)) {
+                                       return;
                                }
                        }
-                       else if (sa->spacetype == SPACE_NODE) {
-                               ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
-                               if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
-                                       SpaceNode *snode = sa->spacedata.first;
-                                       int mval[2] = {mx - ar->winrct.xmin,
-                                                      my - ar->winrct.ymin};
-
-                                       if (ED_space_node_color_sample(CTX_data_scene(C), snode, ar, mval, r_col)) {
-                                               return;
-                                       }
+               }
+               else if (sa->spacetype == SPACE_NODE) {
+                       ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
+                       if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
+                               SpaceNode *snode = sa->spacedata.first;
+                               int mval[2] = {mx - ar->winrct.xmin,
+                                              my - ar->winrct.ymin};
+
+                               if (ED_space_node_color_sample(CTX_data_scene(C), snode, ar, mval, r_col)) {
+                                       return;
                                }
                        }
-                       else if (sa->spacetype == SPACE_CLIP) {
-                               ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
-                               if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
-                                       SpaceClip *sc = sa->spacedata.first;
-                                       int mval[2] = {mx - ar->winrct.xmin,
-                                                      my - ar->winrct.ymin};
-
-                                       if (ED_space_clip_color_sample(CTX_data_scene(C), sc, ar, mval, r_col)) {
-                                               return;
-                                       }
+               }
+               else if (sa->spacetype == SPACE_CLIP) {
+                       ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
+                       if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
+                               SpaceClip *sc = sa->spacedata.first;
+                               int mval[2] = {mx - ar->winrct.xmin,
+                                              my - ar->winrct.ymin};
+
+                               if (ED_space_clip_color_sample(CTX_data_scene(C), sc, ar, mval, r_col)) {
+                                       return;
                                }
                        }
                }
@@ -473,53 +472,49 @@ static void datadropper_id_sample_pt(bContext *C, DataDropper *ddr, int mx, int
 
        /* we could use some clever */
        wmWindow *win = CTX_wm_window(C);
-       ScrArea *sa;
+       ScrArea *sa = BKE_screen_find_area_xy(win->screen, -1, mx, my);
 
        ScrArea *area_prev = CTX_wm_area(C);
        ARegion *ar_prev = CTX_wm_region(C);
 
        ddr->name[0] = '\0';
 
-       for (sa = win->screen->areabase.first; sa; sa = sa->next) {
-               if (BLI_rcti_isect_pt(&sa->totrct, mx, my)) {
-                       if (sa->spacetype == SPACE_VIEW3D) {
-                               ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
-                               if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
-                                       const int mval[2] = {
-                                           mx - ar->winrct.xmin,
-                                           my - ar->winrct.ymin};
-                                       Base *base;
-
-                                       CTX_wm_area_set(C, sa);
-                                       CTX_wm_region_set(C, ar);
-
-                                       /* grr, always draw else we leave stale text */
-                                       ED_region_tag_redraw(ar);
-
-                                       base = ED_view3d_give_base_under_cursor(C, mval);
-                                       if (base) {
-                                               Object *ob = base->object;
-                                               ID *id = NULL;
-                                               if (ddr->idcode == ID_OB) {
-                                                       id = (ID *)ob;
+       if (sa) {
+               if (sa->spacetype == SPACE_VIEW3D) {
+                       ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
+                       if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
+                               const int mval[2] = {
+                                   mx - ar->winrct.xmin,
+                                   my - ar->winrct.ymin};
+                               Base *base;
+
+                               CTX_wm_area_set(C, sa);
+                               CTX_wm_region_set(C, ar);
+
+                               /* grr, always draw else we leave stale text */
+                               ED_region_tag_redraw(ar);
+
+                               base = ED_view3d_give_base_under_cursor(C, mval);
+                               if (base) {
+                                       Object *ob = base->object;
+                                       ID *id = NULL;
+                                       if (ddr->idcode == ID_OB) {
+                                               id = (ID *)ob;
+                                       }
+                                       else if (ob->data) {
+                                               if (GS(((ID *)ob->data)->name) == ddr->idcode) {
+                                                       id = (ID *)ob->data;
                                                }
-                                               else if (ob->data) {
-                                                       if (GS(((ID *)ob->data)->name) == ddr->idcode) {
-                                                               id = (ID *)ob->data;
-                                                       }
-                                                       else {
-                                                               BLI_snprintf(ddr->name, sizeof(ddr->name), "Incompatible, expected a %s",
-                                                                            ddr->idcode_name);
-                                                       }
-                                               }
-
-                                               if (id) {
-                                                       BLI_snprintf(ddr->name, sizeof(ddr->name), "%s: %s",
-                                                                    ddr->idcode_name, id->name + 2);
-                                                       *r_id = id;
+                                               else {
+                                                       BLI_snprintf(ddr->name, sizeof(ddr->name), "Incompatible, expected a %s",
+                                                                    ddr->idcode_name);
                                                }
+                                       }
 
-                                               break;
+                                       if (id) {
+                                               BLI_snprintf(ddr->name, sizeof(ddr->name), "%s: %s",
+                                                            ddr->idcode_name, id->name + 2);
+                                               *r_id = id;
                                        }
                                }
                        }
@@ -756,7 +751,7 @@ static void depthdropper_depth_sample_pt(bContext *C, DepthDropper *ddr, int mx,
 
        /* we could use some clever */
        wmWindow *win = CTX_wm_window(C);
-       ScrArea *sa;
+       ScrArea *sa = BKE_screen_find_area_xy(win->screen, SPACE_TYPE_ANY, mx, my);
        Scene *scene = win->screen->scene;
        UnitSettings *unit = &scene->unit;
        const bool do_split = (unit->flag & USER_UNIT_OPT_SPLIT) != 0;
@@ -766,47 +761,44 @@ static void depthdropper_depth_sample_pt(bContext *C, DepthDropper *ddr, int mx,
 
        ddr->name[0] = '\0';
 
-       for (sa = win->screen->areabase.first; sa; sa = sa->next) {
-               if (BLI_rcti_isect_pt(&sa->totrct, mx, my)) {
-                       if (sa->spacetype == SPACE_VIEW3D) {
-                               ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
-                               if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
-                                       View3D *v3d = sa->spacedata.first;
-                                       RegionView3D *rv3d = ar->regiondata;
-                                       /* weak, we could pass in some reference point */
-                                       const float *view_co = v3d->camera ? v3d->camera->obmat[3] : rv3d->viewinv[3];
-                                       const int mval[2] = {
-                                           mx - ar->winrct.xmin,
-                                           my - ar->winrct.ymin};
-                                       float co[3];
-
-                                       CTX_wm_area_set(C, sa);
-                                       CTX_wm_region_set(C, ar);
-
-                                       /* grr, always draw else we leave stale text */
-                                       ED_region_tag_redraw(ar);
-
-                                       view3d_operator_needs_opengl(C);
-
-                                       if (ED_view3d_autodist(scene, ar, v3d, mval, co, true, NULL)) {
-                                               const float mval_center_fl[2] = {
-                                                   (float)ar->winx / 2,
-                                                   (float)ar->winy / 2};
-                                               float co_align[3];
-
-                                               /* quick way to get view-center aligned point */
-                                               ED_view3d_win_to_3d(ar, co, mval_center_fl, co_align);
-
-                                               *r_depth = len_v3v3(view_co, co_align);
-
-                                               bUnit_AsString(ddr->name, sizeof(ddr->name),
-                                                              (double)*r_depth,
-                                                              4, unit->system, B_UNIT_LENGTH, do_split, false);
-                                       }
-                                       else {
-                                               BLI_strncpy(ddr->name, "Nothing under cursor", sizeof(ddr->name));
-                                       }
-                                       break;
+       if (sa) {
+               if (sa->spacetype == SPACE_VIEW3D) {
+                       ARegion *ar = BKE_area_find_region_type(sa, RGN_TYPE_WINDOW);
+                       if (ar && BLI_rcti_isect_pt(&ar->winrct, mx, my)) {
+                               View3D *v3d = sa->spacedata.first;
+                               RegionView3D *rv3d = ar->regiondata;
+                               /* weak, we could pass in some reference point */
+                               const float *view_co = v3d->camera ? v3d->camera->obmat[3] : rv3d->viewinv[3];
+                               const int mval[2] = {
+                                   mx - ar->winrct.xmin,
+                                   my - ar->winrct.ymin};
+                               float co[3];
+
+                               CTX_wm_area_set(C, sa);
+                               CTX_wm_region_set(C, ar);
+
+                               /* grr, always draw else we leave stale text */
+                               ED_region_tag_redraw(ar);
+
+                               view3d_operator_needs_opengl(C);
+
+                               if (ED_view3d_autodist(scene, ar, v3d, mval, co, true, NULL)) {
+                                       const float mval_center_fl[2] = {
+                                           (float)ar->winx / 2,
+                                           (float)ar->winy / 2};
+                                       float co_align[3];
+
+                                       /* quick way to get view-center aligned point */
+                                       ED_view3d_win_to_3d(ar, co, mval_center_fl, co_align);
+
+                                       *r_depth = len_v3v3(view_co, co_align);
+
+                                       bUnit_AsString(ddr->name, sizeof(ddr->name),
+                                                      (double)*r_depth,
+                                                      4, unit->system, B_UNIT_LENGTH, do_split, false);
+                               }
+                               else {
+                                       BLI_strncpy(ddr->name, "Nothing under cursor", sizeof(ddr->name));
                                }
                        }
                }
index ab28f5fa675d3ad2d1d4d310be7caf0549da6b9c..f7399d4e6cb0f7ce8d2f1dbbbeb4b895ade8079f 100644 (file)
@@ -189,7 +189,7 @@ ScrArea *render_view_open(bContext *C, int mx, int my)
                        }
                        else {
                                /* use any area of decent size */
-                               sa = BKE_screen_find_big_area(CTX_wm_screen(C), -1, 0);
+                               sa = BKE_screen_find_big_area(CTX_wm_screen(C), SPACE_TYPE_ANY, 0);
                                if (sa->spacetype != SPACE_IMAGE) {
                                        // XXX newspace(sa, SPACE_IMAGE);
                                        sima = sa->spacedata.first;
index 39321ec07702f8e8f5225f1e3be3854e0832bba2..0ab04c61ac37786aec1722e40dd19d8f89192e8c 100644 (file)
@@ -598,19 +598,6 @@ typedef struct sActionzoneData {
        int x, y, gesture_dir, modifier;
 } sActionzoneData;
 
-/* used by other operators too */
-static ScrArea *screen_areahascursor(bScreen *scr, int x, int y)
-{
-       ScrArea *sa = NULL;
-       sa = scr->areabase.first;
-       while (sa) {
-               if (BLI_rcti_isect_pt(&sa->totrct, x, y)) break;
-               sa = sa->next;
-       }
-       
-       return sa;
-}
-
 /* quick poll to save operators to be created and handled */
 static int actionzone_area_poll(bContext *C)
 {
@@ -808,7 +795,7 @@ static int actionzone_modal(bContext *C, wmOperator *op, const wmEvent *event)
                        /* gesture is large enough? */
                        if (is_gesture) {
                                /* second area, for join when (sa1 != sa2) */
-                               sad->sa2 = screen_areahascursor(sc, event->x, event->y);
+                               sad->sa2 = BKE_screen_find_area_xy(sc, SPACE_TYPE_ANY, event->x, event->y);
                                /* apply sends event */
                                actionzone_apply(C, op, sad->az->type);
                                actionzone_exit(op);
@@ -929,7 +916,7 @@ static int area_swap_modal(bContext *C, wmOperator *op, const wmEvent *event)
        switch (event->type) {
                case MOUSEMOVE:
                        /* second area, for join */
-                       sad->sa2 = screen_areahascursor(CTX_wm_screen(C), event->x, event->y);
+                       sad->sa2 = BKE_screen_find_area_xy(CTX_wm_screen(C), SPACE_TYPE_ANY, event->x, event->y);
                        break;
                case LEFTMOUSE: /* release LMB */
                        if (event->val == KM_RELEASE) {
@@ -1679,7 +1666,8 @@ static int area_split_modal(bContext *C, wmOperator *op, const wmEvent *event)
                                        sd->sarea->flag &= ~(AREA_FLAG_DRAWSPLIT_H | AREA_FLAG_DRAWSPLIT_V);
                                        ED_area_tag_redraw(sd->sarea);
                                }
-                               sd->sarea = screen_areahascursor(CTX_wm_screen(C), event->x, event->y);  /* area context not set */
+                               /* area context not set */
+                               sd->sarea = BKE_screen_find_area_xy(CTX_wm_screen(C), SPACE_TYPE_ANY, event->x, event->y);
                                
                                if (sd->sarea) {
                                        ED_area_tag_redraw(sd->sarea);
@@ -2482,8 +2470,8 @@ static int area_join_init(bContext *C, wmOperator *op)
        x2 = RNA_int_get(op->ptr, "max_x");
        y2 = RNA_int_get(op->ptr, "max_y");
        
-       sa1 = screen_areahascursor(CTX_wm_screen(C), x1, y1);
-       sa2 = screen_areahascursor(CTX_wm_screen(C), x2, y2);
+       sa1 = BKE_screen_find_area_xy(CTX_wm_screen(C), SPACE_TYPE_ANY, x1, y1);
+       sa2 = BKE_screen_find_area_xy(CTX_wm_screen(C), SPACE_TYPE_ANY, x2, y2);
        if (sa1 == NULL || sa2 == NULL || sa1 == sa2)
                return 0;
        
@@ -2616,7 +2604,7 @@ static int area_join_modal(bContext *C, wmOperator *op, const wmEvent *event)
                        
                case MOUSEMOVE: 
                {
-                       ScrArea *sa = screen_areahascursor(sc, event->x, event->y);
+                       ScrArea *sa = BKE_screen_find_area_xy(sc, SPACE_TYPE_ANY, event->x, event->y);
                        int dir;
                        
                        if (sa) {
index 723964364f6c5d5a9474ff263283a5e425e62936..4a73ff39a6e93dd47ff0d5cc1cdd0b51f11b150c 100644 (file)
@@ -1202,6 +1202,9 @@ typedef enum eSpace_Type {
        SPACEICONMAX = SPACE_CLIP
 } eSpace_Type;
 
+/* use for function args */
+#define SPACE_TYPE_ANY -1
+
 // TODO: SPACE_SCRIPT
 #if (DNA_DEPRECATED_GCC_POISON == 1)
 #pragma GCC poison SPACE_IMASEL SPACE_SOUND