[SOLVED] Mouse Event problem

[EDIT] sorry just realised the original title was wrong so I changed it [/EDIT]

hello,

I am trying to setup a mouse class to deal with clicking of objects.

the problem I find is that the “MouseEvent” never fires… here is how I set everything up

Declarations in Header

	NodePath mCamera;
	PT(CollisionRay) mPickerRay;

	NodePath mPickerNodePath;

	PT(MouseWatcher) mMouseWatcher;
	PT(CollisionHandlerQueue) mCollisionHQ;
	CollisionTraverser* mpCollisionTraverser;

	NodePath mLastSelectedNode;

and implementation in constructor

	mCamera = mpWorldManager->GameWindow()->get_camera_group();  

	mPickerRay = new CollisionRay();
	PT(CollisionNode) pickerNode = new CollisionNode("MousePicker");
	pickerNode->add_solid(mPickerRay);
	pickerNode->set_from_collide_mask(GeomNode::get_default_collide_mask());
	mPickerNodePath = mCamera.attach_new_node(pickerNode);

	mMouseWatcher = (MouseWatcher*)mpWorldManager->GameWindow()->get_mouse().node();

	mCollisionHQ = new CollisionHandlerQueue();
	mpCollisionTraverser = new CollisionTraverser("CollisionTraverser");
	mpCollisionTraverser->add_collider(mPickerNodePath, mCollisionHQ);

	mpWorldManager->GetFramework()->define_key("mouse1", "click", MouseEvent, this);

and here the “event”

void MouseEvent(const Event* pTheEvent, void* pData)
{
	cMouse* pMouse = static_cast<cMouse*>(pData);
}

mpWorldManager contains valid pointers to the
PandaFramework and WindowFramework.

Can anyone see what could cause my event to not fire when I click on my Window ?

Chrys

also doing some further tests you can actually ignore the whole code above. What isn’t working is this.
The collider code has obviously nothing to do with it.

cMouse::cMouse(PandaFramework& framework, WindowFramework* pWindow, cWorldManager* pWorldManager) :
mpWorldManager(pWorldManager),
mFramework(framework),
mpWindow(pWindow)
{
	mFramework.define_key("mouse1", "click", MouseEvent, this);

}

void MouseEvent(const Event* pTheEvent, void* pData)
{
	cMouse* pMouse = static_cast<cMouse*>(pData);
}

the event just doesn’t get called.

I maybe should mention that I execute this code in a mixed c++ environment (managed & unmanaged code because I use .NET for my editor).

Maybe function pointers don’t work ? hmm I will see if I can get another test case running for this.

I’ve made a little test project where the above code fails in the simplest way.

If I stick a breakpoint into
void MouseEvent(const Event* pTheEvent, void* pData)

it never hits.

Anyone got a clue what’s going on here ?

you can download it here
http://www.mediafire.com/?kschpkv5fuh5kei

Well, it works for me when I bind an event to mouse1, though I haven’t tried to puzzle out your solution yet, so I’m not sure what you’re doing. Are you sure your cMouse constructor is being called?

David

hi David,

yes I am sure, if you got a moment , try download the source code I’ve posted (it’s a Vs2008 project).

It’s pretty simple test code.

I suspect it;s related to some managed c++ and function pointers.

The code compiles fine but just fails to call the MouseEvent function :-/

Are you sure you are: (a) compiling in Release mode, and (b) have removed NDEBUG from the defined symbols? When I load up the solution you posted, neither of these things has been done, but maybe that’s just because of the way the solution gets sent on the wire.

There’s a lot of other cruft in there from Microsoft’s default project. I find it’s much more reliable to start with an empty project than one of the default ones. Is this the first C++ Panda project you’ve generated, or have you built other projects successfully before? I ask because the correct way to set up the project initially isn’t always obvious, and if you get it wrong, it might compile successfully but then fail to work in mysterious ways (such as you describe).

David

I’ve built my own Debug version of panda (currently 1.7 still but moving over to 1.7.2 soonish).

yes I have built successfully other projects before. this is just a problem that I’ve come across today because I am trying to implement a “vertex” pincher/dragger. Hence the cMouse helper class.

Specifically it’s for my Editor project written in .NET with managed C++. It hooks into Panda3D to render my 3D world.

The test project I’ve sent you is a simple example of how Panda3D can be hooked into Windows Forms.
It should compile and run as it is.

Well, it doesn’t compile of course, because I don’t have my own Debug version of Panda built and ready. I can do that, but it’ll take a while.

Hi

I haven’t tried define_key yet, but I have been using EventHandler::add_hook, you could try that one too.

in your constructor do:

  EventHandler &eventHandler = mFramework.get_event_handler();

  eventHandler.add_hook("mouse1", MouseEvent, this);

and add function:

void MouseEvent(const Event *pTheEvent, void *pData) {
  cMouse* pMouse = static_cast<cMouse*>(pData);
}

this doesn’t work either, I’m guessing for the same problem… I googled a bit and it looks like function pointers are indeed not straight forward in managed c++.

here is what MSDN says :
http://msdn.microsoft.com/en-us/library/367eeye0.aspx

I’ll try and understand how to make this work. I had to do other workarounds before due to managed/unmanaged c++ problems.

I’ve resolved my problem by moving my cMouse class into the same library as the rest of the panda code.

to clarify my Vs2008 solution consists of 2 projects :

the Game Library (including Panda3D access ) compiled as a static library
and the Editor developed in .Net with managed C++ (compiled with \clr mixed managed unmanaged code).

If the cMouse class was left in the Editor project the function pointer would not work ( I tried to follow the MSDN guideline but I couldn’t get it to work) so I’ve tried again to put the cMouse implementation back into the GameLibrary project ( unmanaged C++ ) but this also wouldn’t work properly.

I couln’t figure out what caused this but by pure luck I found that if I called

window->enable_keyboard();

it all worked as intended. Not sure what this does or what it has to do with the mouse but it’s all good.

\o/