Cleanup: Use typed enum for node resize direction

This commit is contained in:
2021-12-03 11:05:59 -05:00
parent ab927f5ca7
commit cb0fbe1fde
5 changed files with 30 additions and 23 deletions

View File

@@ -25,6 +25,7 @@
#include "BLI_compiler_compat.h" #include "BLI_compiler_compat.h"
#include "BLI_ghash.h" #include "BLI_ghash.h"
#include "BLI_utildefines.h"
#include "DNA_listBase.h" #include "DNA_listBase.h"
@@ -222,6 +223,16 @@ typedef int (*NodeGPUExecFunction)(struct GPUMaterial *mat,
struct GPUNodeStack *in, struct GPUNodeStack *in,
struct GPUNodeStack *out); struct GPUNodeStack *out);
typedef enum NodeResizeDirection {
NODE_RESIZE_NONE = 0,
NODE_RESIZE_TOP = (1 << 0),
NODE_RESIZE_BOTTOM = (1 << 1),
NODE_RESIZE_RIGHT = (1 << 2),
NODE_RESIZE_LEFT = (1 << 3),
} NodeResizeDirection;
ENUM_OPERATORS(NodeResizeDirection, NODE_RESIZE_LEFT);
/** /**
* \brief Defines a node type. * \brief Defines a node type.
* *
@@ -274,7 +285,7 @@ typedef struct bNodeType {
*/ */
void (*labelfunc)(struct bNodeTree *ntree, struct bNode *node, char *label, int maxlen); void (*labelfunc)(struct bNodeTree *ntree, struct bNode *node, char *label, int maxlen);
/** Optional custom resize handle polling. */ /** Optional custom resize handle polling. */
int (*resize_area_func)(struct bNode *node, int x, int y); NodeResizeDirection (*resize_area_func)(struct bNode *node, int x, int y);
/** Optional selection area polling. */ /** Optional selection area polling. */
int (*select_area_func)(struct bNode *node, int x, int y); int (*select_area_func)(struct bNode *node, int x, int y);
/** Optional tweak area polling (for grabbing). */ /** Optional tweak area polling (for grabbing). */
@@ -379,12 +390,6 @@ typedef struct bNodeType {
#define NODE_CLASS_ATTRIBUTE 42 #define NODE_CLASS_ATTRIBUTE 42
#define NODE_CLASS_LAYOUT 100 #define NODE_CLASS_LAYOUT 100
/* node resize directions */
#define NODE_RESIZE_TOP 1
#define NODE_RESIZE_BOTTOM 2
#define NODE_RESIZE_RIGHT 4
#define NODE_RESIZE_LEFT 8
typedef enum eNodeSizePreset { typedef enum eNodeSizePreset {
NODE_SIZE_DEFAULT, NODE_SIZE_DEFAULT,
NODE_SIZE_SMALL, NODE_SIZE_SMALL,

View File

@@ -250,7 +250,7 @@ static void node_buts_math(uiLayout *layout, bContext *UNUSED(C), PointerRNA *pt
uiItemR(layout, ptr, "use_clamp", DEFAULT_FLAGS, nullptr, ICON_NONE); uiItemR(layout, ptr, "use_clamp", DEFAULT_FLAGS, nullptr, ICON_NONE);
} }
static int node_resize_area_default(bNode *node, int x, int y) static NodeResizeDirection node_resize_area_default(bNode *node, const int x, const int y)
{ {
if (node->flag & NODE_HIDDEN) { if (node->flag & NODE_HIDDEN) {
rctf totr = node->totr; rctf totr = node->totr;
@@ -260,12 +260,12 @@ static int node_resize_area_default(bNode *node, int x, int y)
return NODE_RESIZE_RIGHT; return NODE_RESIZE_RIGHT;
} }
return 0; return NODE_RESIZE_NONE;
} }
const float size = NODE_RESIZE_MARGIN; const float size = NODE_RESIZE_MARGIN;
rctf totr = node->totr; rctf totr = node->totr;
int dir = 0; NodeResizeDirection dir = NODE_RESIZE_NONE;
if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax) { if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax) {
dir |= NODE_RESIZE_RIGHT; dir |= NODE_RESIZE_RIGHT;
@@ -478,18 +478,19 @@ static void node_draw_frame(const bContext *C,
node->block = nullptr; node->block = nullptr;
} }
static int node_resize_area_frame(bNode *node, int x, int y) static NodeResizeDirection node_resize_area_frame(bNode *node, const int x, const int y)
{ {
const float size = 10.0f; const float size = 10.0f;
NodeFrame *data = (NodeFrame *)node->storage; NodeFrame *data = (NodeFrame *)node->storage;
rctf totr = node->totr; rctf totr = node->totr;
int dir = 0;
/* shrinking frame size is determined by child nodes */ /* shrinking frame size is determined by child nodes */
if (!(data->flag & NODE_FRAME_RESIZEABLE)) { if (!(data->flag & NODE_FRAME_RESIZEABLE)) {
return 0; return NODE_RESIZE_NONE;
} }
NodeResizeDirection dir = NODE_RESIZE_NONE;
if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax) { if (x >= totr.xmax - size && x < totr.xmax && y >= totr.ymin && y < totr.ymax) {
dir |= NODE_RESIZE_RIGHT; dir |= NODE_RESIZE_RIGHT;
} }

View File

@@ -2284,7 +2284,7 @@ static void node_draw_hidden(const bContext *C,
node->block = nullptr; node->block = nullptr;
} }
int node_get_resize_cursor(int directions) int node_get_resize_cursor(NodeResizeDirection directions)
{ {
if (directions == 0) { if (directions == 0) {
return WM_CURSOR_DEFAULT; return WM_CURSOR_DEFAULT;
@@ -2317,7 +2317,7 @@ void node_set_cursor(wmWindow *win, SpaceNode *snode, float cursor[2])
} }
} }
if (node) { if (node) {
int dir = node->typeinfo->resize_area_func(node, cursor[0], cursor[1]); NodeResizeDirection dir = node->typeinfo->resize_area_func(node, cursor[0], cursor[1]);
wmcursor = node_get_resize_cursor(dir); wmcursor = node_get_resize_cursor(dir);
} }
} }

