/*
* 2016 Maciej Wiecierzewski
*/
/**
* @constructor
* @classdesc GUI's button
*/
function Button(textStr, font, textColor, buttonColor)
{
this.buttonColor = buttonColor;
this.clickEventListener = null;
this.text = new createjs.Text(textStr, font, textColor);
this.shape = new createjs.Shape();
this.container = new createjs.Container();
this.container.addChild(this.shape);
this.container.addChild(this.text);
}
/**
* Sets new size and updates 'shape' object
* @param {number} x
* @param {number} y
* @param {number} w
* @param {number} h
*/
Button.prototype.resize = function(x, y, w, h)
{
this.container.x = x;
this.container.y = y;
this.text.x = (w - this.text.getMeasuredWidth()) / 2;
this.text.y = (h - this.text.getMeasuredHeight()) / 3;
this.shape.graphics.clear();
this.shape.graphics.beginFill(this.buttonColor).drawRect(0, 0, w, h).endFill();
}
/** Hides the button */
Button.prototype.hide = function()
{
this.container.visible = false;
}
/** Shows the button */
Button.prototype.show = function()
{
this.container.visible = true;
}
/**
* Appents the Button to a parent container
* @param {Container} parentContainer CreateJS Container
*/
Button.prototype.appendTo = function(parentContainer)
{
parentContainer.addChild(this.container);
}
/**
* Sets event listener for the click event
* @param {function} eventListener An event listener function
*/
Button.prototype.onClick = function(eventListener)
{
if(this.clickEventListener !== null)
{
this.removeClick();
}
this.container.addEventListener("click", eventListener);
this.clickEventListener = eventListener;
}
/** Removes the event listener */
Button.prototype.removeClick = function()
{
this.container.removeEventListener("click", this.clickEventListener);
}