UNPKG

starling-framework

Version:

A fast, productive library for 2D cross-platform development.

374 lines (357 loc) 14.3 kB
// Class: starling.display.Button var $global = typeof window != "undefined" ? window : typeof global != "undefined" ? global : typeof self != "undefined" ? self : this $global.Object.defineProperty(exports, "__esModule", {value: true}); var __map_reserved = {}; // Imports var $hxClasses = require("./../../hxClasses_stub").default; var $hxEnums = require("./../../hxEnums_stub").default; var $import = require("./../../import_stub").default; var $bind = require("./../../bind_stub").default; var $extend = require("./../../extend_stub").default; function starling_display_DisplayObjectContainer() {return require("./../../starling/display/DisplayObjectContainer");} function starling_text_TextField() {return require("./../../starling/text/TextField");} function Std() {return require("./../../Std");} function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");} function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));} function starling_display_Sprite() {return require("./../../starling/display/Sprite");} function starling_utils_ButtonBehavior() {return require("./../../starling/utils/ButtonBehavior");} function starling_utils_SystemUtil() {return require("./../../starling/utils/SystemUtil");} function starling_display_Image() {return require("./../../starling/display/Image");} function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));} // Constructor var Button = function(upState,text,downState,overState,disabledState) { if(text == null) { text = ""; } (starling_display_DisplayObjectContainer().default).call(this); if(upState == null) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Texture 'upState' cannot be null")); } this.__upState = upState; this.__downState = downState; this.__overState = overState; this.__disabledState = disabledState; this.__behavior = new (starling_utils_ButtonBehavior().default)(this,$bind(this,this.onStateChange),(starling_utils_SystemUtil().default).get_isDesktop() ? 16 : 44); this.__body = new (starling_display_Image().default)(upState); this.__body.set_pixelSnapping(true); this.__scaleWhenDown = downState != null ? 1.0 : 0.9; this.__scaleWhenOver = this.__alphaWhenDown = 1.0; this.__alphaWhenDisabled = disabledState != null ? 1.0 : 0.5; this.__textBounds = new (openfl_geom_Rectangle().default)(0,0,this.__body.get_width(),this.__body.get_height()); this.__contents = new (starling_display_Sprite().default)(); this.__contents.addChild(this.__body); this.addChild(this.__contents); this.__setStateTexture(upState); this.set_touchGroup(true); this.set_text(text); } // Meta Button.__name__ = "starling.display.Button"; Button.__isInterface__ = false; Button.__super__ = (starling_display_DisplayObjectContainer().default); Button.prototype = $extend((starling_display_DisplayObjectContainer().default).prototype, { dispose: function() { if(this.__textField != null) { this.__textField.dispose(); } (starling_display_DisplayObjectContainer().default).prototype.dispose.call(this); }, onStateChange: function(state) { this.__contents.set_x(this.__contents.set_y(0)); this.__contents.set_scaleX(this.__contents.set_scaleY(this.__contents.set_alpha(1.0))); switch(state) { case "disabled": this.__setStateTexture(this.__disabledState); this.__contents.set_alpha(this.__alphaWhenDisabled); break; case "down": this.__setStateTexture(this.__downState); this.__setContentScale(this.__scaleWhenDown); this.__contents.set_alpha(this.__alphaWhenDown); break; case "over": this.__setStateTexture(this.__overState); this.__setContentScale(this.__scaleWhenOver); break; case "up": this.__setStateTexture(this.__upState); break; } }, hitTest: function(localPoint) { return this.__behavior.hitTest(localPoint); }, readjustSize: function(resetTextBounds) { if(resetTextBounds == null) { resetTextBounds = true; } var prevWidth = this.__body.get_width(); var prevHeight = this.__body.get_height(); this.__body.readjustSize(); var scaleX = this.__body.get_width() / prevWidth; var scaleY = this.__body.get_height() / prevHeight; this.__textBounds.x *= scaleX; this.__textBounds.y *= scaleY; this.__textBounds.width *= scaleX; this.__textBounds.height *= scaleY; if(this.__textField != null) { this.__createTextField(); } }, __createTextField: function() { if(this.__textField == null) { this.__textField = new (starling_text_TextField().default)((Std().default).int(this.__textBounds.width),(Std().default).int(this.__textBounds.height)); this.__textField.set_pixelSnapping(this.__body.get_pixelSnapping()); this.__textField.set_touchable(false); this.__textField.set_autoScale(true); this.__textField.set_batchable(true); } this.__textField.set_width(this.__textBounds.width); this.__textField.set_height(this.__textBounds.height); this.__textField.set_x(this.__textBounds.x); this.__textField.set_y(this.__textBounds.y); }, get_state: function() { return this.__behavior.get_state(); }, set_state: function(value) { return this.__behavior.set_state(value); }, __setContentScale: function(scale) { this.__contents.set_scaleX(this.__contents.set_scaleY(scale)); this.__contents.set_x((1.0 - scale) / 2.0 * this.__body.get_width()); this.__contents.set_y((1.0 - scale) / 2.0 * this.__body.get_height()); }, __setStateTexture: function(texture) { this.__body.set_texture(texture != null ? texture : this.__upState); if(this.__body.get_pivotX() != 0 || this.__body.get_pivotY() != 0) { this.set_pivotX(this.__body.get_pivotX()); this.set_pivotY(this.__body.get_pivotY()); this.__body.set_pivotX(0); this.__body.set_pivotY(0); } }, get_scaleWhenDown: function() { return this.__scaleWhenDown; }, set_scaleWhenDown: function(value) { return this.__scaleWhenDown = value; }, get_scaleWhenOver: function() { return this.__scaleWhenOver; }, set_scaleWhenOver: function(value) { return this.__scaleWhenOver = value; }, get_alphaWhenDown: function() { return this.__alphaWhenDown; }, set_alphaWhenDown: function(value) { return this.__alphaWhenDown = value; }, get_alphaWhenDisabled: function() { return this.__alphaWhenDisabled; }, set_alphaWhenDisabled: function(value) { return this.__alphaWhenDisabled = value; }, get_enabled: function() { return this.__behavior.get_enabled(); }, set_enabled: function(value) { return this.__behavior.set_enabled(value); }, get_text: function() { if(this.__textField != null) { return this.__textField.get_text(); } else { return ""; } }, set_text: function(value) { if(value.length == 0) { if(this.__textField != null) { this.__textField.set_text(value); this.__textField.removeFromParent(); } } else { this.__createTextField(); this.__textField.set_text(value); if(this.__textField.get_parent() == null) { this.__contents.addChild(this.__textField); } } return value; }, get_textFormat: function() { if(this.__textField == null) { this.__createTextField(); } return this.__textField.get_format(); }, set_textFormat: function(value) { if(this.__textField == null) { this.__createTextField(); } return this.__textField.set_format(value); }, get_textStyle: function() { if(this.__textField == null) { this.__createTextField(); } return this.__textField.get_style(); }, set_textStyle: function(value) { if(this.__textField == null) { this.__createTextField(); } return this.__textField.set_style(value); }, get_style: function() { return this.__body.get_style(); }, set_style: function(value) { return this.__body.set_style(value); }, get_upState: function() { return this.__upState; }, set_upState: function(value) { if(value == null) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Texture 'upState' cannot be null")); } if(this.__upState != value) { this.__upState = value; var state = this.__behavior.get_state(); if(state == "up" || state == "disabled" && this.__disabledState == null || state == "down" && this.__downState == null || state == "over" && this.__overState == null) { this.__setStateTexture(value); } } return value; }, get_downState: function() { return this.__downState; }, set_downState: function(value) { if(this.__downState != value) { this.__downState = value; if(this.get_state() == "down") { this.__setStateTexture(value); } } return value; }, get_overState: function() { return this.__overState; }, set_overState: function(value) { if(this.__overState != value) { this.__overState = value; if(this.get_state() == "over") { this.__setStateTexture(value); } } return value; }, get_disabledState: function() { return this.__disabledState; }, set_disabledState: function(value) { if(this.__disabledState != value) { this.__disabledState = value; if(this.get_state() == "disabled") { this.__setStateTexture(value); } } return value; }, get_textBounds: function() { return this.__textBounds.clone(); }, set_textBounds: function(value) { this.__textBounds.copyFrom(value); this.__createTextField(); return value; }, get_color: function() { return this.__body.get_color(); }, set_color: function(value) { return this.__body.set_color(value); }, get_textureSmoothing: function() { return this.__body.get_textureSmoothing(); }, set_textureSmoothing: function(value) { return this.__body.set_textureSmoothing(value); }, get_overlay: function() { if(this.__overlay == null) { this.__overlay = new (starling_display_Sprite().default)(); } this.__contents.addChild(this.__overlay); return this.__overlay; }, get_useHandCursor: function() { return this.__behavior.get_useHandCursor(); }, set_useHandCursor: function(value) { return this.__behavior.set_useHandCursor(value); }, get_pixelSnapping: function() { return this.__body.get_pixelSnapping(); }, set_pixelSnapping: function(value) { this.__body.set_pixelSnapping(value); if(this.__textField != null) { this.__textField.set_pixelSnapping(value); } return value; }, set_width: function(value) { var newWidth = value / (this.get_scaleX() != 0 ? this.get_scaleX() : 1.0); var scale = newWidth / (this.__body.get_width() != 0 ? this.__body.get_width() : 1.0); this.__body.set_width(newWidth); this.__textBounds.x *= scale; this.__textBounds.width *= scale; if(this.__textField != null) { this.__textField.set_width(newWidth); } return value; }, set_height: function(value) { var newHeight = value / (this.get_scaleY() != 0 ? this.get_scaleY() : 1.0); var scale = newHeight / (this.__body.get_height() != 0 ? this.__body.get_height() : 1.0); this.__body.set_height(newHeight); this.__textBounds.y *= scale; this.__textBounds.height *= scale; if(this.__textField != null) { this.__textField.set_height(newHeight); } return value; }, get_scale9Grid: function() { return this.__body.get_scale9Grid(); }, set_scale9Grid: function(value) { return this.__body.set_scale9Grid(value); }, get_minHitAreaSize: function() { return this.__behavior.get_minHitAreaSize(); }, set_minHitAreaSize: function(value) { return this.__behavior.set_minHitAreaSize(value); }, get_abortDistance: function() { return this.__behavior.get_abortDistance(); }, set_abortDistance: function(value) { return this.__behavior.set_abortDistance(value); } }); Button.prototype.__class__ = Button.prototype.constructor = $hxClasses["starling.display.Button"] = Button; // Init Object.defineProperties(Button.prototype,{ scaleWhenDown : { get : function () { return this.get_scaleWhenDown (); }, set : function (v) { return this.set_scaleWhenDown (v); }}, scaleWhenOver : { get : function () { return this.get_scaleWhenOver (); }, set : function (v) { return this.set_scaleWhenOver (v); }}, alphaWhenDown : { get : function () { return this.get_alphaWhenDown (); }, set : function (v) { return this.set_alphaWhenDown (v); }}, alphaWhenDisabled : { get : function () { return this.get_alphaWhenDisabled (); }, set : function (v) { return this.set_alphaWhenDisabled (v); }}, text : { get : function () { return this.get_text (); }, set : function (v) { return this.set_text (v); }}, textFormat : { get : function () { return this.get_textFormat (); }, set : function (v) { return this.set_textFormat (v); }}, textStyle : { get : function () { return this.get_textStyle (); }, set : function (v) { return this.set_textStyle (v); }}, style : { get : function () { return this.get_style (); }, set : function (v) { return this.set_style (v); }}, upState : { get : function () { return this.get_upState (); }, set : function (v) { return this.set_upState (v); }}, downState : { get : function () { return this.get_downState (); }, set : function (v) { return this.set_downState (v); }}, overState : { get : function () { return this.get_overState (); }, set : function (v) { return this.set_overState (v); }}, disabledState : { get : function () { return this.get_disabledState (); }, set : function (v) { return this.set_disabledState (v); }}, textBounds : { get : function () { return this.get_textBounds (); }, set : function (v) { return this.set_textBounds (v); }}, color : { get : function () { return this.get_color (); }, set : function (v) { return this.set_color (v); }}, textureSmoothing : { get : function () { return this.get_textureSmoothing (); }, set : function (v) { return this.set_textureSmoothing (v); }}, overlay : { get : function () { return this.get_overlay (); }}, pixelSnapping : { get : function () { return this.get_pixelSnapping (); }, set : function (v) { return this.set_pixelSnapping (v); }}, scale9Grid : { get : function () { return this.get_scale9Grid (); }, set : function (v) { return this.set_scale9Grid (v); }}, minHitAreaSize : { get : function () { return this.get_minHitAreaSize (); }, set : function (v) { return this.set_minHitAreaSize (v); }}, abortDistance : { get : function () { return this.get_abortDistance (); }, set : function (v) { return this.set_abortDistance (v); }}}); // Statics // Export exports.default = Button;