Context operators for adjusting context values directly to avoid adding operators...
[blender.git] / source / blender / editors / transform / transform_numinput.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include <math.h>                       /* fabs */
31 #include <stdio.h>                      /* for sprintf          */
32
33 #include "BKE_global.h"         /* for G                        */
34 #include "BKE_utildefines.h"    /* ABS */
35
36 #include "WM_types.h"
37 #include "DNA_windowmanager_types.h"
38
39 #include "transform.h"
40
41 /* ************************** Functions *************************** */
42
43 /* ************************** NUMINPUT **************************** */
44
45 void initNumInput(NumInput *n)
46 {
47         n->flag         =
48         n->idx          =
49         n->idx_max      =
50         n->inv[0]   =
51         n->inv[1]   =
52         n->inv[2]   =
53         n->ctrl[0]      = 
54         n->ctrl[1]      = 
55         n->ctrl[2]      = 0;
56
57         n->val[0]               = 
58         n->val[1]       = 
59         n->val[2]       = 0.0f;
60 }
61
62 void outputNumInput(NumInput *n, char *str)
63 {
64         char cur;
65         char inv[] = "1/";
66         short i, j;
67
68         for (j=0; j<=n->idx_max; j++) {
69                 /* if AFFECTALL and no number typed and cursor not on number, use first number */
70                 if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
71                         i = 0;
72                 else
73                         i = j;
74
75                 if (n->idx != i)
76                         cur = ' ';
77                 else
78                         cur = '|';
79
80                 if (n->inv[i])
81                         inv[0] = '1';
82                 else
83                         inv[0] = 0;
84
85                 if( n->val[i] > 1e10 || n->val[i] < -1e10 )
86                         sprintf(&str[j*20], "%s%.4e%c", inv, n->val[i], cur);
87                 else
88                         switch (n->ctrl[i]) {
89                         case 0:
90                                 sprintf(&str[j*20], "%sNONE%c", inv, cur);
91                                 break;
92                         case 1:
93                         case -1:
94                                 sprintf(&str[j*20], "%s%.0f%c", inv, n->val[i], cur);
95                                 break;
96                         case 10:
97                         case -10:
98                                 sprintf(&str[j*20], "%s%.f.%c", inv, n->val[i], cur);
99                                 break;
100                         case 100:
101                         case -100:
102                                 sprintf(&str[j*20], "%s%.1f%c", inv, n->val[i], cur);
103                                 break;
104                         case 1000:
105                         case -1000:
106                                 sprintf(&str[j*20], "%s%.2f%c", inv, n->val[i], cur);
107                                 break;
108                         case 10000:
109                         case -10000:
110                                 sprintf(&str[j*20], "%s%.3f%c", inv, n->val[i], cur);
111                                 break;
112                         default:
113                                 sprintf(&str[j*20], "%s%.4e%c", inv, n->val[i], cur);
114                         }
115         }
116 }
117
118 short hasNumInput(NumInput *n)
119 {
120         short i;
121
122         for (i=0; i<=n->idx_max; i++) {
123                 if (n->ctrl[i])
124                         return 1;
125         }
126
127         return 0;
128 }
129
130 void applyNumInput(NumInput *n, float *vec)
131 {
132         short i, j;
133
134         if (hasNumInput(n)) {
135                 for (j=0; j<=n->idx_max; j++) {
136                         /* if AFFECTALL and no number typed and cursor not on number, use first number */
137                         if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
138                                 i = 0;
139                         else
140                                 i = j;
141
142                         if (n->ctrl[i] == 0 && n->flag & NUM_NULL_ONE) {
143                                 vec[j] = 1.0f;
144                         }
145                         else if (n->val[i] == 0.0f && n->flag & NUM_NO_ZERO) {
146                                 vec[j] = 0.0001f;
147                         }
148                         else {
149                                 if (n->inv[i])
150                                 {
151                                         vec[j] = 1.0f / n->val[i];
152                                 }
153                                 else
154                                 {
155                                         vec[j] = n->val[i];
156                                 }
157                         }
158                 }
159         }
160 }
161
162 char handleNumInput(NumInput *n, wmEvent *event)
163 {
164         float Val = 0;
165         short idx = n->idx, idx_max = n->idx_max;
166
167         switch (event->type) {
168         case BACKSPACEKEY:
169                 if (n->ctrl[idx] == 0) {
170                         n->val[0]               = 
171                                 n->val[1]       = 
172                                 n->val[2]       = 0.0f;
173                         n->ctrl[0]              = 
174                                 n->ctrl[1]      = 
175                                 n->ctrl[2]      = 0;
176                         n->inv[0]               = 
177                                 n->inv[1]       = 
178                                 n->inv[2]       = 0;
179                 }
180                 else {
181                         n->val[idx] = 0.0f;
182                         n->ctrl[idx] = 0;
183                         n->inv[idx] = 0;
184                 }
185                 break;
186         case PERIODKEY:
187         case PADPERIOD:
188                 if (n->flag & NUM_NO_FRACTION)
189                         break;
190
191                 switch (n->ctrl[idx])
192                 {
193                 case 0:
194                 case 1:
195                         n->ctrl[idx] = 10;
196                         break;
197                 case -1:
198                         n->ctrl[idx] = -10;
199                 }
200                 break;
201         case PADMINUS:
202                 if(event->alt)
203                         break;
204         case MINUSKEY:
205                 if (n->flag & NUM_NO_NEGATIVE)
206                         break;
207
208                 if (n->ctrl[idx]) {
209                         n->ctrl[idx] *= -1;
210                         n->val[idx] *= -1;
211                 }
212                 else
213                         n->ctrl[idx] = -1;
214                 break;
215         case PADSLASHKEY:
216         case SLASHKEY:
217                 n->inv[idx] = !n->inv[idx];
218                 break;
219         case TABKEY:
220                 idx++;
221                 if (idx > idx_max)
222                         idx = 0;
223                 n->idx = idx;
224                 break;
225         case PAD9:
226         case NINEKEY:
227                 Val += 1.0f;
228         case PAD8:
229         case EIGHTKEY:
230                 Val += 1.0f;
231         case PAD7:
232         case SEVENKEY:
233                 Val += 1.0f;
234         case PAD6:
235         case SIXKEY:
236                 Val += 1.0f;
237         case PAD5:
238         case FIVEKEY:
239                 Val += 1.0f;
240         case PAD4:
241         case FOURKEY:
242                 Val += 1.0f;
243         case PAD3:
244         case THREEKEY:
245                 Val += 1.0f;
246         case PAD2:
247         case TWOKEY:
248                 Val += 1.0f;
249         case PAD1:
250         case ONEKEY:
251                 Val += 1.0f;
252         case PAD0:
253         case ZEROKEY:
254                 if (!n->ctrl[idx])
255                         n->ctrl[idx] = 1;
256
257                 if (fabs(n->val[idx]) > 9999999.0f);
258                 else if (n->ctrl[idx] == 1) {
259                         n->val[idx] *= 10;
260                         n->val[idx] += Val;
261                 }
262                 else if (n->ctrl[idx] == -1) {
263                         n->val[idx] *= 10;
264                         n->val[idx] -= Val;
265                 }
266                 else {
267                         /* float resolution breaks when over six digits after comma */
268                         if( ABS(n->ctrl[idx]) < 10000000) {
269                                 n->val[idx] += Val / (float)n->ctrl[idx];
270                                 n->ctrl[idx] *= 10;
271                         }
272                 }
273                 break;
274         default:
275                 return 0;
276         }
277         
278         /* REDRAW SINCE NUMBERS HAVE CHANGED */
279         return 1;
280 }