[Newbie question] Mouse clicks, overlapping components


#1

Hello everyone! I’m quite new to C++ and Juce, but I have read the tutorial and made some progress. Until now. I was wondering if maybe someone could give me a little help…

What I want to do:

Draw several non-rectangular and possibly overlapping shapes in a component and be able to select one of them with a mouse click.

I can think of two ways to do it:

A) The shapes could all be drawn drawn in the paint method of the background class which also captures the mouse clicks. When mouse clicks are detected the coordinates could be compared with the position data of each shape to find out which one was clicked. Seems awkward.

B) The shapes could be created as objects from a class that inherit from Component and each would draw itself in its on paint() method. That would mean there would be many layers of rectangular components over the background/container class that holds all the objects. Those componsents would be of the same size size as the background to have the same coordinate system, and be mostly transparent apart from pixels occupied by the shape.

  1. How can I detect which one was clicked in this case?

  2. Is there a way of capturing mouse clicks only on the parts of a component that are visible?


#2

Forgot to say thanks in advance :smiley:


#3

Hello!

Yes, use option B, and write components to draw the shapes. Then, give them a custom hitTest() method, and that’ll let you specify the area that can be clicked-on. Easy!


#4

Thanks for helping me Jules!!

Is the hitTest() I implement used automatically, or is it something I need to call? Can’t try for myself right now because I don’t have my Juce-PC here with me:)


#5

You just implement it. It’s called by the system when it needs to find out about your component.


#6

Thanks Jules :smiley: