Split numinput from transform (reusable in other operator).
[blender.git] / source / blender / editors / util / 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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): Jonathan Smith
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 "ED_numinput.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, float increment)
163 {
164         float Val = 0;
165         short idx = n->idx, idx_max = n->idx_max;
166
167         if (event->type == EVT_MODAL_MAP) {
168                 switch (event->val) {
169                 case NUM_MODAL_INCREMENT_UP:
170                         if (!n->ctrl[idx])
171                                 n->ctrl[idx] = 1;
172
173                 n->val[idx] += increment;
174                         break;
175                 case NUM_MODAL_INCREMENT_DOWN:
176                         if (!n->ctrl[idx])
177                                 n->ctrl[idx] = 1;
178
179                 n->val[idx] -= increment;
180                         break;
181                 default:
182                         return 0;
183                 }
184         } else {
185                 switch (event->type) {
186                 case BACKSPACEKEY:
187                         if (n->ctrl[idx] == 0) {
188                                 n->val[0]               =
189                                         n->val[1]       =
190                                         n->val[2]       = 0.0f;
191                                 n->ctrl[0]              =
192                                         n->ctrl[1]      =
193                                         n->ctrl[2]      = 0;
194                                 n->inv[0]               =
195                                         n->inv[1]       =
196                                         n->inv[2]       = 0;
197                         }
198                         else {
199                                 n->val[idx] = 0.0f;
200                                 n->ctrl[idx] = 0;
201                                 n->inv[idx] = 0;
202                         }
203                         break;
204                 case PERIODKEY:
205                 case PADPERIOD:
206                         if (n->flag & NUM_NO_FRACTION)
207                                 return 0;
208
209                         switch (n->ctrl[idx])
210                         {
211                         case 0:
212                         case 1:
213                                 n->ctrl[idx] = 10;
214                                 break;
215                         case -1:
216                                 n->ctrl[idx] = -10;
217                         }
218                         break;
219                 case PADMINUS:
220                         if(event->alt)
221                                 break;
222                 case MINUSKEY:
223                         if (n->flag & NUM_NO_NEGATIVE)
224                                 break;
225
226                         if (n->ctrl[idx]) {
227                                 n->ctrl[idx] *= -1;
228                                 n->val[idx] *= -1;
229                         }
230                         else
231                                 n->ctrl[idx] = -1;
232                         break;
233                 case PADSLASHKEY:
234                 case SLASHKEY:
235                         if (n->flag & NUM_NO_FRACTION)
236                                 return 0;
237
238                         n->inv[idx] = !n->inv[idx];
239                         break;
240                 case TABKEY:
241                         if (idx_max == 0)
242                                 return 0;
243
244                         idx++;
245                         if (idx > idx_max)
246                                 idx = 0;
247                         n->idx = idx;
248                         break;
249                 case PAD9:
250                 case NINEKEY:
251                         Val += 1.0f;
252                 case PAD8:
253                 case EIGHTKEY:
254                         Val += 1.0f;
255                 case PAD7:
256                 case SEVENKEY:
257                         Val += 1.0f;
258                 case PAD6:
259                 case SIXKEY:
260                         Val += 1.0f;
261                 case PAD5:
262                 case FIVEKEY:
263                         Val += 1.0f;
264                 case PAD4:
265                 case FOURKEY:
266                         Val += 1.0f;
267                 case PAD3:
268                 case THREEKEY:
269                         Val += 1.0f;
270                 case PAD2:
271                 case TWOKEY:
272                         Val += 1.0f;
273                 case PAD1:
274                 case ONEKEY:
275                         Val += 1.0f;
276                 case PAD0:
277                 case ZEROKEY:
278                         if (!n->ctrl[idx])
279                                 n->ctrl[idx] = 1;
280
281                         if (fabs(n->val[idx]) > 9999999.0f);
282                         else if (n->ctrl[idx] == 1) {
283                                 n->val[idx] *= 10;
284                                 n->val[idx] += Val;
285                         }
286                         else if (n->ctrl[idx] == -1) {
287                                 n->val[idx] *= 10;
288                                 n->val[idx] -= Val;
289                         }
290                         else {
291                                 /* float resolution breaks when over six digits after comma */
292                                 if( ABS(n->ctrl[idx]) < 10000000) {
293                                         n->val[idx] += Val / (float)n->ctrl[idx];
294                                         n->ctrl[idx] *= 10;
295                                 }
296                         }
297                         break;
298                 default:
299                         return 0;
300                 }
301         }
302         
303         /* REDRAW SINCE NUMBERS HAVE CHANGED */
304         return 1;
305 }