HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UI_Value.h
Go to the documentation of this file.
1 #ifndef __UI_Value__
2 #define __UI_Value__
3 
4 #include "UI_API.h"
5 #include "UI_Object.h"
6 #include <UT/UT_PtrProxy.h>
7 #include <UT/UT_UniquePtr.h>
8 #include <SYS/SYS_Inline.h>
9 #include <SYS/SYS_Types.h>
10 #include <iosfwd>
11 #include <deque>
12 
13 /*
14  * A UI_Value is a utility class which maintains a single value of
15  * variable type and provides conversions between types.
16  */
17 
26 };
27 UI_API size_t format(char *buffer, size_t buffer_size, const UI_ValueType &v);
28 
29 enum UI_Reason {
30  UI_NO_REASON, // event was likely explicitly generated
31  UI_VALUE_CHANGED, // mouse up
32  UI_VALUE_ACTIVE, // mouse drag
33  UI_VALUE_START, // mouse down
34  UI_VALUE_NEW, // dependent added to UI_Value
35  UI_VALUE_RANGE_CHANGED, // UI_ScrollValue range changed
36  UI_VALUE_PICKED, // quick mouse down and release without drag
37  UI_VALUE_LOCATED, // mouse hover (only if wantsLocates() is true)
38  UI_VALUE_ITEMS_CHANGED // only generated by UI_StringList (and subclasses)
39 };
40 UI_API size_t format(char *buffer, size_t buffer_size, const UI_Reason &v);
41 
42 #define UI_STEP_BIG 4
43 
49 };
50 UI_API size_t format(char *buffer, size_t buffer_size, const UI_StepType &v);
51 
52 class UI_Event;
53 class UT_Color;
54 class UT_String;
55 class UT_StringArray;
56 class UT_StringHolder;
57 class UT_StringRef;
58 class UT_IStream;
59 class ui_Client;
60 
61 //====================================================================
62 
63 class UI_API UI_Value : public UI_Object
64 {
65 public:
66  UI_Value();
67  explicit UI_Value(int32 i);
68  explicit UI_Value(int64 i);
69  explicit UI_Value(fpreal32 f);
70  explicit UI_Value(fpreal64 f);
71  explicit UI_Value(fpreal32 fp[], exint n);
72  explicit UI_Value(fpreal64 dp[], exint n);
73  explicit UI_Value(int32 ip[], exint n);
74  explicit UI_Value(int64 ip[], exint n);
75  explicit UI_Value(const UT_StringArray &ssp);
76  explicit UI_Value(const char *s);
77  explicit UI_Value(const UI_Value &v);
78 
79  ~UI_Value() override;
80 
81  virtual void setValue(int32 i);
82  virtual void setValue(int64 i);
83  virtual void setValue(fpreal64 f);
84  virtual void setValue(fpreal64 f, exint n);
85  virtual void setValue(int32 i, exint n);
86  virtual void setValue(int64 i, exint n);
87  virtual void setValue(const fpreal32 fp[], exint n);
88  virtual void setValue(const fpreal64 dp[], exint n);
89  virtual void setValue(const int32 ip[], exint n);
90  virtual void setValue(const int64 ip[], exint n);
91  virtual void setValue(const UT_StringArray &ssp);
92  virtual void setValue(const char *s);
93  virtual void setValue(const UI_Value &v);
94  void setValue(const UT_Color &c);
95  void setPointerValue(void *p);
96  void stringPrintf(const char *fmt, ...);
97 
98  virtual void changed(UI_Object *by,
99  UI_Reason reason = UI_VALUE_CHANGED,
100  UI_DeviceEvent *state = 0);
101  // This method is similar to "changed" but it triggers the callback events
102  // immediately (bypassing the event queue). It also only notifies the
103  // objects that have added interests in this value. Not the immediate
104  // dependents. **This should only be used in special circumstances.**
105  void triggerImmediate(UI_Object *by,
106  UI_Reason reason = UI_VALUE_CHANGED,
107  UI_DeviceEvent *state = 0);
108  // This method is similar to "triggerImmediate" but it notifies the
109  // immediate dependents rather than the objects that have added interests
110  // in this value. **This should only be used in special circumstances.**
111  void triggerDependents(UI_Object *by,
112  UI_Reason reason = UI_VALUE_CHANGED,
113  UI_DeviceEvent *state = 0);
114 
115  virtual void enable(bool state); // Sends enable event to dependents
116  virtual void visible(bool state); // Sends visible event to dependents
117  virtual void override(bool state, int);// Sends override event to dependents
118  virtual void open(bool state); // Sends open/close event to dependents
119  virtual void refresh(); // Sends redraw in place events
120 
121  // This ugly hack attempts to determine the open state of UI_Feel clients
122  // which are open. It returns true only if *any* of them are open.
123  bool areAnyFeelClientsOpen() const;
124 
125  void handleEvent(UI_Event *event) override;
126  void deleteReferences(UI_Object *to_whom) override;
127 
128  virtual void setSteps(fpreal mysmall, fpreal large);
129  virtual void setRange(fpreal min, fpreal max);
130  virtual void getRange(fpreal *min, fpreal *max) const;
131 
132  virtual void rangeCheck();
133  virtual int step(UI_StepType how);
134 
135  int save(std::ostream &os) const;
136  bool load(UT_IStream &is);
137 
138  void setFormat(const char *fmt) { myFormat = fmt; }
139  const char *getFormat() const { return myFormat; }
140 
141  void getValue(int32 *ip) const;
142  void getValue(int64 *ip) const;
143  void getValue(fpreal32 *fp) const;
144  void getValue(fpreal64 *fp) const;
145  void getValue(fpreal32 *fp, exint n) const;
146  void getValue(fpreal64 *dp, exint n) const;
147  void getValue(int32 *ip, exint n) const;
148  void getValue(int64 *ip, exint n) const;
149  void getValue(char *sp, exint bufsize) const;
150  void getValue(char *sp, exint bufsize, const char *format) const;
151  void getValue(UT_String &s) const;
152  void getValue(UT_StringHolder &s) const;
153  void getValue(UT_StringArray &ssp, exint n) const;
154  void getValue(UI_Value &v) const;
155  void getValue(UT_Color &color) const;
156  void getValue(void **pp) const;
157 
158  /// Same as getValue(UT_StringHolder &s) except it uses full precision for
159  /// floating point to string conversions.
160  void getValuePrecise(UT_StringHolder &s) const;
161 
162  void getArrayValue(fpreal *d, exint index) const;
163  void getArrayValue(int32 *d, exint index) const;
164  void getArrayValue(int64 *d, exint index) const;
165  exint getArrayLength() const;
166 
167  SYS_FORCE_INLINE UI_ValueType getType() const { return myType; }
168  virtual int isVector() const { return 0; }
169 
170  void addInterest(UI_Object *client, UI_EventMethod method,
171  bool check_dup = false);
172  void addPriorityInterest(UI_Object *client, UI_EventMethod method,
173  bool check_dup = false);
174  bool hasInterest(UI_Object *in, UI_EventMethod method);
175  bool hasInterest(UI_Object *obj);
176  void removeInterest(UI_Object *in, UI_EventMethod method);
177  void removeInterest(UI_Object *client);
178  void removeAllInterests();
179 
180  // Note: setString sets the char * value whereas setValue(char *) does
181  // a strdup. (ie UI_Value will own the string you set with setString)
182  const char *getString() const; // Short cuts for string manipulation.
183  void setString(char *sp);
184  char *stealString(); // Take the string away from the value.
185 
186  // Expand environment variables and, on OSX and Linux, the ~ character.
187  void expandEnvironment();
188 
189  operator const char *() const;
190  operator bool() const;
191  operator int32() const;
192  operator int64() const;
193  operator fpreal32() const;
194  operator fpreal64() const;
195  operator void *() const;
196 
197  void operator=(int32 i) { setValue(i); }
198  void operator=(int64 i) { setValue(i); }
199  void operator=(fpreal32 f) { setValue(f); }
200  void operator=(fpreal64 f) { setValue(f); }
201  void operator=(const char *s) { setValue(s); }
202  void operator=(const UI_Value &v) { setValue(v); }
203  void operator=(const UT_Color &c) { setValue(c); }
204 
205  bool operator==(const UI_Value &v) const;
206  bool operator==(const fpreal32 fp[]) const;
207  bool operator==(const fpreal64 fp[]) const;
208  bool operator==(const int32 ip[]) const;
209  bool operator==(const int64 ip[]) const;
210  bool operator==(const UT_StringArray &ssp) const;
211  bool operator==(fpreal64 f) const;
212  bool operator==(fpreal32 f) const;
213  bool operator==(int32 i) const;
214  bool operator==(int64 i) const;
215  bool operator==(const char *s) const;
216 
217  const char *className() const override { return "UI_Value"; }
218 
219  // Return the number of interests expressed in this value
220  int getNumInterested() const;
221  UI_Object *getInterest(int index) const;
222 
223  // Utility method for debugging
224  void dumpInterests(std::ostream &os,
225  const char *prefix = 0) const;
226 
227  // Set to true to track
229  {
232  TL_VALUE
233  };
234 
235 
236  bool isTraced() const { return myTraceLevel != TL_NONE; }
237  virtual void setTraceLevel(TraceLevel tl) { myTraceLevel = tl; }
238  TraceLevel getTraceLevel() const { return myTraceLevel; }
239 
240 protected:
241 
242  // Clamps value to given range.
243  // NB: Only valid for float, int, {int,float} arrays
244  void clampValue(fpreal min_value, fpreal max_value);
245 
246  void showTrace();
247 
248 private:
249  void zap();
250 
251  void addMyInterest(UI_Object *client,
252  UI_EventMethod method,
253  bool check_dup,
254  bool priority);
255  void sendEventToInterests(UI_Object *by, UI_Event &event,
256  bool immediate);
257 
258  friend std::ostream &operator<<(std::ostream &os, const UI_Value &v)
259  {
260  v.save(os);
261  return os;
262  }
263 private:
264  union ValueType
265  {
266  exint i;
267  fpreal d;
268  fpreal *a;
269  exint *j;
270  char *s;
271  char **ss;
272  void *p;
273  };
274 
275  class ui_Client
276  {
277  public:
278  ui_Client(UI_Object *who, UI_EventMethod func)
279  : myClientId(who->getProxyId())
280  , myMethod(func)
281  {
282  }
283 
284  // Not copyable
285  ui_Client(const ui_Client& copy) = delete;
286  ui_Client& operator=(const ui_Client& copy) = delete;
287  // But movable
288  ui_Client(ui_Client&& src) = default;
289  ui_Client& operator=(ui_Client&& src) = default;
290 
291  bool isEqual(int id) { return myClientId == id; }
292  int getClientId() { return myClientId; }
293  UI_Object * getClient() { return myClientId; }
294  UI_EventMethod getMethod() { return myMethod; }
295 
296  private:
297  UT_RefProxy<UI_Object> myClientId;
298  UI_EventMethod myMethod;
299  };
300 
301  using ui_ClientList = std::deque<ui_Client>;
302  using ui_ClientListPtr = UT_UniquePtr<ui_ClientList>;
303  using ui_ClientListIter = ui_ClientList::iterator;
304 
306  ui_ClientListIter ibegin() { return myInterests->begin(); }
308  ui_ClientListIter iend() { return myInterests->end(); }
309 
310  ValueType myValue;
311  UI_ValueType myType:4;
312  TraceLevel myTraceLevel:2;
313  ui_ClientListPtr myInterests;
314  const char* myFormat;
315 };
316 
317 UI_API size_t format(char *buffer, size_t buffer_size, const UI_Value &v);
318 
319 inline int
321 {
322  return myInterests ? int(myInterests->size()) : 0;
323 }
324 
325 inline UI_Object *
327 {
328  if (!myInterests || i < 0 || i >= myInterests->size())
329  return nullptr;
330 
331  return (*myInterests)[i].getClient();
332 }
333 
334 inline void
336 {
337  if (min_value > max_value)
338  return;
339 
340  switch (myType)
341  {
342  case UI_VALUE_FLOAT:
343  if (myValue.d < min_value)
344  myValue.d = min_value;
345  else if (myValue.d > max_value)
346  myValue.d = max_value;
347  break;
348 
349  case UI_VALUE_INTEGER:
350  if (myValue.i < min_value)
351  myValue.i = (int)min_value;
352  else if (myValue.i > max_value)
353  myValue.i = (int)max_value;
354  break;
355 
357  for (exint i = 1, n = myValue.a[0]; i < n; ++i)
358  {
359  if (myValue.a[n] < min_value)
360  myValue.a[n] = min_value;
361  else if (myValue.a[n] > max_value)
362  myValue.a[n] = max_value;
363  }
364  break;
365 
366  case UI_VALUE_INT_ARRAY:
367  for (exint i = 1, n = myValue.a[0]; i < n; ++i)
368  {
369  if (myValue.j[n] < (int)min_value)
370  myValue.j[n] = (int)min_value;
371  else if (myValue.j[n] > (int)max_value)
372  myValue.j[n] = (int)max_value;
373  }
374  break;
375 
376  default: break;
377  }
378 }
379 
380 #endif
constexpr auto max_value() -> T
Definition: format.h:303
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
friend std::ostream & operator<<(std::ostream &os, const UI_Value &v)
Definition: UI_Value.h:258
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1818
int int32
Definition: SYS_Types.h:39
UI_API size_t format(char *buffer, size_t buffer_size, const UI_ValueType &v)
bool isTraced() const
Definition: UI_Value.h:236
void operator=(int32 i)
Definition: UI_Value.h:197
void operator=(const UI_Value &v)
Definition: UI_Value.h:202
UI_Object * getInterest(int index) const
Definition: UI_Value.h:326
virtual void setTraceLevel(TraceLevel tl)
Definition: UI_Value.h:237
OIIO_UTIL_API bool copy(string_view from, string_view to, std::string &err)
const GLdouble * v
Definition: glcorearb.h:837
const char * getFormat() const
Definition: UI_Value.h:139
void setFormat(const char *fmt)
Definition: UI_Value.h:138
int64 exint
Definition: SYS_Types.h:125
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
ImageBuf OIIO_API min(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
void operator=(fpreal64 f)
Definition: UI_Value.h:200
__hostdev__ void setValue(uint32_t offset, bool v)
Definition: NanoVDB.h:5750
TraceLevel getTraceLevel() const
Definition: UI_Value.h:238
float fpreal32
Definition: SYS_Types.h:200
void operator=(fpreal32 f)
Definition: UI_Value.h:199
__hostdev__ float getValue(uint32_t i) const
Definition: NanoVDB.h:5578
std::unique_ptr< T, Deleter > UT_UniquePtr
A smart pointer for unique ownership of dynamically allocated objects.
Definition: UT_UniquePtr.h:39
struct _cl_event * event
Definition: glcorearb.h:2961
double fpreal64
Definition: SYS_Types.h:201
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
virtual void handleEvent(UI_Event *event)
void operator=(const char *s)
Definition: UI_Value.h:201
GLdouble n
Definition: glcorearb.h:2008
GLfloat f
Definition: glcorearb.h:1926
Definition: core.h:760
int open(float queuesize) override
virtual int isVector() const
Definition: UI_Value.h:168
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:108
long long int64
Definition: SYS_Types.h:116
GLuint id
Definition: glcorearb.h:655
int save(std::ostream &os) const
UI_Reason
Definition: UI_Value.h:29
GLint j
Definition: glad.h:2733
SYS_FORCE_INLINE UI_ValueType getType() const
Definition: UI_Value.h:167
void clampValue(fpreal min_value, fpreal max_value)
Definition: UI_Value.h:335
GLenum func
Definition: glcorearb.h:783
GLuint color
Definition: glcorearb.h:1261
const char * className() const override
Definition: UI_Value.h:217
fpreal64 fpreal
Definition: SYS_Types.h:277
LeafData & operator=(const LeafData &)=delete
GLuint index
Definition: glcorearb.h:786
int getNumInterested() const
Definition: UI_Value.h:320
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
virtual void deleteReferences(UI_Object *to_whom)
void(UI_Object::* UI_EventMethod)(UI_Event *)
Definition: UI_Object.h:36
void operator=(const UT_Color &c)
Definition: UI_Value.h:203
#define UI_API
Definition: UI_API.h:10
UI_StepType
Definition: UI_Value.h:44
#define UI_STEP_BIG
Definition: UI_Value.h:42
void operator=(int64 i)
Definition: UI_Value.h:198
bool ValueType
Definition: NanoVDB.h:5729
GLenum src
Definition: glcorearb.h:1793
UI_ValueType
Definition: UI_Value.h:18