private bool CreateNewVirtualButtonFromBehaviour(VirtualButtonBehaviour newVBB)
    {
        // Calculate the button area:
        Vector2 leftTop, rightBottom;

        newVBB.CalculateButtonArea(out leftTop, out rightBottom);
        VirtualButton.RectangleData area = new VirtualButton.RectangleData
        {
            leftTopX     = leftTop.x,
            leftTopY     = leftTop.y,
            rightBottomX = rightBottom.x,
            rightBottomY = rightBottom.y
        };

        VirtualButton virtualButton = mImageTarget.CreateVirtualButton(newVBB.VirtualButtonName, area);

        if (virtualButton == null)
        {
            Destroy(newVBB.gameObject);
            return(false);
        }

        IEditorVirtualButtonBehaviour newEditorVBB = newVBB;

        newEditorVBB.InitializeVirtualButton(virtualButton);
        mVirtualButtonBehaviours.Add(virtualButton.ID, newVBB);
        return(true);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Creates virtual buttons for this ImageTarget from native and stores them
    /// </summary>
    private void CreateVirtualButtonsFromNative()
    {
        // Allocate array for all Image Targets.
        int    numVirtualButtons    = QCARWrapper.Instance.ImageTargetGetNumVirtualButtons(mDataSet.DataSetPtr, Name);
        IntPtr virtualButtonDataPtr =
            Marshal.AllocHGlobal(Marshal.SizeOf(typeof(QCARManagerImpl.VirtualButtonData)) * numVirtualButtons);
        IntPtr rectangleDataPtr =
            Marshal.AllocHGlobal(Marshal.SizeOf(typeof(VirtualButton.RectangleData)) * numVirtualButtons);

        // Copy Virtual Button data from native.
        QCARWrapper.Instance.ImageTargetGetVirtualButtons(virtualButtonDataPtr,
                                                          rectangleDataPtr,
                                                          numVirtualButtons,
                                                          mDataSet.DataSetPtr,
                                                          Name);

        for (int i = 0; i < numVirtualButtons; ++i)
        {
            IntPtr vbPtr = new IntPtr(virtualButtonDataPtr.ToInt32() + i *
                                      Marshal.SizeOf(typeof(QCARManagerImpl.VirtualButtonData)));
            QCARManagerImpl.VirtualButtonData vbData = (QCARManagerImpl.VirtualButtonData)
                                                       Marshal.PtrToStructure(vbPtr, typeof(QCARManagerImpl.VirtualButtonData));

            // Do not overwrite existing Virtual Buttons.
            if (mVirtualButtons.ContainsKey(vbData.id))
            {
                continue;
            }

            IntPtr rectPtr = new IntPtr(rectangleDataPtr.ToInt32() + i *
                                        Marshal.SizeOf(typeof(VirtualButton.RectangleData)));
            VirtualButton.RectangleData rectData = (VirtualButton.RectangleData)
                                                   Marshal.PtrToStructure(rectPtr, typeof(VirtualButton.RectangleData));

            // QCAR support names up to 64 characters in length, but here we allocate
            // a slightly larger buffer:
            int nameLength = 128;
            System.Text.StringBuilder vbName = new System.Text.StringBuilder(nameLength);
            if (QCARWrapper.Instance.ImageTargetGetVirtualButtonName(mDataSet.DataSetPtr, Name,
                                                                     i, vbName, nameLength) == 0)
            {
                Debug.LogError("Failed to get virtual button name.");
                continue;
            }

            VirtualButton virtualButton = new VirtualButtonImpl(vbName.ToString(), vbData.id, rectData, this, mDataSet);
            mVirtualButtons.Add(vbData.id, virtualButton);
        }

        Marshal.FreeHGlobal(virtualButtonDataPtr);
        Marshal.FreeHGlobal(rectangleDataPtr);
    }
Ejemplo n.º 3
0
    /// <summary>
    /// Creates a new virtual button and adds it to the ImageTarget
    /// Returns NULL if the corresponding DataSet is currently active.
    /// </summary>
    public VirtualButton CreateVirtualButton(string name, VirtualButton.RectangleData area)
    {
        VirtualButton virtualButton = CreateNewVirtualButtonInNative(name, area);

        if (virtualButton == null)
        {
            Debug.LogError("Could not create Virtual Button.");
        }
        else
        {
            Debug.Log("Created Virtual Button successfully.");
        }

        return(virtualButton);
    }
Ejemplo n.º 4
0
    /// <summary>
    /// Update the virtual button rect in native
    /// </summary>
    public bool UpdateAreaRectangle()
    {
        VirtualButton.RectangleData rectData = new VirtualButton.RectangleData();

        rectData.leftTopX     = mLeftTop.x;
        rectData.leftTopY     = mLeftTop.y;
        rectData.rightBottomX = mRightBottom.x;
        rectData.rightBottomY = mRightBottom.y;

        if (mVirtualButton == null)
        {
            return(false);
        }

        return(mVirtualButton.SetArea(rectData));
    }
Ejemplo n.º 5
0
    // Registers a Virtual Button at native code.
    private VirtualButton CreateNewVirtualButtonInNative(string name, VirtualButton.RectangleData rectangleData)
    {
        // virtual buttons cannot be registered for user defined targets:
        if (ImageTargetType != ImageTargetType.PREDEFINED)
        {
            Debug.LogError("DataSet.RegisterVirtualButton: virtual button '" + name +
                           "' cannot be registered for a user defined target.");
            return(null);
        }


        IntPtr rectPtr = Marshal.AllocHGlobal(
            Marshal.SizeOf(typeof(VirtualButton.RectangleData)));

        Marshal.StructureToPtr(rectangleData, rectPtr, false);

        bool registerWorked =
            (QCARWrapper.Instance.ImageTargetCreateVirtualButton(mDataSet.DataSetPtr, Name,
                                                                 name, rectPtr) != 0);

        VirtualButton vb = null;

        if (registerWorked)
        {
            int id = QCARWrapper.Instance.VirtualButtonGetId(mDataSet.DataSetPtr, Name,
                                                             name);

            // Check we don't have an entry for this id:
            if (!mVirtualButtons.ContainsKey(id))
            {
                // Add:
                vb = new VirtualButtonImpl(name, id, rectangleData, this, mDataSet);
                mVirtualButtons.Add(id, vb);
            }
            else
            {
                vb = mVirtualButtons[id];
            }
        }

        return(vb);
    }
Ejemplo n.º 6
0
    private bool CreateNewVirtualButtonFromBehaviour(VirtualButtonBehaviour newVBB)
    {
        // Calculate the button area:
        Vector2 leftTop, rightBottom;
        newVBB.CalculateButtonArea(out leftTop, out rightBottom);
        VirtualButton.RectangleData area = new VirtualButton.RectangleData
        {
            leftTopX = leftTop.x,
            leftTopY = leftTop.y,
            rightBottomX = rightBottom.x,
            rightBottomY = rightBottom.y
        };

        VirtualButton virtualButton = mImageTarget.CreateVirtualButton(newVBB.VirtualButtonName, area);

        if (virtualButton == null)
        {
            Destroy(newVBB.gameObject);
            return false;
        }

        IEditorVirtualButtonBehaviour newEditorVBB = newVBB;
        newEditorVBB.InitializeVirtualButton(virtualButton);
        mVirtualButtonBehaviours.Add(virtualButton.ID, newVBB);
        return true;
    }
Ejemplo n.º 7
0
    /// <summary>
    /// Associates existing virtual button behaviour with virtualbuttons and creates new VirtualButtons if necessary
    /// </summary>
    void IEditorImageTargetBehaviour.AssociateExistingVirtualButtonBehaviour(VirtualButtonBehaviour virtualButtonBehaviour)
    {
        VirtualButton virtualButton = mImageTarget.GetVirtualButtonByName(virtualButtonBehaviour.VirtualButtonName);

        if (virtualButton == null)
        {
            Vector2 leftTop, rightBottom;
            virtualButtonBehaviour.CalculateButtonArea(out leftTop, out rightBottom);
            VirtualButton.RectangleData area = new VirtualButton.RectangleData
                                                {
                                                    leftTopX = leftTop.x,
                                                    leftTopY = leftTop.y,
                                                    rightBottomX = rightBottom.x,
                                                    rightBottomY = rightBottom.y
                                                };
            virtualButton = mImageTarget.CreateVirtualButton(virtualButtonBehaviour.VirtualButtonName, area);

            // Create the virtual button
            if (virtualButton != null)
            {
                Debug.Log("Successfully created virtual button " +
                          virtualButtonBehaviour.VirtualButtonName +
                          " at startup");

                virtualButtonBehaviour.UnregisterOnDestroy = true;
            }
            else
            {
                Debug.LogError("Failed to create virtual button " +
                               virtualButtonBehaviour.VirtualButtonName +
                               " at startup");
            }
        }

        if (virtualButton != null)
        {
            //  Duplicate check:
            if (!mVirtualButtonBehaviours.ContainsKey(virtualButton.ID))
            {
                // OK:
                IEditorVirtualButtonBehaviour editorVirtualButtonBehaviour = virtualButtonBehaviour;
                editorVirtualButtonBehaviour.InitializeVirtualButton(virtualButton);
                mVirtualButtonBehaviours.Add(virtualButton.ID, virtualButtonBehaviour);

                Debug.Log("Found VirtualButton named " +
                        virtualButtonBehaviour.VirtualButton.Name + " with id " +
                        virtualButtonBehaviour.VirtualButton.ID);

                // Handle any changes to the virtual button in the scene
                // that are not reflected in the config file
                virtualButtonBehaviour.UpdatePose();
                if (!virtualButtonBehaviour.UpdateAreaRectangle() ||
                    !virtualButtonBehaviour.UpdateSensitivity())
                {
                    Debug.LogError("Failed to update virtual button " +
                                   virtualButtonBehaviour.VirtualButton.Name +
                                   " at startup");
                }
                else
                {
                    Debug.Log("Updated virtual button " +
                              virtualButtonBehaviour.VirtualButton.Name +
                              " at startup");
                }
            }
        }
    }
Ejemplo n.º 8
0
 /// <summary>
 /// Creates a new virtual button and adds it to the ImageTarget
 /// This is not supported for CloudReco targets.
 /// </summary>
 public VirtualButton CreateVirtualButton(string name, VirtualButton.RectangleData area)
 {
     Debug.LogError("Virtual buttons are currently not supported for cloud reco targets.");
     return(null);
 }
    /// <summary>
    /// Associates existing virtual button behaviour with virtualbuttons and creates new VirtualButtons if necessary
    /// </summary>
    void IEditorImageTargetBehaviour.AssociateExistingVirtualButtonBehaviour(VirtualButtonBehaviour virtualButtonBehaviour)
    {
        VirtualButton virtualButton = mImageTarget.GetVirtualButtonByName(virtualButtonBehaviour.VirtualButtonName);

        if (virtualButton == null)
        {
            Vector2 leftTop, rightBottom;
            virtualButtonBehaviour.CalculateButtonArea(out leftTop, out rightBottom);
            VirtualButton.RectangleData area = new VirtualButton.RectangleData
            {
                leftTopX     = leftTop.x,
                leftTopY     = leftTop.y,
                rightBottomX = rightBottom.x,
                rightBottomY = rightBottom.y
            };
            virtualButton = mImageTarget.CreateVirtualButton(virtualButtonBehaviour.VirtualButtonName, area);

            // Create the virtual button
            if (virtualButton != null)
            {
                Debug.Log("Successfully created virtual button " +
                          virtualButtonBehaviour.VirtualButtonName +
                          " at startup");

                virtualButtonBehaviour.UnregisterOnDestroy = true;
            }
            else
            {
                Debug.LogError("Failed to create virtual button " +
                               virtualButtonBehaviour.VirtualButtonName +
                               " at startup");
            }
        }

        if (virtualButton != null)
        {
            //  Duplicate check:
            if (!mVirtualButtonBehaviours.ContainsKey(virtualButton.ID))
            {
                // OK:
                IEditorVirtualButtonBehaviour editorVirtualButtonBehaviour = virtualButtonBehaviour;
                editorVirtualButtonBehaviour.InitializeVirtualButton(virtualButton);
                mVirtualButtonBehaviours.Add(virtualButton.ID, virtualButtonBehaviour);

                Debug.Log("Found VirtualButton named " +
                          virtualButtonBehaviour.VirtualButton.Name + " with id " +
                          virtualButtonBehaviour.VirtualButton.ID);

                // Handle any changes to the virtual button in the scene
                // that are not reflected in the config file
                virtualButtonBehaviour.UpdatePose();
                if (!virtualButtonBehaviour.UpdateAreaRectangle() ||
                    !virtualButtonBehaviour.UpdateSensitivity())
                {
                    Debug.LogError("Failed to update virtual button " +
                                   virtualButtonBehaviour.VirtualButton.Name +
                                   " at startup");
                }
                else
                {
                    Debug.Log("Updated virtual button " +
                              virtualButtonBehaviour.VirtualButton.Name +
                              " at startup");
                }
            }
        }
    }
    /// <summary>
    /// Update the virtual button rect in native
    /// </summary>
    public bool UpdateAreaRectangle()
    {
        VirtualButton.RectangleData rectData = new VirtualButton.RectangleData();

        rectData.leftTopX = mLeftTop.x;
        rectData.leftTopY = mLeftTop.y;
        rectData.rightBottomX = mRightBottom.x;
        rectData.rightBottomY = mRightBottom.y;

        if (mVirtualButton == null) return false;

        return mVirtualButton.SetArea(rectData);
    }