Contents

TwAddVarCB (function)

int TwAddVarCB(TwBar *bar, const char *name, TwType type, TwSetCallback setCallback, TwGetCallback getCallback, void *clientData, const char *def)

Description

This function adds a new variable to a tweak bar by providing CallBack (CB) functions to access it. If the setCallback parameter is set to NULL, the variable is declared Read-Only, so it could not be modified interactively by the user. Otherwise, it is a Read-Write variable, and could be modified interactively by the user.

Parameters

bar

The tweak bar to which adding a new variable.

name

The name of the variable. It will be displayed in the tweak bar if no label is specified for this variable. It will also be used to refer to this variable in other functions, so choose a unique, simple and short name and avoid special characters like spaces or punctuation marks.

type

Type of the variable. It must be one of the TwType constants or a user defined type created with TwDefineStruct or TwDefineEnum*.

setCallback

The callback function that will be called by AntTweakBar to change the variable’s value.

You should define this callback function like this:

void TW_CALL SetCallback(const void *value, void *clientData)
{ 
    myVariable = *(const MyVariableType *)value;  // for instance
}

getCallback

The callback function that will be called by AntTweakBar to get the variable’s value.

You should define this callback function like this:

void TW_CALL GetCallback(void *value, void *clientData)
{ 
    *(MyVariableType *)value = myVariable;  // for instance
}

clientData

For your convenience, this is a supplementary pointer that will be passed to the callback functions when they are called. For instance, if you set it to an object pointer, you can use it to access to the object’s members inside the callback functions.

Here is an example:

  class Sphere
  {
  public:
    void  SetRadius(float r);
    float GetRadius() const;
    // ...
    static void TW_CALL SetRadiusCallback(const void *value, void *clientData)
    {
      static_cast<Sphere *>(clientData)->SetRadius(*static_cast<const float *>(value));
    }
    static void TW_CALL GetRadiusCallback(void *value, void *clientData)
    {
      *static_cast<float *>(value) = static_cast<const Sphere *>(clientData)->GetRadius();
    }
    // ...
  };
 
  int main()
  {
    Sphere s;
    // ...
    TwAddVarCB(bar, "s.Radius", TW_TYPE_FLOAT, Sphere::SetRadiusCallback, Sphere::GetRadiusCallback, &s, "");
    // ...
  }

def

An optional definition string used to modify the behavior of this new entry. This string must follow the variable parameters syntax, or set to NULL to get the default behavior. It could be set or modified later by calling the TwDefine or TwSetParam functions.

Return value

See also

TwAddVarRO, TwAddVarRW, TwAddButton, TwRemoveVar, TwAddSeparator, TwDefine, TwSetParam, TwGetParam, TwDefineStruct, TwDefineEnum