starling-framework
Version:
A fast, productive library for 2D cross-platform development.
353 lines (337 loc) • 13.3 kB
JavaScript
// Class: starling.styles.DistanceFieldStyle
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 $import = require("./../../import_stub").default;
var $extend = require("./../../extend_stub").default;
function starling_styles_MeshStyle() {return require("./../../starling/styles/MeshStyle");}
function Std() {return require("./../../Std");}
function starling_styles_DistanceFieldEffect() {return require("./../../starling/styles/DistanceFieldEffect");}
function starling_utils_Color() {return require("./../../starling/utils/Color");}
function starling_utils_MathUtil() {return require("./../../starling/utils/MathUtil");}
function js_Boot() {return require("./../../js/Boot");}
function _$UInt_UInt_$Impl_$() {return require("./../../_UInt/UInt_Impl_");}
function starling_core_Starling() {return require("./../../starling/core/Starling");}
// Constructor
var DistanceFieldStyle = function(softness,threshold) {
if(threshold == null) {
threshold = 0.5;
}
if(softness == null) {
softness = 0.125;
}
(starling_styles_MeshStyle().default).call(this);
this._mode = "basic";
this._threshold = threshold;
this._softness = softness;
this._alpha = 1.0;
this._outerThreshold = this._outerAlphaEnd = 0.0;
this._shadowOffsetX = this._shadowOffsetY = 0.0;
this._outerColor = 0;
this._outerAlphaStart = 0.0;
}
// Meta
DistanceFieldStyle.__name__ = ["starling","styles","DistanceFieldStyle"];
DistanceFieldStyle.__super__ = (starling_styles_MeshStyle().default);
DistanceFieldStyle.prototype = $extend((starling_styles_MeshStyle().default).prototype, {
copyFrom: function(meshStyle) {
var otherStyle = (Std().default)["is"](meshStyle,DistanceFieldStyle) ? meshStyle : null;
if(otherStyle != null) {
this._mode = otherStyle._mode;
this._multiChannel = otherStyle._multiChannel;
this._threshold = otherStyle._threshold;
this._softness = otherStyle._softness;
this._alpha = otherStyle._alpha;
this._outerThreshold = otherStyle._outerThreshold;
this._outerAlphaEnd = otherStyle._outerAlphaEnd;
this._shadowOffsetX = otherStyle._shadowOffsetX;
this._shadowOffsetY = otherStyle._shadowOffsetY;
this._outerColor = otherStyle._outerColor;
this._outerAlphaStart = otherStyle._outerAlphaStart;
}
(starling_styles_MeshStyle().default).prototype.copyFrom.call(this,meshStyle);
},
createEffect: function() {
return new (starling_styles_DistanceFieldEffect().default)();
},
get_vertexFormat: function() {
return DistanceFieldStyle.VERTEX_FORMAT;
},
onTargetAssigned: function(target) {
this.updateVertices();
},
updateVertices: function() {
if(this.get_vertexData() == null) {
return;
}
var numVertices = this.get_vertexData().get_numVertices();
var maxScale = 8;
var maxOuterOffset = 8;
var encodedOuterOffsetX = (this._shadowOffsetX + maxOuterOffset) / (2 * maxOuterOffset);
var encodedOuterOffsetY = (this._shadowOffsetY + maxOuterOffset) / (2 * maxOuterOffset);
var basic = (Std().default)["int"](this._threshold * 255) | (Std().default)["int"](this._alpha * 255) << 8 | (Std().default)["int"](this._softness / 2.0 * 255) << 16 | (Std().default)["int"](1.0 / maxScale * 255) << 24;
var extended = (Std().default)["int"](this._outerThreshold * 255) | (Std().default)["int"](this._outerAlphaEnd * 255) << 8 | (Std().default)["int"](encodedOuterOffsetX * 255) << 16 | (Std().default)["int"](encodedOuterOffsetY * 255) << 24;
var outerColor = (starling_utils_Color().default).getRed(this._outerColor) | (starling_utils_Color().default).getGreen(this._outerColor) << 8 | (starling_utils_Color().default).getBlue(this._outerColor) << 16 | (Std().default)["int"](this._outerAlphaStart * 255) << 24;
var _g1 = 0;
var _g = numVertices;
while(_g1 < _g) {
var i = _g1++;
this.get_vertexData().setUnsignedInt(i,"basic",basic);
this.get_vertexData().setUnsignedInt(i,"extended",extended);
this.get_vertexData().setUnsignedInt(i,"outerColor",outerColor);
}
this.setVertexDataChanged();
},
batchVertexData: function(targetStyle,targetVertexID,matrix,vertexID,numVertices) {
if(numVertices == null) {
numVertices = -1;
}
if(vertexID == null) {
vertexID = 0;
}
if(targetVertexID == null) {
targetVertexID = 0;
}
(starling_styles_MeshStyle().default).prototype.batchVertexData.call(this,targetStyle,targetVertexID,matrix,vertexID,numVertices);
if(matrix != null) {
var scale = Math.sqrt(matrix.a * matrix.a + matrix.c * matrix.c);
if(!(starling_utils_MathUtil().default).isEquivalent(scale,1.0,0.01)) {
var targetVertexData = ((js_Boot().default).__cast(targetStyle , DistanceFieldStyle)).get_vertexData();
var maxScale = 8;
var minScale = maxScale / 255;
if(numVertices < 0) {
numVertices = this.get_vertexData().get_numVertices() - vertexID;
}
var _g1 = 0;
var _g = numVertices;
while(_g1 < _g) {
var i = _g1++;
var srcAttr = this.get_vertexData().getUnsignedInt(vertexID + i,"basic");
var srcScale = (_$UInt_UInt_$Impl_$().default).toFloat(srcAttr >>> 24 & 255) / 255.0 * maxScale;
var tgtScale = (starling_utils_MathUtil().default).clamp(srcScale * scale,minScale,maxScale);
var tgtAttr = srcAttr & 16777215 | (Std().default)["int"](tgtScale / maxScale * 255) << 24;
targetVertexData.setUnsignedInt(targetVertexID + i,"basic",tgtAttr);
}
}
}
},
updateEffect: function(effect,state) {
var dfEffect = effect;
dfEffect.set_mode(this._mode);
dfEffect.set_multiChannel(this._multiChannel);
if(state.get_is3D()) {
dfEffect.set_scale(1.0);
} else {
var matrix = state.get_modelviewMatrix();
var scale = Math.sqrt(matrix.a * matrix.a + matrix.c * matrix.c);
dfEffect.set_scale(scale * (starling_core_Starling().default).get_current().get_contentScaleFactor());
}
(starling_styles_MeshStyle().default).prototype.updateEffect.call(this,effect,state);
},
canBatchWith: function(meshStyle) {
var dfStyle = (Std().default)["is"](meshStyle,DistanceFieldStyle) ? meshStyle : null;
if(dfStyle != null && (starling_styles_MeshStyle().default).prototype.canBatchWith.call(this,meshStyle)) {
if(dfStyle._mode == this._mode) {
return dfStyle._multiChannel == this._multiChannel;
} else {
return false;
}
} else {
return false;
}
},
setupBasic: function() {
this._mode = "basic";
this.setRequiresRedraw();
},
setupOutline: function(width,color,alpha) {
if(alpha == null) {
alpha = 1.0;
}
if(color == null) {
color = 0;
}
if(width == null) {
width = 0.25;
}
this._mode = "outline";
this._outerThreshold = (starling_utils_MathUtil().default).clamp(this._threshold - width,0,this._threshold);
this._outerColor = color;
this._outerAlphaStart = this._outerAlphaEnd = (starling_utils_MathUtil().default).clamp(alpha,0,1);
this._shadowOffsetX = this._shadowOffsetY = 0.0;
this.updateVertices();
},
setupGlow: function(blur,color,alpha) {
if(alpha == null) {
alpha = 0.5;
}
if(color == null) {
color = 16776960;
}
if(blur == null) {
blur = 0.2;
}
this._mode = "glow";
this._outerThreshold = (starling_utils_MathUtil().default).clamp(this._threshold - blur,0,this._threshold);
this._outerColor = color;
this._outerAlphaStart = (starling_utils_MathUtil().default).clamp(alpha,0,1);
this._outerAlphaEnd = 0.0;
this._shadowOffsetX = this._shadowOffsetY = 0.0;
this.updateVertices();
},
setupDropShadow: function(blur,offsetX,offsetY,color,alpha) {
if(alpha == null) {
alpha = 0.5;
}
if(color == null) {
color = 0;
}
if(offsetY == null) {
offsetY = 2;
}
if(offsetX == null) {
offsetX = 2;
}
if(blur == null) {
blur = 0.2;
}
var maxOffset = 8;
this._mode = "shadow";
this._outerThreshold = (starling_utils_MathUtil().default).clamp(this._threshold - blur,0,this._threshold);
this._outerColor = color;
this._outerAlphaStart = (starling_utils_MathUtil().default).clamp(alpha,0,1);
this._outerAlphaEnd = 0.0;
this._shadowOffsetX = (starling_utils_MathUtil().default).clamp(offsetX,-maxOffset,maxOffset);
this._shadowOffsetY = (starling_utils_MathUtil().default).clamp(offsetY,-maxOffset,maxOffset);
this.updateVertices();
},
get_mode: function() {
return this._mode;
},
set_mode: function(value) {
this._mode = value;
this.setRequiresRedraw();
return value;
},
get_multiChannel: function() {
return this._multiChannel;
},
set_multiChannel: function(value) {
this._multiChannel = value;
this.setRequiresRedraw();
return value;
},
get_threshold: function() {
return this._threshold;
},
set_threshold: function(value) {
value = (starling_utils_MathUtil().default).clamp(value,0,1);
if(this._threshold != value) {
this._threshold = value;
this.updateVertices();
}
return value;
},
get_softness: function() {
return this._softness;
},
set_softness: function(value) {
value = (starling_utils_MathUtil().default).clamp(value,0,1);
if(this._softness != value) {
this._softness = value;
this.updateVertices();
}
return value;
},
get_alpha: function() {
return this._alpha;
},
set_alpha: function(value) {
value = (starling_utils_MathUtil().default).clamp(value,0,1);
if(this._alpha != value) {
this._alpha = value;
this.updateVertices();
}
return value;
},
get_outerThreshold: function() {
return this._outerThreshold;
},
set_outerThreshold: function(value) {
value = (starling_utils_MathUtil().default).clamp(value,0,1);
if(this._outerThreshold != value) {
this._outerThreshold = value;
this.updateVertices();
}
return value;
},
get_outerAlphaStart: function() {
return this._outerAlphaStart;
},
set_outerAlphaStart: function(value) {
value = (starling_utils_MathUtil().default).clamp(value,0,1);
if(this._outerAlphaStart != value) {
this._outerAlphaStart = value;
this.updateVertices();
}
return value;
},
get_outerAlphaEnd: function() {
return this._outerAlphaEnd;
},
set_outerAlphaEnd: function(value) {
value = (starling_utils_MathUtil().default).clamp(value,0,1);
if(this._outerAlphaEnd != value) {
this._outerAlphaEnd = value;
this.updateVertices();
}
return value;
},
get_outerColor: function() {
return this._outerColor;
},
set_outerColor: function(value) {
if(this._outerColor != value) {
this._outerColor = value;
this.updateVertices();
}
return value;
},
get_shadowOffsetX: function() {
return this._shadowOffsetX;
},
set_shadowOffsetX: function(value) {
var max = 8;
value = (starling_utils_MathUtil().default).clamp(value,-max,max);
if(this._shadowOffsetX != value) {
this._shadowOffsetX = value;
this.updateVertices();
}
return value;
},
get_shadowOffsetY: function() {
return this._shadowOffsetY;
},
set_shadowOffsetY: function(value) {
var max = 8;
value = (starling_utils_MathUtil().default).clamp(value,-max,max);
if(this._shadowOffsetY != value) {
this._shadowOffsetY = value;
this.updateVertices();
}
return value;
}
});
DistanceFieldStyle.prototype.__class__ = DistanceFieldStyle.prototype.constructor = $hxClasses["starling.styles.DistanceFieldStyle"] = DistanceFieldStyle;
// Init
Object.defineProperties(DistanceFieldStyle.prototype,{ "mode" : { get : function () { return this.get_mode (); }, set : function (v) { return this.set_mode (v); }}, "multiChannel" : { get : function () { return this.get_multiChannel (); }, set : function (v) { return this.set_multiChannel (v); }}, "threshold" : { get : function () { return this.get_threshold (); }, set : function (v) { return this.set_threshold (v); }}, "softness" : { get : function () { return this.get_softness (); }, set : function (v) { return this.set_softness (v); }}, "alpha" : { get : function () { return this.get_alpha (); }, set : function (v) { return this.set_alpha (v); }}, "outerThreshold" : { get : function () { return this.get_outerThreshold (); }, set : function (v) { return this.set_outerThreshold (v); }}, "outerAlphaStart" : { get : function () { return this.get_outerAlphaStart (); }, set : function (v) { return this.set_outerAlphaStart (v); }}, "outerAlphaEnd" : { get : function () { return this.get_outerAlphaEnd (); }, set : function (v) { return this.set_outerAlphaEnd (v); }}, "outerColor" : { get : function () { return this.get_outerColor (); }, set : function (v) { return this.set_outerColor (v); }}, "shadowOffsetX" : { get : function () { return this.get_shadowOffsetX (); }, set : function (v) { return this.set_shadowOffsetX (v); }}, "shadowOffsetY" : { get : function () { return this.get_shadowOffsetY (); }, set : function (v) { return this.set_shadowOffsetY (v); }}});
// Statics
DistanceFieldStyle.VERTEX_FORMAT = (starling_styles_MeshStyle().default).VERTEX_FORMAT.extend("basic:bytes4, extended:bytes4, outerColor:bytes4")
DistanceFieldStyle.MODE_BASIC = "basic"
DistanceFieldStyle.MODE_OUTLINE = "outline"
DistanceFieldStyle.MODE_GLOW = "glow"
DistanceFieldStyle.MODE_SHADOW = "shadow"
// Export
exports.default = DistanceFieldStyle;