Jelajahi Sumber

Added support for extra mouse buttons.

unknown 7 tahun lalu
induk
melakukan
9cc41a59ac

+ 4 - 0
core/global_constants.cpp

@@ -378,6 +378,8 @@ void register_global_constants() {
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_LEFT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_LEFT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_RIGHT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_RIGHT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MIDDLE);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MIDDLE);
+	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_XBUTTON1);
+	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_XBUTTON2);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_WHEEL_UP);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_WHEEL_UP);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_WHEEL_DOWN);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_WHEEL_DOWN);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_WHEEL_LEFT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_WHEEL_LEFT);
@@ -385,6 +387,8 @@ void register_global_constants() {
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MASK_LEFT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MASK_LEFT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MASK_RIGHT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MASK_RIGHT);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MASK_MIDDLE);
 	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MASK_MIDDLE);
+	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MASK_XBUTTON1);
+	BIND_GLOBAL_ENUM_CONSTANT(BUTTON_MASK_XBUTTON2);
 
 
 	//joypads
 	//joypads
 	BIND_GLOBAL_ENUM_CONSTANT(JOY_BUTTON_0);
 	BIND_GLOBAL_ENUM_CONSTANT(JOY_BUTTON_0);

+ 12 - 0
core/os/input_event.cpp

@@ -509,6 +509,12 @@ String InputEventMouseButton::as_text() const {
 		case BUTTON_WHEEL_RIGHT:
 		case BUTTON_WHEEL_RIGHT:
 			button_index_string = "BUTTON_WHEEL_RIGHT";
 			button_index_string = "BUTTON_WHEEL_RIGHT";
 			break;
 			break;
+		case BUTTON_XBUTTON1:
+			button_index_string = "BUTTON_XBUTTON1";
+			break;
+		case BUTTON_XBUTTON2:
+			button_index_string = "BUTTON_XBUTTON2";
+			break;
 		default:
 		default:
 			button_index_string = itos(get_button_index());
 			button_index_string = itos(get_button_index());
 			break;
 			break;
@@ -601,6 +607,12 @@ String InputEventMouseMotion::as_text() const {
 		case BUTTON_MASK_RIGHT:
 		case BUTTON_MASK_RIGHT:
 			button_mask_string = "BUTTON_MASK_RIGHT";
 			button_mask_string = "BUTTON_MASK_RIGHT";
 			break;
 			break;
+		case BUTTON_MASK_XBUTTON1:
+			button_mask_string = "BUTTON_MASK_XBUTTON1";
+			break;
+		case BUTTON_MASK_XBUTTON2:
+			button_mask_string = "BUTTON_MASK_XBUTTON2";
+			break;
 		default:
 		default:
 			button_mask_string = itos(get_button_mask());
 			button_mask_string = itos(get_button_mask());
 			break;
 			break;

+ 4 - 1
core/os/input_event.h

@@ -53,10 +53,13 @@ enum ButtonList {
 	BUTTON_WHEEL_DOWN = 5,
 	BUTTON_WHEEL_DOWN = 5,
 	BUTTON_WHEEL_LEFT = 6,
 	BUTTON_WHEEL_LEFT = 6,
 	BUTTON_WHEEL_RIGHT = 7,
 	BUTTON_WHEEL_RIGHT = 7,
+	BUTTON_XBUTTON1 = 8,
+	BUTTON_XBUTTON2 = 9,
 	BUTTON_MASK_LEFT = (1 << (BUTTON_LEFT - 1)),
 	BUTTON_MASK_LEFT = (1 << (BUTTON_LEFT - 1)),
 	BUTTON_MASK_RIGHT = (1 << (BUTTON_RIGHT - 1)),
 	BUTTON_MASK_RIGHT = (1 << (BUTTON_RIGHT - 1)),
 	BUTTON_MASK_MIDDLE = (1 << (BUTTON_MIDDLE - 1)),
 	BUTTON_MASK_MIDDLE = (1 << (BUTTON_MIDDLE - 1)),
-
+	BUTTON_MASK_XBUTTON1 = (1 << (BUTTON_XBUTTON1 - 1)),
+	BUTTON_MASK_XBUTTON2 = (1 << (BUTTON_XBUTTON2 - 1))
 };
 };
 
 
 enum JoystickList {
 enum JoystickList {

+ 4 - 4
editor/project_settings_editor.cpp

@@ -452,10 +452,10 @@ void ProjectSettingsEditor::_add_item(int p_item, Ref<InputEvent> p_exiting_even
 			device_index->add_item(TTR("Middle Button"));
 			device_index->add_item(TTR("Middle Button"));
 			device_index->add_item(TTR("Wheel Up Button"));
 			device_index->add_item(TTR("Wheel Up Button"));
 			device_index->add_item(TTR("Wheel Down Button"));
 			device_index->add_item(TTR("Wheel Down Button"));
-			device_index->add_item(TTR("Button 6"));
-			device_index->add_item(TTR("Button 7"));
-			device_index->add_item(TTR("Button 8"));
-			device_index->add_item(TTR("Button 9"));
+			device_index->add_item(TTR("Wheel Left Button"));
+			device_index->add_item(TTR("Wheel Right Button"));
+			device_index->add_item(TTR("X Button 1"));
+			device_index->add_item(TTR("X Button 2"));
 			device_input->popup_centered_minsize(Size2(350, 95) * EDSCALE);
 			device_input->popup_centered_minsize(Size2(350, 95) * EDSCALE);
 
 
 			Ref<InputEventMouseButton> mb = p_exiting_event;
 			Ref<InputEventMouseButton> mb = p_exiting_event;

+ 4 - 1
platform/javascript/os_javascript.cpp

@@ -46,6 +46,8 @@
 #define DOM_BUTTON_LEFT 0
 #define DOM_BUTTON_LEFT 0
 #define DOM_BUTTON_MIDDLE 1
 #define DOM_BUTTON_MIDDLE 1
 #define DOM_BUTTON_RIGHT 2
 #define DOM_BUTTON_RIGHT 2
+#define DOM_BUTTON_XBUTTON1 3
+#define DOM_BUTTON_XBUTTON2 4
 
 
 template <typename T>
 template <typename T>
 static void dom2godot_mod(T emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event) {
 static void dom2godot_mod(T emscripten_event_ptr, Ref<InputEventWithModifiers> godot_event) {
@@ -156,11 +158,12 @@ static EM_BOOL _mousebutton_callback(int event_type, const EmscriptenMouseEvent
 	ev->set_position(Point2(mouse_event->canvasX, mouse_event->canvasY));
 	ev->set_position(Point2(mouse_event->canvasX, mouse_event->canvasY));
 	ev->set_global_position(ev->get_position());
 	ev->set_global_position(ev->get_position());
 	dom2godot_mod(mouse_event, ev);
 	dom2godot_mod(mouse_event, ev);
-
 	switch (mouse_event->button) {
 	switch (mouse_event->button) {
 		case DOM_BUTTON_LEFT: ev->set_button_index(BUTTON_LEFT); break;
 		case DOM_BUTTON_LEFT: ev->set_button_index(BUTTON_LEFT); break;
 		case DOM_BUTTON_MIDDLE: ev->set_button_index(BUTTON_MIDDLE); break;
 		case DOM_BUTTON_MIDDLE: ev->set_button_index(BUTTON_MIDDLE); break;
 		case DOM_BUTTON_RIGHT: ev->set_button_index(BUTTON_RIGHT); break;
 		case DOM_BUTTON_RIGHT: ev->set_button_index(BUTTON_RIGHT); break;
+		case DOM_BUTTON_XBUTTON1: ev->set_button_index(BUTTON_XBUTTON1); break;
+		case DOM_BUTTON_XBUTTON2: ev->set_button_index(BUTTON_XBUTTON2); break;
 		default: return false;
 		default: return false;
 	}
 	}
 
 

+ 22 - 6
platform/osx/os_osx.mm

@@ -625,10 +625,18 @@ static void _mouseDownEvent(NSEvent *event, int index, int mask, bool pressed) {
 
 
 - (void)otherMouseDown:(NSEvent *)event {
 - (void)otherMouseDown:(NSEvent *)event {
 
 
-	if ((int)[event buttonNumber] != 2)
-		return;
+	if ((int)[event buttonNumber] == 2) {
+		_mouseDownEvent(event, BUTTON_MIDDLE, BUTTON_MASK_MIDDLE, true);
+
+	} else if ((int)[event buttonNumber] == 3) {
+		_mouseDownEvent(event, BUTTON_XBUTTON1, BUTTON_MASK_XBUTTON1, true);
+
+	} else if ((int)[event buttonNumber] == 4) {
+		_mouseDownEvent(event, BUTTON_XBUTTON2, BUTTON_MASK_XBUTTON2, true);
 
 
-	_mouseDownEvent(event, BUTTON_MIDDLE, BUTTON_MASK_MIDDLE, true);
+	} else {
+		return;
+	}
 }
 }
 
 
 - (void)otherMouseDragged:(NSEvent *)event {
 - (void)otherMouseDragged:(NSEvent *)event {
@@ -637,10 +645,18 @@ static void _mouseDownEvent(NSEvent *event, int index, int mask, bool pressed) {
 
 
 - (void)otherMouseUp:(NSEvent *)event {
 - (void)otherMouseUp:(NSEvent *)event {
 
 
-	if ((int)[event buttonNumber] != 2)
-		return;
+	if ((int)[event buttonNumber] == 2) {
+		_mouseDownEvent(event, BUTTON_MIDDLE, BUTTON_MASK_MIDDLE, false);
+
+	} else if ((int)[event buttonNumber] == 3) {
+		_mouseDownEvent(event, BUTTON_XBUTTON1, BUTTON_MASK_XBUTTON1, false);
+
+	} else if ((int)[event buttonNumber] == 4) {
+		_mouseDownEvent(event, BUTTON_XBUTTON2, BUTTON_MASK_XBUTTON2, false);
 
 
-	_mouseDownEvent(event, BUTTON_MIDDLE, BUTTON_MASK_MIDDLE, false);
+	} else {
+		return;
+	}
 }
 }
 
 
 - (void)mouseExited:(NSEvent *)event {
 - (void)mouseExited:(NSEvent *)event {

+ 152 - 136
platform/windows/os_windows.cpp

@@ -434,11 +434,12 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 			bmask |= (wParam & MK_LBUTTON) ? (1 << 0) : 0;
 			bmask |= (wParam & MK_LBUTTON) ? (1 << 0) : 0;
 			bmask |= (wParam & MK_RBUTTON) ? (1 << 1) : 0;
 			bmask |= (wParam & MK_RBUTTON) ? (1 << 1) : 0;
 			bmask |= (wParam & MK_MBUTTON) ? (1 << 2) : 0;
 			bmask |= (wParam & MK_MBUTTON) ? (1 << 2) : 0;
+			bmask |= (wParam & MK_XBUTTON1) ? (1 << 7) : 0;
+			bmask |= (wParam & MK_XBUTTON2) ? (1 << 8) : 0;
 			mm->set_button_mask(bmask);
 			mm->set_button_mask(bmask);
 
 
 			last_button_state = mm->get_button_mask();
 			last_button_state = mm->get_button_mask();
-			/*mm->get_button_mask()|=(wParam&MK_XBUTTON1)?(1<<5):0;
-			mm->get_button_mask()|=(wParam&MK_XBUTTON2)?(1<<6):0;*/
+
 			mm->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
 			mm->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
 			mm->set_global_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
 			mm->set_global_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
 
 
@@ -495,153 +496,168 @@ LRESULT OS_Windows::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
 		case WM_LBUTTONDBLCLK:
 		case WM_LBUTTONDBLCLK:
 		case WM_MBUTTONDBLCLK:
 		case WM_MBUTTONDBLCLK:
 		case WM_RBUTTONDBLCLK:
 		case WM_RBUTTONDBLCLK:
-			/*case WM_XBUTTONDOWN:
-		case WM_XBUTTONUP: */ {
-
-				Ref<InputEventMouseButton> mb;
-				mb.instance();
-
-				switch (uMsg) {
-					case WM_LBUTTONDOWN: {
-						mb->set_pressed(true);
-						mb->set_button_index(1);
-					} break;
-					case WM_LBUTTONUP: {
-						mb->set_pressed(false);
-						mb->set_button_index(1);
-					} break;
-					case WM_MBUTTONDOWN: {
-						mb->set_pressed(true);
-						mb->set_button_index(3);
-
-					} break;
-					case WM_MBUTTONUP: {
-						mb->set_pressed(false);
-						mb->set_button_index(3);
-					} break;
-					case WM_RBUTTONDOWN: {
-						mb->set_pressed(true);
-						mb->set_button_index(2);
-					} break;
-					case WM_RBUTTONUP: {
-						mb->set_pressed(false);
-						mb->set_button_index(2);
-					} break;
-					case WM_LBUTTONDBLCLK: {
-
-						mb->set_pressed(true);
-						mb->set_button_index(1);
-						mb->set_doubleclick(true);
-					} break;
-					case WM_RBUTTONDBLCLK: {
-
-						mb->set_pressed(true);
-						mb->set_button_index(2);
-						mb->set_doubleclick(true);
-					} break;
-					case WM_MBUTTONDBLCLK: {
-
-						mb->set_pressed(true);
-						mb->set_button_index(3);
-						mb->set_doubleclick(true);
-					} break;
-					case WM_MOUSEWHEEL: {
-
-						mb->set_pressed(true);
-						int motion = (short)HIWORD(wParam);
-						if (!motion)
-							return 0;
-
-						if (motion > 0)
-							mb->set_button_index(BUTTON_WHEEL_UP);
-						else
-							mb->set_button_index(BUTTON_WHEEL_DOWN);
-
-					} break;
-					case WM_MOUSEHWHEEL: {
-
-						mb->set_pressed(true);
-						int motion = (short)HIWORD(wParam);
-						if (!motion)
-							return 0;
-
-						if (motion < 0) {
-							mb->set_button_index(BUTTON_WHEEL_LEFT);
-							mb->set_factor(fabs((double)motion / (double)WHEEL_DELTA));
-						} else {
-							mb->set_button_index(BUTTON_WHEEL_RIGHT);
-							mb->set_factor(fabs((double)motion / (double)WHEEL_DELTA));
-						}
-					} break;
-					/*
-				case WM_XBUTTONDOWN: {
-					mb->is_pressed()=true;
-					mb->get_button_index()=(HIWORD(wParam)==XBUTTON1)?6:7;
+		case WM_XBUTTONDBLCLK:
+		case WM_XBUTTONDOWN:
+		case WM_XBUTTONUP: {
+
+			Ref<InputEventMouseButton> mb;
+			mb.instance();
+
+			switch (uMsg) {
+				case WM_LBUTTONDOWN: {
+					mb->set_pressed(true);
+					mb->set_button_index(1);
 				} break;
 				} break;
-				case WM_XBUTTONUP:
-					mb->is_pressed()=true;
-					mb->get_button_index()=(HIWORD(wParam)==XBUTTON1)?6:7;
-				} break;*/
-					default: { return 0; }
-				}
+				case WM_LBUTTONUP: {
+					mb->set_pressed(false);
+					mb->set_button_index(1);
+				} break;
+				case WM_MBUTTONDOWN: {
+					mb->set_pressed(true);
+					mb->set_button_index(3);
 
 
-				mb->set_control((wParam & MK_CONTROL) != 0);
-				mb->set_shift((wParam & MK_SHIFT) != 0);
-				mb->set_alt(alt_mem);
-				//mb->get_alt()=(wParam&MK_MENU)!=0;
-				int bmask = 0;
-				bmask |= (wParam & MK_LBUTTON) ? (1 << 0) : 0;
-				bmask |= (wParam & MK_RBUTTON) ? (1 << 1) : 0;
-				bmask |= (wParam & MK_MBUTTON) ? (1 << 2) : 0;
-				mb->set_button_mask(bmask);
-
-				last_button_state = mb->get_button_mask();
-				/*
-			mb->get_button_mask()|=(wParam&MK_XBUTTON1)?(1<<5):0;
-			mb->get_button_mask()|=(wParam&MK_XBUTTON2)?(1<<6):0;*/
-				mb->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
-
-				if (mouse_mode == MOUSE_MODE_CAPTURED) {
-
-					mb->set_position(Vector2(old_x, old_y));
-				}
+				} break;
+				case WM_MBUTTONUP: {
+					mb->set_pressed(false);
+					mb->set_button_index(3);
+				} break;
+				case WM_RBUTTONDOWN: {
+					mb->set_pressed(true);
+					mb->set_button_index(2);
+				} break;
+				case WM_RBUTTONUP: {
+					mb->set_pressed(false);
+					mb->set_button_index(2);
+				} break;
+				case WM_LBUTTONDBLCLK: {
 
 
-				if (uMsg != WM_MOUSEWHEEL && uMsg != WM_MOUSEHWHEEL) {
-					if (mb->is_pressed()) {
+					mb->set_pressed(true);
+					mb->set_button_index(1);
+					mb->set_doubleclick(true);
+				} break;
+				case WM_RBUTTONDBLCLK: {
 
 
-						if (++pressrc > 0)
-							SetCapture(hWnd);
-					} else {
+					mb->set_pressed(true);
+					mb->set_button_index(2);
+					mb->set_doubleclick(true);
+				} break;
+				case WM_MBUTTONDBLCLK: {
 
 
-						if (--pressrc <= 0) {
-							ReleaseCapture();
-							pressrc = 0;
-						}
+					mb->set_pressed(true);
+					mb->set_button_index(3);
+					mb->set_doubleclick(true);
+				} break;
+				case WM_MOUSEWHEEL: {
+
+					mb->set_pressed(true);
+					int motion = (short)HIWORD(wParam);
+					if (!motion)
+						return 0;
+
+					if (motion > 0)
+						mb->set_button_index(BUTTON_WHEEL_UP);
+					else
+						mb->set_button_index(BUTTON_WHEEL_DOWN);
+
+				} break;
+				case WM_MOUSEHWHEEL: {
+
+					mb->set_pressed(true);
+					int motion = (short)HIWORD(wParam);
+					if (!motion)
+						return 0;
+
+					if (motion < 0) {
+						mb->set_button_index(BUTTON_WHEEL_LEFT);
+						mb->set_factor(fabs((double)motion / (double)WHEEL_DELTA));
+					} else {
+						mb->set_button_index(BUTTON_WHEEL_RIGHT);
+						mb->set_factor(fabs((double)motion / (double)WHEEL_DELTA));
 					}
 					}
-				} else if (mouse_mode != MOUSE_MODE_CAPTURED) {
-					// for reasons unknown to mankind, wheel comes in screen cordinates
-					POINT coords;
-					coords.x = mb->get_position().x;
-					coords.y = mb->get_position().y;
+				} break;
+				case WM_XBUTTONDOWN: {
+
+					mb->set_pressed(true);
+					if (HIWORD(wParam) == XBUTTON1)
+						mb->set_button_index(BUTTON_XBUTTON1);
+					else
+						mb->set_button_index(BUTTON_XBUTTON2);
+				} break;
+				case WM_XBUTTONUP: {
 
 
-					ScreenToClient(hWnd, &coords);
+					mb->set_pressed(false);
+					if (HIWORD(wParam) == XBUTTON1)
+						mb->set_button_index(BUTTON_XBUTTON1);
+					else
+						mb->set_button_index(BUTTON_XBUTTON2);
+				} break;
+				case WM_XBUTTONDBLCLK: {
 
 
-					mb->set_position(Vector2(coords.x, coords.y));
-				}
+					mb->set_pressed(true);
+					if (HIWORD(wParam) == XBUTTON1)
+						mb->set_button_index(BUTTON_XBUTTON1);
+					else
+						mb->set_button_index(BUTTON_XBUTTON2);
+					mb->set_doubleclick(true);
+				} break;
+				default: { return 0; }
+			}
+
+			mb->set_control((wParam & MK_CONTROL) != 0);
+			mb->set_shift((wParam & MK_SHIFT) != 0);
+			mb->set_alt(alt_mem);
+			//mb->get_alt()=(wParam&MK_MENU)!=0;
+			int bmask = 0;
+			bmask |= (wParam & MK_LBUTTON) ? (1 << 0) : 0;
+			bmask |= (wParam & MK_RBUTTON) ? (1 << 1) : 0;
+			bmask |= (wParam & MK_MBUTTON) ? (1 << 2) : 0;
+			bmask |= (wParam & MK_XBUTTON1) ? (1 << 7) : 0;
+			bmask |= (wParam & MK_XBUTTON2) ? (1 << 8) : 0;
+			mb->set_button_mask(bmask);
+
+			last_button_state = mb->get_button_mask();
+			mb->set_position(Vector2(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)));
 
 
-				mb->set_global_position(mb->get_position());
+			if (mouse_mode == MOUSE_MODE_CAPTURED) {
+
+				mb->set_position(Vector2(old_x, old_y));
+			}
+
+			if (uMsg != WM_MOUSEWHEEL && uMsg != WM_MOUSEHWHEEL) {
+				if (mb->is_pressed()) {
 
 
-				if (main_loop) {
-					input->parse_input_event(mb);
-					if (mb->is_pressed() && mb->get_button_index() > 3) {
-						//send release for mouse wheel
-						Ref<InputEventMouseButton> mbd = mb->duplicate();
-						mbd->set_pressed(false);
-						input->parse_input_event(mbd);
+					if (++pressrc > 0)
+						SetCapture(hWnd);
+				} else {
+
+					if (--pressrc <= 0) {
+						ReleaseCapture();
+						pressrc = 0;
 					}
 					}
 				}
 				}
+			} else if (mouse_mode != MOUSE_MODE_CAPTURED) {
+				// for reasons unknown to mankind, wheel comes in screen cordinates
+				POINT coords;
+				coords.x = mb->get_position().x;
+				coords.y = mb->get_position().y;
+
+				ScreenToClient(hWnd, &coords);
+
+				mb->set_position(Vector2(coords.x, coords.y));
+			}
+
+			mb->set_global_position(mb->get_position());
+
+			if (main_loop) {
+				input->parse_input_event(mb);
+				if (mb->is_pressed() && mb->get_button_index() > 3 && mb->get_button_index() < 8) {
+					//send release for mouse wheel
+					Ref<InputEventMouseButton> mbd = mb->duplicate();
+					mbd->set_pressed(false);
+					input->parse_input_event(mbd);
+				}
 			}
 			}
-			break;
+		} break;
 
 
 		case WM_SIZE: {
 		case WM_SIZE: {
 			int window_w = LOWORD(lParam);
 			int window_w = LOWORD(lParam);