LassoComponent help


#1

Hi,

I would like to understand the LassoComponent but I'm having an hard time reading from the IntroJucer files.
I have a parent component with several child components (Cable) that I want to select.

In my parent component declaration I have this:

class myParentClass : public Component, public LassoSource<Cable*>
public:
    void findLassoItemsInArea (Array <Cable*>& results, const Rectangle<int>& area);
    SelectedItemSet<Cable*> selected;
    SelectedItemSet<Cable*>& getLassoSelection();

then:

void myParentClass::mouseDown(const juce::MouseEvent &e)
{
    addChildComponent(lassoCable);
    lassoCable.beginLasso(e, this);
}


void myParentClass::mouseDrag(const juce::MouseEvent &e)
{
    lassoCable.toFront(false);
    lassoCable.dragLasso(e);
}

void myParentClass::mouseUp(const juce::MouseEvent &e)
{
    lassoCable.endLasso();
    removeChildComponent(&lassoCable);
}


void ModularSpace::findLassoItemsInArea (Array <Cable*>& results, const Rectangle<int>& area)
{

}

SelectedItemSet<Cable*>& ModularSpace::getLassoSelection()
{
    return selected;
}

 

The lasso drawing is fine, but I don't understand how can I manage which component (Cable) is selected.
How am I supposed to use findLassoItemsInArea and the getLassoSelection?

Thanks!


#2

Hello!

You can use findLassoItemsInArea to decide what is "selected" and what is not. Here's a simple example (C++11, if you're using an older compiler you'll have to change the for loop syntax):

void YourClass::findLassoItemsInArea (Array<YourComponentType*>& itemsFound, const Rectangle<int>& area)
{
    for (auto& element : components)
    {
        if (area.contains (element.getBounds()))
        {
            itemsFound.add (&element);
        }
    }
}

...where components is an iteratable collection of components you'd like to check against. Of course, you're welcome to implement this however you like, the important part is that you add to itemsFound.

Hope this helps!