← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~widelands-dev/widelands/handle_apple_mouse_quirks into lp:widelands

 

SirVer has proposed merging lp:~widelands-dev/widelands/handle_apple_mouse_quirks into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)

For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/handle_apple_mouse_quirks/+merge/164796

Try to handle apple mouse quirks in only one place and not in many. I will merge in a few days when i hear nothing back :).
-- 
https://code.launchpad.net/~widelands-dev/widelands/handle_apple_mouse_quirks/+merge/164796
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/handle_apple_mouse_quirks into lp:widelands.
=== modified file 'src/editor/editorinteractive.cc'
--- src/editor/editorinteractive.cc	2013-03-08 14:17:14 +0000
+++ src/editor/editorinteractive.cc	2013-05-20 19:44:30 +0000
@@ -50,6 +50,7 @@
 	Interactive_Base(e, g_options.pull_section("global")),
 	m_need_save(false),
 	m_realtime(WLApplication::get()->get_time()),
+	m_left_mouse_button_is_down(false),
 	m_history(m_undo, m_redo),
 
 #define INIT_BUTTON(picture, name, tooltip)                         \
@@ -272,18 +273,27 @@
 	set_need_save(true);
 }
 
+bool Editor_Interactive::handle_mouserelease(Uint8 btn, int32_t x, int32_t y) {
+	if (btn == SDL_BUTTON_LEFT) {
+		m_left_mouse_button_is_down = false;
+	}
+	return Interactive_Base::handle_mouserelease(btn, x, y);
+}
+
+bool Editor_Interactive::handle_mousepress(Uint8 btn, int32_t x, int32_t y) {
+	if (btn == SDL_BUTTON_LEFT) {
+		m_left_mouse_button_is_down = true;
+	}
+	return Interactive_Base::handle_mousepress(btn, x, y);
+}
+
 /// Needed to get freehand painting tools (hold down mouse and move to edit).
 void Editor_Interactive::set_sel_pos(Widelands::Node_and_Triangle<> const sel) {
 	bool const target_changed =
 	    tools.current().operates_on_triangles() ?
 	    sel.triangle != get_sel_pos().triangle : sel.node != get_sel_pos().node;
 	Interactive_Base::set_sel_pos(sel);
-	int32_t mask = SDL_BUTTON_LMASK;
-#ifdef __APPLE__
-	// workaround for SDLs middle button emulation
-	mask |= SDL_BUTTON_MMASK;
-#endif
-	if (target_changed and (SDL_GetMouseState(0, 0) & mask))
+	if (target_changed and m_left_mouse_button_is_down)
 		map_clicked(true);
 }
 
@@ -310,7 +320,6 @@
 
 }
 
-
 void Editor_Interactive::toolsize_menu_btn() {
 	if (m_toolsizemenu.window)
 		delete m_toolsizemenu.window;
@@ -318,7 +327,6 @@
 		new Editor_Toolsize_Menu(*this, m_toolsizemenu);
 }
 
-
 void Editor_Interactive::set_sel_radius_and_update_menu(uint32_t const val) {
 	if (UI::UniqueWindow * const w = m_toolsizemenu.window)
 		ref_cast<Editor_Toolsize_Menu, UI::UniqueWindow>(*w).update(val);

=== modified file 'src/editor/editorinteractive.h'
--- src/editor/editorinteractive.h	2013-02-10 19:36:24 +0000
+++ src/editor/editorinteractive.h	2013-05-20 19:44:30 +0000
@@ -65,8 +65,10 @@
 	virtual void set_sel_pos(Widelands::Node_and_Triangle<>);
 	void set_sel_radius_and_update_menu(uint32_t);
 
-	//  gets called when a keyboard event occurs
+	//  Handle UI elements.
 	bool handle_key(bool down, SDL_keysym);
+	bool handle_mousepress(Uint8 btn, int32_t x, int32_t y);
+	bool handle_mouserelease(Uint8 btn, int32_t x, int32_t y);
 
 	struct Tools {
 		Tools()
@@ -140,6 +142,7 @@
 	std::vector<Player_References> m_player_tribe_references;
 
 	int32_t m_realtime;
+	bool m_left_mouse_button_is_down;
 
 	Editor_History m_history;
 

=== modified file 'src/wlapplication.cc'
--- src/wlapplication.cc	2013-04-20 20:20:34 +0000
+++ src/wlapplication.cc	2013-05-20 19:44:30 +0000
@@ -258,6 +258,7 @@
 m_game_type            (NONE),
 journal                (0),
 m_mouse_swapped        (false),
+m_faking_middle_mouse_button(false),
 m_mouse_position       (0, 0),
 m_mouse_locked         (0),
 m_mouse_compensate_warp(0, 0),
@@ -680,39 +681,10 @@
 				cb->key(ev.type == SDL_KEYDOWN, ev.key.keysym);
 			}
 			break;
+
 		case SDL_MOUSEBUTTONDOWN:
-			if (cb and cb->mouse_press) {
-				if (m_mouse_swapped) {
-					switch (ev.button.button) {
-					case SDL_BUTTON_LEFT:
-						ev.button.button = SDL_BUTTON_RIGHT;
-						break;
-					case SDL_BUTTON_RIGHT:
-						ev.button.button = SDL_BUTTON_LEFT;
-						break;
-					default:;
-					}
-				}
-				assert(ev.button.state == SDL_PRESSED);
-				cb->mouse_press(ev.button.button, ev.button.x, ev.button.y);
-			}
-			break;
 		case SDL_MOUSEBUTTONUP:
-			if (cb and cb->mouse_release) {
-				if (m_mouse_swapped) {
-					switch (ev.button.button) {
-					case SDL_BUTTON_LEFT:
-						ev.button.button = SDL_BUTTON_RIGHT;
-						break;
-					case SDL_BUTTON_RIGHT:
-						ev.button.button = SDL_BUTTON_LEFT;
-						break;
-					default:;
-					}
-				}
-				assert(ev.button.state == SDL_RELEASED);
-				cb->mouse_release(ev.button.button, ev.button.x, ev.button.y);
-			}
+			_handle_mousebutton(ev, cb);
 			break;
 
 		case SDL_MOUSEMOTION:
@@ -733,6 +705,51 @@
 	}
 }
 
