diff options
author | Joshua Leung <aligorith@gmail.com> | 2008-12-31 13:44:00 +0300 |
---|---|---|
committer | Joshua Leung <aligorith@gmail.com> | 2008-12-31 13:44:00 +0300 |
commit | 28d0bab8ed913629fc02463156bb8fade7dfe886 (patch) | |
tree | b8c9df0ed9f733c50487ac6a5f70061751ae462e | |
parent | 58c5fd7066a978af61d8287acaa7722e6de2219b (diff) |
View2D:
Added methods for easier checking of visiblity/position of items arranged in some regular table format (i.e. columns and/or rows).
Last commit from me for 2008!
-rw-r--r-- | source/blender/editors/include/UI_view2d.h | 4 | ||||
-rw-r--r-- | source/blender/editors/interface/view2d.c | 110 |
2 files changed, 113 insertions, 1 deletions
diff --git a/source/blender/editors/include/UI_view2d.h b/source/blender/editors/include/UI_view2d.h index 8992e71759c..43f11e0ec8e 100644 --- a/source/blender/editors/include/UI_view2d.h +++ b/source/blender/editors/include/UI_view2d.h @@ -159,6 +159,10 @@ View2DScrollers *UI_view2d_scrollers_calc(const struct bContext *C, struct View2 void UI_view2d_scrollers_draw(const struct bContext *C, struct View2D *v2d, View2DScrollers *scrollers); void UI_view2d_scrollers_free(View2DScrollers *scrollers); +/* list view tools */ +void UI_view2d_listview_get_cell(struct View2D *v2d, short columnwidth, short rowheight, float startx, float starty, float viewx, float viewy, int *column, int *row); +void UI_view2d_listview_visible_cells(struct View2D *v2d, short columnwidth, short rowheight, float startx, float starty, int *column_min, int *column_max, int *row_min, int *row_max); + /* coordinate conversion */ void UI_view2d_region_to_view(struct View2D *v2d, int x, int y, float *viewx, float *viewy); void UI_view2d_view_to_region(struct View2D *v2d, float x, float y, short *regionx, short *regiony); diff --git a/source/blender/editors/interface/view2d.c b/source/blender/editors/interface/view2d.c index 474d057beea..ffacbab0ff6 100644 --- a/source/blender/editors/interface/view2d.c +++ b/source/blender/editors/interface/view2d.c @@ -1086,7 +1086,7 @@ void UI_view2d_grid_free(View2DGrid *grid) } /* *********************************************************************** */ -/* Scrollbars */ +/* Scrollers */ /* View2DScrollers is typedef'd in UI_view2d.h * WARNING: the start of this struct must not change, as view2d_ops.c uses this too. @@ -1562,6 +1562,114 @@ void UI_view2d_scrollers_free(View2DScrollers *scrollers) } /* *********************************************************************** */ +/* List View Utilities */ + +/* Get the 'cell' (row, column) that the given 2D-view coordinates (i.e. in 'tot' rect space) lie in. + * - columnwidth, rowheight = size of each 'cell' + * - startx, starty = coordinates (in 'tot' rect space) that the list starts from + * This should be (0,0) for most views. However, for those where the starting row was offsetted + * (like for Animation Editor channel lists, to make the first entry more visible), these will be + * the min-coordinates of the first item. + * - viewx, viewy = 2D-coordinates (in 2D-view / 'tot' rect space) to get the cell for + * - column, row = the 'coordinates' of the relevant 'cell' + */ +void UI_view2d_listview_get_cell(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, + float viewx, float viewy, int *column, int *row) +{ + const int x= (int)(floor(viewx + 0.5f) - startx); + const int y= (int)(floor(viewy + 0.5f) - starty); + + /* sizes must not be negative */ + if ( (v2d == NULL) || ((columnwidth <= 0) && (rowheight <= 0)) ) { + if (column) *column= 0; + if (row) *row= 0; + + return; + } + + /* get column */ + if ((column) && (columnwidth > 0)) { + /* which way to get column depends on the alignment of the 'tot' rect + * - we favour positive-x here, as that's the recommended configuration for listviews + */ + if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) { + /* contents are in positive-x half */ + if (x > 0) + *column= x % columnwidth; + else + *column= 0; + } + else if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) { + /* contents are in negative-x half */ + if (x < 0) + *column= (-x) % columnwidth; + else + *column= 0; + } + else { + /* contents are centered around x==0 */ + // temp case for now... + *column= 0; + } + } + else if (column) { + /* we want the column, but column width is undefined */ + *column= 0; + } + + /* get row */ + if ((row) && (rowheight > 0)) { + /* which way to get column depends on the alignment of the 'tot' rect + * - we favour negative-y here, as that's the recommended configuration for listviews + */ + if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) { + /* contents are in negative-y half */ + if (y < 0) + *row= (-y) % rowheight; + else + *row= 0; + } + else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) { + /* contents are in positive-y half */ + if (y > 0) + *row= y % rowheight; + else + *row= 0; + } + else { + /* contents are centered around y==0 */ + // temp case for now... + *row= 0; + + } + } + else if (row) { + /* we want the row, but row height is undefined */ + *row= 0; + } +} + +/* Get the 'extreme' (min/max) column and row indices which are visible within the 'cur' rect + * - columnwidth, rowheight = size of each 'cell' + * - startx, starty = coordinates that the list starts from, which should be (0,0) for most views + * - column/row_min/max = the starting and ending column/row indices + */ +void UI_view2d_listview_visible_cells(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, + int *column_min, int *column_max, int *row_min, int *row_max) +{ + /* using 'cur' rect coordinates, call the cell-getting function to get the cells for this */ + if (v2d) { + /* min */ + UI_view2d_listview_get_cell(v2d, columnwidth, rowheight, startx, starty, + v2d->cur.xmin, v2d->cur.ymin, column_min, row_min); + + /* max*/ + UI_view2d_listview_get_cell(v2d, columnwidth, rowheight, startx, starty, + v2d->cur.xmax, v2d->cur.ymax, column_max, row_max); + } +} + +/* *********************************************************************** */ /* Coordinate Conversions */ /* Convert from screen/region space to 2d-View space |