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