+/*
+ * Capsule repetitive code for mouse buttons
+ */
+void WLApplication::_handle_mousebutton
+	(SDL_Event & ev, InputCallback const * cb)
+{
+		if (m_mouse_swapped) {
+			switch (ev.button.button) {
+				case SDL_BUTTON_LEFT:
+					ev.button.button = SDL_BUTTON_RIGHT;
+					break;
+				case SDL_BUTTON_RIGHT:
+					ev.button.button = SDL_BUTTON_LEFT;
+					break;
+			}
+		}
+
+#ifdef __APPLE__
+		//  On Mac, SDL does middle mouse button emulation (alt+left). This
+		//  interferes with the editor, which is using alt+left click for
+		//  third tool. So if we ever see a middle mouse button on Mac,
+		//  check if any ALT Key is pressed and if, treat it like a left
+		//  mouse button.
+		if
+			(ev.button.button == SDL_BUTTON_MIDDLE and
+			 (get_key_state(SDLK_LALT) || get_key_state(SDLK_RALT)))
+		{
+			ev.button.button = SDL_BUTTON_LEFT;
+			m_faking_middle_mouse_button = true;
+		}
+#endif
+
+		if (ev.type == SDL_MOUSEBUTTONDOWN && cb and cb->mouse_press)
+			cb->mouse_press(ev.button.button, ev.button.x, ev.button.y);
+		else if (ev.type == SDL_MOUSEBUTTONUP) {
+			if (cb and cb->mouse_release) {
+				if (ev.button.button == SDL_BUTTON_MIDDLE and m_faking_middle_mouse_button) {
+					cb->mouse_release(SDL_BUTTON_LEFT, ev.button.x, ev.button.y);
+					m_faking_middle_mouse_button = false;
+				}
+				cb->mouse_release(ev.button.button, ev.button.x, ev.button.y);
+			}
+		}
+}
+
 /**
  * Return the current time, in milliseconds
  * \todo Use our internally defined time type

=== modified file 'src/wlapplication.h'
--- src/wlapplication.h	2013-02-10 20:07:27 +0000
+++ src/wlapplication.h	2013-05-20 19:44:30 +0000
@@ -269,6 +269,10 @@
 	///True if left and right mouse button should be swapped
 	bool  m_mouse_swapped;
 
+	/// When apple is involved, the middle mouse button is sometimes send, even
+	/// if it wasn't pressed. We try to revert this and this helps.
+	bool  m_faking_middle_mouse_button;
+
 	///The current position of the mouse pointer
 	Point m_mouse_position;
 
@@ -309,6 +313,9 @@
 	///created already. NULL otherwise.
 	///\note This is private on purpose. Read the class documentation.
 	static WLApplication * the_singleton;
+
+	void _handle_mousebutton(SDL_Event &, InputCallback const *);
+
 };
 
 #endif

=== modified file 'src/wui/mapview.cc'
--- src/wui/mapview.cc	2013-02-10 19:36:24 +0000
+++ src/wui/mapview.cc	2013-05-20 19:44:30 +0000
@@ -159,17 +159,7 @@
 bool Map_View::handle_mousepress
 	(Uint8 const btn, int32_t const x, int32_t const y)
 {
-#ifdef __APPLE__
-	//  SDL does on Mac hardcoded middle mouse button emulation (alt+left).
-	//  This interferes with the editor, which is using alt+left click for third
-	//  tool.  So just handle middle mouse button like left one.
-	//  TODO This should be handled in a more general way someplace else. What
-	//  TODO kind of stupid idea is it to hardcode something like that in SDL?
-	//  TODO Sometimes, people are funny....
-	if (btn == SDL_BUTTON_MIDDLE || btn == SDL_BUTTON_LEFT)
-#else
 	if (btn == SDL_BUTTON_LEFT)
-#endif
 	{
 		stop_dragging();
 		track_sel(Point(x, y));


Follow ups