请求按钮状态
你可以从场景中被激活的目标中以迭代按钮子对象的方式请求按钮状态。下面的代码片段提供了这种行为的列子程序。
// Iterate through this target virtual buttons:
for (int i = 0; i < targetResult->getNumVirtualButtons(); ++i)
{
const VirtualButtonResult* button = targetResult->getVirtualButtonResult(i);
// If the button is pressed, then use this texture:
if (buttonResult->isPressed())
{
textureIndex = i+1;
break;
}
}
对象从QCAR SDK中接收update回调函数
为了动态的创建和销毁虚拟按钮,你应该在tracker升级状态后做出这些改变。在tracker投送了一个有效状态后,高通SDK在每一帧调用被注册的回调函数,如下面的代码片段所述。
// Object to receive update callbacks from QCAR SDK
class VirtualButton_UpdateCallback : public QCAR::UpdateCallback
{
// Update runs in the tracking thread therefore it is guaranteed that the tracker is
// not doing anything at this point. => Reconfiguration is possible.
virtual void QCAR_onUpdate(QCAR::State& /*state*/)
{
if (updateBtns)
{
// Collect the references to the imageTarget
...
if (buttonMask & BUTTON_1)
{
toggleVirtualButton(imageTarget, virtualButtonColors[0],
-108.68f, -53.52f, -75.75f, -65.87f);
}
if (buttonMask & BUTTON_2)
{
...
}
...
buttonMask = 0;
updateBtns = false;
}
}
} qcarUpdate;
创建和销毁虚拟按钮
下面的方法执行按钮的实际切换开关。这个方法被在上一个代码片段中创建的每一个按钮所调用。下面的代码片段显示了如何创建一个新的按钮和如何得到一个现有的按钮或者销毁它。
// Create/destroy a Virtual Button at runtime
//
// Note: This will NOT work if the tracker is active!
bool
toggleVirtualButton(QCAR::ImageTarget* imageTarget, const char* name,
float left, float top, float right, float bottom)
{
bool buttonToggleSuccess = false;
QCAR::VirtualButton* virtualButton = imageTarget->getVirtualButton(name);
if (virtualButton != NULL)
{
// Destroying Virtual Button
buttonToggleSuccess = imageTarget->destroyVirtualButton(virtualButton);
}
else
{
// Creating Virtual Button
QCAR::Rectangle vbRectangle(left, top, right, bottom);
QCAR::VirtualButton* virtualButton = imageTarget->createVirtualButton(name, vbRectangle);
if (virtualButton != NULL)
buttonToggleSuccess = true;
}
return buttonToggleSuccess;
}