Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Params.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Color.h"
26 #include "cinder/Quaternion.h"
27 #include "cinder/Function.h"
28 
29 #include <string>
30 #include <map>
31 #include <memory>
32 
33 typedef struct CTwBar TwBar;
34 
35 namespace cinder {
36 
37 namespace app {
38  class Window;
39  typedef std::shared_ptr<Window> WindowRef;
40 }
41 
42 namespace params {
43 
44 typedef std::shared_ptr<class InterfaceGl> InterfaceGlRef;
45 
47 class InterfaceGl {
48  public:
50  static InterfaceGlRef create( const std::string &title, const Vec2i &size, const ColorA &color = ColorA( 0.3f, 0.3f, 0.3f, 0.4f ) );
52  static InterfaceGlRef create( const cinder::app::WindowRef &window, const std::string &title, const Vec2i &size, const ColorA &color = ColorA( 0.3f, 0.3f, 0.3f, 0.4f ) );
53 
57  InterfaceGl( const std::string &title, const Vec2i &size, const ColorA &color = ColorA( 0.3f, 0.3f, 0.3f, 0.4f ) );
59  InterfaceGl( const cinder::app::WindowRef &window, const std::string &title, const Vec2i &size, const ColorA &color = ColorA( 0.3f, 0.3f, 0.3f, 0.4f ) );
60 
62  class OptionsBase {
63  public:
64  const std::string& getName() const { return mName; }
65  void* getVoidPtr() const { return mVoidPtr; }
66  const std::string& getKeyIncr() const { return mKeyIncr; }
67  const std::string& getKeyDecr() const { return mKeyDecr; }
68 
69  protected:
70  OptionsBase( const std::string &name, void *targetVoidPtr, InterfaceGl *parent );
71 
72  void setMin( float minVal );
73  void setMax( float maxVal );
74  void setStep( float maxVal );
75  void setPrecision( int precVal );
76  void setKeyIncr( const std::string &keyIncr );
77  void setKeyDecr( const std::string &keyDecr );
78  void setKey( const std::string &key );
79  void setGroup( const std::string &group );
80  void setOptionsStr( const std::string &optionsStr );
81 
82  void reAddOptions();
83 
85  void* mVoidPtr;
86  float mMin, mMax, mStep;
89 
91 
92  friend class InterfaceGl;
93  };
94 
96  template <typename T>
97  class Options : public OptionsBase {
98  public:
99  Options( const std::string &name, T *target, int type, InterfaceGl *parent );
100 
101  typedef std::function<void ( T )> SetterFn;
102  typedef std::function<T ()> GetterFn;
103  typedef std::function<void ()> UpdateFn;
104 
106  Options& min( float minVal ) { setMin( minVal ); return *this; }
108  Options& max( float maxVal ) { setMax( maxVal ); return *this; }
110  Options& step( float stepVal ) { setStep( stepVal ); return *this; }
112  Options& precision( int precVal ) { setPrecision( precVal ); return *this; }
114  Options& keyIncr( const std::string &keyIncr ) { setKeyIncr( keyIncr ); return *this; }
116  Options& keyDecr( const std::string &keyDecr ) { setKeyDecr( keyDecr ); return *this; }
118  Options& key( const std::string &key ) { setKey( key ); return *this; }
120  Options& group( const std::string &group ) { setGroup( group ); return *this; }
122  Options& optionsStr( const std::string &optionsStr ) { setOptionsStr( optionsStr ); return *this; }
123 
125  Options& accessors( const SetterFn &setterFn, const GetterFn &getterFn );
127  Options& updateFn( const UpdateFn &updateFn );
128 
129  private:
130  T* mTarget;
131  int mTwType;
132 
133  friend class InterfaceGl;
134  };
135 
137  void draw();
139  void show( bool visible = true );
141  void hide();
143  bool isVisible() const;
144 
146  void maximize( bool maximized = true );
148  void minimize();
150  bool isMaximized() const;
152  void setPosition( const ci::Vec2i &pos );
154  template <typename T>
155  Options<T> addParam( const std::string &name, T *target, bool readOnly = false );
156 
158  template <typename T>
159  Options<T> addParam( const std::string &name, const std::function<void ( T )> &setterFn, const std::function<T ()> &getterFn );
160 
162  void addParam( const std::string &name, const std::vector<std::string> &enumNames, int *param, const std::string &optionsStr = "", bool readOnly = false );
163 
165  void addSeparator( const std::string &name = "", const std::string &optionsStr = "" );
167  void addText( const std::string &name = "", const std::string &optionsStr = "" );
169  void addButton( const std::string &name, const std::function<void()> &callback, const std::string &optionsStr = "" );
171  void removeParam( const std::string &name );
173  void setOptions( const std::string &name = "", const std::string &optionsStr = "" );
175  void clear();
176 
178  void addParam( const std::string &name, bool *boolParam, const std::string &optionsStr, bool readOnly = false );
180  void addParam( const std::string &name, bool *boolParam, const char *optionsStr, bool readOnly = false ) { addParam( name, boolParam, std::string( optionsStr ), readOnly ); }
182  void addParam( const std::string &name, float *floatParam, const std::string &optionsStr, bool readOnly = false );
184  void addParam( const std::string &name, float *floatParam, const char *optionsStr, bool readOnly = false ) { addParam( name, floatParam, std::string( optionsStr ), readOnly ); }
186  void addParam( const std::string &name, double *doubleParam, const std::string &optionsStr, bool readOnly = false );
188  void addParam( const std::string &name, double *doubleParam, const char *optionsStr, bool readOnly = false ) { addParam( name, doubleParam, std::string( optionsStr ), readOnly ); }
190  void addParam( const std::string &name, int32_t *intParam, const std::string &optionsStr, bool readOnly = false );
192  void addParam( const std::string &name, int32_t *intParam, const char *optionsStr, bool readOnly = false ) { addParam( name, intParam, std::string( optionsStr ), readOnly ); }
194  void addParam( const std::string &name, Vec3f *vectorParam, const std::string &optionsStr, bool readOnly = false );
196  void addParam( const std::string &name, Vec3f *vectorParam, const char *optionsStr, bool readOnly = false ) { addParam( name, vectorParam, std::string( optionsStr ), readOnly ); }
198  void addParam( const std::string &name, Quatf *quatParam, const std::string &optionsStr, bool readOnly = false );
200  void addParam( const std::string &name, Quatf *quatParam, const char *optionsStr, bool readOnly = false ) { addParam( name, quatParam, std::string( optionsStr ), readOnly ); }
202  void addParam( const std::string &name, Color *colorParam, const std::string &optionsStr, bool readOnly = false );
204  void addParam( const std::string &name, Color *colorParam, const char *optionsStr, bool readOnly = false ) { addParam( name, colorParam, std::string( optionsStr ), readOnly ); }
206  void addParam( const std::string &name, ColorA *colorParam, const std::string &optionsStr, bool readOnly = false );
208  void addParam( const std::string &name, ColorA *colorParam, const char *optionsStr, bool readOnly = false ) { addParam( name, colorParam, std::string( optionsStr ), readOnly ); }
210  void addParam( const std::string &name, std::string *strParam, const std::string &optionsStr, bool readOnly = false );
212  void addParam( const std::string &name, std::string *strParam, const char *optionsStr, bool readOnly = false ) { addParam( name, strParam, std::string( optionsStr ), readOnly ); }
213 
214  protected:
215  void init( app::WindowRef window, const std::string &title, const Vec2i &size, const ColorA color );
216  void implAddParamDeprecated( const std::string &name, void *param, int type, const std::string &optionsStr, bool readOnly );
217 
218  template <typename T>
219  Options<T> addParamImpl( const std::string &name, T *param, int type, bool readOnly );
220  template <class T>
221  void addParamCallbackImpl( const std::function<void (T)> &setter, const std::function<T ()> &getter, const Options<T> &options );
222 
223  std::weak_ptr<app::Window> mWindow;
224  std::shared_ptr<TwBar> mBar;
226 
227  std::map<std::string, std::shared_ptr<void> > mStoredCallbacks; // key = name, value = memory managed pointer
228 };
229 
230 template <typename T>
231 InterfaceGl::Options<T> InterfaceGl::addParam( const std::string &name, const std::function<void ( T )> &setterFn, const std::function<T ()> &getterFn )
232 {
233  return addParam<T>( name, nullptr ).accessors( setterFn, getterFn );
234 }
235 
236 template <typename T>
238 {
239  if( mTarget )
240  mParent->removeParam( getName() );
241 
242  mParent->addParamCallbackImpl( setterFn, getterFn, *this );
243 
244  reAddOptions();
245  return *this;
246 }
247 
248 template <typename T>
250 {
251  T* target = mTarget;
252  assert( target );
253 
254  std::function<void( T )> setter = [target, updateFn]( T var ) { *target = var; updateFn(); };
255  std::function<T ()> getter = [target]() { return *target; };
256 
257  return accessors( setter, getter );
258 }
259 
260 } } // namespace cinder::params
Base class for chainable options.
Definition: Params.h:62
std::shared_ptr< class InterfaceGl > InterfaceGlRef
Definition: Params.h:44
Options & keyDecr(const std::string &keyDecr)
Sets a decrement shortcut key.
Definition: Params.h:116
void setPosition(const ci::Vec2i &pos)
Sets the position of this interface instance.
Definition: Params.cpp:328
int mTwWindowId
Definition: Params.h:225
bool mMaxSet
Definition: Params.h:88
Options & accessors(const SetterFn &setterFn, const GetterFn &getterFn)
! Sets setterFn and getterFn as callbacks for this param. the target is ignored in this case...
Definition: Params.h:237
void addParam(const std::string &name, Vec3f *vectorParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:196
void setOptionsStr(const std::string &optionsStr)
Definition: Params.cpp:424
GLsizei const GLchar ** string
Definition: GLee.h:2427
void addParam(const std::string &name, Quatf *quatParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:200
void addParamCallbackImpl(const std::function< void(T)> &setter, const std::function< T()> &getter, const Options< T > &options)
const std::string & getKeyIncr() const
Definition: Params.h:66
std::string mKeyIncr
Definition: Params.h:84
void setKeyIncr(const std::string &keyIncr)
Definition: Params.cpp:388
const std::string & getKeyDecr() const
Definition: Params.h:67
void show(bool visible=true)
Shows the interface. If visible is false, hides the interface.
Definition: Params.cpp:284
InterfaceGl * mParent
Definition: Params.h:90
void minimize()
Minimizes the interface face to the bottom left corner of the window.
Definition: Params.cpp:314
struct CTwBar TwBar
Definition: Params.h:33
void addParam(const std::string &name, int32_t *intParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:192
void addButton(const std::string &name, const std::function< void()> &callback, const std::string &optionsStr="")
Adds a button that fires callback when clicked.
Definition: Params.cpp:540
static InterfaceGlRef create(const std::string &title, const Vec2i &size, const ColorA &color=ColorA(0.3f, 0.3f, 0.3f, 0.4f))
Creates and returns an InterfaceGl referenced by title and with size dimensions. Optionally takes col...
Definition: Params.cpp:243
bool mStepSet
Definition: Params.h:88
void setStep(float maxVal)
Definition: Params.cpp:366
Options & key(const std::string &key)
Sets a shortcut key for param types that cannot be incremented / decremented (ex. bool) ...
Definition: Params.h:118
const std::string & getName() const
Definition: Params.h:64
void addSeparator(const std::string &name="", const std::string &optionsStr="")
Adds a separator to the interface.
Definition: Params.cpp:526
Options(const std::string &name, T *target, int type, InterfaceGl *parent)
Definition: Params.cpp:339
GLenum target
Definition: GLee.h:13607
void init(app::WindowRef window, const std::string &title, const Vec2i &size, const ColorA color)
Definition: Params.cpp:253
Options & max(float maxVal)
Sets the minimum value for the associated target param.
Definition: Params.h:108
std::string mGroup
Definition: Params.h:84
void * mVoidPtr
Definition: Params.h:85
Options & precision(int precVal)
Sets the number of significant digits for floating point variables (float or double type only)...
Definition: Params.h:112
bool isVisible() const
Returns whether the interface is visible or not.
Definition: Params.cpp:297
void setOptions(const std::string &name="", const std::string &optionsStr="")
Sets runtime options on the param referred to by name.
Definition: Params.cpp:566
void implAddParamDeprecated(const std::string &name, void *param, int type, const std::string &optionsStr, bool readOnly)
Definition: Params.cpp:451
void setPrecision(int precVal)
Definition: Params.cpp:377
std::string mName
Definition: Params.h:84
Interface for adding params to your window. Wraps AntTweakBar.
Definition: Params.h:47
void clear()
Removes all the variables, buttons and separators previously added.
Definition: Params.cpp:558
std::shared_ptr< Window > WindowRef
Definition: Event.h:49
std::string mKeyDecr
Definition: Params.h:84
void addParam(const std::string &name, Color *colorParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:204
Options & updateFn(const UpdateFn &updateFn)
! Sets an update function that will be called after the target param is updated.
Definition: Params.h:249
bool mMinSet
Definition: Params.h:88
std::shared_ptr< TwBar > mBar
Definition: Params.h:224
void addParam(const std::string &name, double *doubleParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:188
ColorAT< float > ColorA
Definition: Color.h:408
void setMax(float maxVal)
Definition: Params.cpp:355
void removeParam(const std::string &name)
Removes the param referred to by name.
Definition: Params.cpp:550
Options & optionsStr(const std::string &optionsStr)
Sets other implementation defined options via string.
Definition: Params.h:122
std::function< T()> GetterFn
Definition: Params.h:102
bool isMaximized() const
Returns whether the interface is maximized or not.
Definition: Params.cpp:319
Options< T > addParam(const std::string &name, T *target, bool readOnly=false)
Adds target as a param to the interface, referring to it with name.
Options & keyIncr(const std::string &keyIncr)
Sets an increment shortcut key.
Definition: Params.h:114
float mMax
Definition: Params.h:86
void setMin(float minVal)
Definition: Params.cpp:344
float mStep
Definition: Params.h:86
void setKey(const std::string &key)
Definition: Params.cpp:406
int mPrecision
Definition: Params.h:87
InterfaceGl()
Definition: Params.h:55
void draw()
Draw the interface.
Definition: Params.cpp:277
void addParam(const std::string &name, ColorA *colorParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:208
void * getVoidPtr() const
Definition: Params.h:65
Window window
Definition: GLee.h:17134
GLuint GLuint GLsizei GLenum type
Definition: GLee.h:963
Options & min(float minVal)
Sets the maximum value for the associated target param.
Definition: Params.h:106
GLenum const GLfloat * params
Definition: GLee.h:975
GLuint const GLchar * name
Definition: GLee.h:2259
void hide()
Hides the interface.
Definition: Params.cpp:292
void reAddOptions()
Definition: Params.cpp:431
void setGroup(const std::string &group)
Definition: Params.cpp:415
OptionsBase(const std::string &name, void *targetVoidPtr, InterfaceGl *parent)
Definition: Params.cpp:334
void addParam(const std::string &name, std::string *strParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:212
float mMin
Definition: Params.h:86
bool mPrecisionSet
Definition: Params.h:88
std::string mKey
Definition: Params.h:84
Options< T > addParamImpl(const std::string &name, T *param, int type, bool readOnly)
Definition: Params.cpp:578
Options & step(float stepVal)
Sets step increment for the associated target param.
Definition: Params.h:110
void addText(const std::string &name="", const std::string &optionsStr="")
Adds text to the interface.
Definition: Params.cpp:533
std::function< void(T)> SetterFn
Definition: Params.h:101
std::string mOptionsStr
Definition: Params.h:84
std::map< std::string, std::shared_ptr< void > > mStoredCallbacks
Definition: Params.h:227
Provides chainable options, returned from addParam().
Definition: Params.h:97
std::weak_ptr< app::Window > mWindow
Definition: Params.h:223
void maximize(bool maximized=true)
Maximizes the interface, making it visible in its default presentation mode. If maximized is false...
Definition: Params.cpp:306
GLclampf f
Definition: GLee.h:15307
void setKeyDecr(const std::string &keyDecr)
Definition: Params.cpp:397
void addParam(const std::string &name, float *floatParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:184
GLsizeiptr size
Definition: GLee.h:2089
void addParam(const std::string &name, bool *boolParam, const char *optionsStr, bool readOnly=false)
Definition: Params.h:180
GLfloat param
Definition: GLee.h:1737
std::function< void()> UpdateFn
Definition: Params.h:103
GLuint color
Definition: GLee.h:3198
Options & group(const std::string &group)
Sets the param group.
Definition: Params.h:120