MouseListener misunderstanding


#1

Hi, 

here what i'm tryng to do : 

MainContentComponent::MainContentComponent()

{

    c1 = new myChild();
    c2 = new myChild();
    addAndMakeVisible(c1);
    addAndMakeVisible(c2);
    addMouseListener(this, true);
    ...
}

void MainContentComponent::mouseDown(const MouseEvent &event)

{

    if (event.eventComponent == c1){
     //do something on c1, e.g. c1.setAlpha(1.0);
     //do something esle on c2, e.g. c2.setAlpha(0.75);

    }

else if (eventComponent == c2){
     //do something on c2, e.g. c2.setAlpha(1.0);
     //do something else on c1, e.g. c1.setAlpha(0.75);

    }

My problem is that this won't work if I click on c1 or c2 children components...although it's ok if I click on c1 or c2 directly.

I'm pretty sure i'm missing some crucial basic point....


#2

It should work as far as I can see from that snippet. Maybe something elsewhere in your code that's stopping it?


#3

Hi,

i'm really testing on the simplest code ever..in my child component I have a simple TextButton and nothing else .

I just created a basic GUI Application, implemented a MouseListener in MainContentComponent as shown before, added myChild component (which inherits from Component) and added a TextButton in myChild ... 

Am I supposed to do anything more ? I feel like..really stupid..:)


#4

Can't see anything stupid in what you posted, but hard to see the whole picture without a complete example of what you're doing.


#5
#include "../JuceLibraryCode/JuceHeader.h"

#include "child.h"


//==============================================================================

/*

    This component lives inside our window, and this is where you should put all

    your controls and content.

*/

class MainContentComponent   : public Component

{

public:

    //==============================================================================

    MainContentComponent();

    ~MainContentComponent();


    void paint (Graphics&);

    void resized();

    

    void mouseDown(const MouseEvent &event);

    

private:

    child *c1;

    child *c2;

    //==============================================================================

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MainContentComponent)

};

 

 


#include "MainComponent.h"

//==============================================================================

MainContentComponent::MainContentComponent()

{

    c1 = new child();

    c2 = new child();

    c1->setName("c1");

    c2->setName("c2");

    addAndMakeVisible(c1);

    addAndMakeVisible(c2);

   

    addMouseListener(this, true);

    setSize (600, 400);

}

MainContentComponent::~MainContentComponent()

{

    delete c1;

    delete c2;

    c1 = nullptr;

    c2 = nullptr;

   

    removeMouseListener(c1);

    removeMouseListener(c2);

    removeAllChildren();

}

void MainContentComponent::paint (Graphics& g)

{

    g.fillAll (Colour (0xff001F36));

    g.setFont (Font (16.0f));

    g.setColour (Colours::white);

    g.drawText ("Hello World!", getLocalBounds(), Justification::centred, true);

}

void MainContentComponent::resized()

{

    c1->setBoundsRelative(0.0f, 0.0f, 0.25f, 0.5f);

    c2->setBoundsRelative(0.501f, 0.0f, 0.25f, 0.5f);

    // This is called when the MainContentComponent is resized.

    // If you add any child components, this is where you should

    // update their positions.

}

void MainContentComponent::mouseDown(const MouseEvent &event)

{

    if (event.eventComponent == c1){

        printf("c1\n");

        c1->setAlpha(1.0);

        c2->setAlpha(0.25);

    }

    else if (event.eventComponent == c2){

        printf("c2\n");

        c2->setAlpha(1.0);

        c1->setAlpha(0.25);

    }

}

 

#include "../JuceLibraryCode/JuceHeader.h"


class child : public Component

{

public:

    child();

    ~child(){};

    

    void paint (Graphics&);

    void resized();


private:

    TextButton t1;

};

 

#include "child.h"


child::child()


{

    addAndMakeVisible(&t1);

}


void child::paint(Graphics& g)

{

    g.fillAll(Colours::darkgrey);

}


void child::resized()

{

    t1.setBoundsRelative(0.2f, 0.2f, 0.5f, 0.5f);

}

 

Hope this clarifies abit. Thx a lot btw.


#6

Try originalComponent instead of eventComponent?


#7

and same behavior : when i click on TextButton nothing happens..


#8

.. doing something like :

 if (event.originalComponent == c1

        || event.eventComponent == c1->getChildComponent(0)){

        printf("c1\n");

        c1->setAlpha(1.0);

        c2->setAlpha(0.25);

    }

 

then i get control over children components of c1 from MainContentComponent :/