WM: add wmGenericUserData utility struct
Useful to have a generic user data with an optional custom free function, use for wmGesture.
This commit is contained in:
@@ -148,7 +148,7 @@ static int mesh_bisect_invoke(bContext *C, wmOperator *op, const wmEvent *event)
|
||||
BisectData *opdata;
|
||||
|
||||
opdata = MEM_mallocN(sizeof(BisectData), "inset_operator_data");
|
||||
gesture->userdata = opdata;
|
||||
gesture->user_data.data = opdata;
|
||||
|
||||
opdata->backup_len = objects_len;
|
||||
opdata->backup = MEM_callocN(sizeof(*opdata->backup) * objects_len, __func__);
|
||||
@@ -193,7 +193,7 @@ static void edbm_bisect_exit(bContext *C, BisectData *opdata)
|
||||
static int mesh_bisect_modal(bContext *C, wmOperator *op, const wmEvent *event)
|
||||
{
|
||||
wmGesture *gesture = op->customdata;
|
||||
BisectData *opdata = gesture->userdata;
|
||||
BisectData *opdata = gesture->user_data.data;
|
||||
BisectData opdata_back = *opdata; /* annoyance, WM_gesture_straightline_modal, frees */
|
||||
int ret;
|
||||
|
||||
@@ -276,7 +276,7 @@ static int mesh_bisect_exec(bContext *C, wmOperator *op)
|
||||
}
|
||||
|
||||
wmGesture *gesture = op->customdata;
|
||||
BisectData *opdata = (gesture != NULL) ? gesture->userdata : NULL;
|
||||
BisectData *opdata = (gesture != NULL) ? gesture->user_data.data : NULL;
|
||||
|
||||
/* -------------------------------------------------------------------- */
|
||||
/* Modal support */
|
||||
|
||||
@@ -695,7 +695,7 @@ static void gradientVertInit__mapFunc(void *userData,
|
||||
static int paint_weight_gradient_modal(bContext *C, wmOperator *op, const wmEvent *event)
|
||||
{
|
||||
wmGesture *gesture = op->customdata;
|
||||
WPGradient_vertStoreBase *vert_cache = gesture->userdata;
|
||||
WPGradient_vertStoreBase *vert_cache = gesture->user_data.data;
|
||||
int ret = WM_gesture_straightline_modal(C, op, event);
|
||||
|
||||
if (ret & OPERATOR_RUNNING_MODAL) {
|
||||
@@ -751,15 +751,15 @@ static int paint_weight_gradient_exec(bContext *C, wmOperator *op)
|
||||
WPGradient_userData data = {NULL};
|
||||
|
||||
if (is_interactive) {
|
||||
if (gesture->userdata == NULL) {
|
||||
gesture->userdata = MEM_mallocN(sizeof(WPGradient_vertStoreBase) +
|
||||
(sizeof(WPGradient_vertStore) * me->totvert),
|
||||
__func__);
|
||||
gesture->userdata_free = false;
|
||||
if (gesture->user_data.data == NULL) {
|
||||
gesture->user_data.data = MEM_mallocN(sizeof(WPGradient_vertStoreBase) +
|
||||
(sizeof(WPGradient_vertStore) * me->totvert),
|
||||
__func__);
|
||||
gesture->user_data.use_free = false;
|
||||
data.is_init = true;
|
||||
|
||||
wpaint_prev_create(
|
||||
&((WPGradient_vertStoreBase *)gesture->userdata)->wpp, me->dvert, me->totvert);
|
||||
&((WPGradient_vertStoreBase *)gesture->user_data.data)->wpp, me->dvert, me->totvert);
|
||||
|
||||
/* on init only, convert face -> vert sel */
|
||||
if (me->editflag & ME_EDIT_PAINT_FACE_SEL) {
|
||||
@@ -767,7 +767,7 @@ static int paint_weight_gradient_exec(bContext *C, wmOperator *op)
|
||||
}
|
||||
}
|
||||
|
||||
vert_cache = gesture->userdata;
|
||||
vert_cache = gesture->user_data.data;
|
||||
}
|
||||
else {
|
||||
if (ED_wpaint_ensure_data(C, op->reports, 0, NULL) == false) {
|
||||
|
||||
@@ -67,6 +67,7 @@ struct wmMsgSubscribeValue;
|
||||
struct wmOperator;
|
||||
struct wmOperatorType;
|
||||
struct wmPaintCursor;
|
||||
struct wmGenericUserData;
|
||||
|
||||
#ifdef WITH_INPUT_NDOF
|
||||
struct wmNDOFMotionData;
|
||||
@@ -795,6 +796,9 @@ void WM_tooltip_init(struct bContext *C, struct wmWindow *win);
|
||||
void WM_tooltip_refresh(struct bContext *C, struct wmWindow *win);
|
||||
double WM_tooltip_time_closed(void);
|
||||
|
||||
/* wmGenericUserData */
|
||||
void WM_generic_user_data_free(struct wmGenericUserData *user_data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -120,6 +120,13 @@ struct wmWindowManager;
|
||||
/* Include external gizmo API's */
|
||||
#include "gizmo/WM_gizmo_api.h"
|
||||
|
||||
typedef struct wmGenericUserData {
|
||||
void *data;
|
||||
/** When NULL, use #MEM_freeN. */
|
||||
void (*free_fn)(void *data);
|
||||
bool use_free;
|
||||
} wmGenericUserData;
|
||||
|
||||
/* ************** wmOperatorType ************************ */
|
||||
|
||||
/* flag */
|
||||
@@ -442,8 +449,7 @@ typedef struct wmGesture {
|
||||
/* customdata for straight line is a recti: (xmin,ymin) is start, (xmax, ymax) is end */
|
||||
|
||||
/* free pointer to use for operator allocs (if set, its freed on exit)*/
|
||||
void *userdata;
|
||||
bool userdata_free;
|
||||
wmGenericUserData user_data;
|
||||
} wmGesture;
|
||||
|
||||
/* ************** wmEvent ************************ */
|
||||
|
||||
@@ -62,7 +62,7 @@ wmGesture *WM_gesture_new(bContext *C, const wmEvent *event, int type)
|
||||
gesture->type = type;
|
||||
gesture->event_type = event->type;
|
||||
gesture->winrct = ar->winrct;
|
||||
gesture->userdata_free = true; /* Free if userdata is set. */
|
||||
gesture->user_data.use_free = true; /* Free if userdata is set. */
|
||||
gesture->modal_state = GESTURE_MODAL_NOP;
|
||||
|
||||
if (ELEM(type,
|
||||
@@ -106,9 +106,7 @@ void WM_gesture_end(bContext *C, wmGesture *gesture)
|
||||
}
|
||||
BLI_remlink(&win->gesture, gesture);
|
||||
MEM_freeN(gesture->customdata);
|
||||
if (gesture->userdata && gesture->userdata_free) {
|
||||
MEM_freeN(gesture->userdata);
|
||||
}
|
||||
WM_generic_user_data_free(&gesture->user_data);
|
||||
MEM_freeN(gesture);
|
||||
}
|
||||
|
||||
|
||||
@@ -2290,3 +2290,15 @@ void WM_opengl_context_release(void *context)
|
||||
BLI_assert(GPU_framebuffer_active_get() == NULL);
|
||||
GHOST_ReleaseOpenGLContext((GHOST_ContextHandle)context);
|
||||
}
|
||||
|
||||
void WM_generic_user_data_free(wmGenericUserData *wm_userdata)
|
||||
{
|
||||
if (wm_userdata->data && wm_userdata->use_free) {
|
||||
if (wm_userdata->free_fn) {
|
||||
wm_userdata->free_fn(wm_userdata->data);
|
||||
}
|
||||
else {
|
||||
MEM_freeN(wm_userdata->data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user