1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup editors
19  */
20 
21 #pragma once
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 #define NUM_STR_REP_LEN 64
28 #define NUM_MAX_ELEMENTS 3
29 
30 struct wmEvent;
31 
32 typedef struct NumInput {
33   /** idx_max < NUM_MAX_ELEMENTS */
34   short idx_max;
35   int unit_sys;
36   /** Each value can have a different type */
37   int unit_type[NUM_MAX_ELEMENTS];
38   bool unit_use_radians;
39 
40   /** Flags affecting all values' behavior */
41   short flag;
42   /** Per-value flags */
43   short val_flag[NUM_MAX_ELEMENTS];
44   /** Direct value of the input */
45   float val[NUM_MAX_ELEMENTS];
46   /** Original value of the input, for reset */
47   float val_org[NUM_MAX_ELEMENTS];
48   /** Increment steps */
49   float val_inc[NUM_MAX_ELEMENTS];
50 
51   /** Active element/value */
52   short idx;
53   /** String as typed by user for edited value (we assume ASCII world!) */
54   char str[NUM_STR_REP_LEN];
55   /** Current position of cursor in edited value str
56    * (first byte of "current" letter, so 0 for an empty str) */
57   int str_cur;
58 } NumInput;
59 
60 /* NumInput.flag */
61 enum {
62   NUM_AFFECT_ALL = (1 << 0),
63   /* (1 << 9) and above are reserved for internal flags! */
64 };
65 
66 /* NumInput.val_flag[] */
67 enum {
68   /* Public! */
69   NUM_NULL_ONE = (1 << 0),
70   NUM_NO_NEGATIVE = (1 << 1),
71   NUM_NO_ZERO = (1 << 2),
72   NUM_NO_FRACTION = (1 << 3),
73   /* (1 << 9) and above are reserved for internal flags! */
74 };
75 
76 struct UnitSettings;
77 
78 /* -------------------------------------------------------------------- */
79 /** \name NumInput
80  * \{ */
81 
82 /**
83  * There are important things to note here for code using numinput:
84  * - Values passed to #applyNumInput() should be valid and are stored as default ones (val_org),
85  *   if it is not EDITED.
86  * - bool returned by #applyNumInput should be used to decide whether to apply
87  *   numinput-specific post-process to data.
88  * - Once #applyNumInput has been called,
89  *   #hasNumInput returns a valid value to decide whether to use numinput as drawstr source or not
90  *   (i.e. to call #outputNumInput).
91  *
92  * Those two steps have to be separated
93  * (so do not use a common call to #hasNumInput() to do both in the same time!).
94  */
95 
96 void initNumInput(NumInput *n);
97 void outputNumInput(NumInput *n, char *str, struct UnitSettings *unit_settings);
98 bool hasNumInput(const NumInput *n);
99 bool applyNumInput(NumInput *n, float *vec);
100 bool handleNumInput(struct bContext *C, NumInput *n, const struct wmEvent *event);
101 
102 #define NUM_MODAL_INCREMENT_UP 18
103 #define NUM_MODAL_INCREMENT_DOWN 19
104 
105 bool user_string_to_number(bContext *C,
106                            const char *str,
107                            const struct UnitSettings *unit,
108                            int type,
109                            const char *error_prefix,
110                            double *r_value);
111 
112 /** \} */
113 
114 #ifdef __cplusplus
115 }
116 #endif
117