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 
20 /** \file
21  * \ingroup edtransform
22  */
23 
24 #include "DNA_space_types.h"
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "BLI_math.h"
29 
30 #include "BKE_context.h"
31 #include "BKE_node.h"
32 #include "BKE_report.h"
33 
34 #include "ED_node.h"
35 
36 #include "UI_interface.h"
37 
38 #include "transform.h"
39 #include "transform_convert.h"
40 #include "transform_snap.h"
41 
42 /* -------------------------------------------------------------------- */
43 /** \name Node Transform Creation
44  *
45  * \{ */
46 
47 /* transcribe given node into TransData2D for Transforming */
NodeToTransData(TransData * td,TransData2D * td2d,bNode * node,const float dpi_fac)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->flag = 0;
74 
75   td->loc = td2d->loc;
76   copy_v3_v3(td->iloc, td->loc);
77   /* use node center instead of origin (top-left corner) */
78   td->center[0] = td2d->loc[0];
79   td->center[1] = td2d->loc[1];
80   td->center[2] = 0.0f;
81 
82   memset(td->axismtx, 0, sizeof(td->axismtx));
83   td->axismtx[2][2] = 1.0f;
84 
85   td->ext = NULL;
86   td->val = NULL;
87 
88   td->flag |= TD_SELECTED;
89   td->dist = 0.0;
90 
91   unit_m3(td->mtx);
92   unit_m3(td->smtx);
93 
94   td->extra = node;
95 }
96 
is_node_parent_select(bNode * node)97 static bool is_node_parent_select(bNode *node)
98 {
99   while ((node = node->parent)) {
100     if (node->flag & NODE_TRANSFORM) {
101       return true;
102     }
103   }
104   return false;
105 }
106 
createTransNodeData(TransInfo * t)107 void createTransNodeData(TransInfo *t)
108 {
109   const float dpi_fac = UI_DPI_FAC;
110   TransData *td;
111   TransData2D *td2d;
112   SpaceNode *snode = t->area->spacedata.first;
113   bNode *node;
114 
115   TransDataContainer *tc = TRANS_DATA_CONTAINER_FIRST_SINGLE(t);
116 
117   tc->data_len = 0;
118 
119   if (!snode->edittree) {
120     return;
121   }
122 
123   /* nodes dont support PET and probably never will */
124   t->flag &= ~T_PROP_EDIT_ALL;
125 
126   /* set transform flags on nodes */
127   for (node = snode->edittree->nodes.first; node; node = node->next) {
128     if (node->flag & NODE_SELECT && is_node_parent_select(node) == false) {
129       node->flag |= NODE_TRANSFORM;
130       tc->data_len++;
131     }
132     else {
133       node->flag &= ~NODE_TRANSFORM;
134     }
135   }
136 
137   td = tc->data = MEM_callocN(tc->data_len * sizeof(TransData), "TransNode TransData");
138   td2d = tc->data_2d = MEM_callocN(tc->data_len * sizeof(TransData2D), "TransNode TransData2D");
139 
140   for (node = snode->edittree->nodes.first; node; node = node->next) {
141     if (node->flag & NODE_TRANSFORM) {
142       NodeToTransData(td++, td2d++, node, dpi_fac);
143     }
144   }
145 }
146 
147 /** \} */
148 
149 /* -------------------------------------------------------------------- */
150 /** \name Node Transform Creation
151  *
152  * \{ */
153 
flushTransNodes(TransInfo * t)154 void flushTransNodes(TransInfo *t)
155 {
156   const float dpi_fac = UI_DPI_FAC;
157 
158   FOREACH_TRANS_DATA_CONTAINER (t, tc) {
159     int a;
160     TransData *td;
161     TransData2D *td2d;
162 
163     applyGridAbsolute(t);
164 
165     /* flush to 2d vector from internally used 3d vector */
166     for (a = 0, td = tc->data, td2d = tc->data_2d; a < tc->data_len; a++, td++, td2d++) {
167       bNode *node = td->extra;
168       float locx, locy;
169 
170       /* weirdo - but the node system is a mix of free 2d elements and dpi sensitive UI */
171 #ifdef USE_NODE_CENTER
172       locx = (td2d->loc[0] - (BLI_rctf_size_x(&node->totr)) * +0.5f) / dpi_fac;
173       locy = (td2d->loc[1] - (BLI_rctf_size_y(&node->totr)) * -0.5f) / dpi_fac;
174 #else
175       locx = td2d->loc[0] / dpi_fac;
176       locy = td2d->loc[1] / dpi_fac;
177 #endif
178 
179       /* account for parents (nested nodes) */
180       if (node->parent) {
181         nodeFromView(node->parent, locx, locy, &node->locx, &node->locy);
182       }
183       else {
184         node->locx = locx;
185         node->locy = locy;
186       }
187     }
188 
189     /* handle intersection with noodles */
190     if (tc->data_len == 1) {
191       ED_node_link_intersect_test(t->area, 1);
192     }
193   }
194 }
195 
196 /** \} */
197 
198 /* -------------------------------------------------------------------- */
199 /** \name Special After Transform Node
200  * \{ */
201 
special_aftertrans_update__node(bContext * C,TransInfo * t)202 void special_aftertrans_update__node(bContext *C, TransInfo *t)
203 {
204   struct Main *bmain = CTX_data_main(C);
205   const bool canceled = (t->state == TRANS_CANCEL);
206 
207   SpaceNode *snode = (SpaceNode *)t->area->spacedata.first;
208   if (canceled && t->remove_on_cancel) {
209     /* remove selected nodes on cancel */
210     bNodeTree *ntree = snode->edittree;
211     if (ntree) {
212       bNode *node, *node_next;
213       for (node = ntree->nodes.first; node; node = node_next) {
214         node_next = node->next;
215         if (node->flag & NODE_SELECT) {
216           nodeRemoveNode(bmain, ntree, node, true);
217         }
218       }
219       ntreeUpdateTree(bmain, ntree);
220     }
221   }
222 
223   if (!canceled) {
224     ED_node_post_apply_transform(C, snode->edittree);
225     ED_node_link_insert(bmain, t->area);
226   }
227 
228   /* clear link line */
229   ED_node_link_intersect_test(t->area, 0);
230 }
231 
232 /** \} */
233