Allowing mouse input to trigger key bindings while the cursor is on (TGEA).
by Dean · 06/13/2007 (10:26 am) · 8 comments
The problem is, when you've got the cursor on ( cursorOn(); ) all mouse input goes to the gui and isn't used to trigger the key bindings. In the source code, ActionMap::handleEvent(event) is used to trigger functions in your key mappings (default.bind.cs). But the way it's set up, if the cursor is on, the gui will take that input with Canvas->processInputEvent(event) and not allow handleEvent to use your mouse input. This code in main.cpp should give you the general idea:
...but all we're going to do is tell this section not to handle ANY mouse click events. We'll handle that somewhere else. we're not going to change the keyboard events because we don't want to be typing in a text box for example and having our character run forward every time we type a "w". So make it look like this (added code is in bold):
In gameInterface.cpp there's a section that looks like this (the places where I added code are in bold). I added tEvent so i can find out if the event is a mouse click or keyboard stroke. We only want handleevent to activate if it's a mouse click event:
When that line is put there, your mouse button bindings will work with the cursor on or off... But mouse movements are handled elsewhere. In order to make this work, a mousemoveevent needs to be converted into an input event so it can be sent with handleevent. I added new code in processMouseMoveEvent to keep things simple. You should see a call for processMouseMoveEvent in the above code. Go to the definition of processMouseMoveEvent in guiCanvas.cpp and turn it into this:
You'll also need to make sure to include actionMap.h at the top of guiCanvas.cpp and gameInterface.cpp. my include line looks like this: #include "sim/actionMap.h"
I didn't do anything with the z axis (mouse wheel), but if you need to, you should be able to figure it out from this example. Rebuild, and now your mouse will trigger your key mappings along with the gui when your cursor is on.
Keep in mind that your cursor will now activate your mappings while in gui mode, so for example if your game moves your player when you click on the ground, your character will still move to the area underneath a gui menu if you click on the menu. There's ways around that problem, but now you're forewarned.
Like I said, this is my first resource and I'm still learning torque so feedback is welcome :)
PROFILE_START(ProcessInputEvent);
if (!ActionMap::handleEventGlobal(event))
{
// Other input consumers here...
if (!(Canvas && Canvas->processInputEvent(event)))
ActionMap::handleEvent(event);
}
PROFILE_END();...but all we're going to do is tell this section not to handle ANY mouse click events. We'll handle that somewhere else. we're not going to change the keyboard events because we don't want to be typing in a text box for example and having our character run forward every time we type a "w". So make it look like this (added code is in bold):
PROFILE_START(ProcessInputEvent);
if (!ActionMap::handleEventGlobal(event))
{
//
// Other input consumers here...
if (!(Canvas && Canvas->processInputEvent(event)))
[b]if (event->deviceType != MouseDeviceType)//if it's NOT a mouse click event[/b]
ActionMap::handleEvent(event);
}
PROFILE_END();In gameInterface.cpp there's a section that looks like this (the places where I added code are in bold). I added tEvent so i can find out if the event is a mouse click or keyboard stroke. We only want handleevent to activate if it's a mouse click event:
[b]InputEvent *tEvent = (InputEvent *)event; [/b]
switch(event->type)
{
case PacketReceiveEventType:
processPacketReceiveEvent((PacketReceiveEvent *) event);
break;
case MouseMoveEventType:
processMouseMoveEvent((MouseMoveEvent *) event);
break;
case InputEventType:
[b] if (tEvent->deviceType == MouseDeviceType)//if its a mouse click event
ActionMap::handleEvent((InputEvent *) event);//activate keymap[/b]
processInputEvent((InputEvent *) event);
break;
case QuitEventType:
processQuitEvent();
break;
case TimeEventType:
processTimeEvent((TimeEvent *) event);
break;
case ConsoleEventType:
processConsoleEvent((ConsoleEvent *) event);
break;
case ConnectedAcceptEventType:
processConnectedAcceptEvent( (ConnectedAcceptEvent *) event );
break;
case ConnectedReceiveEventType:
processConnectedReceiveEvent( (ConnectedReceiveEvent *) event );
break;
case ConnectedNotifyEventType:
processConnectedNotifyEvent( (ConnectedNotifyEvent *) event );
break;
}
.... etcWhen that line is put there, your mouse button bindings will work with the cursor on or off... But mouse movements are handled elsewhere. In order to make this work, a mousemoveevent needs to be converted into an input event so it can be sent with handleevent. I added new code in processMouseMoveEvent to keep things simple. You should see a call for processMouseMoveEvent in the above code. Go to the definition of processMouseMoveEvent in guiCanvas.cpp and turn it into this:
void GuiCanvas::processMouseMoveEvent(const MouseMoveEvent *event)
{
InputEvent iEvent;
iEvent.deviceType = MouseDeviceType;
iEvent.modifier = event->modifier;
iEvent.action = SI_MOVE;
iEvent.deviceInst = 0;
iEvent.objType = SI_XAXIS;
iEvent.fValue = event->xPos - cursorPt.x;
processInputEvent(&iEvent);
ActionMap::handleEvent(&iEvent);
iEvent.objType = SI_YAXIS;
iEvent.fValue = event->yPos - cursorPt.y;
processInputEvent(&iEvent);
ActionMap::handleEvent(&iEvent);
}You'll also need to make sure to include actionMap.h at the top of guiCanvas.cpp and gameInterface.cpp. my include line looks like this: #include "sim/actionMap.h"
I didn't do anything with the z axis (mouse wheel), but if you need to, you should be able to figure it out from this example. Rebuild, and now your mouse will trigger your key mappings along with the gui when your cursor is on.
Keep in mind that your cursor will now activate your mappings while in gui mode, so for example if your game moves your player when you click on the ground, your character will still move to the area underneath a gui menu if you click on the menu. There's ways around that problem, but now you're forewarned.
Like I said, this is my first resource and I'm still learning torque so feedback is welcome :)
#2
p.s. We are using TGE and it worked
02/20/2008 (1:59 pm)
This has enabled us to be able to use the mouse clicks how we want them. Before, after turning on the cursor, it wouldn't turn off. This is great! :) Thank you.p.s. We are using TGE and it worked
#3
Just a comment: I think you forgot to mention to add
now is time to test it throughly.
Thx
Juan
03/31/2008 (3:08 pm)
Great resource, at work I use TGE 1.5 and it compiled with no errors.Just a comment: I think you forgot to mention to add
#include "sim/actionMap.h"at the top of of the file gameInterface.cpp as well.
now is time to test it throughly.
Thx
Juan
#4
or in any other file at that. Do you know if this was changed in 1.7?
Im trying to do something like this so when the user right clicks the cursor goes away and they can rotate there character.
EDIT*
Sorry that didnt format right but its your top code box.
05/06/2008 (3:38 pm)
Idk if im missing something but I dont have a main.cpp that has any of PROFILE_START(ProcessInputEvent); if (!ActionMap::handleEventGlobal(event)) { // Other input consumers here... if (!(Canvas && Canvas->processInputEvent(event))) ActionMap::handleEvent(event); } PROFILE_END();or in any other file at that. Do you know if this was changed in 1.7?
Im trying to do something like this so when the user right clicks the cursor goes away and they can rotate there character.
EDIT*
Sorry that didnt format right but its your top code box.
#5
08/23/2008 (5:28 am)
Those with AFX can check out the afxTSCtrl.h/cpp to do this..all you have to do is intercept the correct events and make sure you set the consumeLastEvent to false.
#6
09/09/2008 (8:40 am)
Can't find any of these files in TGEA 1.7.1 =[
#7
10/03/2008 (3:13 pm)
Thank you this helped out a lot!!!! Exactly what I was looking for
#8
I also can't find any of these code in TGEA 1.7.1.
01/05/2009 (11:32 pm)
Have anyone ever managed to get it work with TGEA 1.7.1?I also can't find any of these code in TGEA 1.7.1.
Torque Owner Scott Doerrfeld