I have a custom Midi Keyboard component. As a simple test, I called getNoteAtPosition from the mouseEnter event handler. It works fine.
class CustomMidiKeyboard : public juce::MidiKeyboardComponent
{
...
void mouseEnter(const MouseEvent& e) override
{
int note = getNoteAtPosition(e.position);
}
private:
int note;
};
However, when I call getNoteAtPosition from a custom control, -1 is returned
class CustomMidiKeyboard : public juce::MidiKeyboardComponent
{
public:
//=============== MY NESTED CLASS =====================
class MyComponent : public juce::Component
{
public:
MyComponent()=default;
~MyComponent() = default;
void mouseDown(const MouseEvent& e) override
{
dragger.startDraggingComponent(this, e);
}
void mouseDrag(const MouseEvent& e) override
{
// Convert the coordinates to the coordinates
// of the Midi Keyboard
//
MouseEvent midiEvent = e.getEventRelativeTo(midi);
// Now I pass the "Point<float>" object using the coordinates
// of the Midi Keyboard
int note = midi->getNoteAtPosition(midiEvent.position);
// The value returned is -1;
dragger.dragComponent(this, e, nullptr);
}
void paint(Graphics&)override
{
// Draw component
}
void addMidiKeyboard(CustomMidiKeyboard *kb)
{
midi = kb;
}
private:
ComponentDragger dragger;
CustomMidiKeyboard * midi;
};
//============================================
protected:
void mouseEnter(const MouseEvent&) override
{
// This works perfectly fine
//
int note = getNoteAtPosition(e.position);
}
};
If you look at the code above. You’ll see that I also overrode “mouseEnter”.
I took the values from mouseEnter and used them in my mouseDrag method and -1 was still returned.
Sample code zipped and uploaded. It’s a minimal project.
CustomMidiKeyboard.h
void mouseDrag(const juce::MouseEvent& e) override
{
// Convert the coordinates to the coordinates
// of the Midi Keyboard
//
juce::MouseEvent midiEvent = e.getEventRelativeTo(midi);
// Now I pass the "Point<float>" object using the coordinates
// of the Midi Keyboard
int note = midi->getNoteAtPosition(midiEvent.position);
// The value returned is -1;
dragger.dragComponent(this, e, nullptr);
}
CustomMidiKeyboard.cpp
void CustomMidiKeyboard::mouseEnter(const juce::MouseEvent& e)
{
// Put a break point here.
// You’ll see a legit value returned
auto note = getNoteAtPosition(e.position);
}
Last night I was tracing through this code like a crazy person and found “HitTest” to be suspect. I don’t think there’s a defect in the code, but rather in my understanding of it’s functionality.
I came up with a slightly different conclusion from you:
Notice that if you set true in the JUCE code if (! reallyContains (pos.toInt(), false)), your code should work fine (i mean instead of false as 2nd argument). Of course changing JUCE code like that is something nobody should do at home!
Actually, I have made changes to JUCE before that I will be submitting at some point. It allows me to get an incremental slider like the one pictured below without altering the basic flow of the slider logic.
Hey, thanks for going in on this with me. It was good having two brains on this ultimately reaching the same conclusion. I won’t have to second guess myself now and proceed to look for a work around.