mirror of
https://github.com/panda3d/panda3d.git
synced 2025-09-30 08:44:19 -04:00
Implement an M_confined mouse mode to ensure mouse stays in window.
This commit is contained in:
parent
500da29013
commit
48cc1aa496
@ -736,25 +736,11 @@ set_properties_now(WindowProperties &properties) {
|
|||||||
// Fullscreen property specified, but unchanged.
|
// Fullscreen property specified, but unchanged.
|
||||||
properties.clear_fullscreen();
|
properties.clear_fullscreen();
|
||||||
}
|
}
|
||||||
if (properties.has_mouse_mode() ) {
|
if (properties.has_mouse_mode() &&
|
||||||
|
properties.get_mouse_mode() == _properties.get_mouse_mode()) {
|
||||||
if (properties.get_mouse_mode() == _properties.get_mouse_mode()) {
|
// Mouse mode specified, but unchanged.
|
||||||
properties.clear_mouse_mode();
|
properties.clear_mouse_mode();
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
if(properties.get_mouse_mode() == WindowProperties::M_absolute) {
|
|
||||||
_properties.set_mouse_mode(WindowProperties::M_absolute);
|
|
||||||
mouse_mode_absolute();
|
|
||||||
properties.clear_mouse_mode();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
_properties.set_mouse_mode(WindowProperties::M_relative);
|
|
||||||
mouse_mode_relative();
|
|
||||||
properties.clear_mouse_mode();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
|
@ -798,19 +798,30 @@ clear_z_order() {
|
|||||||
// Function: WindowProperties::set_mouse_mode
|
// Function: WindowProperties::set_mouse_mode
|
||||||
// Access: Published
|
// Access: Published
|
||||||
// Description: Specifies the mode in which the window is to operate
|
// Description: Specifies the mode in which the window is to operate
|
||||||
// its mouse pointer. The default is M_absolute, which
|
// its mouse pointer.
|
||||||
// is the normal mode in which a mouse pointer operates;
|
//
|
||||||
// but you can also set M_relative, which is
|
// M_absolute: the normal mode in which a mouse pointer
|
||||||
// particularly useful for FPS-style mouse movements
|
// operates, where the mouse can move outside the window
|
||||||
// where you have hidden the mouse pointer and are are
|
// and the mouse coordinates are relative to its
|
||||||
// more interested in how fast the mouse is moving,
|
// position in the window.
|
||||||
// rather than precisely where the pointer is hovering.
|
//
|
||||||
|
// M_relative (OSX or Unix/X11 only): a mode where only
|
||||||
|
// relative movements are reported; particularly useful
|
||||||
|
// for FPS-style mouse movements where you have hidden
|
||||||
|
// the mouse pointer and are are more interested in how
|
||||||
|
// fast the mouse is moving, rather than precisely where
|
||||||
|
// the pointer is hovering.
|
||||||
|
//
|
||||||
|
// This has no effect on Windows. On Unix/X11, this
|
||||||
|
// requires the Xxf86dga extension to be available.
|
||||||
|
//
|
||||||
|
// M_confined: this mode reports absolute mouse
|
||||||
|
// positions, but confines the mouse pointer to
|
||||||
|
// the window boundary. It can portably replace
|
||||||
|
// M_relative for an FPS, but you need to periodically
|
||||||
|
// move the pointer to the center of the window
|
||||||
|
// and track movement deltas.
|
||||||
//
|
//
|
||||||
// This has no effect on Windows, which does not
|
|
||||||
// have this concept; but is important to do on OSX
|
|
||||||
// and Unix/X11 to properly enable a smooth FPS-style
|
|
||||||
// mouselook mode. On Unix/X11, this requires the
|
|
||||||
// Xxf86dga extension to be available.
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
INLINE void WindowProperties::
|
INLINE void WindowProperties::
|
||||||
set_mouse_mode(MouseMode mode) {
|
set_mouse_mode(MouseMode mode) {
|
||||||
|
@ -400,6 +400,8 @@ operator << (ostream &out, WindowProperties::MouseMode mode) {
|
|||||||
return out << "absolute";
|
return out << "absolute";
|
||||||
case WindowProperties::M_relative:
|
case WindowProperties::M_relative:
|
||||||
return out << "relative";
|
return out << "relative";
|
||||||
|
case WindowProperties::M_confined:
|
||||||
|
return out << "confined";
|
||||||
}
|
}
|
||||||
return out << "**invalid WindowProperties::MouseMode(" << (int)mode << ")**";
|
return out << "**invalid WindowProperties::MouseMode(" << (int)mode << ")**";
|
||||||
}
|
}
|
||||||
@ -413,6 +415,8 @@ operator >> (istream &in, WindowProperties::MouseMode &mode) {
|
|||||||
mode = WindowProperties::M_absolute;
|
mode = WindowProperties::M_absolute;
|
||||||
} else if (word == "relative") {
|
} else if (word == "relative") {
|
||||||
mode = WindowProperties::M_relative;
|
mode = WindowProperties::M_relative;
|
||||||
|
} else if (word == "confined") {
|
||||||
|
mode = WindowProperties::M_confined;
|
||||||
} else {
|
} else {
|
||||||
display_cat.warning()
|
display_cat.warning()
|
||||||
<< "Unknown mouse mode: " << word << "\n";
|
<< "Unknown mouse mode: " << word << "\n";
|
||||||
|
@ -40,6 +40,7 @@ PUBLISHED:
|
|||||||
enum MouseMode {
|
enum MouseMode {
|
||||||
M_absolute,
|
M_absolute,
|
||||||
M_relative,
|
M_relative,
|
||||||
|
M_confined,
|
||||||
};
|
};
|
||||||
|
|
||||||
WindowProperties();
|
WindowProperties();
|
||||||
|
@ -35,6 +35,8 @@ WinGraphicsWindow *WinGraphicsWindow::_creating_window = NULL;
|
|||||||
WinGraphicsWindow *WinGraphicsWindow::_cursor_window = NULL;
|
WinGraphicsWindow *WinGraphicsWindow::_cursor_window = NULL;
|
||||||
bool WinGraphicsWindow::_cursor_hidden = false;
|
bool WinGraphicsWindow::_cursor_hidden = false;
|
||||||
|
|
||||||
|
RECT WinGraphicsWindow::_mouse_unconfined_cliprect;
|
||||||
|
|
||||||
// These are used to save the previous state of the fancy Win2000
|
// These are used to save the previous state of the fancy Win2000
|
||||||
// effects that interfere with rendering when the mouse wanders into a
|
// effects that interfere with rendering when the mouse wanders into a
|
||||||
// window's client area.
|
// window's client area.
|
||||||
@ -359,6 +361,50 @@ set_properties_now(WindowProperties &properties) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (properties.has_mouse_mode()) {
|
||||||
|
if (properties.get_mouse_mode() != _properties.get_mouse_mode()) {
|
||||||
|
switch (properties.get_mouse_mode()) {
|
||||||
|
case WindowProperties::M_absolute:
|
||||||
|
case WindowProperties::M_relative: // not implemented, treat as absolute
|
||||||
|
|
||||||
|
if (_properties.get_mouse_mode() == WindowProperties::M_confined) {
|
||||||
|
ClipCursor(NULL);
|
||||||
|
windisplay_cat.info() << "Unconfining cursor from window\n";
|
||||||
|
}
|
||||||
|
_properties.set_mouse_mode(WindowProperties::M_absolute);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WindowProperties::M_confined:
|
||||||
|
{
|
||||||
|
RECT clip;
|
||||||
|
|
||||||
|
if (!GetWindowRect(_hWnd, &clip)) {
|
||||||
|
if (windisplay_cat.is_debug()) {
|
||||||
|
windisplay_cat.debug()
|
||||||
|
<< "GetWindowRect() failed in set_properties_now. Cannot confine cursor.\n";
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
windisplay_cat.debug()
|
||||||
|
<< "ClipCursor() to " << clip.left << "," << clip.top << " to "
|
||||||
|
<< clip.right << "," << clip.bottom << endl;
|
||||||
|
|
||||||
|
GetClipCursor(&_mouse_unconfined_cliprect);
|
||||||
|
if (!ClipCursor(&clip)) {
|
||||||
|
windisplay_cat.debug()
|
||||||
|
<< "ClipCursor() failed in set_properties_now. Ignoring.\n";
|
||||||
|
} else {
|
||||||
|
_properties.set_mouse_mode(WindowProperties::M_confined);
|
||||||
|
windisplay_cat.info() << "Confining cursor to window\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
properties.clear_mouse_mode();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
|
@ -214,6 +214,9 @@ private:
|
|||||||
static BOOL _saved_cursor_shadow;
|
static BOOL _saved_cursor_shadow;
|
||||||
static BOOL _saved_mouse_vanish;
|
static BOOL _saved_mouse_vanish;
|
||||||
|
|
||||||
|
// The mouse constraints before applying mouse mode M_confined.
|
||||||
|
static RECT _mouse_unconfined_cliprect;
|
||||||
|
|
||||||
// Since the Panda API requests icons and cursors by filename, we
|
// Since the Panda API requests icons and cursors by filename, we
|
||||||
// need a table mapping filenames to handles, so we can avoid
|
// need a table mapping filenames to handles, so we can avoid
|
||||||
// re-reading the file each time we change icons.
|
// re-reading the file each time we change icons.
|
||||||
|
@ -711,36 +711,24 @@ set_properties_now(WindowProperties &properties) {
|
|||||||
properties.clear_foreground();
|
properties.clear_foreground();
|
||||||
}
|
}
|
||||||
|
|
||||||
set_wm_properties(wm_properties, true);
|
if (properties.has_mouse_mode()) {
|
||||||
}
|
switch (properties.get_mouse_mode()) {
|
||||||
|
case WindowProperties::M_absolute:
|
||||||
////////////////////////////////////////////////////////////////////
|
|
||||||
// Function: x11GraphicsWindow::mouse_mode_absolute
|
|
||||||
// Access: Private, Virtual
|
|
||||||
// Description: Overridden from GraphicsWindow.
|
|
||||||
////////////////////////////////////////////////////////////////////
|
|
||||||
void x11GraphicsWindow::
|
|
||||||
mouse_mode_absolute() {
|
|
||||||
#ifdef HAVE_XF86DGA
|
|
||||||
if (!_dga_mouse_enabled) return;
|
|
||||||
|
|
||||||
XUngrabPointer(_display, CurrentTime);
|
XUngrabPointer(_display, CurrentTime);
|
||||||
|
#ifdef HAVE_XF86DGA
|
||||||
|
if (_dga_mouse_enabled) {
|
||||||
x11display_cat.info() << "Disabling relative mouse using XF86DGA extension\n";
|
x11display_cat.info() << "Disabling relative mouse using XF86DGA extension\n";
|
||||||
XF86DGADirectVideo(_display, _screen, 0);
|
XF86DGADirectVideo(_display, _screen, 0);
|
||||||
_dga_mouse_enabled = false;
|
_dga_mouse_enabled = false;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
_properties.set_mouse_mode(WindowProperties::M_absolute);
|
||||||
|
properties.clear_mouse_mode();
|
||||||
|
break;
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
case WindowProperties::M_relative:
|
||||||
// Function: x11GraphicsWindow::mouse_mode_relative
|
|
||||||
// Access: Private, Virtual
|
|
||||||
// Description: Overridden from GraphicsWindow.
|
|
||||||
////////////////////////////////////////////////////////////////////
|
|
||||||
void x11GraphicsWindow::
|
|
||||||
mouse_mode_relative() {
|
|
||||||
#ifdef HAVE_XF86DGA
|
#ifdef HAVE_XF86DGA
|
||||||
if (_dga_mouse_enabled) return;
|
if (!_dga_mouse_enabled) {
|
||||||
|
|
||||||
int major_ver, minor_ver;
|
int major_ver, minor_ver;
|
||||||
if (XF86DGAQueryVersion(_display, &major_ver, &minor_ver)) {
|
if (XF86DGAQueryVersion(_display, &major_ver, &minor_ver)) {
|
||||||
|
|
||||||
@ -754,17 +742,12 @@ mouse_mode_relative() {
|
|||||||
if (XGrabPointer(_display, _xwindow, True, 0, GrabModeAsync,
|
if (XGrabPointer(_display, _xwindow, True, 0, GrabModeAsync,
|
||||||
GrabModeAsync, _xwindow, cursor, CurrentTime) != GrabSuccess) {
|
GrabModeAsync, _xwindow, cursor, CurrentTime) != GrabSuccess) {
|
||||||
x11display_cat.error() << "Failed to grab pointer!\n";
|
x11display_cat.error() << "Failed to grab pointer!\n";
|
||||||
return;
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
x11display_cat.info() << "Enabling relative mouse using XF86DGA extension\n";
|
x11display_cat.info() << "Enabling relative mouse using XF86DGA extension\n";
|
||||||
XF86DGADirectVideo(_display, _screen, XF86DGADirectMouse);
|
XF86DGADirectVideo(_display, _screen, XF86DGADirectMouse);
|
||||||
|
|
||||||
|
_properties.set_mouse_mode(WindowProperties::M_relative);
|
||||||
_dga_mouse_enabled = true;
|
_dga_mouse_enabled = true;
|
||||||
} else {
|
|
||||||
x11display_cat.info() << "XF86DGA extension not available\n";
|
|
||||||
_dga_mouse_enabled = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the real mouse position, so we can add/subtract
|
// Get the real mouse position, so we can add/subtract
|
||||||
// our relative coordinates later.
|
// our relative coordinates later.
|
||||||
@ -773,7 +756,68 @@ mouse_mode_relative() {
|
|||||||
&event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root,
|
&event.xbutton.window, &event.xbutton.x_root, &event.xbutton.y_root,
|
||||||
&event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
|
&event.xbutton.x, &event.xbutton.y, &event.xbutton.state);
|
||||||
_input_devices[0].set_pointer_in_window(event.xbutton.x, event.xbutton.y);
|
_input_devices[0].set_pointer_in_window(event.xbutton.x, event.xbutton.y);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
x11display_cat.info() << "XF86DGA extension not available\n";
|
||||||
|
_dga_mouse_enabled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
#endif
|
#endif
|
||||||
|
{
|
||||||
|
// can't change
|
||||||
|
properties.clear_mouse_mode();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case WindowProperties::M_confined:
|
||||||
|
{
|
||||||
|
#ifdef HAVE_XF86DGA
|
||||||
|
if (_dga_mouse_enabled) {
|
||||||
|
XF86DGADirectVideo(_display, _screen, 0);
|
||||||
|
_dga_mouse_enabled = false;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
X11_Cursor cursor = None;
|
||||||
|
if (_properties.get_cursor_hidden()) {
|
||||||
|
x11GraphicsPipe *x11_pipe;
|
||||||
|
DCAST_INTO_V(x11_pipe, _pipe);
|
||||||
|
cursor = x11_pipe->get_hidden_cursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (XGrabPointer(_display, _xwindow, True, 0, GrabModeAsync,
|
||||||
|
GrabModeAsync, _xwindow, cursor, CurrentTime) != GrabSuccess) {
|
||||||
|
x11display_cat.error() << "Failed to grab pointer!\n";
|
||||||
|
} else {
|
||||||
|
_properties.set_mouse_mode(WindowProperties::M_confined);
|
||||||
|
properties.clear_mouse_mode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
set_wm_properties(wm_properties, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// Function: x11GraphicsWindow::mouse_mode_absolute
|
||||||
|
// Access: Private, Virtual
|
||||||
|
// Description: Overridden from GraphicsWindow.
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
void x11GraphicsWindow::
|
||||||
|
mouse_mode_absolute() {
|
||||||
|
// unused: remove in 1.10!
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
// Function: x11GraphicsWindow::mouse_mode_relative
|
||||||
|
// Access: Private, Virtual
|
||||||
|
// Description: Overridden from GraphicsWindow.
|
||||||
|
////////////////////////////////////////////////////////////////////
|
||||||
|
void x11GraphicsWindow::
|
||||||
|
mouse_mode_relative() {
|
||||||
|
// unused: remove in 1.10!
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
x
Reference in New Issue
Block a user