Skip to content

Instantly share code, notes, and snippets.

@foxinni
Last active August 29, 2015 14:26
Show Gist options
  • Save foxinni/5f0236cb5d80d20ed558 to your computer and use it in GitHub Desktop.
Save foxinni/5f0236cb5d80d20ed558 to your computer and use it in GitHub Desktop.
HaXe Luxe Button Example with Hover and Click
import luxe.Input;
import luxe.Sprite;
import luxe.Color;
import luxe.Component;
import luxe.tween.Actuate;
import luxe.Vector;
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
class Main extends luxe.Game {
var icon : Sprite;
var hover : HoverAlpha;
override function ready() {
icon = new Sprite({
name : "button",
pos : new Vector(0,0),
color : new Color().rgb(0xffffff),
size : new Vector(160,160)
});
//enable it , keep the reference for later
hover = icon.add(new HoverAlpha());
} //ready
//use it for mouseclick
override function onmousedown( event:MouseEvent ) {
if(hover.hovered) {
trace('clicked');
}
}
override function onkeyup( e:KeyEvent ) {
if(e.keycode == Key.escape) {
Luxe.shutdown();
}
} //onkeyup
override function update(dt:Float) {
} //update
} //Main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment