如何-在运行时创建和销毁虚拟按钮

请求按钮状态

    你可以从场景中被激活的目标中以迭代按钮子对象的方式请求按钮状态。下面的代码片段提供了这种行为的列子程序。

// 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;
}


转载于:https://my.oschina.net/sunhao1232856/blog/649821

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值