Blender  V2.93
transform_convert_node.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
24 #include "DNA_space_types.h"
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "BLI_listbase.h"
29 #include "BLI_math.h"
30 
31 #include "BKE_context.h"
32 #include "BKE_node.h"
33 #include "BKE_report.h"
34 
35 #include "ED_node.h"
36 
37 #include "UI_interface.h"
38 
39 #include "transform.h"
40 #include "transform_convert.h"
41 #include "transform_snap.h"
42 
43 /* -------------------------------------------------------------------- */
47 /* transcribe given node into TransData2D for Transforming */
48 static void NodeToTransData(TransData *td, TransData2D *td2d, bNode *node, const float dpi_fac)
49 {
50  float locx, locy;
51 
52  /* account for parents (nested nodes) */
53  if (node->parent) {
54  nodeToView(node->parent, node->locx, node->locy, &locx, &locy);
55  }
56  else {
57  locx = node->locx;
58  locy = node->locy;
59  }
60 
61  /* use top-left corner as the transform origin for nodes */
62  /* weirdo - but the node system is a mix of free 2d elements and dpi sensitive UI */
63 #ifdef USE_NODE_CENTER
64  td2d->loc[0] = (locx * dpi_fac) + (BLI_rctf_size_x(&node->totr) * +0.5f);
65  td2d->loc[1] = (locy * dpi_fac) + (BLI_rctf_size_y(&node->totr) * -0.5f);
66 #else
67  td2d->loc[0] = locx * dpi_fac;
68  td2d->loc[1] = locy * dpi_fac;
69 #endif
70  td2d->loc[2] = 0.0f;
71  td2d->loc2d = td2d->loc; /* current location */
72 
73  td->loc = td2d->loc;
74  copy_v3_v3(td->iloc, td->loc);
75  /* use node center instead of origin (top-left corner) */
76  td->center[0] = td2d->loc[0];
77  td->center[1] = td2d->loc[1];
78  td->center[2] = 0.0f;
79 
80  memset(td->axismtx, 0, sizeof(td->axismtx));
81  td->axismtx[2][2] = 1.0f;
82 
83  td->ext = NULL;
84  td->val = NULL;
85 
86  td->flag = TD_SELECTED;
87  td->dist = 0.0f;
88 
89  unit_m3(td->mtx);
90  unit_m3(td->smtx);
91 
92  td->extra = node;
93 }
94 
96 {
97  while ((node = node->parent)) {
98  if (node->flag & NODE_TRANSFORM) {
99  return true;
100  }
101  }
102  return false;
103 }
104 
106 {
107  const float dpi_fac = UI_DPI_FAC;
108  SpaceNode *snode = t->area->spacedata.first;
109 
111 
112  tc->data_len = 0;
113 
114  if (!snode->edittree) {
115  return;
116  }
117 
118  /* Nodes don't support PET and probably never will. */
119  t->flag &= ~T_PROP_EDIT_ALL;
120 
121  /* set transform flags on nodes */
122  LISTBASE_FOREACH (bNode *, node, &snode->edittree->nodes) {
123  if (node->flag & NODE_SELECT && is_node_parent_select(node) == false) {
124  node->flag |= NODE_TRANSFORM;
125  tc->data_len++;
126  }
127  else {
128  node->flag &= ~NODE_TRANSFORM;
129  }
130  }
131 
132  TransData *td = tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransNode TransData");
133  TransData2D *td2d = tc->data_2d = MEM_callocN(tc->data_len * sizeof(TransData2D),
134  "TransNode TransData2D");
135 
136  LISTBASE_FOREACH (bNode *, node, &snode->edittree->nodes) {
137  if (node->flag & NODE_TRANSFORM) {
138  NodeToTransData(td++, td2d++, node, dpi_fac);
139  }
140  }
141 }
142 
145 /* -------------------------------------------------------------------- */
150 {
151  const float dpi_fac = UI_DPI_FAC;
152 
155 
156  /* flush to 2d vector from internally used 3d vector */
157  for (int i = 0; i < tc->data_len; i++) {
158  TransData *td = &tc->data[i];
159  TransData2D *td2d = &tc->data_2d[i];
160  bNode *node = td->extra;
161 
162  /* weirdo - but the node system is a mix of free 2d elements and dpi sensitive UI */
163 #ifdef USE_NODE_CENTER
164  float locx = (td2d->loc[0] - (BLI_rctf_size_x(&node->totr)) * +0.5f) / dpi_fac;
165  float locy = (td2d->loc[1] - (BLI_rctf_size_y(&node->totr)) * -0.5f) / dpi_fac;
166 #else
167  float locx = td2d->loc[0] / dpi_fac;
168  float locy = td2d->loc[1] / dpi_fac;
169 #endif
170 
171  /* account for parents (nested nodes) */
172  if (node->parent) {
173  nodeFromView(node->parent, locx, locy, &node->locx, &node->locy);
174  }
175  else {
176  node->locx = locx;
177  node->locy = locy;
178  }
179  }
180 
181  /* handle intersection with noodles */
182  if (tc->data_len == 1) {
183  ED_node_link_intersect_test(t->area, 1);
184  }
185  }
186 }
187 
190 /* -------------------------------------------------------------------- */
195 {
196  struct Main *bmain = CTX_data_main(C);
197  const bool canceled = (t->state == TRANS_CANCEL);
198 
199  SpaceNode *snode = (SpaceNode *)t->area->spacedata.first;
200  if (canceled && t->remove_on_cancel) {
201  /* remove selected nodes on cancel */
202  bNodeTree *ntree = snode->edittree;
203  if (ntree) {
205  if (node->flag & NODE_SELECT) {
206  nodeRemoveNode(bmain, ntree, node, true);
207  }
208  }
209  ntreeUpdateTree(bmain, ntree);
210  }
211  }
212 
213  if (!canceled) {
215  ED_node_link_insert(bmain, t->area);
216  }
217 
218  /* clear link line */
219  ED_node_link_intersect_test(t->area, 0);
220 }
221 
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void nodeFromView(const struct bNode *node, float x, float y, float *rx, float *ry)
void nodeRemoveNode(struct Main *bmain, struct bNodeTree *ntree, struct bNode *node, bool do_id_user)
Definition: node.cc:2932
void ntreeUpdateTree(struct Main *main, struct bNodeTree *ntree)
Definition: node.cc:4262
void nodeToView(const struct bNode *node, float x, float y, float *rx, float *ry)
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:172
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
Definition: BLI_listbase.h:188
void unit_m3(float m[3][3])
Definition: math_matrix.c:58
MINLINE void copy_v3_v3(float r[3], const float a[3])
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition: BLI_rect.h:161
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition: BLI_rect.h:165
#define NODE_SELECT
#define NODE_TRANSFORM
void ED_node_link_insert(struct Main *bmain, struct ScrArea *area)
void ED_node_post_apply_transform(struct bContext *C, struct bNodeTree *ntree)
void ED_node_link_intersect_test(struct ScrArea *area, int test)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
Read Guarded memory(de)allocation.
#define C
Definition: RandGen.cpp:39
#define UI_DPI_FAC
Definition: UI_interface.h:309
OperationNode * node
bNodeTree * ntree
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
Definition: BKE_main.h:116
struct bNodeTree * edittree
float loc[3]
TransData * data
Definition: transform.h:448
TransData2D * data_2d
Definition: transform.h:452
float smtx[3][3]
float axismtx[3][3]
float mtx[3][3]
TransDataExtension * ext
float * val
ListBase nodes
@ T_PROP_EDIT_ALL
Definition: transform.h:114
#define TRANS_DATA_CONTAINER_FIRST_SINGLE(t)
Definition: transform.h:810
@ TRANS_CANCEL
Definition: transform.h:193
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition: transform.h:813
conversion and adaptation of different datablocks to a common struct.
static void NodeToTransData(TransData *td, TransData2D *td2d, bNode *node, const float dpi_fac)
void createTransNodeData(TransInfo *t)
void flushTransNodes(TransInfo *t)
static bool is_node_parent_select(bNode *node)
void special_aftertrans_update__node(bContext *C, TransInfo *t)
@ TD_SELECTED
void applyGridAbsolute(TransInfo *t)