Blender  V2.59
numinput.c
Go to the documentation of this file.
00001 /*
00002  * $Id: numinput.c 35890 2011-03-30 04:58:45Z campbellbarton $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00021  * All rights reserved.
00022  *
00023  * The Original Code is: all of this file.
00024  *
00025  * Contributor(s): Jonathan Smith
00026  *
00027  * ***** END GPL LICENSE BLOCK *****
00028  */
00029 
00035 #include <math.h>                       /* fabs */
00036 #include <stdio.h>                      /* for sprintf          */
00037 
00038 #include "BLI_utildefines.h"
00039 
00040 #include "WM_types.h"
00041 
00042 #include "ED_numinput.h"
00043 
00044 /* ************************** Functions *************************** */
00045 
00046 /* ************************** NUMINPUT **************************** */
00047 
00048 void initNumInput(NumInput *n)
00049 {
00050         n->flag         =
00051         n->idx          =
00052         n->idx_max      =
00053         n->inv[0]   =
00054         n->inv[1]   =
00055         n->inv[2]   =
00056         n->ctrl[0]      = 
00057         n->ctrl[1]      = 
00058         n->ctrl[2]      = 0;
00059 
00060         n->val[0]               = 
00061         n->val[1]       = 
00062         n->val[2]       = 0.0f;
00063 }
00064 
00065 void outputNumInput(NumInput *n, char *str)
00066 {
00067         char cur;
00068         char inv[] = "1/";
00069         short i, j;
00070 
00071         for (j=0; j<=n->idx_max; j++) {
00072                 /* if AFFECTALL and no number typed and cursor not on number, use first number */
00073                 if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
00074                         i = 0;
00075                 else
00076                         i = j;
00077 
00078                 if (n->idx != i)
00079                         cur = ' ';
00080                 else
00081                         cur = '|';
00082 
00083                 if (n->inv[i])
00084                         inv[0] = '1';
00085                 else
00086                         inv[0] = 0;
00087 
00088                 if( n->val[i] > 1e10f || n->val[i] < -1e10f )
00089                         sprintf(&str[j*20], "%s%.4e%c", inv, n->val[i], cur);
00090                 else
00091                         switch (n->ctrl[i]) {
00092                         case 0:
00093                                 sprintf(&str[j*20], "%sNONE%c", inv, cur);
00094                                 break;
00095                         case 1:
00096                         case -1:
00097                                 sprintf(&str[j*20], "%s%.0f%c", inv, n->val[i], cur);
00098                                 break;
00099                         case 10:
00100                         case -10:
00101                                 sprintf(&str[j*20], "%s%.f.%c", inv, n->val[i], cur);
00102                                 break;
00103                         case 100:
00104                         case -100:
00105                                 sprintf(&str[j*20], "%s%.1f%c", inv, n->val[i], cur);
00106                                 break;
00107                         case 1000:
00108                         case -1000:
00109                                 sprintf(&str[j*20], "%s%.2f%c", inv, n->val[i], cur);
00110                                 break;
00111                         case 10000:
00112                         case -10000:
00113                                 sprintf(&str[j*20], "%s%.3f%c", inv, n->val[i], cur);
00114                                 break;
00115                         default:
00116                                 sprintf(&str[j*20], "%s%.4e%c", inv, n->val[i], cur);
00117                         }
00118         }
00119 }
00120 
00121 short hasNumInput(NumInput *n)
00122 {
00123         short i;
00124 
00125         for (i=0; i<=n->idx_max; i++) {
00126                 if (n->ctrl[i])
00127                         return 1;
00128         }
00129 
00130         return 0;
00131 }
00132 
00133 void applyNumInput(NumInput *n, float *vec)
00134 {
00135         short i, j;
00136 
00137         if (hasNumInput(n)) {
00138                 for (j=0; j<=n->idx_max; j++) {
00139                         /* if AFFECTALL and no number typed and cursor not on number, use first number */
00140                         if (n->flag & NUM_AFFECT_ALL && n->idx != j && n->ctrl[j] == 0)
00141                                 i = 0;
00142                         else
00143                                 i = j;
00144 
00145                         if (n->ctrl[i] == 0 && n->flag & NUM_NULL_ONE) {
00146                                 vec[j] = 1.0f;
00147                         }
00148                         else if (n->val[i] == 0.0f && n->flag & NUM_NO_ZERO) {
00149                                 vec[j] = 0.0001f;
00150                         }
00151                         else {
00152                                 if (n->inv[i])
00153                                 {
00154                                         vec[j] = 1.0f / n->val[i];
00155                                 }
00156                                 else
00157                                 {
00158                                         vec[j] = n->val[i];
00159                                 }
00160                         }
00161                 }
00162         }
00163 }
00164 
00165 char handleNumInput(NumInput *n, wmEvent *event)
00166 {
00167         float Val = 0;
00168         short idx = n->idx, idx_max = n->idx_max;
00169 
00170         if (event->type == EVT_MODAL_MAP) {
00171                 switch (event->val) {
00172                 case NUM_MODAL_INCREMENT_UP:
00173                         if (!n->ctrl[idx])
00174                                 n->ctrl[idx] = 1;
00175 
00176                         n->val[idx] += n->increment;
00177                         break;
00178                 case NUM_MODAL_INCREMENT_DOWN:
00179                         if (!n->ctrl[idx])
00180                                 n->ctrl[idx] = 1;
00181 
00182                         n->val[idx] -= n->increment;
00183                         break;
00184                 default:
00185                         return 0;
00186                 }
00187         } else {
00188                 switch (event->type) {
00189                 case BACKSPACEKEY:
00190                         if (n->ctrl[idx] == 0) {
00191                                 n->val[0]               =
00192                                         n->val[1]       =
00193                                         n->val[2]       = 0.0f;
00194                                 n->ctrl[0]              =
00195                                         n->ctrl[1]      =
00196                                         n->ctrl[2]      = 0;
00197                                 n->inv[0]               =
00198                                         n->inv[1]       =
00199                                         n->inv[2]       = 0;
00200                         }
00201                         else {
00202                                 n->val[idx] = 0.0f;
00203                                 n->ctrl[idx] = 0;
00204                                 n->inv[idx] = 0;
00205                         }
00206                         break;
00207                 case PERIODKEY:
00208                 case PADPERIOD:
00209                         if (n->flag & NUM_NO_FRACTION)
00210                                 return 0;
00211 
00212                         switch (n->ctrl[idx])
00213                         {
00214                         case 0:
00215                         case 1:
00216                                 n->ctrl[idx] = 10;
00217                                 break;
00218                         case -1:
00219                                 n->ctrl[idx] = -10;
00220                         }
00221                         break;
00222                 case PADMINUS:
00223                         if(event->alt)
00224                                 break;
00225                 case MINUSKEY:
00226                         if (n->flag & NUM_NO_NEGATIVE)
00227                                 break;
00228 
00229                         if (n->ctrl[idx]) {
00230                                 n->ctrl[idx] *= -1;
00231                                 n->val[idx] *= -1;
00232                         }
00233                         else
00234                                 n->ctrl[idx] = -1;
00235                         break;
00236                 case PADSLASHKEY:
00237                 case SLASHKEY:
00238                         if (n->flag & NUM_NO_FRACTION)
00239                                 return 0;
00240 
00241                         n->inv[idx] = !n->inv[idx];
00242                         break;
00243                 case TABKEY:
00244                         if (idx_max == 0)
00245                                 return 0;
00246 
00247                         idx++;
00248                         if (idx > idx_max)
00249                                 idx = 0;
00250                         n->idx = idx;
00251                         break;
00252                 case PAD9:
00253                 case NINEKEY:
00254                         Val += 1.0f;
00255                 case PAD8:
00256                 case EIGHTKEY:
00257                         Val += 1.0f;
00258                 case PAD7:
00259                 case SEVENKEY:
00260                         Val += 1.0f;
00261                 case PAD6:
00262                 case SIXKEY:
00263                         Val += 1.0f;
00264                 case PAD5:
00265                 case FIVEKEY:
00266                         Val += 1.0f;
00267                 case PAD4:
00268                 case FOURKEY:
00269                         Val += 1.0f;
00270                 case PAD3:
00271                 case THREEKEY:
00272                         Val += 1.0f;
00273                 case PAD2:
00274                 case TWOKEY:
00275                         Val += 1.0f;
00276                 case PAD1:
00277                 case ONEKEY:
00278                         Val += 1.0f;
00279                 case PAD0:
00280                 case ZEROKEY:
00281                         if (!n->ctrl[idx])
00282                                 n->ctrl[idx] = 1;
00283 
00284                         if (fabsf(n->val[idx]) > 9999999.0f);
00285                         else if (n->ctrl[idx] == 1) {
00286                                 n->val[idx] *= 10;
00287                                 n->val[idx] += Val;
00288                         }
00289                         else if (n->ctrl[idx] == -1) {
00290                                 n->val[idx] *= 10;
00291                                 n->val[idx] -= Val;
00292                         }
00293                         else {
00294                                 /* float resolution breaks when over six digits after comma */
00295                                 if( ABS(n->ctrl[idx]) < 10000000) {
00296                                         n->val[idx] += Val / (float)n->ctrl[idx];
00297                                         n->ctrl[idx] *= 10;
00298                                 }
00299                         }
00300                         break;
00301                 default:
00302                         return 0;
00303                 }
00304         }
00305         
00306         // printf("%f\n", n->val[idx]);
00307 
00308         /* REDRAW SINCE NUMBERS HAVE CHANGED */
00309         return 1;
00310 }