style cleanup, brackets in else/if, some indentation.
[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         } else {
185                 switch (event->type) {
186                 case BACKSPACEKEY:
187                         if (n->ctrl[idx] == 0) {
188                                 n->val[0]               =
189                                         n->val[1]       =
190                                         n->val[2]       = 0.0f;
191                                 n->ctrl[0]              =
192                                         n->ctrl[1]      =
193                                         n->ctrl[2]      = 0;
194                                 n->inv[0]               =
195                                         n->inv[1]       =
196                                         n->inv[2]       = 0;
197                         }
198                         else {
199                                 n->val[idx] = 0.0f;
200                                 n->ctrl[idx] = 0;
201                                 n->inv[idx] = 0;
202                         }
203                         break;
204                 case PERIODKEY:
205                 case PADPERIOD:
206                         if (n->flag & NUM_NO_FRACTION)
207                                 return 0;
208
209                         switch (n->ctrl[idx])
210                         {
211                         case 0:
212                         case 1:
213                                 n->ctrl[idx] = 10;
214                                 break;
215                         case -1:
216                                 n->ctrl[idx] = -10;
217                         }
218                         break;
219                 case PADMINUS:
220                         if(event->alt)
221                                 break;
222                 case MINUSKEY:
223                         if (n->flag & NUM_NO_NEGATIVE)
224                                 break;
225
226                         if (n->ctrl[idx]) {
227                                 n->ctrl[idx] *= -1;
228                                 n->val[idx] *= -1;
229                         }
230                         else
231                                 n->ctrl[idx] = -1;
232                         break;
233                 case PADSLASHKEY:
234                 case SLASHKEY:
235                         if (n->flag & NUM_NO_FRACTION)
236                                 return 0;
237
238                         n->inv[idx] = !n->inv[idx];
239                         break;
240                 case TABKEY:
241                         if (idx_max == 0)
242                                 return 0;
243
244                         idx++;
245                         if (idx > idx_max)
246                                 idx = 0;
247                         n->idx = idx;
248                         break;
249                 case PAD9:
250                 case NINEKEY:
251                         Val += 1.0f;
252                 case PAD8:
253                 case EIGHTKEY:
254                         Val += 1.0f;
255                 case PAD7:
256                 case SEVENKEY:
257                         Val += 1.0f;
258                 case PAD6:
259                 case SIXKEY:
260                         Val += 1.0f;
261                 case PAD5:
262                 case FIVEKEY:
263                         Val += 1.0f;
264                 case PAD4:
265                 case FOURKEY:
266                         Val += 1.0f;
267                 case PAD3:
268                 case THREEKEY:
269                         Val += 1.0f;
270                 case PAD2:
271                 case TWOKEY:
272                         Val += 1.0f;
273                 case PAD1:
274                 case ONEKEY:
275                         Val += 1.0f;
276                 case PAD0:
277                 case ZEROKEY:
278                         if (!n->ctrl[idx])
279                                 n->ctrl[idx] = 1;
280
281                         if (fabsf(n->val[idx]) > 9999999.0f);
282                         else if (n->ctrl[idx] == 1) {
283                                 n->val[idx] *= 10;
284                                 n->val[idx] += Val;
285                         }
286                         else if (n->ctrl[idx] == -1) {
287                                 n->val[idx] *= 10;
288                                 n->val[idx] -= Val;
289                         }
290                         else {
291                                 /* float resolution breaks when over six digits after comma */
292                                 if( ABS(n->ctrl[idx]) < 10000000) {
293                                         n->val[idx] += Val / (float)n->ctrl[idx];
294                                         n->ctrl[idx] *= 10;
295                                 }
296                         }
297                         break;
298                 default:
299                         return 0;
300                 }
301         }
302         
303         // printf("%f\n", n->val[idx]);
304
305         /* REDRAW SINCE NUMBERS HAVE CHANGED */
306         return 1;
307 }