Created
November 7, 2018 01:23
-
-
Save roipeker/5f4cd654fe35bd0bbf240b6612d346ca to your computer and use it in GitHub Desktop.
Custom TabBar for Feathers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================================================================================= | |
// | |
// Created by Rodrigo Lopez [roipeker™] on 06/11/2018. | |
// | |
// ================================================================================================= | |
package { | |
import feathers.controls.TabBar; | |
import feathers.controls.ToggleButton; | |
import flash.geom.Rectangle; | |
import starling.animation.Transitions; | |
import starling.core.Starling; | |
import starling.display.DisplayObject; | |
import starling.display.Quad; | |
public class AnimatedTabBar extends TabBar { | |
// helper Rectangle. | |
protected static const RECT:Rectangle = new Rectangle(); | |
private var _bar:Quad; | |
private var bg:Quad; | |
public var animate:Boolean = true; | |
public function AnimatedTabBar() { | |
super(); | |
} | |
override protected function initialize():void { | |
super.initialize(); | |
bg = new Quad(4, 4, 0x444444); | |
_bar = new Quad(4, 2, 0xffffff); | |
// no buttons will be selected, so hide bar. | |
showSelected(false); | |
addChild(bg); | |
addChild(_bar); | |
if (_selectedIndex != -1) { | |
// 1 frame delay, waiting feathers to finish tabs UI invalidation. | |
Starling.juggler.delayCall(showSelected, 0, false); | |
} | |
} | |
override protected function refreshTabs(isFactoryInvalid:Boolean):void { | |
super.refreshTabs(isFactoryInvalid); | |
addChild(_bar); | |
} | |
override protected function draw():void { | |
super.draw(); | |
if (isInvalid(INVALIDATION_FLAG_SIZE)) { | |
bg.readjustSize(actualWidth, actualHeight); | |
_bar.y = actualHeight - _bar.height - 2; | |
} | |
} | |
override protected function commitSelection():void { | |
super.commitSelection(); | |
// this will be called before the delay, but the tab will have no children built, | |
// so it will exit the method. | |
showSelected(animate); | |
} | |
private function showSelected(doAnimate:Boolean):void { | |
var tab:ToggleButton = toggleGroup.selectedItem as ToggleButton; | |
Starling.juggler.removeTweens(_bar); | |
if (!tab) { | |
// hide effect? | |
_bar.width = _bar.x = 0; | |
_bar.visible = false; | |
} else { | |
_bar.visible = true; | |
if (tab.numChildren == 0) { | |
trace("Tab buttons requires children."); | |
return; | |
} | |
// not the most performant approach, but works. | |
// Choose by default icon or label, whatever is available first. | |
var obj:DisplayObject = tab.getChildAt(tab.numChildren - 1); | |
obj.getBounds(tab.parent, RECT); | |
Starling.juggler.tween(_bar, doAnimate ? .25 : 0, { | |
x: RECT.x, width: RECT.width, | |
transition: Transitions.EASE_IN_OUT | |
}); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment