From 9d3e12707a1f0c91c7b826a54f3385c45f2abe38 Mon Sep 17 00:00:00 2001 From: Dan Korostelev Date: Tue, 15 Oct 2019 12:08:56 +0200 Subject: [PATCH] WIP on hit test bounds updating and culling --- src/openfl/display/Bitmap.hx | 2 +- src/openfl/display/DisplayObject.hx | 20 ++++++++++++++++++++ src/openfl/display/DisplayObjectContainer.hx | 9 +++++++++ src/openfl/display/Sprite.hx | 1 + src/openfl/text/TextField.hx | 2 +- 5 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/openfl/display/Bitmap.hx b/src/openfl/display/Bitmap.hx index aebe601d3d..de3492b5f3 100644 --- a/src/openfl/display/Bitmap.hx +++ b/src/openfl/display/Bitmap.hx @@ -90,7 +90,7 @@ class Bitmap extends DisplayObject { } - private override function __getBounds (rect:Rectangle, matrix:Matrix):Void { + private override function __getOwnBounds (rect:Rectangle, matrix:Matrix):Void { if (__bitmapData != null) { diff --git a/src/openfl/display/DisplayObject.hx b/src/openfl/display/DisplayObject.hx index 3dac64a117..a73b75ad03 100644 --- a/src/openfl/display/DisplayObject.hx +++ b/src/openfl/display/DisplayObject.hx @@ -90,6 +90,7 @@ class DisplayObject extends EventDispatcher implements IBitmapDrawable { private var __children:Array; private var __filters:Array; private var __graphics:Graphics; + private var __hitTestBounds:Rectangle; private var __isMask:Bool; private var __loaderInfo:LoaderInfo; private var __mask:DisplayObject; @@ -138,6 +139,7 @@ class DisplayObject extends EventDispatcher implements IBitmapDrawable { __scaleX = 1; __scaleY = 1; + __hitTestBounds = new Rectangle (); __worldAlpha = 1; __worldBlendMode = NORMAL; __worldTransform = new Matrix (); @@ -489,6 +491,13 @@ class DisplayObject extends EventDispatcher implements IBitmapDrawable { private function __getBounds (rect:Rectangle, matrix:Matrix):Void { + __getOwnBounds (rect, matrix); + + } + + + private function __getOwnBounds (rect:Rectangle, matrix:Matrix):Void { + if (__graphics != null) { __graphics.__getBounds (rect, matrix); @@ -498,6 +507,14 @@ class DisplayObject extends EventDispatcher implements IBitmapDrawable { } + function __updateHitTestBounds () { + + __hitTestBounds.setEmpty (); + __getOwnBounds (__hitTestBounds, __worldTransform); + + } + + private function __getCursor ():MouseCursor { return null; @@ -643,6 +660,8 @@ class DisplayObject extends EventDispatcher implements IBitmapDrawable { private function __hitTest (x:Float, y:Float, shapeFlag:Bool, stack:Array, interactiveOnly:Bool, hitObject:DisplayObject, hitTestWhenMouseDisabled:Bool = false):Bool { + if (!__hitTestBounds.contains(x, y)) return false; + if (__graphics != null) { if (!hitObject.visible || __isMask) return false; @@ -1194,6 +1213,7 @@ class DisplayObject extends EventDispatcher implements IBitmapDrawable { __calculateAbsoluteTransform (__transform, parent.__worldTransform, __worldTransform); __calculateAbsoluteTransform (__transform, parent.__renderTransform, __renderTransform); + __updateHitTestBounds (); // TODO: scrollRect should clip this } else { diff --git a/src/openfl/display/DisplayObjectContainer.hx b/src/openfl/display/DisplayObjectContainer.hx index ac2b021c83..532883fd79 100644 --- a/src/openfl/display/DisplayObjectContainer.hx +++ b/src/openfl/display/DisplayObjectContainer.hx @@ -504,6 +504,7 @@ class DisplayObjectContainer extends InteractiveObject { private override function __hitTest (x:Float, y:Float, shapeFlag:Bool, stack:Array, interactiveOnly:Bool, hitObject:DisplayObject, hitTestWhenMouseDisabled:Bool = false):Bool { if (!hitObject.visible || __isMask || (!hitTestWhenMouseDisabled && interactiveOnly && !mouseEnabled && !mouseChildren)) return false; + if (!__hitTestBounds.contains(x, y)) return false; if (mask != null && !mask.__hitTestMask (x, y)) return false; if (!__isPointInScrollRect (x, y)) { @@ -866,9 +867,14 @@ class DisplayObjectContainer extends InteractiveObject { } else if (__updateTraverse) { + __updateHitTestBounds (); + for (child in __children) { child.__traverse (); + + var childBounds = child.__hitTestBounds; + __hitTestBounds.__expand (childBounds.x, childBounds.y, childBounds.width, childBounds.height); } @@ -894,6 +900,9 @@ class DisplayObjectContainer extends InteractiveObject { child.__update (resetUpdateDirty); + var childBounds = child.__hitTestBounds; + __hitTestBounds.__expand (childBounds.x, childBounds.y, childBounds.width, childBounds.height); + } } diff --git a/src/openfl/display/Sprite.hx b/src/openfl/display/Sprite.hx index 4d656cf239..67a495656c 100644 --- a/src/openfl/display/Sprite.hx +++ b/src/openfl/display/Sprite.hx @@ -71,6 +71,7 @@ class Sprite extends DisplayObjectContainer { private override function __hitTest (x:Float, y:Float, shapeFlag:Bool, stack:Array, interactiveOnly:Bool, hitObject:DisplayObject, hitTestWhenMouseDisabled:Bool = false):Bool { if (!hitObject.visible || __isMask) return false; + if (!__hitTestBounds.contains(x, y)) return false; if (interactiveOnly && !mouseEnabled && !mouseChildren) { diff --git a/src/openfl/text/TextField.hx b/src/openfl/text/TextField.hx index c034818926..f7df531180 100644 --- a/src/openfl/text/TextField.hx +++ b/src/openfl/text/TextField.hx @@ -977,7 +977,7 @@ class TextField extends InteractiveObject { } - private override function __getBounds (rect:Rectangle, matrix:Matrix):Void { + private override function __getOwnBounds (rect:Rectangle, matrix:Matrix):Void { __updateLayout ();