View3D Snap Cursor: don't limit the number of states

The benefit of a flat array in this case is small and limiting, so use a linklist.
This commit is contained in:
Germano Cavalcante 2021-10-25 20:16:28 -03:00
parent 40f59b5dad
commit 32cc9ff037
Notes: blender-bot 2023-02-14 06:49:54 +01:00
Referenced by commit b69195dd13, Fix memory leak in cursor snap deactivation
2 changed files with 18 additions and 50 deletions

View File

@ -524,10 +524,6 @@ static void view3d_ob_drop_draw_activate(struct wmDropBox *drop, wmDrag *drag)
}
state = drop->draw_data = ED_view3d_cursor_snap_active();
if (!state) {
/* The maximum snap status stack value has been reached. */
return;
}
float dimensions[3] = {0.0f};
if (drag->type == WM_DRAG_ID) {

View File

@ -54,22 +54,19 @@
#include "WM_api.h"
#define STATE_LEN 8
#define STATE_INTERN_GET(state) \
(SnapStateIntern *)((char *)state - offsetof(SnapStateIntern, snap_state))
typedef struct SnapStateIntern {
struct SnapStateIntern *next, *prev;
V3DSnapCursorState snap_state;
int state_active_prev;
bool is_active;
} SnapStateIntern;
typedef struct SnapCursorDataIntern {
V3DSnapCursorState state_default;
SnapStateIntern state_intern[STATE_LEN];
ListBase state_intern;
V3DSnapCursorData snap_data;
int state_active_len;
int state_active;
struct SnapObjectContext *snap_context_v3d;
const Scene *scene;
short snap_elem_hidden;
@ -852,10 +849,11 @@ static void v3d_cursor_snap_draw_fn(bContext *C, int x, int y, void *UNUSED(cust
V3DSnapCursorState *ED_view3d_cursor_snap_state_get(void)
{
if (!g_data_intern.state_active_len) {
SnapCursorDataIntern *data_intern = &g_data_intern;
if (BLI_listbase_is_empty(&data_intern->state_intern)) {
return &g_data_intern.state_default;
}
return (V3DSnapCursorState *)&g_data_intern.state_intern[g_data_intern.state_active];
return &((SnapStateIntern *)data_intern->state_intern.last)->snap_state;
}
static void v3d_cursor_snap_activate(void)
@ -894,11 +892,7 @@ static void v3d_cursor_snap_free(void)
data_intern->snap_context_v3d = NULL;
}
for (SnapStateIntern *state_intern = data_intern->state_intern;
state_intern < &data_intern->state_intern[STATE_LEN];
state_intern++) {
state_intern->is_active = false;
}
BLI_freelistN(&data_intern->state_intern);
}
void ED_view3d_cursor_snap_state_default_set(V3DSnapCursorState *state)
@ -909,51 +903,29 @@ void ED_view3d_cursor_snap_state_default_set(V3DSnapCursorState *state)
V3DSnapCursorState *ED_view3d_cursor_snap_active(void)
{
SnapCursorDataIntern *data_intern = &g_data_intern;
if (!data_intern->state_active_len) {
if (!data_intern->handle) {
v3d_cursor_snap_activate();
}
data_intern->state_active_len++;
for (int i = 0; i < STATE_LEN; i++) {
SnapStateIntern *state_intern = &g_data_intern.state_intern[i];
if (!state_intern->is_active) {
state_intern->snap_state = g_data_intern.state_default;
state_intern->is_active = true;
state_intern->state_active_prev = data_intern->state_active;
data_intern->state_active = i;
return (V3DSnapCursorState *)state_intern;
}
}
SnapStateIntern *state_intern = MEM_mallocN(sizeof(*state_intern), __func__);
state_intern->snap_state = g_data_intern.state_default;
BLI_addtail(&g_data_intern.state_intern, state_intern);
data_intern->state_active_len--;
return NULL;
return (V3DSnapCursorState *)&state_intern->snap_state;
}
void ED_view3d_cursor_snap_deactive(V3DSnapCursorState *state)
{
SnapCursorDataIntern *data_intern = &g_data_intern;
if (!data_intern->state_active_len) {
BLI_assert(false);
if (BLI_listbase_is_empty(&data_intern->state_intern)) {
return;
}
if (!state) {
return;
}
SnapStateIntern *state_intern = (SnapStateIntern *)state;
if (!state_intern->is_active) {
return;
}
state_intern->is_active = false;
data_intern->state_active_len--;
if (!data_intern->state_active_len) {
SnapStateIntern *state_intern = STATE_INTERN_GET(state);
BLI_remlink(&data_intern->state_intern, state_intern);
if (BLI_listbase_is_empty(&data_intern->state_intern)) {
v3d_cursor_snap_free();
}
else {
data_intern->state_active = state_intern->state_active_prev;
}
}
void ED_view3d_cursor_snap_prevpoint_set(V3DSnapCursorState *state, const float prev_point[3])
@ -977,7 +949,7 @@ V3DSnapCursorData *ED_view3d_cursor_snap_data_get(V3DSnapCursorState *state,
const int y)
{
SnapCursorDataIntern *data_intern = &g_data_intern;
if (C && data_intern->state_active_len) {
if (C) {
wmWindowManager *wm = CTX_wm_manager(C);
if (v3d_cursor_eventstate_has_changed(data_intern, state, wm, x, y)) {
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);