View File

@@ -938,13 +938,15 @@ struct NodeSizeWidget {
int directions; int directions;
}; };
static void node_resize_init( static void node_resize_init(bContext *C,
bContext *C, wmOperator *op, const wmEvent *UNUSED(event), bNode *node, int dir) wmOperator *op,
const wmEvent *UNUSED(event),
bNode *node,
NodeResizeDirection dir)
{ {
SpaceNode *snode = CTX_wm_space_node(C); SpaceNode *snode = CTX_wm_space_node(C);
NodeSizeWidget *nsw = (NodeSizeWidget *)MEM_callocN(sizeof(NodeSizeWidget), NodeSizeWidget *nsw = (NodeSizeWidget *)MEM_callocN(sizeof(NodeSizeWidget), __func__);
"size widget op data");
op->customdata = nsw; op->customdata = nsw;
nsw->mxstart = snode->runtime->cursor[0] * UI_DPI_FAC; nsw->mxstart = snode->runtime->cursor[0] * UI_DPI_FAC;
@@ -1090,15 +1092,14 @@ static int node_resize_invoke(bContext *C, wmOperator *op, const wmEvent *event)
SpaceNode *snode = CTX_wm_space_node(C); SpaceNode *snode = CTX_wm_space_node(C);
ARegion *region = CTX_wm_region(C); ARegion *region = CTX_wm_region(C);
bNode *node = nodeGetActive(snode->edittree); bNode *node = nodeGetActive(snode->edittree);
int dir;
if (node) { if (node) {
float cursor[2]; float cursor[2];
/* convert mouse coordinates to v2d space */ /* convert mouse coordinates to v2d space */
UI_view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &cursor[0], &cursor[1]); UI_view2d_region_to_view(&region->v2d, event->mval[0], event->mval[1], &cursor[0], &cursor[1]);
dir = node->typeinfo->resize_area_func(node, cursor[0], cursor[1]); const NodeResizeDirection dir = node->typeinfo->resize_area_func(node, cursor[0], cursor[1]);
if (dir != 0) { if (dir != NODE_RESIZE_NONE) {
node_resize_init(C, op, event, node, dir); node_resize_init(C, op, event, node, dir);
return OPERATOR_RUNNING_MODAL; return OPERATOR_RUNNING_MODAL;
} }

View File

@@ -97,7 +97,7 @@ void node_link_calculate_multi_input_position(const float socket_x,
int node_get_colorid(bNode *node); int node_get_colorid(bNode *node);
void node_draw_extra_info_panel(const SpaceNode *snode, const bNode *node); void node_draw_extra_info_panel(const SpaceNode *snode, const bNode *node);
int node_get_resize_cursor(int directions); int node_get_resize_cursor(NodeResizeDirection directions);
void node_draw_shadow(const SpaceNode *snode, const bNode *node, float radius, float alpha); void node_draw_shadow(const SpaceNode *snode, const bNode *node, float radius, float alpha);
void node_draw_default(const bContext *C, void node_draw_default(const bContext *C,
ARegion *region, ARegion *region,