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