2
0
Эх сурвалжийг харах

kill events outside of masked Interactive

ncannasse 9 жил өмнө
parent
commit
3f7fa361e1
2 өөрчлөгдсөн 55 нэмэгдсэн , 0 устгасан
  1. 32 0
      h2d/Interactive.hx
  2. 23 0
      h2d/Mask.hx

+ 32 - 0
h2d/Interactive.hx

@@ -18,6 +18,7 @@ class Interactive extends Drawable implements hxd.SceneEvents.Interactive {
 	public var enableRightButton : Bool;
 	var scene : Scene;
 	var mouseDownButton : Int = -1;
+	var parentMask : Mask;
 
 	public function new(width, height, ?parent) {
 		super(parent);
@@ -29,6 +30,7 @@ class Interactive extends Drawable implements hxd.SceneEvents.Interactive {
 	override function onAlloc() {
 		this.scene = getScene();
 		if( scene != null ) scene.addEventTarget(this);
+		updateMask();
 		super.onAlloc();
 	}
 
@@ -46,6 +48,20 @@ class Interactive extends Drawable implements hxd.SceneEvents.Interactive {
 			scene.removeEventTarget(this);
 			scene.addEventTarget(this);
 		}
+		updateMask();
+	}
+
+	function updateMask() {
+		parentMask = null;
+		var p = parent;
+		while( p != null ) {
+			var m = Std.instance(p, Mask);
+			if( m != null ) {
+				parentMask = m;
+				break;
+			}
+			p = p.parent;
+		}
 	}
 
 	override function onDelete() {
@@ -68,6 +84,22 @@ class Interactive extends Drawable implements hxd.SceneEvents.Interactive {
 	}
 
 	@:noCompletion public function handleEvent( e : hxd.Event ) {
+		if( parentMask != null ) {
+			var p = parentMask;
+			var pt = new h2d.col.Point(e.relX, e.relY);
+			localToGlobal(pt);
+			var saveX = pt.x, saveY = pt.y;
+			while( p != null ) {
+				pt.x = saveX;
+				pt.y = saveY;
+				var pt = p.globalToLocal(pt);
+				if( pt.x < 0 || pt.y < 0 || pt.x > p.width || pt.y > p.height ) {
+					e.cancel = true;
+					return;
+				}
+				p = @:privateAccess p.parentMask;
+			}
+		}
 		if( isEllipse && checkBounds(e) ) {
 			var cx = width * 0.5, cy = height * 0.5;
 			var dx = (e.relX - cx) / cx;

+ 23 - 0
h2d/Mask.hx

@@ -4,6 +4,7 @@ class Mask extends Sprite {
 
 	public var width : Int;
 	public var height : Int;
+	var parentMask : Mask;
 
 	public function new(width, height, ?parent) {
 		super(parent);
@@ -11,6 +12,28 @@ class Mask extends Sprite {
 		this.height = height;
 	}
 
+	override function onParentChanged() {
+		updateMask();
+	}
+
+	override function onAlloc() {
+		super.onAlloc();
+		updateMask();
+	}
+
+	function updateMask() {
+		parentMask = null;
+		var p = parent;
+		while( p != null ) {
+			var m = Std.instance(p, Mask);
+			if( m != null ) {
+				parentMask = m;
+				break;
+			}
+			p = p.parent;
+		}
+	}
+
 	override function getBoundsRec( relativeTo, out, forSize ) {
 		super.getBoundsRec(relativeTo, out, forSize);
 		var xMin = out.xMin, yMin = out.yMin, xMax = out.xMax, yMax = out.yMax;