Add events when mouse enters/leaves canvas on emscripten. Handle that in fps-camera.

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/branches/ogl-es@5457 dfc29bdd-3216-0410-991c-e03cc46cb475
master
cutealien 2017-04-26 16:05:03 +00:00
parent d4a0f30d82
commit 92f42e41bf
6 changed files with 87 additions and 19 deletions

View File

@ -78,7 +78,7 @@ namespace irr
//! Pass on raw events from the OS
EET_SYSTEM_EVENT,
//! Application state events like a resume, pause etc.
EET_APPLICATION_EVENT,
@ -140,6 +140,14 @@ namespace irr
//! This event is generated after the third EMIE_MMOUSE_PRESSED_DOWN event.
EMIE_MMOUSE_TRIPLE_CLICK,
//! Mouse enters canvas used for rendering.
//! Only generated on emscripten
EMIE_MOUSE_ENTER_CANVAS,
//! Mouse leaves canvas used for rendering.
//! Only generated on emscripten
EMIE_MOUSE_LEAVE_CANVAS,
//! No real event. Just for convenience to get number of events
EMIE_COUNT
};
@ -186,25 +194,25 @@ namespace irr
//! No real event, but to get number of event types
ESET_COUNT
};
//! Enumeration for a commonly used application state events (it's useful mainly for mobile devices)
enum EAPPLICATION_EVENT_TYPE
{
//! The application will be resumed.
EAET_WILL_RESUME = 0,
//! The application has been resumed.
EAET_DID_RESUME,
//! The application will be paused.
EAET_WILL_PAUSE,
//! The application has been paused.
EAET_DID_PAUSE,
//! The application will be terminated.
EAET_WILL_TERMINATE,
//! The application received a memory warning.
EAET_MEMORY_WARNING,
@ -560,7 +568,7 @@ struct SEvent
struct SAndroidCmd AndroidCmd;
};
};
// Application state event
struct SApplicationEvent
{

View File

@ -60,13 +60,55 @@ namespace irr
namespace irr
{
#ifdef _IRR_EMSCRIPTEN_PLATFORM_
EM_BOOL MouseUpDownCallback(int eventType, const EmscriptenMouseEvent * event, void* userData)
EM_BOOL CIrrDeviceSDL::MouseUpDownCallback(int eventType, const EmscriptenMouseEvent * event, void* userData)
{
// We need this callback so far only because otherwise "emscripten_request_pointerlock" calls will
// fail as their request are infinitely deferred.
// Not exactly certain why, maybe SDL does catch those mouse-events otherwise and not pass them on.
return EM_FALSE;
}
EM_BOOL CIrrDeviceSDL::MouseEnterCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
{
CIrrDeviceSDL * This = static_cast<CIrrDeviceSDL*>(userData);
SEvent irrevent;
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.Event = irr::EMIE_MOUSE_ENTER_CANVAS;
This->MouseX = irrevent.MouseInput.X = mouseEvent->canvasX;
This->MouseY = irrevent.MouseInput.Y = mouseEvent->canvasY;
This->MouseXRel = mouseEvent->movementX; // should be 0 I guess? Or can it enter while pointer is locked()?
This->MouseYRel = mouseEvent->movementY;
irrevent.MouseInput.ButtonStates = This->MouseButtonStates; // TODO: not correct, but couldn't figure out the bitset of mouseEvent->buttons yet.
irrevent.MouseInput.Shift = mouseEvent->shiftKey;
irrevent.MouseInput.Control = mouseEvent->ctrlKey;
This->postEventFromUser(irrevent);
return EM_FALSE;
}
EM_BOOL CIrrDeviceSDL::MouseLeaveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData)
{
CIrrDeviceSDL * This = static_cast<CIrrDeviceSDL*>(userData);
SEvent irrevent;
irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
irrevent.MouseInput.Event = irr::EMIE_MOUSE_LEAVE_CANVAS;
This->MouseX = irrevent.MouseInput.X = mouseEvent->canvasX;
This->MouseY = irrevent.MouseInput.Y = mouseEvent->canvasY;
This->MouseXRel = mouseEvent->movementX; // should be 0 I guess? Or can it enter while pointer is locked()?
This->MouseYRel = mouseEvent->movementY;
irrevent.MouseInput.ButtonStates = This->MouseButtonStates; // TODO: not correct, but couldn't figure out the bitset of mouseEvent->buttons yet.
irrevent.MouseInput.Shift = mouseEvent->shiftKey;
irrevent.MouseInput.Control = mouseEvent->ctrlKey;
This->postEventFromUser(irrevent);
return EM_FALSE;
}
#endif
//! constructor
@ -171,8 +213,12 @@ bool CIrrDeviceSDL::createWindow()
emscripten_set_canvas_size( Width, Height);
Screen = SDL_SetVideoMode( 0, 0, 32, SDL_OPENGL ); // 0,0 will use the canvas size
emscripten_set_mousedown_callback(0, 0, true, MouseUpDownCallback);
emscripten_set_mouseup_callback(0, 0, true, MouseUpDownCallback);
// "#canvas" is for the opengl context
emscripten_set_mousedown_callback("#canvas", (void*)this, true, MouseUpDownCallback);
emscripten_set_mouseup_callback("#canvas", (void*)this, true, MouseUpDownCallback);
emscripten_set_mouseenter_callback("#canvas", (void*)this, false, MouseEnterCallback);
emscripten_set_mouseleave_callback("#canvas", (void*)this, false, MouseLeaveCallback);
return true;
#else // !_IRR_EMSCRIPTEN_PLATFORM_
if ( Close )
@ -386,10 +432,12 @@ bool CIrrDeviceSDL::run()
if ( CursorControl->isVisible() && pointerlockStatus.isActive )
{
emscripten_exit_pointerlock();
return !Close;
}
else if ( !CursorControl->isVisible() && !pointerlockStatus.isActive )
{
emscripten_request_pointerlock(0, true);
return !Close;
}
}
}

