Cleanup: simplify lasso reallocation

Remove unneeded define, double allocations when increasing.
This commit is contained in:
2017-10-16 13:18:50 +11:00
parent cae9770954
commit 201b02f2a7
4 changed files with 11 additions and 16 deletions

View File

@@ -412,7 +412,7 @@ typedef struct wmGesture {
int type; /* gesture type define */
int swinid; /* initial subwindow id where it started */
int points; /* optional, amount of points stored */
int size; /* optional, maximum amount of points stored */
int points_alloc; /* optional, maximum amount of points stored */
/* For modal operators which may be running idle, waiting for an event to activate the gesture.
* Typically this is set when the user is click-dragging the gesture (border and circle select for eg). */

View File

@@ -97,11 +97,11 @@ wmGesture *WM_gesture_new(bContext *C, const wmEvent *event, int type)
}
else if (ELEM(type, WM_GESTURE_LINES, WM_GESTURE_LASSO)) {
short *lasso;
gesture->customdata = lasso = MEM_callocN(2 * sizeof(short) * WM_LASSO_MIN_POINTS, "lasso points");
gesture->points_alloc = 1024;
gesture->customdata = lasso = MEM_mallocN(sizeof(short[2]) * gesture->points_alloc, "lasso points");
lasso[0] = event->x - sx;
lasso[1] = event->y - sy;
gesture->points = 1;
gesture->size = WM_LASSO_MIN_POINTS;
}
return gesture;

View File

@@ -2692,28 +2692,24 @@ int WM_gesture_lasso_modal(bContext *C, wmOperator *op, const wmEvent *event)
switch (event->type) {
case MOUSEMOVE:
case INBETWEEN_MOUSEMOVE:
wm_gesture_tag_redraw(C);
wm_subwindow_origin_get(CTX_wm_window(C), gesture->swinid, &sx, &sy);
if (gesture->points == gesture->size) {
short *old_lasso = gesture->customdata;
gesture->customdata = MEM_callocN(2 * sizeof(short) * (gesture->size + WM_LASSO_MIN_POINTS), "lasso points");
memcpy(gesture->customdata, old_lasso, 2 * sizeof(short) * gesture->size);
gesture->size = gesture->size + WM_LASSO_MIN_POINTS;
MEM_freeN(old_lasso);
// printf("realloc\n");
if (gesture->points == gesture->points_alloc) {
gesture->points_alloc *= 2;
gesture->customdata = MEM_reallocN(gesture->customdata, sizeof(short[2]) * gesture->points_alloc);
}
{
int x, y;
short *lasso = gesture->customdata;
lasso += (2 * gesture->points - 2);
x = (event->x - sx - lasso[0]);
y = (event->y - sy - lasso[1]);
/* make a simple distance check to get a smoother lasso
* add only when at least 2 pixels between this and previous location */
if ((x * x + y * y) > 4) {
@@ -2724,7 +2720,7 @@ int WM_gesture_lasso_modal(bContext *C, wmOperator *op, const wmEvent *event)
}
}
break;
case LEFTMOUSE:
case MIDDLEMOUSE:
case RIGHTMOUSE:

View File

@@ -60,7 +60,6 @@ void wm_window_keymap(wmKeyConfig *keyconf);
void wm_tweakevent_test(bContext *C, const wmEvent *event, int action);
/* wm_gesture.c */
#define WM_LASSO_MIN_POINTS 1024
void wm_gesture_draw(struct wmWindow *win);
int wm_gesture_evaluate(wmGesture *gesture);
void wm_gesture_tag_redraw(bContext *C);