style cleanup
[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 size_t */
35
36 #include "BLI_utildefines.h"
37 #include "BLI_string.h"
38
39 #include "WM_types.h"
40
41 #include "ED_numinput.h"
42
43 /* ************************** Functions *************************** */
44
45 /* ************************** NUMINPUT **************************** */
46
47 void initNumInput(NumInput *n)
48 {
49         n->flag     =
50         n->idx      =
51         n->idx_max  =
52         n->inv[0]   =
53         n->inv[1]   =
54         n->inv[2]   =
55         n->ctrl[0]  = 
56         n->ctrl[1]  = 
57         n->ctrl[2]  = 0;
58
59         n->val[0]   = 
60         n->val[1]   = 
61         n->val[2]   = 0.0f;
62 }
63
64 void outputNumInput(NumInput *n, char *str)
65 {
66         char cur;
67         char inv[] = "1/";
68         short i, j;
69
70         for (j = 0; j <= n->idx_max; j++) {
71                 /* if AFFECTALL and no number typed and cursor not on number, use first number */
72                 if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
73                         i = 0;
74                 else
75                         i = j;
76
77                 if (n->idx != i)
78                         cur = ' ';
79                 else
80                         cur = '|';
81
82                 if (n->inv[i])
83                         inv[0] = '1';
84                 else
85                         inv[0] = 0;
86
87                 if (n->val[i] > 1e10f || n->val[i] < -1e10f)
88                         BLI_snprintf(&str[j * 20], 20, "%s%.4e%c", inv, n->val[i], cur);
89                 else
90                         switch (n->ctrl[i]) {
91                                 case 0:
92                                         BLI_snprintf(&str[j * 20], 20, "%sNONE%c", inv, cur);
93                                         break;
94                                 case 1:
95                                 case -1:
96                                         BLI_snprintf(&str[j * 20], 20, "%s%.0f%c", inv, n->val[i], cur);
97                                         break;
98                                 case 10:
99                                 case -10:
100                                         BLI_snprintf(&str[j * 20], 20, "%s%.f.%c", inv, n->val[i], cur);
101                                         break;
102                                 case 100:
103                                 case -100:
104                                         BLI_snprintf(&str[j * 20], 20, "%s%.1f%c", inv, n->val[i], cur);
105                                         break;
106                                 case 1000:
107                                 case -1000:
108                                         BLI_snprintf(&str[j * 20], 20, "%s%.2f%c", inv, n->val[i], cur);
109                                         break;
110                                 case 10000:
111                                 case -10000:
112                                         BLI_snprintf(&str[j * 20], 20, "%s%.3f%c", inv, n->val[i], cur);
113                                         break;
114                                 default:
115                                         BLI_snprintf(&str[j * 20], 20, "%s%.4e%c", inv, n->val[i], cur);
116                         }
117         }
118 }
119
120 short hasNumInput(NumInput *n)
121 {
122         short i;
123
124         for (i = 0; i <= n->idx_max; i++) {
125                 if (n->ctrl[i])
126                         return 1;
127         }
128
129         return 0;
130 }
131
132 void applyNumInput(NumInput *n, float *vec)
133 {
134         short i, j;
135
136         if (hasNumInput(n)) {
137                 for (j = 0; j <= n->idx_max; j++) {
138                         /* if AFFECTALL and no number typed and cursor not on number, use first number */
139                         if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
140                                 i = 0;
141                         else
142                                 i = j;
143
144                         if (n->ctrl[i] == 0 && n->flag & NUM_NULL_ONE) {
145                                 vec[j] = 1.0f;
146                         }
147                         else if (n->val[i] == 0.0f && n->flag & NUM_NO_ZERO) {
148                                 vec[j] = 0.0001f;
149                         }
150                         else {
151                                 if (n->inv[i]) {
152                                         vec[j] = 1.0f / n->val[i];
153                                 }
154                                 else {
155                                         vec[j] = n->val[i];
156                                 }
157                         }
158                 }
159         }
160 }
161
162 char handleNumInput(NumInput *n, wmEvent *event)
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] += n->increment;
174                                 break;
175                         case NUM_MODAL_INCREMENT_DOWN:
176                                 if (!n->ctrl[idx])
177                                         n->ctrl[idx] = 1;
178
179                                 n->val[idx] -= n->increment;
180                                 break;
181                         default:
182                                 return 0;
183                 }
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 }