View File

@ -217,6 +217,12 @@ namespace irr
private:
#ifdef _IRR_EMSCRIPTEN_PLATFORM_
static EM_BOOL MouseUpDownCallback(int eventType, const EmscriptenMouseEvent * event, void* userData);
static EM_BOOL MouseEnterCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
static EM_BOOL MouseLeaveCallback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData);
#endif
//! create the driver
void createDriver();

View File

@ -91,6 +91,11 @@ bool CSceneNodeAnimatorCameraFPS::OnEvent(const SEvent& evt)
CursorPos = CursorControl->getRelativePosition();
return true;
}
if ( evt.MouseInput.Event == EMIE_MOUSE_ENTER_CANVAS)
{
resetCursorPos();
return false;
}
break;
default:
@ -277,6 +282,13 @@ void CSceneNodeAnimatorCameraFPS::animateNode(ISceneNode* node, u32 timeMs)
camera->setTarget(target);
}
void CSceneNodeAnimatorCameraFPS::resetCursorPos()
{
CursorControl->setPosition(0.5f, 0.5f);
CenterCursor = CursorControl->getRelativePosition();
CursorPos = CenterCursor;
}
void CSceneNodeAnimatorCameraFPS::allKeysUp()
{

View File

@ -109,8 +109,9 @@ namespace scene
//! Reads attributes of the scene node animator.
virtual void deserializeAttributes(io::IAttributes* in, io::SAttributeReadWriteOptions* options=0) _IRR_OVERRIDE_;
private:
void resetCursorPos();
void allKeysUp();
gui::ICursorControl *CursorControl;

View File

@ -85,14 +85,7 @@ bool CSceneNodeAnimatorCameraMaya::OnEvent(const SEvent& event)
MousePos = CursorControl->getRelativePosition();
}
break;
case EMIE_MOUSE_WHEEL:
case EMIE_LMOUSE_DOUBLE_CLICK:
case EMIE_RMOUSE_DOUBLE_CLICK:
case EMIE_MMOUSE_DOUBLE_CLICK:
case EMIE_LMOUSE_TRIPLE_CLICK:
case EMIE_RMOUSE_TRIPLE_CLICK:
case EMIE_MMOUSE_TRIPLE_CLICK:
case EMIE_COUNT:
default:
return false;
}
return true;