Cleanup: Pass cursor position as a single array to eyedropper functions

Since `event->xy` is now an array these functions can be
simplified to accept the sample point as an array.

Clarifications were also made to variable names:
- `eye->last_x/y` --> `eye->cursor_last`
- `mx/my` --> `cursor`

Differential Revision: https://developer.blender.org/D13671
This commit is contained in:
Aaron Carlisle 2022-01-29 20:44:27 -05:00
parent f8b8727873
commit f18172b023
7 changed files with 78 additions and 83 deletions

View File

@ -25,6 +25,7 @@
#include "DNA_space_types.h"
#include "BLI_math_color.h"
#include "BLI_math_vector.h"
#include "BKE_context.h"
#include "BKE_screen.h"
@ -107,7 +108,7 @@ wmKeyMap *eyedropper_colorband_modal_keymap(wmKeyConfig *keyconf)
/** \name Generic Shared Functions
* \{ */
static void eyedropper_draw_cursor_text_ex(const int x, const int y, const char *name)
static void eyedropper_draw_cursor_text_ex(const int xy[2], const char *name)
{
const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
@ -119,7 +120,7 @@ static void eyedropper_draw_cursor_text_ex(const int x, const int y, const char
rgba_uchar_to_float(col_fg, wcol->text);
rgba_uchar_to_float(col_bg, wcol->inner);
UI_fontstyle_draw_simple_backdrop(fstyle, x, y + U.widget_unit, name, col_fg, col_bg);
UI_fontstyle_draw_simple_backdrop(fstyle, xy[0], xy[1] + U.widget_unit, name, col_fg, col_bg);
}
void eyedropper_draw_cursor_text_window(const struct wmWindow *window, const char *name)
@ -128,19 +129,16 @@ void eyedropper_draw_cursor_text_window(const struct wmWindow *window, const cha
return;
}
const int x = window->eventstate->xy[0];
const int y = window->eventstate->xy[1];
eyedropper_draw_cursor_text_ex(x, y, name);
eyedropper_draw_cursor_text_ex(window->eventstate->xy, name);
}
void eyedropper_draw_cursor_text_region(const int x, const int y, const char *name)
void eyedropper_draw_cursor_text_region(const int xy[2], const char *name)
{
if (name[0] == '\0') {
return;
}
eyedropper_draw_cursor_text_ex(x, y, name);
eyedropper_draw_cursor_text_ex(xy, name);
}
uiBut *eyedropper_get_property_button_under_mouse(bContext *C, const wmEvent *event)
@ -173,8 +171,7 @@ void datadropper_win_area_find(
}
}
else if (mval != r_mval) {
r_mval[0] = mval[0];
r_mval[1] = mval[1];
copy_v2_v2_int(r_mval, mval);
}
}

View File

