Cleanup: Store runtime space node variables in a separate struct

This commit moves runtime-only variables from the `SpaceNode`
DNA struct to a private struct in `node_intern.h`. Before, it was hard
to tell which data needed to be saved in files, this should make it
more clear.

Node that the `edittree` field is basically a runtime variable, since
it's set from the `treepath` list on read, but moving it would require
some more invasive changes that I don't think are worth it right now.
Also, not all of the moved variables were explicitly cleared on read--
`aspect` is set at the start of a redraw, `cursor` is set in a region
callback, and `recalc` was used as an update flag.

Differential Revision: https://developer.blender.org/D10141
This commit is contained in:
Hans Goudey 2021-01-19 16:43:08 -06:00
parent da6dea5701
commit 6290091bac
11 changed files with 133 additions and 75 deletions

View File

@ -1599,8 +1599,7 @@ static void direct_link_area(BlendDataReader *reader, ScrArea *area)
BLO_read_list(reader, &snode->treepath);
snode->edittree = NULL;
snode->iofsd = NULL;
BLI_listbase_clear(&snode->linkdrag);
snode->runtime = NULL;
}
else if (sl->spacetype == SPACE_TEXT) {
SpaceText *st = (SpaceText *)sl;

View File

@ -51,6 +51,10 @@ typedef enum {
#define NODE_GRID_STEPS 5
/* space_node.c */
void ED_node_cursor_location_get(const struct SpaceNode *snode, float value[2]);
void ED_node_cursor_location_set(struct SpaceNode *snode, const float value[2]);
int ED_node_tree_path_length(struct SpaceNode *snode);
void ED_node_tree_path_get(struct SpaceNode *snode, char *value);
void ED_node_tree_path_get_fixedbuf(struct SpaceNode *snode, char *value, int max_length);

View File

@ -455,7 +455,7 @@ static void node_draw_frame(const bContext *C,
}
/* label */
node_draw_frame_label(ntree, node, snode->aspect);
node_draw_frame_label(ntree, node, snode->runtime->aspect);
UI_block_end(C, node->block);
UI_block_draw(C, node->block);
@ -3833,10 +3833,10 @@ static bool node_link_bezier_handles(const View2D *v2d,
float cursor[2] = {0.0f, 0.0f};
/* this function can be called with snode null (via cut_links_intersect) */
/* XXX map snode->cursor back to view space */
/* XXX map snode->runtime->cursor back to view space */
if (snode) {
cursor[0] = snode->cursor[0] * UI_DPI_FAC;
cursor[1] = snode->cursor[1] * UI_DPI_FAC;
cursor[0] = snode->runtime->cursor[0] * UI_DPI_FAC;
cursor[1] = snode->runtime->cursor[1] * UI_DPI_FAC;
}
/* in v0 and v3 we put begin/end points */
@ -4107,7 +4107,7 @@ static void nodelink_batch_draw(const SpaceNode *snode)
GPU_batch_program_set_builtin(g_batch_link.batch, GPU_SHADER_2D_NODELINK_INST);
GPU_batch_uniform_4fv_array(g_batch_link.batch, "colors", 6, colors);
GPU_batch_uniform_1f(g_batch_link.batch, "expandSize", snode->aspect * LINK_WIDTH);
GPU_batch_uniform_1f(g_batch_link.batch, "expandSize", snode->runtime->aspect * LINK_WIDTH);
GPU_batch_uniform_1f(g_batch_link.batch, "arrowSize", ARROW_SIZE);
GPU_batch_draw(g_batch_link.batch);
@ -4194,7 +4194,7 @@ void node_draw_link_bezier(const View2D *v2d,
GPU_batch_program_set_builtin(batch, GPU_SHADER_2D_NODELINK);
GPU_batch_uniform_2fv_array(batch, "bezierPts", 4, vec);
GPU_batch_uniform_4fv_array(batch, "colors", 3, colors);
GPU_batch_uniform_1f(batch, "expandSize", snode->aspect * LINK_WIDTH);
GPU_batch_uniform_1f(batch, "expandSize", snode->runtime->aspect * LINK_WIDTH);
GPU_batch_uniform_1f(batch, "arrowSize", ARROW_SIZE);
GPU_batch_uniform_1i(batch, "doArrow", drawarrow);
GPU_batch_draw(batch);

View File

@ -341,7 +341,7 @@ static int node_add_file_exec(bContext *C, wmOperator *op)
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
node = node_add_node(C, NULL, type, snode->cursor[0], snode->cursor[1]);
node = node_add_node(C, NULL, type, snode->runtime->cursor[0], snode->runtime->cursor[1]);
if (!node) {
BKE_report(op->reports, RPT_WARNING, "Could not add an image node");
@ -370,11 +370,14 @@ static int node_add_file_invoke(bContext *C, wmOperator *op, const wmEvent *even
SpaceNode *snode = CTX_wm_space_node(C);
/* convert mouse coordinates to v2d space */
UI_view2d_region_to_view(
&region->v2d, event->mval[0], event->mval[1], &snode->cursor[0], &snode->cursor[1]);
UI_view2d_region_to_view(&region->v2d,
event->mval[0],
event->mval[1],
&snode->runtime->cursor[0],
&snode->runtime->cursor[1]);
snode->cursor[0] /= UI_DPI_FAC;
snode->cursor[1] /= UI_DPI_FAC;
snode->runtime->cursor[0] /= UI_DPI_FAC;
snode->runtime->cursor[1] /= UI_DPI_FAC;
if (RNA_struct_property_is_set(op->ptr, "filepath") ||
RNA_struct_property_is_set(op->ptr, "name")) {
@ -435,7 +438,8 @@ static int node_add_mask_exec(bContext *C, wmOperator *op)
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
node = node_add_node(C, NULL, CMP_NODE_MASK, snode->cursor[0], snode->cursor[1]);
node = node_add_node(
C, NULL, CMP_NODE_MASK, snode->runtime->cursor[0], snode->runtime->cursor[1]);
if (!node) {
BKE_report(op->reports, RPT_WARNING, "Could not add a mask node");

View File

@ -951,7 +951,7 @@ void node_draw_shadow(const SpaceNode *snode, const bNode *node, float radius, f
{
const rctf *rct = &node->totr;
UI_draw_roundbox_corner_set(UI_CNR_ALL);
ui_draw_dropshadow(rct, radius, snode->aspect, alpha, node->flag & SELECT);
ui_draw_dropshadow(rct, radius, snode->runtime->aspect, alpha, node->flag & SELECT);
}
void node_draw_sockets(const View2D *v2d,
@ -1473,19 +1473,19 @@ static void node_draw_hidden(const bContext *C,
immVertex2f(pos, rct->xmax - dx, centy - 4.0f);
immVertex2f(pos, rct->xmax - dx, centy + 4.0f);
immVertex2f(pos, rct->xmax - dx - 3.0f * snode->aspect, centy - 4.0f);
immVertex2f(pos, rct->xmax - dx - 3.0f * snode->aspect, centy + 4.0f);
immVertex2f(pos, rct->xmax - dx - 3.0f * snode->runtime->aspect, centy - 4.0f);
immVertex2f(pos, rct->xmax - dx - 3.0f * snode->runtime->aspect, centy + 4.0f);
immEnd();
immUniformThemeColorShade(color_id, 30);
dx -= snode->aspect;
dx -= snode->runtime->aspect;
immBegin(GPU_PRIM_LINES, 4);
immVertex2f(pos, rct->xmax - dx, centy - 4.0f);
immVertex2f(pos, rct->xmax - dx, centy + 4.0f);
immVertex2f(pos, rct->xmax - dx - 3.0f * snode->aspect, centy - 4.0f);
immVertex2f(pos, rct->xmax - dx - 3.0f * snode->aspect, centy + 4.0f);
immVertex2f(pos, rct->xmax - dx - 3.0f * snode->runtime->aspect, centy - 4.0f);
immVertex2f(pos, rct->xmax - dx - 3.0f * snode->runtime->aspect, centy + 4.0f);
immEnd();
immUnbindProgram();
@ -1661,7 +1661,7 @@ static void snode_setup_v2d(SpaceNode *snode, ARegion *region, const float cente
UI_view2d_view_ortho(v2d);
/* aspect+font, set each time */
snode->aspect = BLI_rctf_size_x(&v2d->cur) / (float)region->winx;
snode->runtime->aspect = BLI_rctf_size_x(&v2d->cur) / (float)region->winx;
// XXX snode->curfont = uiSetCurFont_ext(snode->aspect);
}
@ -1717,14 +1717,15 @@ void node_draw_space(const bContext *C, ARegion *region)
GPU_depth_test(GPU_DEPTH_NONE);
GPU_scissor_test(true);
/* XXX snode->cursor set in coordspace for placing new nodes, used for drawing noodles too */
/* XXX snode->runtime->cursor set in coordspace for placing new nodes, used for drawing noodles
* too */
UI_view2d_region_to_view(&region->v2d,
win->eventstate->x - region->winrct.xmin,
win->eventstate->y - region->winrct.ymin,
&snode->cursor[0],
&snode->cursor[1]);
snode->cursor[0] /= UI_DPI_FAC;
snode->cursor[1] /= UI_DPI_FAC;
&snode->runtime->cursor[0],
&snode->runtime->cursor[1]);
snode->runtime->cursor[0] /= UI_DPI_FAC;
snode->runtime->cursor[1] /= UI_DPI_FAC;
int grid_levels = UI_GetThemeValueType(TH_NODE_GRID_LEVELS, SPACE_NODE);
@ -1814,7 +1815,7 @@ void node_draw_space(const bContext *C, ARegion *region)
/* temporary links */
GPU_blend(GPU_BLEND_ALPHA);
GPU_line_smooth(true);
LISTBASE_FOREACH (bNodeLinkDrag *, nldrag, &snode->linkdrag) {
LISTBASE_FOREACH (bNodeLinkDrag *, nldrag, &snode->runtime->linkdrag) {
LISTBASE_FOREACH (LinkData *, linkdata, &nldrag->links) {
node_draw_link(v2d, snode, (bNodeLink *)linkdata->data);
}

View File

@ -880,8 +880,8 @@ static void node_resize_init(
NodeSizeWidget *nsw = MEM_callocN(sizeof(NodeSizeWidget), "size widget op data");
op->customdata = nsw;
nsw->mxstart = snode->cursor[0] * UI_DPI_FAC;
nsw->mystart = snode->cursor[1] * UI_DPI_FAC;
nsw->mxstart = snode->runtime->cursor[0] * UI_DPI_FAC;
nsw->mystart = snode->runtime->cursor[1] * UI_DPI_FAC;
/* store old */
nsw->oldlocx = node->locx;

View File

@ -37,6 +37,7 @@ struct bContext;
struct bNode;
struct bNodeLink;
struct bNodeSocket;
struct NodeInsertOfsData;
struct wmGizmoGroupType;
struct wmKeyConfig;
struct wmWindow;
@ -53,6 +54,23 @@ typedef struct bNodeLinkDrag {
int in_out;
} bNodeLinkDrag;
typedef struct SpaceNode_Runtime {
float aspect;
/** Mouse position for drawing socket-less links and adding nodes. */
float cursor[2];
/** For auto compositing. */
bool recalc;
/** Temporary data for modal linking operator. */
struct ListBase linkdrag;
/* XXX hack for translate_attach op-macros to pass data from transform op to insert_offset op */
/** Temporary data for node insert offset (in UI called Auto-offset). */
struct NodeInsertOfsData *iofsd;
} SpaceNode_Runtime;
/* space_node.c */
/* transform between View2Ds in the tree path */

View File

@ -663,7 +663,7 @@ static void node_link_exit(bContext *C, wmOperator *op, bool apply_links)
snode_dag_update(C, snode);
}
BLI_remlink(&snode->linkdrag, nldrag);
BLI_remlink(&snode->runtime->linkdrag, nldrag);
/* links->data pointers are either held by the tree or freed already */
BLI_freelistN(&nldrag->links);
MEM_freeN(nldrag);
@ -903,7 +903,7 @@ static int node_link_invoke(bContext *C, wmOperator *op, const wmEvent *event)
if (nldrag) {
op->customdata = nldrag;
BLI_addtail(&snode->linkdrag, nldrag);
BLI_addtail(&snode->runtime->linkdrag, nldrag);
/* add modal handler */
WM_event_add_modal_handler(C, op);
@ -918,7 +918,7 @@ static void node_link_cancel(bContext *C, wmOperator *op)
SpaceNode *snode = CTX_wm_space_node(C);
bNodeLinkDrag *nldrag = op->customdata;
BLI_remlink(&snode->linkdrag, nldrag);
BLI_remlink(&snode->runtime->linkdrag, nldrag);
BLI_freelistN(&nldrag->links);
MEM_freeN(nldrag);
@ -1798,7 +1798,7 @@ static void node_link_insert_offset_ntree(NodeInsertOfsData *iofsd,
static int node_insert_offset_modal(bContext *C, wmOperator *UNUSED(op), const wmEvent *event)
{
SpaceNode *snode = CTX_wm_space_node(C);
NodeInsertOfsData *iofsd = snode->iofsd;
NodeInsertOfsData *iofsd = snode->runtime->iofsd;
bool redraw = false;
if (!snode || event->type != TIMER || iofsd == NULL || iofsd->anim_timer != event->customdata) {
@ -1837,7 +1837,7 @@ static int node_insert_offset_modal(bContext *C, wmOperator *UNUSED(op), const w
node->anim_init_locx = node->anim_ofsx = 0.0f;
}
snode->iofsd = NULL;
snode->runtime->iofsd = NULL;
MEM_freeN(iofsd);
return (OPERATOR_FINISHED | OPERATOR_PASS_THROUGH);
@ -1851,7 +1851,7 @@ static int node_insert_offset_modal(bContext *C, wmOperator *UNUSED(op), const w
static int node_insert_offset_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
const SpaceNode *snode = CTX_wm_space_node(C);
NodeInsertOfsData *iofsd = snode->iofsd;
NodeInsertOfsData *iofsd = snode->runtime->iofsd;
if (!iofsd || !iofsd->insert) {
return OPERATOR_CANCELLED;
@ -1927,7 +1927,7 @@ void ED_node_link_insert(Main *bmain, ScrArea *area)
iofsd->prev = link->fromnode;
iofsd->next = node;
snode->iofsd = iofsd;
snode->runtime->iofsd = iofsd;
}
ntreeUpdateTree(bmain, snode->edittree); /* needed for pointers */

View File

@ -321,11 +321,18 @@ static void node_free(SpaceLink *sl)
LISTBASE_FOREACH_MUTABLE (bNodeTreePath *, path, &snode->treepath) {
MEM_freeN(path);
}
MEM_SAFE_FREE(snode->runtime);
}
/* spacetype; init callback */
static void node_init(struct wmWindowManager *UNUSED(wm), ScrArea *UNUSED(area))
static void node_init(struct wmWindowManager *UNUSED(wm), ScrArea *area)
{
SpaceNode *snode = (SpaceNode *)area->spacedata.first;
if (snode->runtime == NULL) {
snode->runtime = MEM_callocN(sizeof(SpaceNode_Runtime), __func__);
}
}
static void node_area_listener(const wmSpaceTypeListenerParams *params)
@ -362,7 +369,7 @@ static void node_area_listener(const wmSpaceTypeListenerParams *params)
case ND_TRANSFORM_DONE:
if (ED_node_is_compositor(snode)) {
if (snode->flag & SNODE_AUTO_RENDER) {
snode->recalc = 1;
snode->runtime->recalc = true;
ED_area_tag_refresh(area);
}
}
@ -521,8 +528,8 @@ static void node_area_refresh(const struct bContext *C, ScrArea *area)
Scene *scene = (Scene *)snode->id;
if (scene->use_nodes) {
/* recalc is set on 3d view changes for auto compo */
if (snode->recalc) {
snode->recalc = 0;
if (snode->runtime->recalc) {
snode->runtime->recalc = false;
node_render_changed_exec((struct bContext *)C, NULL);
}
else {
@ -546,8 +553,10 @@ static SpaceLink *node_duplicate(SpaceLink *sl)
BLI_duplicatelist(&snoden->treepath, &snode->treepath);
/* clear or remove stuff from old */
BLI_listbase_clear(&snoden->linkdrag);
if (snode->runtime != NULL) {
snoden->runtime = MEM_dupallocN(snode->runtime);
BLI_listbase_clear(&snoden->runtime->linkdrag);
}
/* Note: no need to set node tree user counts,
* the editor only keeps at least 1 (id_us_ensure_real),
@ -589,6 +598,16 @@ static void node_toolbar_region_draw(const bContext *C, ARegion *region)
ED_region_panels(C, region);
}
void ED_node_cursor_location_get(const SpaceNode *snode, float value[2])
{
copy_v2_v2(value, snode->runtime->cursor);
}
void ED_node_cursor_location_set(SpaceNode *snode, const float value[2])
{
copy_v2_v2(snode->runtime->cursor, value);
}
static void node_cursor(wmWindow *win, ScrArea *area, ARegion *region)
{
SpaceNode *snode = area->spacedata.first;
@ -597,15 +616,15 @@ static void node_cursor(wmWindow *win, ScrArea *area, ARegion *region)
UI_view2d_region_to_view(&region->v2d,
win->eventstate->x - region->winrct.xmin,
win->eventstate->y - region->winrct.ymin,
&snode->cursor[0],
&snode->cursor[1]);
&snode->runtime->cursor[0],
&snode->runtime->cursor[1]);
/* here snode->cursor is used to detect the node edge for sizing */
node_set_cursor(win, snode, snode->cursor);
/* here snode->runtime->cursor is used to detect the node edge for sizing */
node_set_cursor(win, snode, snode->runtime->cursor);
/* XXX snode->cursor is in placing new nodes space */
snode->cursor[0] /= UI_DPI_FAC;
snode->cursor[1] /= UI_DPI_FAC;
/* XXX snode->runtime->cursor is in placing new nodes space */
snode->runtime->cursor[0] /= UI_DPI_FAC;
snode->runtime->cursor[1] /= UI_DPI_FAC;
}
/* Initialize main region, setting handlers. */

View File

@ -63,6 +63,9 @@ struct wmTimer;
/* Defined in `buttons_intern.h`. */
typedef struct SpaceProperties_Runtime SpaceProperties_Runtime;
/* Defined in `node_intern.h`. */
typedef struct SpaceNode_Runtime SpaceNode_Runtime;
/* -------------------------------------------------------------------- */
/** \name SpaceLink (Base)
* \{ */
@ -1518,19 +1521,17 @@ typedef struct SpaceNode {
/** Context, no need to save in file? well... pinning... */
struct ID *id, *from;
/** Menunr: browse id block in header. */
short flag;
char _pad1[2];
/** Internal state variables. */
float aspect;
char _pad2[4];
/** Direction for offsetting nodes on insertion. */
char insert_ofs_dir;
char _pad1;
/** Offset for drawing the backdrop. */
float xof, yof;
/** Zoom for backdrop. */
float zoom;
/** Mouse pos for drawing socketless link and adding nodes. */
float cursor[2];
/**
* XXX nodetree pointer info is all in the path stack now,
@ -1541,33 +1542,25 @@ typedef struct SpaceNode {
*/
ListBase treepath;
struct bNodeTree *nodetree, *edittree;
/* The tree farthest down in the group heirarchy. */
struct bNodeTree *edittree;
struct bNodeTree *nodetree;
/* tree type for the current node tree */
char tree_idname[64];
/** Treetype: as same nodetree->type. */
int treetype DNA_DEPRECATED;
char _pad3[4];
/** Texfrom object, world or brush. */
short texfrom;
/** Shader from object or world. */
short shaderfrom;
/** Currently on 0/1, for auto compo. */
short recalc;
/** Direction for offsetting nodes on insertion. */
char insert_ofs_dir;
char _pad4;
/** Temporary data for modal linking operator. */
ListBase linkdrag;
/* XXX hack for translate_attach op-macros to pass data from transform op to insert_offset op */
/** Temporary data for node insert offset (in UI called Auto-offset). */
struct NodeInsertOfsData *iofsd;
/** Grease-pencil data. */
struct bGPdata *gpd;
SpaceNode_Runtime *runtime;
} SpaceNode;
/* SpaceNode.flag */

View File

@ -2279,6 +2279,20 @@ static bool rna_SpaceNodeEditor_tree_type_poll(void *Cv, bNodeTreeType *type)
}
}
static void rna_SpaceNodeEditor_cursor_location_get(PointerRNA *ptr, float value[2])
{
const SpaceNode *snode = (SpaceNode *)ptr->data;
ED_node_cursor_location_get(snode, value);
}
static void rna_SpaceNodeEditor_cursor_location_set(PointerRNA *ptr, const float value[2])
{
SpaceNode *snode = (SpaceNode *)ptr->data;
ED_node_cursor_location_set(snode, value);
}
const EnumPropertyItem *RNA_enum_node_tree_types_itemf_impl(bContext *C, bool *r_free)
{
return rna_node_tree_type_itemf(C, rna_SpaceNodeEditor_tree_type_poll, r_free);
@ -2346,9 +2360,13 @@ static void rna_SpaceNodeEditor_cursor_location_from_region(SpaceNode *snode,
{
ARegion *region = CTX_wm_region(C);
UI_view2d_region_to_view(&region->v2d, x, y, &snode->cursor[0], &snode->cursor[1]);
snode->cursor[0] /= UI_DPI_FAC;
snode->cursor[1] /= UI_DPI_FAC;
float cursor_location[2];
UI_view2d_region_to_view(&region->v2d, x, y, &cursor_location[0], &cursor_location[1]);
cursor_location[0] /= UI_DPI_FAC;
cursor_location[1] /= UI_DPI_FAC;
ED_node_cursor_location_set(snode, cursor_location);
}
static void rna_SpaceClipEditor_clip_set(PointerRNA *ptr,
@ -6815,11 +6833,13 @@ static void rna_def_space_node(BlenderRNA *brna)
RNA_def_property_enum_items(prop, backdrop_channels_items);
RNA_def_property_ui_text(prop, "Display Channels", "Channels of the image to draw");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_NODE_VIEW, NULL);
/* the mx/my "cursor" in the node editor is used only by operators to store the mouse position */
prop = RNA_def_property(srna, "cursor_location", PROP_FLOAT, PROP_XYZ);
RNA_def_property_array(prop, 2);
RNA_def_property_float_sdna(prop, NULL, "cursor");
RNA_def_property_float_funcs(prop,
"rna_SpaceNodeEditor_cursor_location_get",
"rna_SpaceNodeEditor_cursor_location_set",
NULL);
RNA_def_property_ui_text(prop, "Cursor Location", "Location for adding new nodes");
RNA_def_property_update(prop, NC_SPACE | ND_SPACE_NODE_VIEW, NULL);