I want to implement one feature in my program. For example, I have a component with 10 ImageButtons (in a row) and I want enable multiple buttons by dragging mouse (clicking each button is annoying). I've tried some ways to solve this problem, but without any good results.
For example, I tried something like that:
void MyComponent::mouseEnter (const MouseEvent& e)
{
for (int i = 0; i < mButtons.size(); ++i)
{
if (e.originalComponent == mButtons[i] && mButtons[i]->isDown() == false)
{
mButtons[i]->triggerClick();
break;
}
}
}
It won't be called during a drag - mouse events are either redirected to the original (initial) mouseDown recipient or muted. Looking at the OS-specific implementation I guess a generic scrub isn't possible without some complicated timer logic like the midi keyboard component uses?
I've tried this before creating that post...The problem is, when we press left mouse button on the button, other buttons don't get events mouseEnter or mouseDrag events :(
So you offer to overlay all buttons by transparent component? In this case we should handle all clicks on this component, check if their position in bounds of buttons. And so we should do by dragging, just by hardcode mouseDrag and activate buttons by position? I think it's not so convenient.
But I have one issue: if I don't add that line (right after creating ImageButton),
btn->addMouseListener (this, false);
the MainContentComponent didn't get any events like mouseDown, mouseDrag, etc.
But if I add that line, by pressing button it gets incorrect position: it gets position relatively to the left top corner of the button where click happened, not the position of click in MainContentComponent. What can I do to solve that problem?
Thank you! I really appreciate your help! Your solution was the most convenient and proper I think. It helped me to implement the feature I wanted to add.
That works great! for a midi keyboard-like scrub you just need to add the trigger off events (or re-trigger for a toggle) and it's worth noting that if you use a reverse iterator in getButtonAtLocation() it'll mimic the UI collision detection, i.e. buttons added later will have a z-depth above earlier ones, f.ex. on a midi keyboard you'd want to test black keys before white keys since the latter may overlap the former.
I still /guess/ that Juce's midi scrub logic is better / thread-safe.