MouseListener Problem


#1

Hi,

I have an OpenGLComponent. I put it here in simple form. It seems silly since I simplified it. consider I want to stop the computation when the mouse moves over the component. when the window is initialized, there is no problem with it and it enters mouseEnter() correctly but when I run the Computaion() the program never enters mouseEnter() by moving the mouse over the component and it never stops.

class A : public OpenGLComponent 
{
      bool StopConditionMet;
      //...
      void mouseEnter  (  const MouseEvent &  e   ) 
      {
            DBG(JUCE_T("TEST"));
            StopConditionMet = true;
      }
      //...
      void Computation () 
      {
             StopConditionMet = false;
             while (!StopConditionMet) 
             {  
                    // ...
                    // heavy computation
                    // ...
             }
      }
}

I have the same problem with grab keyboard focus and keyPress()
can anyone help me please ?

:: Nikola Williams


#2

is it just mouse and keyboard stuff that’s not getting called, or is everything else also on hold? when is ‘computation’ getting called? (it’s not a virtual function so i guess you’re invoking it yourself somewhere).

It looks to me like you’re starting it from the main message thread - which would mean that it would be trapped inside the loop until it somehow had the chance to also send mouse event. What you’re doing (looping computation which can be interrupted by some external flag) is the sort of thing that should go in the ‘run()’ body of a Thread subclass.

This is just off the top of my head here, but this could be one answer…

class A : public OpenGLComponent, public Thread
{
      void mouseEnter  (  const MouseEvent &  e   )
      {
            DBG(JUCE_T("TEST"));
            signalThreadShouldExit ();
      }
      //...
      void run ()
      {
             while (!threadShouldExit ())
             { 
                    // ...
                    // heavy computation
                    // ...
             }
      }
} 

the computation can be started with startThread(). This means that your loop can continue whilst the message thread is still able to do its thing. I’m not sure how tricky OpenGL gets with multi-threading, but it’s safe to say that you should always be careful when using threads.

Also, I’m not sure how many of these components you may have, it may not be a wise move to derive your OpenGLComponent from Thread too; you might want to make a shared Thread for driving more than one [if so, you could just have the ‘Computation’ function do a single cycle, and have the thread call it (for each registered object) each time round its loop.

Sorry if i’m telling you stuff you already know (quite likely), but heck, more information just in case shouldn’t do any harm!


#3

well , I tried the above code. but it seems that OpenGLComponent and Thread / ThreadWithProgressWindow have conflict with each other. when I compile the program, I get a compilation error like :

error C2385: ambiguous access of 'new’
1> could be the ‘new’ in base 'juce::OpenGLComponent’
1> or could be the ‘new’ in base ‘juce::Thread’

over line :

myA = new A;

am I miss something here ? or there is something wrong with class definition which is inherited from these two classes ?


#4

i don’t think it’s the best idea to make a Component also be a thread - i imagine it could be a little risky. I wrote that code mainly as a basic example of what the class was actually doing. It’s probably safer to make an external thread and give it a way of registering your OpenGLComponent subclasses so they can have their ‘Computation’ function called. But, for ease, and the sake of testing, i think it should be okay.

If you wanted to try it though, the actual problem you’re facing is probably a simple one - try adding

to the public section of your class declaration.


#5

Dude !! you are a genius :smiley: Thanks. It worked

:: Nikola Williams


#6