Cannot mouseDown() on child Component


#1

Hello,

In one of my project, I have a main Component that contains 2 images (black screen). I have a class ‘My2DScreen’ that inherits Component and Graphics. I create 2 2DScreen inside those images, and I’d like to make the mouseDown() redefined in ‘My2DScreen’ called whenever I click on one of those component.
However, it is indeed the mouseDown() of my mainComponent that is called and if I try "Logger::outputDebugString(e.source.getComponentUnderMouse()->getName()); I get the name of the main Component instead of the one of the 2DScreen.

I’ve tried setInterceptsMouseClick() on both components, and setAlwaysOnTop() on the 2DScreen but it doesn’t change anything.

Is there anything I can do?

Thank you!


#2

Mouse events will certainly go to a component if it’s visible and in the right place. So it probably isn’t where you think it is, or isn’t visible, or something simple like that. You don’t need to call setInterceptsMouseClick.


#3

Well add and make visible wasn’t there but even with it, it still doesn’t work and I’m sure it is at the right place knowing that when I do drawImageAt on my 2DScreen, the pictures appear at the right place.
I still have the name of the mainComponent on the console everytime I click on the screens


#4

Nobody can psychically debug your program for you without even seeing it. Try asking more specific questions.


#5

Ok, I’ll try to give some more details, at least the important ones. Here is what I have. In My2DScreen:

The constructor:
My2DScreen::My2DScreen(PannerComponent* component, const Image &imageToDrawOnto) : Graphics(imageToDrawOnto)
{
myComp = component;
}

A redefinition of mouseDown:
void My2DScreen::mouseDown(const MouseEvent& event)
{
Logger::outputDebugString(“OK”);
}

In PannerComponent:

In the constructor (the cached images have been placed using the Jucer):

[i]setName(“Test”);
cachedImage__51193634_jpg2 = ImageCache::getFromMemory (_51193634_jpg2, _51193634_jpg2Size);
cachedImage__51193634_jpg3 = ImageCache::getFromMemory (_51193634_jpg3, _51193634_jpg3Size);

addAndMakeVisible (g = new My2DScreen(this, cachedImage__51193634_jpg2));
addAndMakeVisible (g2 = new My2DScreen(this, cachedImage__51193634_jpg3));
g->setName (“Screen1”);
g2->setName (“Screen2”);
g->setAlwaysOnTop(true);
g2->setAlwaysOnTop(true);

g->drawImageAt(boule.rescaled(60,60), soundX-30, soundY-30);
g2->drawImageAt(boule.rescaled(60,60), soundX-30, soundZ-30);[/i]

(the images “boule” appear well inside the 2 screens)

In mouseDown():

Logger::outputDebugString(e.source.getComponentUnderMouse()->getName());

And then, everytime I click on the screens, the word “test” appear in the console, whereas it should be “screen1” or “screen2”

Thank you very much for your help


#6

wtf? You NEVER keep a graphics object hanging around - you create one temporarily, draw into it, and delete it!

wtf? You NEVER keep a graphics object hanging around - you create one temporarily, draw into it, and delete it!


#7

…not to mention the fact that you’re deriving from a Graphics??? Yikes!


#8

Is that where the problem comes from?? It was a way to draw images in it


#9

So what would be the best way to draw images inside the black screens (cached image) and be able to move them around?


#10

You need to back way up, and revisit a few basics here.

To create a drawable canvas you need to crete a component subclass and implement its paint method to draw whatever you need.

As a very rough outline:


class MyComp : public Component
{
  MyComp() 
  :
     Component(),
     text("Test")
  {
  }

  // ...

  virtual paint(Graphics& g)
  {
    // drawText() needs positioning, left out for brevity
    g.drawtext(text);
  }
  

  void setText(const String& newText)
  {
    text = newText;
    // maybe call repaint here to do a refresh
    repaint();
  }

private:
  String text;
}

If you want to cache the display, then consider storing an image in your component, and updating that. In which case your paint() override is going to simply draw the image.


#11

Got that, thanks!