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