Skip to content

Commit

Permalink
FlxButton: don't check for touch overlaps after finding a mouse overl…
Browse files Browse the repository at this point in the history
…ap, closes #1686

The checkInput() call that is performed to find overlaps has side-effects, which is why it should at maximum be performed once per frame.
  • Loading branch information
Gama11 committed Jan 15, 2016
1 parent 69718e8 commit 2c9a545
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions flixel/ui/FlxButton.hx
Original file line number Diff line number Diff line change
Expand Up @@ -380,46 +380,59 @@ class FlxTypedButton<T:FlxSprite> extends FlxSprite implements IFlxInput
private function updateButton():Void
{
// We're looking for any touch / mouse overlaps with this button
var overlapFound = false;
var overlapFound = checkMouseOverlap();
if (!overlapFound)
overlapFound = checkTouchOverlap();

#if !FLX_NO_TOUCH // there's only a mouse event listener for onUp
if (currentInput != null && currentInput.justReleased && Std.is(currentInput, FlxTouch) && overlapFound)
{
onUpHandler();
}
#end

if (status != FlxButton.NORMAL &&
(!overlapFound || (currentInput != null && currentInput.justReleased)))
{
onOutHandler();
}
}

private function checkMouseOverlap():Bool
{
#if !FLX_NO_MOUSE
for (camera in cameras)
{
#if !FLX_NO_MOUSE
for (buttonID in mouseButtons)
{
var button = FlxMouseButton.getFromID(buttonID);

if (button != null && checkInput(FlxG.mouse, button, button.justPressedPosition, camera))
{
overlapFound = true;
}
}
#end

#if !FLX_NO_TOUCH
for (touch in FlxG.touches.list)
for (buttonID in mouseButtons)
{
var button = FlxMouseButton.getFromID(buttonID);
if (button != null && checkInput(FlxG.mouse, button, button.justPressedPosition, camera))
{
if (checkInput(touch, touch, touch.justPressedPosition, camera))
{
overlapFound = true;
break;
}
return true;
}
#end
}
}
#end

#if !FLX_NO_TOUCH // there's only a mouse event listener for onUp
if (currentInput != null && currentInput.justReleased && Std.is(currentInput, FlxTouch) && overlapFound)
return false;
}

private function checkTouchOverlap():Bool
{
#if !FLX_NO_TOUCH
for (camera in cameras)
{
for (touch in FlxG.touches.list)
{
onUpHandler();
if (checkInput(touch, touch, touch.justPressedPosition, camera))
{
return true;
}
}
}
#end

if (status != FlxButton.NORMAL &&
(!overlapFound || (currentInput != null && currentInput.justReleased)))
{
onOutHandler();
}
return false;
}

private function checkInput(pointer:FlxPointer, input:IFlxInput, justPressedPosition:FlxPoint, camera:FlxCamera):Bool
Expand Down

0 comments on commit 2c9a545

Please sign in to comment.