Last active
August 29, 2015 14:12
-
-
Save ruby0x1/2c53d1d0bf9b9732c38b to your computer and use it in GitHub Desktop.
Basic hover alpha button component
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
import luxe.Input; | |
import luxe.Sprite; | |
import luxe.Color; | |
import luxe.Component; | |
import luxe.tween.Actuate; | |
class HoverAlpha extends Component { | |
public var amount : Float = 0.3; | |
public var start : Float = 1; | |
public var active : Bool = true; | |
public var speed : Float = 0.1; | |
public var hovered : Bool = false; | |
var sprite:Sprite; | |
public function new() { | |
super({ name:'hover' }); | |
} | |
override function init() { | |
} //init | |
override function onadded() { | |
sprite = cast entity; | |
} | |
override function onmousemove( event:MouseEvent ) { | |
if(!active) { | |
return; | |
} | |
if(sprite.point_inside_AABB(event.pos)) { | |
if(!hovered) { | |
hover(); | |
} | |
} else { | |
if(hovered) { | |
unhover(); | |
} | |
} //point inside | |
} //mousemove | |
public function hover() { | |
hovered = true; | |
sprite.color.tween(speed, { a:start-amount }); | |
} //hover | |
public function unhover() { | |
hovered = false; | |
sprite.color.tween(speed, { a:start }); | |
} //unhover | |
} //HoverAlpha |
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
var icon = new Sprite({ | |
... | |
}); | |
//enable it , keep the reference for later | |
var hover = icon.add(new HoverAlpha()); | |
//or you can fetch it later, after add | |
var hover = icon.get('hover'); | |
//disable it | |
icon.remove('hover'); | |
//use it for mouseclick | |
override function onmousedown( event:MouseEvent ) { | |
if(hover.hovered) { | |
//clicked | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment