svn merge -r 31145:31210 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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
34 #include "WM_types.h"
35
36 #include "ED_numinput.h"
37
38 /* ************************** Functions *************************** */
39
40 /* ************************** NUMINPUT **************************** */
41
42 void initNumInput(NumInput *n)
43 {
44         n->flag         =
45         n->idx          =
46         n->idx_max      =
47         n->inv[0]   =
48         n->inv[1]   =
49         n->inv[2]   =
50         n->ctrl[0]      = 
51         n->ctrl[1]      = 
52         n->ctrl[2]      = 0;
53
54         n->val[0]               = 
55         n->val[1]       = 
56         n->val[2]       = 0.0f;
57 }
58
59 void outputNumInput(NumInput *n, char *str)
60 {
61         char cur;
62         char inv[] = "1/";
63         short i, j;
64
65         for (j=0; j<=n->idx_max; j++) {
66                 /* if AFFECTALL and no number typed and cursor not on number, use first number */
67                 if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
68                         i = 0;
69                 else
70                         i = j;
71
72                 if (n->idx != i)
73                         cur = ' ';
74                 else
75                         cur = '|';
76
77                 if (n->inv[i])
78                         inv[0] = '1';
79                 else
80                         inv[0] = 0;
81
82                 if( n->val[i] > 1e10 || n->val[i] < -1e10 )
83                         sprintf(&str[j*20], "%s%.4e%c", inv, n->val[i], cur);
84                 else
85                         switch (n->ctrl[i]) {
86                         case 0:
87                                 sprintf(&str[j*20], "%sNONE%c", inv, cur);
88                                 break;
89                         case 1:
90                         case -1:
91                                 sprintf(&str[j*20], "%s%.0f%c", inv, n->val[i], cur);
92                                 break;
93                         case 10:
94                         case -10:
95                                 sprintf(&str[j*20], "%s%.f.%c", inv, n->val[i], cur);
96                                 break;
97                         case 100:
98                         case -100:
99                                 sprintf(&str[j*20], "%s%.1f%c", inv, n->val[i], cur);
100                                 break;
101                         case 1000:
102                         case -1000:
103                                 sprintf(&str[j*20], "%s%.2f%c", inv, n->val[i], cur);
104                                 break;
105                         case 10000:
106                         case -10000:
107                                 sprintf(&str[j*20], "%s%.3f%c", inv, n->val[i], cur);
108                                 break;
109                         default:
110                                 sprintf(&str[j*20], "%s%.4e%c", inv, n->val[i], cur);
111                         }
112         }
113 }
114
115 short hasNumInput(NumInput *n)
116 {
117         short i;
118
119         for (i=0; i<=n->idx_max; i++) {
120                 if (n->ctrl[i])
121                         return 1;
122         }
123
124         return 0;
125 }
126
127 void applyNumInput(NumInput *n, float *vec)
128 {
129         short i, j;
130
131         if (hasNumInput(n)) {
132                 for (j=0; j<=n->idx_max; j++) {
133                         /* if AFFECTALL and no number typed and cursor not on number, use first number */
134                         if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
135                                 i = 0;
136                         else
137                                 i = j;
138
139                         if (n->ctrl[i] == 0 && n->flag & NUM_NULL_ONE) {
140                                 vec[j] = 1.0f;
141                         }
142                         else if (n->val[i] == 0.0f && n->flag & NUM_NO_ZERO) {
143                                 vec[j] = 0.0001f;
144                         }
145                         else {
146                                 if (n->inv[i])
147                                 {
148                                         vec[j] = 1.0f / n->val[i];
149                                 }
150                                 else
151                                 {
152                                         vec[j] = n->val[i];
153                                 }
154                         }
155                 }
156         }
157 }
158
159 char handleNumInput(NumInput *n, wmEvent *event)
160 {
161         float Val = 0;
162         short idx = n->idx, idx_max = n->idx_max;
163
164         if (event->type == EVT_MODAL_MAP) {
165                 switch (event->val) {
166                 case NUM_MODAL_INCREMENT_UP:
167                         if (!n->ctrl[idx])
168                                 n->ctrl[idx] = 1;
169
170                         n->val[idx] += n->increment;
171                         break;
172                 case NUM_MODAL_INCREMENT_DOWN:
173                         if (!n->ctrl[idx])
174                                 n->ctrl[idx] = 1;
175
176                         n->val[idx] -= n->increment;
177                         break;
178                 default:
179                         return 0;
180                 }
181         } else {
182                 switch (event->type) {
183                 case BACKSPACEKEY:
184                         if (n->ctrl[idx] == 0) {
185                                 n->val[0]               =
186                                         n->val[1]       =
187                                         n->val[2]       = 0.0f;
188                                 n->ctrl[0]              =
189                                         n->ctrl[1]      =
190                                         n->ctrl[2]      = 0;
191                                 n->inv[0]               =
192                                         n->inv[1]       =
193                                         n->inv[2]       = 0;
194                         }
195                         else {
196                                 n->val[idx] = 0.0f;
197                                 n->ctrl[idx] = 0;
198                                 n->inv[idx] = 0;
199                         }
200                         break;
201                 case PERIODKEY:
202                 case PADPERIOD:
203                         if (n->flag & NUM_NO_FRACTION)
204                                 return 0;
205
206                         switch (n->ctrl[idx])
207                         {
208                         case 0:
209                         case 1:
210                                 n->ctrl[idx] = 10;
211                                 break;
212                         case -1:
213                                 n->ctrl[idx] = -10;
214                         }
215                         break;
216                 case PADMINUS:
217                         if(event->alt)
218                                 break;
219                 case MINUSKEY:
220                         if (n->flag & NUM_NO_NEGATIVE)
221                                 break;
222
223                         if (n->ctrl[idx]) {
224                                 n->ctrl[idx] *= -1;
225                                 n->val[idx] *= -1;
226                         }
227                         else
228                                 n->ctrl[idx] = -1;
229                         break;
230                 case PADSLASHKEY:
231                 case SLASHKEY:
232                         if (n->flag & NUM_NO_FRACTION)
233                                 return 0;
234
235                         n->inv[idx] = !n->inv[idx];
236                         break;
237                 case TABKEY:
238                         if (idx_max == 0)
239                                 return 0;
240
241                         idx++;
242                         if (idx > idx_max)
243                                 idx = 0;
244                         n->idx = idx;
245                         break;
246                 case PAD9:
247                 case NINEKEY:
248                         Val += 1.0f;
249                 case PAD8:
250                 case EIGHTKEY:
251                         Val += 1.0f;
252                 case PAD7:
253                 case SEVENKEY:
254                         Val += 1.0f;
255                 case PAD6:
256                 case SIXKEY:
257                         Val += 1.0f;
258                 case PAD5:
259                 case FIVEKEY:
260                         Val += 1.0f;
261                 case PAD4:
262                 case FOURKEY:
263                         Val += 1.0f;
264                 case PAD3:
265                 case THREEKEY:
266                         Val += 1.0f;
267                 case PAD2:
268                 case TWOKEY:
269                         Val += 1.0f;
270                 case PAD1:
271                 case ONEKEY:
272                         Val += 1.0f;
273                 case PAD0:
274                 case ZEROKEY:
275                         if (!n->ctrl[idx])
276                                 n->ctrl[idx] = 1;
277
278                         if (fabs(n->val[idx]) > 9999999.0f);
279                         else if (n->ctrl[idx] == 1) {
280                                 n->val[idx] *= 10;
281                                 n->val[idx] += Val;
282                         }
283                         else if (n->ctrl[idx] == -1) {
284                                 n->val[idx] *= 10;
285                                 n->val[idx] -= Val;
286                         }
287                         else {
288                                 /* float resolution breaks when over six digits after comma */
289                                 if( ABS(n->ctrl[idx]) < 10000000) {
290                                         n->val[idx] += Val / (float)n->ctrl[idx];
291                                         n->ctrl[idx] *= 10;
292                                 }
293                         }
294                         break;
295                 default:
296                         return 0;
297                 }
298         }
299         
300         printf("%f\n", n->val[idx]);
301
302         /* REDRAW SINCE NUMBERS HAVE CHANGED */
303         return 1;
304 }