Log in
Please log in or register.
Pages: [1]
  Print  
Author Topic: Working implementation workaround for Popup-blockers.  (Read 553 times)
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« on: Thu, Sep 10, 2009 »

This is old! I've made a much better FlxURLButton!




Here's my approach to avoid popup-blockers when calling FlxG.openURL from a FlxButton.

You need to modify a 3 classes, but not with weird modifications.
FlxButton is the more "intense" modification. For FlxCore only a member variable is added and in FlxLayer only add/remove functions are modified a bit.


FlxCore
New Member Variables for FlxCore
Code:
public var ownerLayer:FlxLayer = null;

FlxLayer
Modification for add and remove
Code:
virtual public function add(Core:FlxCore):FlxCore
{
_children.add(Core)
Core.ownerLayer = this;
return Core;
}

virtual public function remove(Core:FlxCore):void
{
_children.remove(Core, true);
if(Core.ownerLayer == this)
Core.ownerLayer = null;
}



FlxButton
New member variables for FlxButton
Code:
private var _mouseHooked:Boolean = false;

New functions for FlxButton:

Code:
public function isReallyClickable():Boolean
{
var ancestor:FlxCore = this;
do {  
if(!ancestor.exists || !ancestor.active) {
return false;
}
ancestor = ancestor.ownerLayer;
} while(ancestor != null);
return true;
}

private function onMouseDown(event:MouseEvent):void
{
if(_off.overlapsPoint(FlxG.mouse.x,FlxG.mouse.y) && isReallyClickable())
{
event.stopImmediatePropagation();
trace("Mouse down event for FlxButton @ "+FlxG.mouse.x+","+FlxG.mouse.y);
_callback();
}
}

private function onRemovedFromStage(event:Event):void
{
if(FlxG.state.stage) {
FlxG.state.stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
FlxG.state.stage.removeEventListener(Event.REMOVED, onRemovedFromStage);
}
}


Modified update() function
Code:
override public function update():void
{
//STUFF WAS ADDED HERE <<<<<<<<<<<<<<<<<<<<
if(!_mouseHooked && FlxG.state.stage) {
FlxG.state.stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
FlxG.state.stage.addEventListener(Event.REMOVED, onRemovedFromStage);
}

super.update();

if(!FlxG.kMouse) //if mouse is not down (globally), it shouldn't be pressed
_pressed=false;

if((_off != null) && _off.exists && _off.active) _off.update();
if((_on != null) && _on.exists && _on.active) _on.update();
if(_offT != null)
{
if((_offT != null) && _offT.exists && _offT.active) _offT.update();
if((_onT != null) && _onT.exists && _onT.active) _onT.update();
}

visibility(false);
if(_off.overlapsPoint(FlxG.mouse.x,FlxG.mouse.y))
{


//STUFF WAS REMOVED FROM HERE <<<<<<<<<<<<<<<<<<<<


visibility(!_pressed);
}
if(_onToggle) visibility(_off.visible);
updatePositions();
}


FIX: the event was REMOVED not REMOVED_FROM_STAGE.

I hope you like it Wink
-Martín
« Last Edit: Fri, Sep 11, 2009 by nitram_cero » Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
Merve
Newbie
*
Posts: 39


View Profile
« Reply #1 on: Thu, Sep 10, 2009 »

Ok so you do... with the thing? and... where?

No, you lost me. Any chance you can explain why this works for the slow people in the room Wink

From what I can tell you are creating a trail of ownership and adding a couple of event listeners, one for clicking and one for removal. But I don't know how this solves the problem
Logged
nitram_cero (2bam)
Sr. Member
****
Posts: 473



View Profile WWW
« Reply #2 on: Fri, Sep 11, 2009 »

 Grin Grin Grin
Yeah, it was written for Flash/Flixel savvy people.

Currently (v1.25) when you click on a FlxButton that calls FlxG.openURL, the browser's popup blocker can block your call.
This solve it (in my case at least).

I changed FlxCore/Layer in order to know in which layer the core is in (if ther core inside a layer, which also is a core, and itself is inside a layer... you could know that)

Why? To know if the all the owner-ladder is active/existing.
Usually this is not needed as update() is not called for inactive/non-existing cores, but as I'm adding an event listener that get's called regardless of the layer-tree, I use this method to check if the button actually should get updates.

This way you get to call FlxG.openURL from inside a mouse event, letting the browser think that the user was actually pressing something link-ish (as opposed to you popping up stuff because you want).

Kudos to Adam for the tip on that last part.

I hope this explains it a bit better, sorry for the autistic post before, I was really sleepy

Regards
-Martín
Logged

2BAM
I Hate Islands, SpaceCoffee, Finding Her, Caverns, Explosive Cats, The Duke, Run, A View on Relationships, RabbitClock's Quest
Pages: [1]
  Print  
 
Jump to: