Browse Source

Merge pull request #7721 from RandomShaper/improve-touch-button-2.1

Fix touch button issues (2.1)
Rémi Verschelde 8 years ago
parent
commit
3b09d77208
2 changed files with 57 additions and 86 deletions
  1. 53 86
      scene/2d/screen_button.cpp
  2. 4 0
      scene/2d/screen_button.h

+ 53 - 86
scene/2d/screen_button.cpp

@@ -145,8 +145,12 @@ void TouchScreenButton::_notification(int p_what) {
 		} break;
 		} break;
 		case NOTIFICATION_EXIT_TREE: {
 		case NOTIFICATION_EXIT_TREE: {
 			if (is_pressed())
 			if (is_pressed())
-				Input::get_singleton()->action_release(action);
+				_release(true);
 		} break;
 		} break;
+		case NOTIFICATION_PAUSED: {
+			// So the button can be pressed again even though the release gets unhandled because of coming during pause
+			allow_repress=true;
+		}
 	}
 	}
 }
 }
 
 
@@ -184,22 +188,7 @@ void TouchScreenButton::_input(const InputEvent& p_event) {
 
 
 		if (p_event.type==InputEvent::SCREEN_TOUCH && !p_event.screen_touch.pressed && finger_pressed==p_event.screen_touch.index) {
 		if (p_event.type==InputEvent::SCREEN_TOUCH && !p_event.screen_touch.pressed && finger_pressed==p_event.screen_touch.index) {
 
 
-			emit_signal("released");
-
-			if (action_id!=-1) {
-
-				Input::get_singleton()->action_release(action);
-				InputEvent ie;
-				ie.type=InputEvent::ACTION;
-				ie.ID=0;
-				ie.action.action=action_id;
-				ie.action.pressed=false;
-				get_tree()->input_event(ie);
-			}
-			finger_pressed=-1;
-
-			update();
-
+			_release();
 		}
 		}
 
 
 		if ((p_event.type==InputEvent::SCREEN_TOUCH && p_event.screen_touch.pressed)|| p_event.type==InputEvent::SCREEN_DRAG) {
 		if ((p_event.type==InputEvent::SCREEN_TOUCH && p_event.screen_touch.pressed)|| p_event.type==InputEvent::SCREEN_DRAG) {
@@ -225,44 +214,12 @@ void TouchScreenButton::_input(const InputEvent& p_event) {
 
 
 
 
 				if (touched) {
 				if (touched) {
-
 					if (finger_pressed==-1) {
 					if (finger_pressed==-1) {
-						finger_pressed=p_event.screen_touch.index;
-						//emit change stuff
-						emit_signal("pressed");
-						if (action_id!=-1) {
-
-							Input::get_singleton()->action_press(action);
-							InputEvent ie;
-							ie.type=InputEvent::ACTION;
-							ie.ID=0;
-							ie.action.action=action_id;
-							ie.action.pressed=true;
-							get_tree()->input_event(ie);
-						}
-
-						update();
+						_press(p_event.screen_touch.index);
 					}
 					}
-
 				} else {
 				} else {
-
 					if (finger_pressed!=-1) {
 					if (finger_pressed!=-1) {
-
-						emit_signal("released");
-
-						if (action_id!=-1) {
-
-							Input::get_singleton()->action_release(action);
-							InputEvent ie;
-							ie.type=InputEvent::ACTION;
-							ie.ID=0;
-							ie.action.action=action_id;
-							ie.action.pressed=false;
-							get_tree()->input_event(ie);
-						}
-						finger_pressed=-1;
-
-						update();
+						_release();
 					}
 					}
 				}
 				}
 
 
@@ -280,7 +237,8 @@ void TouchScreenButton::_input(const InputEvent& p_event) {
 				if (!is_visible())
 				if (!is_visible())
 					return;
 					return;
 
 
-				if (finger_pressed!=-1)
+				const bool can_press=finger_pressed==-1 || allow_repress;
+				if (!can_press)
 					return; //already fingering
 					return; //already fingering
 
 
 				Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(Point2(p_event.screen_touch.x,p_event.screen_touch.y));
 				Point2 coord = (get_global_transform_with_canvas()).affine_inverse().xform(Point2(p_event.screen_touch.x,p_event.screen_touch.y));
@@ -311,48 +269,56 @@ void TouchScreenButton::_input(const InputEvent& p_event) {
 				}
 				}
 
 
 
 
-
 				if (touched) {
 				if (touched) {
-
-					finger_pressed=p_event.screen_touch.index;
-					//emit change stuff
-					emit_signal("pressed");
-					if (action_id!=-1) {
-
-						Input::get_singleton()->action_press(action);
-						InputEvent ie;
-						ie.type=InputEvent::ACTION;
-						ie.ID=0;
-						ie.action.action=action_id;
-						ie.action.pressed=true;
-						get_tree()->input_event(ie);
-					}
-					update();
-
+					_press(p_event.screen_touch.index);
 				}
 				}
 			} else {
 			} else {
+				if (p_event.screen_touch.index==finger_pressed) {
+					_release();
+				}
+			}
+		}
+	}
+}
 
 
+void TouchScreenButton::_press(int p_finger_pressed) {
 
 
-				if (p_event.screen_touch.index==finger_pressed) {
-					//untouch
+	finger_pressed=p_finger_pressed;
+	allow_repress=false;
 
 
-					emit_signal("released");
+	if (action_id!=-1) {
 
 
-					if (action_id!=-1) {
+		Input::get_singleton()->action_press(action);
+		InputEvent ie;
+		ie.type=InputEvent::ACTION;
+		ie.ID=0;
+		ie.action.action=action_id;
+		ie.action.pressed=true;
+		get_tree()->input_event(ie);
+	}
 
 
-						Input::get_singleton()->action_release(action);
-						InputEvent ie;
-						ie.type=InputEvent::ACTION;
-						ie.ID=0;
-						ie.action.action=action_id;
-						ie.action.pressed=false;
-						get_tree()->input_event(ie);
-					}
-					finger_pressed=-1;
-					update();
-				}
-			}
-		}
+	emit_signal("pressed");
+	update();
+}
+
+void TouchScreenButton::_release(bool p_exiting_tree) {
+
+	finger_pressed=-1;
+
+	if (action_id!=-1) {
+
+		Input::get_singleton()->action_release(action);
+		InputEvent ie;
+		ie.type=InputEvent::ACTION;
+		ie.ID=0;
+		ie.action.action=action_id;
+		ie.action.pressed=false;
+		get_tree()->input_event(ie);
+	}
+
+	if (!p_exiting_tree) {
+		emit_signal("released");
+		update();
 	}
 	}
 }
 }
 
 
@@ -438,6 +404,7 @@ void TouchScreenButton::_bind_methods() {
 TouchScreenButton::TouchScreenButton() {
 TouchScreenButton::TouchScreenButton() {
 
 
 	finger_pressed=-1;
 	finger_pressed=-1;
+	allow_repress=false;
 	action_id=-1;
 	action_id=-1;
 	passby_press=false;
 	passby_press=false;
 	visibility=VISIBILITY_ALWAYS;
 	visibility=VISIBILITY_ALWAYS;

+ 4 - 0
scene/2d/screen_button.h

@@ -56,12 +56,16 @@ private:
 	StringName action;
 	StringName action;
 	bool passby_press;
 	bool passby_press;
 	int finger_pressed;
 	int finger_pressed;
+	bool allow_repress;
 	int action_id;
 	int action_id;
 
 
 	VisibilityMode visibility;
 	VisibilityMode visibility;
 
 
 	void _input(const InputEvent& p_Event);
 	void _input(const InputEvent& p_Event);
 
 
+	void _press(int p_finger_pressed);
+	void _release(bool p_exiting_tree=false);
+
 protected:
 protected:
 
 
 	void _notification(int p_what);
 	void _notification(int p_what);