Refactor: WM: Use mval for polyline gesture

When this was originally implemented, the `mval` attribute `wmGesture`
didn't exist. This commit switches the current mouse position from being
stored in the last element of the point array to `mval`.

Pull Request: https://projects.blender.org/blender/blender/pulls/124489
This commit is contained in:
Sean Kim
2024-07-11 20:17:02 +02:00
committed by Sean Kim
parent 10b871a211
commit 5ddaf6dfd7
2 changed files with 29 additions and 31 deletions

View File

@@ -87,9 +87,9 @@ wmGesture *WM_gesture_new(wmWindow *window, const ARegion *region, const wmEvent
gesture->customdata = border;
border[0] = xy[0] - gesture->winrct.xmin;
border[1] = xy[1] - gesture->winrct.ymin;
border[2] = border[0];
border[3] = border[1];
gesture->points = 2;
gesture->mval.x = border[0];
gesture->mval.y = border[1];
gesture->points = 1;
}
return gesture;
@@ -443,7 +443,7 @@ static void draw_start_vertex_circle(const wmGesture &gt, const uint shdr_pos)
const short(*border)[2] = static_cast<short int(*)[2]>(gt.customdata);
const float start_pos[2] = {float(border[0][0]), float(border[0][1])};
const float current_pos[2] = {float(border[numverts - 1][0]), float(border[numverts - 1][1])};
const float current_pos[2] = {float(gt.mval.x), float(gt.mval.y)};
const float dist = len_v2v2(start_pos, current_pos);
const float limit = pow2f(blender::wm::gesture::POLYLINE_CLICK_RADIUS * UI_SCALE_FAC);
@@ -463,7 +463,7 @@ static void wm_gesture_draw_polyline(wmGesture *gt)
{
draw_filled_lasso(gt);
const int numverts = gt->points;
const int numverts = gt->points + 1;
if (numverts < 2) {
return;
}
@@ -486,9 +486,10 @@ static void wm_gesture_draw_polyline(wmGesture *gt)
immBegin(GPU_PRIM_LINE_LOOP, numverts);
const short *border = (short *)gt->customdata;
for (int i = 0; i < numverts; i++, border += 2) {
for (int i = 0; i < gt->points; i++, border += 2) {
immVertex2f(shdr_pos, float(border[0]), float(border[1]));
}
immVertex2f(shdr_pos, float(gt->mval.x), float(gt->mval.y));
immEnd();

View File

@@ -757,20 +757,17 @@ static int gesture_polyline_valid_points(const wmGesture &wmGesture)
const int num_points = wmGesture.points;
short(*points)[2] = static_cast<short int(*)[2]>(wmGesture.customdata);
const short last_x = points[num_points - 1][0];
const short last_y = points[num_points - 1][1];
const short prev_x = points[num_points - 1][0];
const short prev_y = points[num_points - 1][1];
const short prev_x = points[num_points - 2][0];
const short prev_y = points[num_points - 2][1];
return (last_x == prev_x && last_y == prev_y) ? num_points - 1 : num_points;
return (wmGesture.mval.x == prev_x && wmGesture.mval.y == prev_y) ? num_points : num_points + 1;
}
/* Evaluates whether the polyline has at least three points and represents
* a shape and can be submitted for other gesture operators to act on. */
static bool gesture_polyline_can_apply(const wmGesture &wmGesture)
{
if (wmGesture.points <= 2) {
if (wmGesture.points < 2) {
return false;
}
@@ -788,8 +785,6 @@ static int gesture_polyline_apply(bContext *C, wmOperator *op)
BLI_assert(gesture_polyline_can_apply(*gesture));
const int valid_points = gesture_polyline_valid_points(*gesture);
gesture->points = valid_points;
const short *border = static_cast<const short int *>(gesture->customdata);
PointerRNA itemptr;
@@ -801,6 +796,12 @@ static int gesture_polyline_apply(bContext *C, wmOperator *op)
RNA_collection_add(op->ptr, "path", &itemptr);
RNA_float_set_array(&itemptr, "loc", loc);
}
if (valid_points > gesture->points) {
loc[0] = gesture->mval.x;
loc[1] = gesture->mval.y;
RNA_collection_add(op->ptr, "path", &itemptr);
RNA_float_set_array(&itemptr, "loc", loc);
}
gesture_modal_end(C, op);
@@ -825,17 +826,14 @@ int WM_gesture_polyline_modal(bContext *C, wmOperator *op, const wmEvent *event)
case GESTURE_MODAL_SELECT: {
wm_gesture_tag_redraw(CTX_wm_window(C));
short(*border)[2] = static_cast<short int(*)[2]>(gesture->customdata);
const short cur_x = border[gesture->points - 1][0];
const short cur_y = border[gesture->points - 1][1];
const short prev_x = border[gesture->points - 1][0];
const short prev_y = border[gesture->points - 1][1];
const short prev_x = border[gesture->points - 2][0];
const short prev_y = border[gesture->points - 2][1];
if (cur_x == prev_x && cur_y == prev_y) {
if (gesture->mval.x == prev_x && gesture->mval.y == prev_y) {
break;
}
const float2 cur(cur_x, cur_y);
const float2 cur(gesture->mval);
const float2 orig(border[0][0], border[0][1]);
const float dist = len_v2v2(cur, orig);
@@ -847,8 +845,8 @@ int WM_gesture_polyline_modal(bContext *C, wmOperator *op, const wmEvent *event)
}
gesture->points++;
border[gesture->points - 1][0] = cur_x;
border[gesture->points - 1][1] = cur_y;
border[gesture->points - 1][0] = gesture->mval.x;
border[gesture->points - 1][1] = gesture->mval.y;
break;
}
case GESTURE_MODAL_CONFIRM:
@@ -866,6 +864,8 @@ int WM_gesture_polyline_modal(bContext *C, wmOperator *op, const wmEvent *event)
case MOUSEMOVE:
case INBETWEEN_MOUSEMOVE: {
wm_gesture_tag_redraw(CTX_wm_window(C));
gesture->mval = int2((event->xy[0] - gesture->winrct.xmin),
(event->xy[1] - gesture->winrct.ymin));
if (gesture->points == gesture->points_alloc) {
gesture->points_alloc *= 2;
gesture->customdata = MEM_reallocN(gesture->customdata,
@@ -875,17 +875,14 @@ int WM_gesture_polyline_modal(bContext *C, wmOperator *op, const wmEvent *event)
/* move the lasso */
if (gesture->move) {
const int x = ((event->xy[0] - gesture->winrct.xmin) - border[gesture->points - 1][0]);
const int y = ((event->xy[1] - gesture->winrct.ymin) - border[gesture->points - 1][1]);
const int dx = gesture->mval.x - border[gesture->points - 1][0];
const int dy = gesture->mval.y - border[gesture->points - 1][1];
for (int i = 0; i < gesture->points; i++) {
border[i][0] += x;
border[i][1] += y;
border[i][0] += dx;
border[i][1] += dy;
}
}
border[gesture->points - 1][0] = event->xy[0] - gesture->winrct.xmin;
border[gesture->points - 1][1] = event->xy[1] - gesture->winrct.ymin;
break;
}
}