On 03/07/2013 at 06:52, xxxxxxxx wrote:
GvOperatorData - Simple graph-view calculation example
While I tried to figure this out, an example would have been more than I could have ever dreamt
of. Remo was so kind to hand one over to me, which helped me figuring out a few things. Here is
an example of mine:
class MyOperatorData : public GvOperatorData {
typedef GvOperatorData super;
public:
//| GvOperatorData Overrides
virtual Bool InitCalculation(GvNode* node, GvCalc* calc, GvRun* run) {
if (!node || !calc || !run) return FALSE;
if (!GvBuildValuesTable(node, m_values, calc, run, GV_EXISTING_PORTS)) {
return FALSE;
}
return super::InitCalculation(node, calc, run);
}
virtual void FreeCalculation(GvNode* node, GvCalc* calc) {
GvFreeValuesTable(node, m_values);
super::FreeCalculation(node, calc);
}
virtual Bool Calculate(GvNode* node, GvPort* outPort, GvRun* run, GvCalc* calc) {
if (!node || !run || !calc) return FALSE;
if (!GvCalculateInValuesTable(node, run, calc, m_values)) {
return FALSE;
}
Bool index = 0;
GvPort* indexPort = node->GetInPortFirstMainID(ID_MYPORT_INDEX);
if (indexPort && indexPort->GetInteger(&index, run)) ;
else {
// Either work with index = 0 or stop calculation. Returning FALSE
// will make the XPresso Node turn yellow!
}
// Limit the index.
LONG count = GetFooBarArrayCountFromSomewhere();
if (index < 0) index = 0;
else if (index >= count) index = count - 1;
if (!outPort) {
// If your node is an initiator, handle this step here. Eg. when
// your input-ports should be directed to values on an object or
// similar, you can retrieve the values here and fill in the object.
//
// Example (imagine an array of objects that can be addressed via
// via the index retrieved above) :
Real value = 0.0;
GvPort* valuePort = node->GetInPortFirstMainID(ID_MYPORT_VALUE);
if (valuePort && valuePort->GetReal(&value, run)) ;
else {
// The port does either not exist or is not connected. If
// the port is described as an INPORT in the operator
// description resource, the value can even be retrieved when
// the port is not connected (since the value is defined in
// the Attributes Manager).
return FALSE;
}
Real* data = GetFooBarArrayFromSomewhere();
data[index] = value;
return TRUE;
}
else {
// Set the data for an output port.
switch (outPort->GetMainID()) {
case ID_MYPORT_COUNT:
return outPort->SetInteger(count, run);
default:
break;
}
}
// If we reached this statement, something went wrong.
return FALSE;
}
private:
GvValuesInfo m_values;
};