@ -254,8 +254,10 @@ static bool eyedropper_cryptomatte_sample_image_fl(const bNode *node,
return success;
}
static bool eyedropper_cryptomatte_sample_fl(
bContext *C, Eyedropper *eye, int mx, int my, float r_col[3])
static bool eyedropper_cryptomatte_sample_fl(bContext *C,
Eyedropper *eye,
const int m_xy[2],
float r_col[3])
{
bNode *node = eye->crypto_node;
NodeCryptomatte *crypto = node ? ((NodeCryptomatte *)node->storage) : NULL;
@ -265,17 +267,17 @@ static bool eyedropper_cryptomatte_sample_fl(
}
bScreen *screen = CTX_wm_screen(C);
ScrArea *area = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, (const int[2]){mx, my});
ScrArea *area = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, m_xy);
if (!area || !ELEM(area->spacetype, SPACE_IMAGE, SPACE_NODE, SPACE_CLIP)) {
return false;
}
ARegion *region = BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, (const int[2]){mx, my});
ARegion *region = BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, m_xy);
if (!region) {
return false;
}
int mval[2] = {mx - region->winrct.xmin, my - region->winrct.ymin};
int mval[2] = {m_xy[0] - region->winrct.xmin, m_xy[1] - region->winrct.ymin};
float fpos[2] = {-1.0f, -1.0};
switch (area->spacetype) {
case SPACE_IMAGE: {
@ -324,7 +326,7 @@ static bool eyedropper_cryptomatte_sample_fl(
return false;
}
void eyedropper_color_sample_fl(bContext *C, int mx, int my, float r_col[3])
void eyedropper_color_sample_fl(bContext *C, const int m_xy[2], float r_col[3])
{
/* we could use some clever */
Main *bmain = CTX_data_main(C);
@ -332,10 +334,10 @@ void eyedropper_color_sample_fl(bContext *C, int mx, int my, float r_col[3])
const char *display_device = CTX_data_scene(C)->display_settings.display_device;
struct ColorManagedDisplay *display = IMB_colormanagement_display_get_named(display_device);
int mval[2];
wmWindow *win;
ScrArea *area;
int mval[2] = {mx, my};
datadropper_win_area_find(C, mval, mval, &win, &area);
datadropper_win_area_find(C, m_xy, mval, &win, &area);
if (area) {
if (area->spacetype == SPACE_IMAGE) {
@ -406,17 +408,17 @@ static void eyedropper_color_set(bContext *C, Eyedropper *eye, const float col[3
RNA_property_update(C, &eye->ptr, eye->prop);
}
static void eyedropper_color_sample(bContext *C, Eyedropper *eye, int mx, int my)
static void eyedropper_color_sample(bContext *C, Eyedropper *eye, const int m_xy[2])
{
/* Accumulate color. */
float col[3];
if (eye->crypto_node) {
if (!eyedropper_cryptomatte_sample_fl(C, eye, mx, my, col)) {
if (!eyedropper_cryptomatte_sample_fl(C, eye, m_xy, col)) {
return;
}
}
else {
eyedropper_color_sample_fl(C, mx, my, col);
eyedropper_color_sample_fl(C, m_xy, col);
}
if (!eye->crypto_node) {
@ -439,13 +441,13 @@ static void eyedropper_color_sample(bContext *C, Eyedropper *eye, int mx, int my
eyedropper_color_set(C, eye, accum_col);
}
static void eyedropper_color_sample_text_update(bContext *C, Eyedropper *eye, int mx, int my)
static void eyedropper_color_sample_text_update(bContext *C, Eyedropper *eye, const int m_xy[2])
{
float col[3];
eye->sample_text[0] = '\0';
if (eye->cryptomatte_session) {
if (eyedropper_cryptomatte_sample_fl(C, eye, mx, my, col)) {
if (eyedropper_cryptomatte_sample_fl(C, eye, m_xy, col)) {
BKE_cryptomatte_find_name(
eye->cryptomatte_session, col[0], eye->sample_text, sizeof(eye->sample_text));
eye->sample_text[sizeof(eye->sample_text) - 1] = '\0';
@ -476,7 +478,7 @@ static int eyedropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
case EYE_MODAL_SAMPLE_CONFIRM: {
const bool is_undo = eye->is_undo;
if (eye->accum_tot == 0) {
eyedropper_color_sample(C, eye, event->xy[0], event->xy[1]);
eyedropper_color_sample(C, eye, event->xy);
}
eyedropper_exit(C, op);
/* Could support finished & undo-skip. */
@ -485,23 +487,23 @@ static int eyedropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
case EYE_MODAL_SAMPLE_BEGIN:
/* enable accum and make first sample */
eye->accum_start = true;
eyedropper_color_sample(C, eye, event->xy[0], event->xy[1]);
eyedropper_color_sample(C, eye, event->xy);
break;
case EYE_MODAL_SAMPLE_RESET:
eye->accum_tot = 0;
zero_v3(eye->accum_col);
eyedropper_color_sample(C, eye, event->xy[0], event->xy[1]);
eyedropper_color_sample(C, eye, event->xy);
break;
}
}
else if (ELEM(event->type, MOUSEMOVE, INBETWEEN_MOUSEMOVE)) {
if (eye->accum_start) {
/* button is pressed so keep sampling */
eyedropper_color_sample(C, eye, event->xy[0], event->xy[1]);
eyedropper_color_sample(C, eye, event->xy);
}
if (eye->draw_handle_sample_text) {
eyedropper_color_sample_text_update(C, eye, event->xy[0], event->xy[1]);
eyedropper_color_sample_text_update(C, eye, event->xy);
ED_region_tag_redraw(CTX_wm_region(C));
}
}

View File

@ -58,7 +58,7 @@ typedef struct Colorband_RNAUpdateCb {
} Colorband_RNAUpdateCb;
typedef struct EyedropperColorband {
int last_x, last_y;
int event_xy_last[2];
/* Alpha is currently fixed at 1.0, may support in future. */
float (*color_buffer)[4];
int color_buffer_alloc;
@ -142,13 +142,12 @@ static bool eyedropper_colorband_init(bContext *C, wmOperator *op)
static void eyedropper_colorband_sample_point(bContext *C,
EyedropperColorband *eye,
int mx,
int my)
const int m_xy[2])
{
if (eye->last_x != mx || eye->last_y != my) {
if (eye->event_xy_last[0] != m_xy[0] || eye->event_xy_last[1] != m_xy[1]) {
float col[4];
col[3] = 1.0f; /* TODO: sample alpha */
eyedropper_color_sample_fl(C, mx, my, col);
eyedropper_color_sample_fl(C, m_xy, col);
if (eye->color_buffer_len + 1 == eye->color_buffer_alloc) {
eye->color_buffer_alloc *= 2;
eye->color_buffer = MEM_reallocN(eye->color_buffer,
@ -156,8 +155,7 @@ static void eyedropper_colorband_sample_point(bContext *C,
}
copy_v4_v4(eye->color_buffer[eye->color_buffer_len], col);
eye->color_buffer_len += 1;
eye->last_x = mx;
eye->last_y = my;
copy_v2_v2_int(eye->event_xy_last, m_xy);
eye->is_set = true;
}
}
@ -167,21 +165,20 @@ static bool eyedropper_colorband_sample_callback(int mx, int my, void *userdata)
struct EyedropperColorband_Context *data = userdata;
bContext *C = data->context;
EyedropperColorband *eye = data->eye;
eyedropper_colorband_sample_point(C, eye, mx, my);
const int cursor[2] = {mx, my};
eyedropper_colorband_sample_point(C, eye, cursor);
return true;
}
static void eyedropper_colorband_sample_segment(bContext *C,
EyedropperColorband *eye,
int mx,
int my)
const int m_xy[2])
{
/* Since the mouse tends to move rather rapidly we use #BLI_bitmap_draw_2d_line_v2v2i
* to interpolate between the reported coordinates */
struct EyedropperColorband_Context userdata = {C, eye};
const int p1[2] = {eye->last_x, eye->last_y};
const int p2[2] = {mx, my};
BLI_bitmap_draw_2d_line_v2v2i(p1, p2, eyedropper_colorband_sample_callback, &userdata);
BLI_bitmap_draw_2d_line_v2v2i(
eye->event_xy_last, m_xy, eyedropper_colorband_sample_callback, &userdata);
}
static void eyedropper_colorband_exit(bContext *C, wmOperator *op)
@ -233,7 +230,7 @@ static int eyedropper_colorband_modal(bContext *C, wmOperator *op, const wmEvent
return OPERATOR_CANCELLED;
case EYE_MODAL_SAMPLE_CONFIRM: {
const bool is_undo = eye->is_undo;
eyedropper_colorband_sample_segment(C, eye, event->xy[0], event->xy[1]);
eyedropper_colorband_sample_segment(C, eye, event->xy);
eyedropper_colorband_apply(C, op);
eyedropper_colorband_exit(C, op);
/* Could support finished & undo-skip. */
@ -242,10 +239,9 @@ static int eyedropper_colorband_modal(bContext *C, wmOperator *op, const wmEvent
case EYE_MODAL_SAMPLE_BEGIN:
/* enable accum and make first sample */
eye->sample_start = true;
eyedropper_colorband_sample_point(C, eye, event->xy[0], event->xy[1]);
eyedropper_colorband_sample_point(C, eye, event->xy);
eyedropper_colorband_apply(C, op);
eye->last_x = event->xy[0];
eye->last_y = event->xy[1];
copy_v2_v2_int(eye->event_xy_last, event->xy);
break;
case EYE_MODAL_SAMPLE_RESET:
break;
@ -253,7 +249,7 @@ static int eyedropper_colorband_modal(bContext *C, wmOperator *op, const wmEvent
}
else if (event->type == MOUSEMOVE) {
if (eye->sample_start) {
eyedropper_colorband_sample_segment(C, eye, event->xy[0], event->xy[1]);
eyedropper_colorband_sample_segment(C, eye, event->xy);
eyedropper_colorband_apply(C, op);
}
}
@ -280,7 +276,7 @@ static int eyedropper_colorband_point_modal(bContext *C, wmOperator *op, const w
}
break;
case EYE_MODAL_POINT_SAMPLE:
eyedropper_colorband_sample_point(C, eye, event->xy[0], event->xy[1]);
eyedropper_colorband_sample_point(C, eye, event->xy);
eyedropper_colorband_apply(C, op);
if (eye->color_buffer_len == MAXCOLORBAND) {
eyedropper_colorband_exit(C, op);

View File

@ -32,6 +32,7 @@
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "BLI_math_vector.h"
#include "BLI_string.h"
#include "BLT_translation.h"
@ -80,7 +81,7 @@ static void datadropper_draw_cb(const struct bContext *UNUSED(C),
void *arg)
{
DataDropper *ddr = arg;
eyedropper_draw_cursor_text_region(UNPACK2(ddr->name_pos), ddr->name);
eyedropper_draw_cursor_text_region(ddr->name_pos, ddr->name);
}
static int datadropper_init(bContext *C, wmOperator *op)
@ -152,7 +153,7 @@ static void datadropper_exit(bContext *C, wmOperator *op)
* \brief get the ID from the 3D view or outliner.
*/
static void datadropper_id_sample_pt(
bContext *C, wmWindow *win, ScrArea *area, DataDropper *ddr, int mx, int my, ID **r_id)
bContext *C, wmWindow *win, ScrArea *area, DataDropper *ddr, const int m_xy[2], ID **r_id)
{
wmWindow *win_prev = CTX_wm_window(C);
ScrArea *area_prev = CTX_wm_area(C);
@ -162,9 +163,9 @@ static void datadropper_id_sample_pt(
if (area) {
if (ELEM(area->spacetype, SPACE_VIEW3D, SPACE_OUTLINER)) {
ARegion *region = BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, (const int[2]){mx, my});
ARegion *region = BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, m_xy);
if (region) {
const int mval[2] = {mx - region->winrct.xmin, my - region->winrct.ymin};
const int mval[2] = {m_xy[0] - region->winrct.xmin, m_xy[1] - region->winrct.ymin};
Base *base;
CTX_wm_window_set(C, win);
@ -205,8 +206,7 @@ static void datadropper_id_sample_pt(
*r_id = id;
}
ddr->name_pos[0] = mval[0];
ddr->name_pos[1] = mval[1];
copy_v2_v2_int(ddr->name_pos, mval);
}
}
}
@ -234,17 +234,16 @@ static bool datadropper_id_set(bContext *C, DataDropper *ddr, ID *id)
}
/* single point sample & set */
static bool datadropper_id_sample(bContext *C, DataDropper *ddr, int mx, int my)
static bool datadropper_id_sample(bContext *C, DataDropper *ddr, const int m_xy[2])
{
ID *id = NULL;
int mval[2];
wmWindow *win;
ScrArea *area;
datadropper_win_area_find(C, m_xy, mval, &win, &area);
int mval[] = {mx, my};
datadropper_win_area_find(C, mval, mval, &win, &area);
datadropper_id_sample_pt(C, win, area, ddr, mval[0], mval[1], &id);
datadropper_id_sample_pt(C, win, area, ddr, mval, &id);
return datadropper_id_set(C, ddr, id);
}
@ -292,7 +291,7 @@ static int datadropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
return OPERATOR_CANCELLED;
case EYE_MODAL_SAMPLE_CONFIRM: {
const bool is_undo = ddr->is_undo;
const bool success = datadropper_id_sample(C, ddr, event->xy[0], event->xy[1]);
const bool success = datadropper_id_sample(C, ddr, event->xy);
datadropper_exit(C, op);
if (success) {
/* Could support finished & undo-skip. */
@ -306,16 +305,15 @@ static int datadropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
else if (event->type == MOUSEMOVE) {
ID *id = NULL;
int mval[2];
wmWindow *win;
ScrArea *area;
int mval[] = {event->xy[0], event->xy[1]};
datadropper_win_area_find(C, mval, mval, &win, &area);
datadropper_win_area_find(C, event->xy, mval, &win, &area);
/* Set the region for eyedropper cursor text drawing */
datadropper_set_draw_callback_region(area, ddr);
datadropper_id_sample_pt(C, win, area, ddr, mval[0], mval[1], &id);
datadropper_id_sample_pt(C, win, area, ddr, mval, &id);
}
return OPERATOR_RUNNING_MODAL;

View File

@ -81,7 +81,7 @@ static void depthdropper_draw_cb(const struct bContext *UNUSED(C),
void *arg)
{
DepthDropper *ddr = arg;
eyedropper_draw_cursor_text_region(UNPACK2(ddr->name_pos), ddr->name);
eyedropper_draw_cursor_text_region(ddr->name_pos, ddr->name);
}
static int depthdropper_init(bContext *C, wmOperator *op)
@ -152,12 +152,14 @@ static void depthdropper_exit(bContext *C, wmOperator *op)
/**
* \brief get the ID from the screen.
*/
static void depthdropper_depth_sample_pt(
bContext *C, DepthDropper *ddr, int mx, int my, float *r_depth)
static void depthdropper_depth_sample_pt(bContext *C,
DepthDropper *ddr,
const int m_xy[2],
float *r_depth)
{
/* we could use some clever */
bScreen *screen = CTX_wm_screen(C);
ScrArea *area = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, (const int[2]){mx, my});
ScrArea *area = BKE_screen_find_area_xy(screen, SPACE_TYPE_ANY, m_xy);
Scene *scene = CTX_data_scene(C);
ScrArea *area_prev = CTX_wm_area(C);
@ -167,14 +169,14 @@ static void depthdropper_depth_sample_pt(
if (area) {
if (area->spacetype == SPACE_VIEW3D) {
ARegion *region = BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, (const int[2]){mx, my});
ARegion *region = BKE_area_find_region_xy(area, RGN_TYPE_WINDOW, m_xy);
if (region) {
struct Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
View3D *v3d = area->spacedata.first;
RegionView3D *rv3d = region->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 - region->winrct.xmin, my - region->winrct.ymin};
const int mval[2] = {m_xy[0] - region->winrct.xmin, m_xy[1] - region->winrct.ymin};
copy_v2_v2_int(ddr->name_pos, mval);
float co[3];
@ -234,19 +236,19 @@ static void depthdropper_depth_set_accum(bContext *C, DepthDropper *ddr)
}
/* single point sample & set */
static void depthdropper_depth_sample(bContext *C, DepthDropper *ddr, int mx, int my)
static void depthdropper_depth_sample(bContext *C, DepthDropper *ddr, const int m_xy[2])
{
float depth = -1.0f;
if (depth != -1.0f) {
depthdropper_depth_sample_pt(C, ddr, mx, my, &depth);
depthdropper_depth_sample_pt(C, ddr, m_xy, &depth);
depthdropper_depth_set(C, ddr, depth);
}
}
static void depthdropper_depth_sample_accum(bContext *C, DepthDropper *ddr, int mx, int my)
static void depthdropper_depth_sample_accum(bContext *C, DepthDropper *ddr, const int m_xy[2])
{
float depth = -1.0f;
depthdropper_depth_sample_pt(C, ddr, mx, my, &depth);
depthdropper_depth_sample_pt(C, ddr, m_xy, &depth);
if (depth != -1.0f) {
ddr->accum_depth += depth;
ddr->accum_tot++;
@ -276,7 +278,7 @@ static int depthdropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
case EYE_MODAL_SAMPLE_CONFIRM: {
const bool is_undo = ddr->is_undo;
if (ddr->accum_tot == 0) {
depthdropper_depth_sample(C, ddr, event->xy[0], event->xy[1]);
depthdropper_depth_sample(C, ddr, event->xy);
}
else {
depthdropper_depth_set_accum(C, ddr);
@ -288,12 +290,12 @@ static int depthdropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
case EYE_MODAL_SAMPLE_BEGIN:
/* enable accum and make first sample */
ddr->accum_start = true;
depthdropper_depth_sample_accum(C, ddr, event->xy[0], event->xy[1]);
depthdropper_depth_sample_accum(C, ddr, event->xy);
break;
case EYE_MODAL_SAMPLE_RESET:
ddr->accum_tot = 0;
ddr->accum_depth = 0.0f;
depthdropper_depth_sample_accum(C, ddr, event->xy[0], event->xy[1]);
depthdropper_depth_sample_accum(C, ddr, event->xy);
depthdropper_depth_set_accum(C, ddr);
break;
}
@ -301,7 +303,7 @@ static int depthdropper_modal(bContext *C, wmOperator *op, const wmEvent *event)
else if (event->type == MOUSEMOVE) {
if (ddr->accum_start) {
/* button is pressed so keep sampling */
depthdropper_depth_sample_accum(C, ddr, event->xy[0], event->xy[1]);
depthdropper_depth_sample_accum(C, ddr, event->xy);
depthdropper_depth_set_accum(C, ddr);
}
}

View File

@ -265,9 +265,9 @@ static void eyedropper_gpencil_color_set(bContext *C, const wmEvent *event, Eyed
}
/* Sample the color below cursor. */
static void eyedropper_gpencil_color_sample(bContext *C, EyedropperGPencil *eye, int mx, int my)
static void eyedropper_gpencil_color_sample(bContext *C, EyedropperGPencil *eye, const int m_xy[2])
{
eyedropper_color_sample_fl(C, mx, my, eye->color);
eyedropper_color_sample_fl(C, m_xy, eye->color);
}
/* Cancel operator. */
@ -292,7 +292,7 @@ static int eyedropper_gpencil_modal(bContext *C, wmOperator *op, const wmEvent *
return OPERATOR_CANCELLED;
}
case EYE_MODAL_SAMPLE_CONFIRM: {
eyedropper_gpencil_color_sample(C, eye, event->xy[0], event->xy[1]);
eyedropper_gpencil_color_sample(C, eye, event->xy);
/* Create material. */
eyedropper_gpencil_color_set(C, event, eye);
@ -309,7 +309,7 @@ static int eyedropper_gpencil_modal(bContext *C, wmOperator *op, const wmEvent *
}
case MOUSEMOVE:
case INBETWEEN_MOUSEMOVE: {
eyedropper_gpencil_color_sample(C, eye, event->xy[0], event->xy[1]);
eyedropper_gpencil_color_sample(C, eye, event->xy);
break;
}
default: {

View File

@ -25,7 +25,7 @@
/* interface_eyedropper.c */
void eyedropper_draw_cursor_text_window(const struct wmWindow *window, const char *name);
void eyedropper_draw_cursor_text_region(int x, int y, const char *name);
void eyedropper_draw_cursor_text_region(const int xy[2], const char *name);
/**
* Utility to retrieve a button representing a RNA property that is currently under the cursor.
*
@ -51,7 +51,7 @@ void datadropper_win_area_find(const struct bContext *C,
*
* \note Exposed by 'interface_eyedropper_intern.h' for use with color band picking.
*/
void eyedropper_color_sample_fl(bContext *C, int mx, int my, float r_col[3]);
void eyedropper_color_sample_fl(bContext *C, const int m_xy[2], float r_col[3]);
/* Used for most eye-dropper operators. */
enum {