Discovered a missing "break" inside a switch statemtn while looking for
[blender.git] / source / blender / src / transform_numinput.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <stdio.h>              /* for sprintf          */
34
35 #include "BKE_global.h" /* for G                        */
36
37 #include "mydevice.h"   /* for KEY defines      */
38
39 #include "transform.h"
40
41 /* ************************** Functions *************************** */
42
43 /* ************************** NUMINPUT **************************** */
44
45 void outputNumInput(NumInput *n, char *str)
46 {
47         char cur;
48         short i, j;
49
50         for (j=0; j<=n->idx_max; j++) {
51                 /* if AFFECTALL and no number typed and cursor not on number, use first number */
52                 if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
53                         i = 0;
54                 else
55                         i = j;
56
57                 if (n->idx != i)
58                         cur = ' ';
59                 else
60                         cur = '|';
61
62                 if( n->val[i] > 1e10 || n->val[i] < -1e10 )
63                         sprintf(&str[j*20], "%.4e%c", n->val[i], cur);
64                 else
65                         switch (n->ctrl[i]) {
66                         case 0:
67                                 sprintf(&str[j*20], "NONE%c", cur);
68                                 break;
69                         case 1:
70                         case -1:
71                                 sprintf(&str[j*20], "%.0f%c", n->val[i], cur);
72                                 break;
73                         case 10:
74                         case -10:
75                                 sprintf(&str[j*20], "%.f.%c", n->val[i], cur);
76                                 break;
77                         case 100:
78                         case -100:
79                                 sprintf(&str[j*20], "%.1f%c", n->val[i], cur);
80                                 break;
81                         case 1000:
82                         case -1000:
83                                 sprintf(&str[j*20], "%.2f%c", n->val[i], cur);
84                                 break;
85                         case 10000:
86                         case -10000:
87                                 sprintf(&str[j*20], "%.3f%c", n->val[i], cur);
88                                 break;
89                         default:
90                                 sprintf(&str[j*20], "%.4e%c", n->val[i], cur);
91                         }
92         }
93 }
94
95 short hasNumInput(NumInput *n)
96 {
97         short i;
98
99         for (i=0; i<=n->idx_max; i++) {
100                 if (n->ctrl[i])
101                         return 1;
102         }
103
104         return 0;
105 }
106
107 void applyNumInput(NumInput *n, float *vec)
108 {
109         short i, j;
110         float val[3];
111
112         if (hasNumInput(n)) {
113                 convertDisplayNumToVec(n->val, val);
114
115                 for (j=0; j<=n->idx_max; j++) {
116                         /* if AFFECTALL and no number typed and cursor not on number, use first number */
117                         if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
118                                 i = 0;
119                         else
120                                 i = j;
121
122                         if (n->ctrl[i] == 0 && n->flag & NUM_NULL_ONE) {
123                                 vec[j] = 1.0f;
124                         }
125                         else if (val[i] == 0.0f && n->flag & NUM_NO_ZERO) {
126                                 vec[j] = 0.0001f;
127                         }
128                         else {
129                                 vec[j] = val[i];
130                         }
131                 }
132         }
133 }
134
135 char handleNumInput(NumInput *n, unsigned short event)
136 {
137         float Val = 0;
138         short idx = n->idx, idx_max = n->idx_max;
139
140         switch (event) {
141         case BACKSPACEKEY:
142                 if (n->ctrl[idx] == 0) {
143                         n->val[0]               = 
144                                 n->val[1]       = 
145                                 n->val[2]       = 0.0f;
146                         n->ctrl[0]              = 
147                                 n->ctrl[1]      = 
148                                 n->ctrl[2]      = 0;
149                 }
150                 else {
151                         n->val[idx] = 0.0f;
152                         n->ctrl[idx] = 0;
153                 }
154                 break;
155         case PERIODKEY:
156         case PADPERIOD:
157                 if (n->flag & NUM_NO_FRACTION)
158                         break;
159
160                 switch (n->ctrl[idx])
161                 {
162                 case 0:
163                 case 1:
164                         n->ctrl[idx] = 10;
165                         break;
166                 case -1:
167                         n->ctrl[idx] = -10;
168                 }
169                 break;
170         case PADMINUS:
171                 if(G.qual & LR_ALTKEY)
172                         break;
173         case MINUSKEY:
174                 if (n->flag & NUM_NO_NEGATIVE)
175                         break;
176
177                 if (n->ctrl[idx]) {
178                         n->ctrl[idx] *= -1;
179                         n->val[idx] *= -1;
180                 }
181                 else
182                         n->ctrl[idx] = -1;
183                 break;
184         case TABKEY:
185                 idx++;
186                 if (idx > idx_max)
187                         idx = 0;
188                 n->idx = idx;
189                 break;
190         case PAD9:
191         case NINEKEY:
192                 Val += 1.0f;
193         case PAD8:
194         case EIGHTKEY:
195                 Val += 1.0f;
196         case PAD7:
197         case SEVENKEY:
198                 Val += 1.0f;
199         case PAD6:
200         case SIXKEY:
201                 Val += 1.0f;
202         case PAD5:
203         case FIVEKEY:
204                 Val += 1.0f;
205         case PAD4:
206         case FOURKEY:
207                 Val += 1.0f;
208         case PAD3:
209         case THREEKEY:
210                 Val += 1.0f;
211         case PAD2:
212         case TWOKEY:
213                 Val += 1.0f;
214         case PAD1:
215         case ONEKEY:
216                 Val += 1.0f;
217         case PAD0:
218         case ZEROKEY:
219                 if (!n->ctrl[idx])
220                         n->ctrl[idx] = 1;
221
222                 if (n->ctrl[idx] == 1) {
223                         n->val[idx] *= 10;
224                         n->val[idx] += Val;
225                 }
226                 else if (n->ctrl[idx] == -1) {
227                         n->val[idx] *= 10;
228                         n->val[idx] -= Val;
229                 }
230                 else {
231                         n->val[idx] += Val / (float)n->ctrl[idx];
232                         n->ctrl[idx] *= 10;
233                 }
234                 break;
235         default:
236                 return 0;
237         }
238         /* REDRAW SINCE NUMBERS HAVE CHANGED */
239         return 1;
240 }