simplify drag toggle operator, use BLI_rctf_isect_segment between mouse events rather then many calls to ui_but_find_mouse_over().

This commit is contained in:
Campbell Barton
2013-02-26 00:09:26 +00:00
parent 88be0a852e
commit fcac25e08d
3 changed files with 62 additions and 66 deletions

View File

@@ -153,6 +153,7 @@ typedef struct uiHandleButtonData {
int maxlen, selextend, selstartx;
/* number editing / dragging */
/* coords are Window/uiBlock relative (depends on the button) */
int draglastx, draglasty;
int dragstartx, dragstarty;
int dragchange, draglock, dragsel;
@@ -5474,6 +5475,24 @@ static int ui_mouse_inside_button(ARegion *ar, uiBut *but, int x, int y)
return 1;
}
/**
* Can we mouse over the button or is it hidden/disabled/layout.
*/
bool ui_is_but_interactive(uiBut *but)
{
/* note, LABEL is included for highlights, this allows drags */
if (but->type == LABEL && but->dragpoin == NULL)
return false;
if (ELEM3(but->type, ROUNDBOX, SEPR, LISTBOX))
return false;
if (but->flag & UI_HIDDEN)
return false;
if (but->flag & UI_SCROLLED)
return false;
return true;
}
uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
{
uiBlock *block;
@@ -5491,18 +5510,12 @@ uiBut *ui_but_find_mouse_over(ARegion *ar, int x, int y)
ui_window_to_block(ar, block, &mx, &my);
for (but = block->buttons.first; but; but = but->next) {
/* note, LABEL is included for highlights, this allows drags */
if (but->type == LABEL && but->dragpoin == NULL)
continue;
if (ELEM3(but->type, ROUNDBOX, SEPR, LISTBOX))
continue;
if (but->flag & UI_HIDDEN)
continue;
if (but->flag & UI_SCROLLED)
continue;
if (ui_but_contains_pt(but, mx, my))
if (ui_is_but_interactive(but)) {
if (ui_but_contains_pt(but, mx, my)) {
butover = but;
}
}
}
/* CLIP_EVENTS prevents the event from reaching other blocks */
if (block->flag & UI_BLOCK_CLIP_EVENTS) {
@@ -5849,6 +5862,7 @@ static void button_activate_exit(bContext *C, uiBut *but, uiHandleButtonData *da
MEM_freeN(but->active);
but->active = NULL;
}
but->flag &= ~(UI_ACTIVE | UI_SELECT);
but->flag |= UI_BUT_LAST_ACTIVE;
if (!onfree)

View File

@@ -172,7 +172,7 @@ struct uiBut {
char strdata[UI_MAX_NAME_STR];
char drawstr[UI_MAX_DRAW_STR];
rctf rect;
rctf rect; /* block relative coords */
char *poin;
float hardmin, hardmax, softmin, softmax;
@@ -225,7 +225,7 @@ struct uiBut {
void *rename_orig;
uiLink *link;
short linkto[2];
short linkto[2]; /* region relative coords */
const char *tip, *lockstr;
@@ -410,6 +410,7 @@ extern int ui_is_but_bool(uiBut *but);
extern int ui_is_but_unit(uiBut *but);
extern int ui_is_but_rna_valid(uiBut *but);
extern int ui_is_but_utf8(uiBut *but);
extern bool ui_is_but_interactive(uiBut *but);
extern void ui_bounds_block(uiBlock *block);
extern void ui_block_translate(uiBlock *block, int x, int y);

View File

@@ -1081,63 +1081,45 @@ typedef struct DragOpInfo {
eButType but_type_start;
} DragOpInfo;
typedef struct DragOpPlotData {
bContext *C;
ARegion *ar;
bool is_set;
eButType but_type_start;
bool do_draw;
const uiBut *but_prev;
} DragOpPlotData;
static const uiBut *ui_but_set_xy(bContext *C, ARegion *ar, const bool is_set, const eButType but_type_start,
const int xy[2], const uiBut *but_prev)
static bool ui_but_set_xy_xy(bContext *C, ARegion *ar, const bool is_set, const eButType but_type_start,
const int xy_src[2], const int xy_dst[2])
{
uiBut *but = ui_but_find_mouse_over(ar, xy[0], xy[1]);
bool change = false;
uiBlock *block;
if (but_prev == but) {
return but_prev;
}
for (block = ar->uiblocks.first; block; block = block->next) {
uiBut *but;
if (but && ui_is_but_bool(but) && but->type == but_type_start) {
float xy_a_block[2] = {UNPACK2(xy_src)};
float xy_b_block[2] = {UNPACK2(xy_dst)};
ui_window_to_block_fl(ar, block, &xy_a_block[0], &xy_a_block[1]);
ui_window_to_block_fl(ar, block, &xy_b_block[0], &xy_b_block[1]);
for (but = block->buttons.first; but; but = but->next) {
if (ui_is_but_interactive(but)) {
if (BLI_rctf_isect_segment(&but->rect, xy_a_block, xy_b_block)) {
/* execute the button */
if (ui_is_but_bool(but) && but->type == but_type_start) {
/* is it pressed? */
bool is_set_but = (ui_get_but_val(but) != 0.0);
BLI_assert(ui_is_but_bool(but) == true);
if (is_set_but != is_set) {
uiButExecute(C, but);
return but;
change = true;
}
}
/* done */
}
}
}
}
return but_prev;
return change;
}
static int ui_but_set_cb(int x, int y, void *data_v)
{
DragOpPlotData *data = data_v;
int xy[2] = {x, y};
data->but_prev = ui_but_set_xy(data->C, data->ar, data->is_set, data->but_type_start, xy, data->but_prev);
return 1; /* keep going */
}
/* operates on buttons between 2 mouse-points */
static bool ui_but_set_xy_xy(bContext *C, ARegion *ar, const bool is_set, const eButType but_type_start,
const int xy_src[2], const int xy_dst[2])
{
DragOpPlotData data;
data.C = C;
data.ar = ar;
data.is_set = is_set;
data.but_type_start = but_type_start;
data.do_draw = false;
data.but_prev = NULL;
/* prevent dragging too fast loosing buttons */
plot_line_v2v2i(xy_src, xy_dst, ui_but_set_cb, &data);
return data.do_draw;
}
static void ui_drag_but_set(bContext *C, wmOperator *op, const int xy_input[2])
{
@@ -1307,4 +1289,3 @@ void UI_buttons_operatortypes(void)
WM_operatortype_append(UI_OT_reloadtranslation);
WM_operatortype_append(UI_OT_drag_toggle);
}