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