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  virtual ~UI_Value();
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  virtual void handleEvent(UI_Event *event);
126  virtual void deleteReferences(UI_Object *to_whom);
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  void getArrayValue(fpreal *d, exint index) const;
159  void getArrayValue(int32 *d, exint index) const;
160  void getArrayValue(int64 *d, exint index) const;
161  exint getArrayLength() const;
162 
163  SYS_FORCE_INLINE UI_ValueType getType() const { return myType; }
164  virtual int isVector() const { return 0; }
165 
166  void addInterest(UI_Object *client, UI_EventMethod method,
167  bool check_dup = false);
168  void addPriorityInterest(UI_Object *client, UI_EventMethod method,
169  bool check_dup = false);
170  bool hasInterest(UI_Object *in, UI_EventMethod method);
171  bool hasInterest(UI_Object *obj);
172  void removeInterest(UI_Object *in, UI_EventMethod method);
173  void removeInterest(UI_Object *client);
174  void removeAllInterests();
175 
176  // Note: setString sets the char * value whereas setValue(char *) does
177  // a strdup. (ie UI_Value will own the string you set with setString)
178  const char *getString() const; // Short cuts for string manipulation.
179  void setString(char *sp);
180  char *stealString(); // Take the string away from the value.
181 
182  // Expand environment variables and, on OSX and Linux, the ~ character.
183  void expandEnvironment();
184 
185  operator const char *() const;
186  operator bool() const;
187  operator int32() const;
188  operator int64() const;
189  operator fpreal32() const;
190  operator fpreal64() const;
191  operator void *() const;
192 
193  void operator=(int32 i) { setValue(i); }
194  void operator=(int64 i) { setValue(i); }
195  void operator=(fpreal32 f) { setValue(f); }
196  void operator=(fpreal64 f) { setValue(f); }
197  void operator=(const char *s) { setValue(s); }
198  void operator=(const UI_Value &v) { setValue(v); }
199  void operator=(const UT_Color &c) { setValue(c); }
200 
201  bool operator==(const UI_Value &v) const;
202  bool operator==(const fpreal32 fp[]) const;
203  bool operator==(const fpreal64 fp[]) const;
204  bool operator==(const int32 ip[]) const;
205  bool operator==(const int64 ip[]) const;
206  bool operator==(const UT_StringArray &ssp) const;
207  bool operator==(fpreal64 f) const;
208  bool operator==(fpreal32 f) const;
209  bool operator==(int32 i) const;
210  bool operator==(int64 i) const;
211  bool operator==(const char *s) const;
212 
213  virtual const char *className() const { return "UI_Value"; }
214 
215  // Return the number of interests expressed in this value
216  int getNumInterested() const;
217  UI_Object *getInterest(int index) const;
218 
219  // Set to true to track
221  {
224  TL_VALUE
225  };
226 
227 
228  bool isTraced() const { return myTraceLevel != TL_NONE; }
229  virtual void setTraceLevel(TraceLevel tl) { myTraceLevel = tl; }
230  TraceLevel getTraceLevel() const { return myTraceLevel; }
231 
232 protected:
233 
234  // Clamps value to given range.
235  // NB: Only valid for float, int, {int,float} arrays
236  void clampValue(fpreal min_value, fpreal max_value);
237 
238  void showTrace();
239 
240 private:
241  void zap();
242 
243  void addMyInterest(UI_Object *client,
244  UI_EventMethod method,
245  bool check_dup,
246  bool priority);
247  void sendEventToInterests(UI_Object *by, UI_Event &event,
248  bool immediate);
249 
250  friend std::ostream &operator<<(std::ostream &os, const UI_Value &v)
251  {
252  v.save(os);
253  return os;
254  }
255 private:
256  union ValueType
257  {
258  exint i;
259  fpreal d;
260  fpreal *a;
261  exint *j;
262  char *s;
263  char **ss;
264  void *p;
265  };
266 
267  class ui_Client
268  {
269  public:
270  ui_Client(UI_Object *who, UI_EventMethod func)
271  : myClientId(who->getProxyId())
272  , myMethod(func)
273  {
274  }
275 
276  // Not copyable
277  ui_Client(const ui_Client& copy) = delete;
278  ui_Client& operator=(const ui_Client& copy) = delete;
279  // But movable
280  ui_Client(ui_Client&& src) = default;
281  ui_Client& operator=(ui_Client&& src) = default;
282 
283  bool isEqual(int id) { return myClientId == id; }
284  int getClientId() { return myClientId; }
285  UI_Object * getClient() { return myClientId; }
286  UI_EventMethod getMethod() { return myMethod; }
287 
288  private:
289  UT_RefProxy<UI_Object> myClientId;
290  UI_EventMethod myMethod;
291  };
292 
293  using ui_ClientList = std::deque<ui_Client>;
294  using ui_ClientListPtr = UT_UniquePtr<ui_ClientList>;
295  using ui_ClientListIter = ui_ClientList::iterator;
296 
298  ui_ClientListIter ibegin() { return myInterests->begin(); }
300  ui_ClientListIter iend() { return myInterests->end(); }
301 
302  ValueType myValue;
303  UI_ValueType myType:4;
304  TraceLevel myTraceLevel:2;
305  ui_ClientListPtr myInterests;
306  const char* myFormat;
307 };
308 
309 UI_API size_t format(char *buffer, size_t buffer_size, const UI_Value &v);
310 
311 inline int
313 {
314  return myInterests ? int(myInterests->size()) : 0;
315 }
316 
317 inline UI_Object *
319 {
320  if (!myInterests || i < 0 || i >= myInterests->size())
321  return nullptr;
322 
323  return (*myInterests)[i].getClient();
324 }
325 
326 inline void
327 UI_Value::clampValue(fpreal min_value, fpreal max_value)
328 {
329  if (min_value > max_value)
330  return;
331 
332  switch (myType)
333  {
334  case UI_VALUE_FLOAT:
335  if (myValue.d < min_value)
336  myValue.d = min_value;
337  else if (myValue.d > max_value)
338  myValue.d = max_value;
339  break;
340 
341  case UI_VALUE_INTEGER:
342  if (myValue.i < min_value)
343  myValue.i = (int)min_value;
344  else if (myValue.i > max_value)
345  myValue.i = (int)max_value;
346  break;
347 
349  for (exint i = 1, n = myValue.a[0]; i < n; ++i)
350  {
351  if (myValue.a[n] < min_value)
352  myValue.a[n] = min_value;
353  else if (myValue.a[n] > max_value)
354  myValue.a[n] = max_value;
355  }
356  break;
357 
358  case UI_VALUE_INT_ARRAY:
359  for (exint i = 1, n = myValue.a[0]; i < n; ++i)
360  {
361  if (myValue.j[n] < (int)min_value)
362  myValue.j[n] = (int)min_value;
363  else if (myValue.j[n] > (int)max_value)
364  myValue.j[n] = (int)max_value;
365  }
366  break;
367 
368  default: break;
369  }
370 }
371 
372 #endif
GLdouble s
Definition: glew.h:1390
vint4 max(const vint4 &a, const vint4 &b)
Definition: simd.h:4703
friend std::ostream & operator<<(std::ostream &os, const UI_Value &v)
Definition: UI_Value.h:250
GLuint id
Definition: glew.h:1679
GLenum src
Definition: glew.h:2410
UI_API size_t format(char *buffer, size_t buffer_size, const UI_ValueType &v)
bool isTraced() const
Definition: UI_Value.h:228
GLsizei bufsize
Definition: glew.h:2581
void operator=(int32 i)
Definition: UI_Value.h:193
void operator=(const UI_Value &v)
Definition: UI_Value.h:198
UI_Object * getInterest(int index) const
Definition: UI_Value.h:318
virtual void setTraceLevel(TraceLevel tl)
Definition: UI_Value.h:229
GLuint index
Definition: glew.h:1814
const char * getFormat() const
Definition: UI_Value.h:139
void setFormat(const char *fmt)
Definition: UI_Value.h:138
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
const GLdouble * v
Definition: glew.h:1391
void operator=(fpreal64 f)
Definition: UI_Value.h:196
GLhandleARB obj
Definition: glew.h:6236
TraceLevel getTraceLevel() const
Definition: UI_Value.h:230
void operator=(fpreal32 f)
Definition: UI_Value.h:195
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
long long int64
Definition: SYS_Types.h:111
virtual void handleEvent(UI_Event *event)
void operator=(const char *s)
Definition: UI_Value.h:197
GLclampf f
Definition: glew.h:3499
GLuint in
Definition: glew.h:11510
int64 exint
Definition: SYS_Types.h:120
GLuint buffer
Definition: glew.h:1680
virtual int isVector() const
Definition: UI_Value.h:164
double fpreal64
Definition: SYS_Types.h:196
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
GLsizei n
Definition: glew.h:4040
const GLfloat * c
Definition: glew.h:16296
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
GLuint color
Definition: glew.h:7902
int int32
Definition: SYS_Types.h:39
virtual int open(float queuesize)
int save(std::ostream &os) const
UI_Reason
Definition: UI_Value.h:29
SYS_FORCE_INLINE UI_ValueType getType() const
Definition: UI_Value.h:163
GLfloat GLfloat p
Definition: glew.h:16321
double fpreal
Definition: SYS_Types.h:276
cl_event event
Definition: glew.h:3695
void clampValue(fpreal min_value, fpreal max_value)
Definition: UI_Value.h:327
GLenum func
Definition: glcorearb.h:782
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: glew.h:1254
int getNumInterested() const
Definition: UI_Value.h:312
OIIO_API bool copy(string_view from, string_view to, std::string &err)
virtual void deleteReferences(UI_Object *to_whom)
void(UI_Object::* UI_EventMethod)(UI_Event *)
Definition: UI_Object.h:35
void operator=(const UT_Color &c)
Definition: UI_Value.h:199
#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:194
vint4 min(const vint4 &a, const vint4 &b)
Definition: simd.h:4694
virtual const char * className() const
Definition: UI_Value.h:213
float fpreal32
Definition: SYS_Types.h:195
GLboolean enable
Definition: glew.h:2745
UI_ValueType
Definition: UI_Value.h:18