starling-framework
Version:
A fast, productive library for 2D cross-platform development.
430 lines (415 loc) • 18.1 kB
JavaScript
// Class: starling.display.Image
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 $extend = require("./../../extend_stub").default;
function starling_display_Quad() {return require("./../../starling/display/Quad");}
function starling_utils_MathUtil() {return require("./../../starling/utils/MathUtil");}
function starling_utils_Pool() {return require("./../../starling/utils/Pool");}
function starling_utils_RectangleUtil() {return require("./../../starling/utils/RectangleUtil");}
function openfl__$Vector_Vector_$Impl_$() {return require("./../../openfl/_Vector/Vector_Impl_");}
function Std() {return require("./../../Std");}
function haxe_ds_ObjectMap() {return require("./../../haxe/ds/ObjectMap");}
function starling_utils_Padding() {return require("./../../starling/utils/Padding");}
function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));}
function starling_display_SetupAutomator() {return require("./../../starling/display/SetupAutomator");}
// Constructor
var Image = function(texture) {
(starling_display_Quad().default).call(this,100,100);
this.set_texture(texture);
this.readjustSize();
}
// Meta
Image.__name__ = "starling.display.Image";
Image.__isInterface__ = false;
Image.__super__ = (starling_display_Quad().default);
Image.prototype = $extend((starling_display_Quad().default).prototype, {
get_scale9Grid: function() {
return this.__scale9Grid;
},
set_scale9Grid: function(value) {
if(value != null) {
if(this.__scale9Grid == null) {
this.__scale9Grid = value.clone();
} else {
this.__scale9Grid.copyFrom(value);
}
this.readjustSize();
this.__tileGrid = null;
} else {
this.__scale9Grid = null;
}
this.__setupVertices();
return value;
},
get_tileGrid: function() {
return this.__tileGrid;
},
set_tileGrid: function(value) {
if(value != null) {
if(this.__tileGrid == null) {
this.__tileGrid = value.clone();
} else {
this.__tileGrid.copyFrom(value);
}
this.__scale9Grid = null;
} else {
this.__tileGrid = null;
}
this.__setupVertices();
return value;
},
__setupVertices: function() {
if(this.get_texture() != null && this.__scale9Grid != null) {
this.__setupScale9Grid();
} else if(this.get_texture() != null && this.__tileGrid != null) {
this.__setupTileGrid();
} else {
(starling_display_Quad().default).prototype.__setupVertices.call(this);
}
},
set_scaleX: function(value) {
(starling_display_Quad().default).prototype.set_scaleX.call(this,value);
if(this.get_texture() != null && (this.__scale9Grid != null || this.__tileGrid != null)) {
this.__setupVertices();
}
return value;
},
set_scaleY: function(value) {
(starling_display_Quad().default).prototype.set_scaleY.call(this,value);
if(this.get_texture() != null && (this.__scale9Grid != null || this.__tileGrid != null)) {
this.__setupVertices();
}
return value;
},
set_texture: function(value) {
if(value != this.get_texture()) {
var setupAutomator = null;
if(this.get_texture() != null && Image.sAutomators.exists(this.get_texture())) {
setupAutomator = Image.sAutomators.get(this.get_texture());
if(setupAutomator.onRelease != null) {
setupAutomator.onRelease(this);
}
}
(starling_display_Quad().default).prototype.set_texture.call(this,value);
if(value != null && Image.sAutomators.exists(value)) {
setupAutomator = Image.sAutomators.get(value);
if(setupAutomator.onAssign != null) {
setupAutomator.onAssign(this);
}
} else if(this.__scale9Grid != null && value != null) {
this.readjustSize();
}
}
return value;
},
__setupScale9Grid: function() {
var texture = this.get_texture();
var frame = texture.get_frame();
var absScaleX = this.get_scaleX() > 0 ? this.get_scaleX() : -this.get_scaleX();
var absScaleY = this.get_scaleY() > 0 ? this.get_scaleY() : -this.get_scaleY();
if(absScaleX == 0.0 || absScaleY == 0) {
return;
}
if((starling_utils_MathUtil().default).isEquivalent(this.__scale9Grid.width,texture.get_frameWidth())) {
absScaleY /= absScaleX;
} else if((starling_utils_MathUtil().default).isEquivalent(this.__scale9Grid.height,texture.get_frameHeight())) {
absScaleX /= absScaleY;
}
var invScaleX = 1.0 / absScaleX;
var invScaleY = 1.0 / absScaleY;
var vertexData = this.get_vertexData();
var indexData = this.get_indexData();
var prevNumVertices = vertexData.get_numVertices();
var correction;
var gridCenter = (starling_utils_Pool().default).getRectangle();
var textureBounds = (starling_utils_Pool().default).getRectangle();
var pixelBounds = (starling_utils_Pool().default).getRectangle();
var intersection = (starling_utils_Pool().default).getRectangle();
gridCenter.copyFrom(this.__scale9Grid);
textureBounds.setTo(0,0,texture.get_frameWidth(),texture.get_frameHeight());
if(frame != null) {
pixelBounds.setTo(-frame.x,-frame.y,texture.get_width(),texture.get_height());
} else {
pixelBounds.copyFrom(textureBounds);
}
(starling_utils_RectangleUtil().default).intersect(gridCenter,pixelBounds,intersection);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasCols,0,(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasCols,2,0));
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasRows,0,(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasRows,2,0));
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasCols,1,intersection.width);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasRows,1,intersection.height);
if(pixelBounds.x < gridCenter.x) {
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasCols,0,gridCenter.x - pixelBounds.x);
}
if(pixelBounds.y < gridCenter.y) {
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasRows,0,gridCenter.y - pixelBounds.y);
}
if(pixelBounds.get_right() > gridCenter.get_right()) {
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasCols,2,pixelBounds.get_right() - gridCenter.get_right());
}
if(pixelBounds.get_bottom() > gridCenter.get_bottom()) {
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sBasRows,2,pixelBounds.get_bottom() - gridCenter.get_bottom());
}
if(pixelBounds.x < gridCenter.x) {
Image.sPadding.set_left(pixelBounds.x * invScaleX);
} else {
Image.sPadding.set_left(gridCenter.x * invScaleX + pixelBounds.x - gridCenter.x);
}
if(pixelBounds.get_right() > gridCenter.get_right()) {
Image.sPadding.set_right((textureBounds.width - pixelBounds.get_right()) * invScaleX);
} else {
Image.sPadding.set_right((textureBounds.width - gridCenter.get_right()) * invScaleX + gridCenter.get_right() - pixelBounds.get_right());
}
if(pixelBounds.y < gridCenter.y) {
Image.sPadding.set_top(pixelBounds.y * invScaleY);
} else {
Image.sPadding.set_top(gridCenter.y * invScaleY + pixelBounds.y - gridCenter.y);
}
if(pixelBounds.get_bottom() > gridCenter.get_bottom()) {
Image.sPadding.set_bottom((textureBounds.height - pixelBounds.get_bottom()) * invScaleY);
} else {
Image.sPadding.set_bottom((textureBounds.height - gridCenter.get_bottom()) * invScaleY + gridCenter.get_bottom() - pixelBounds.get_bottom());
}
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sPosCols,0,Image.sBasCols[0] * invScaleX);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sPosCols,2,Image.sBasCols[2] * invScaleX);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sPosCols,1,textureBounds.width - Image.sPadding.get_left() - Image.sPadding.get_right() - Image.sPosCols[0] - Image.sPosCols[2]);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sPosRows,0,Image.sBasRows[0] * invScaleY);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sPosRows,2,Image.sBasRows[2] * invScaleY);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sPosRows,1,textureBounds.height - Image.sPadding.get_top() - Image.sPadding.get_bottom() - Image.sPosRows[0] - Image.sPosRows[2]);
if(Image.sPosCols[1] <= 0) {
correction = textureBounds.width / (textureBounds.width - gridCenter.width) * absScaleX;
var _g = Image.sPadding;
_g.set_left(_g.get_left() * correction);
var _g1 = Image.sPosCols;
(openfl__$Vector_Vector_$Impl_$().default).set(_g1,0,_g1[0] * correction);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sPosCols,1,0.0);
var _g2 = Image.sPosCols;
(openfl__$Vector_Vector_$Impl_$().default).set(_g2,2,_g2[2] * correction);
}
if(Image.sPosRows[1] <= 0) {
correction = textureBounds.height / (textureBounds.height - gridCenter.height) * absScaleY;
var _g3 = Image.sPadding;
_g3.set_top(_g3.get_top() * correction);
var _g4 = Image.sPosRows;
(openfl__$Vector_Vector_$Impl_$().default).set(_g4,0,_g4[0] * correction);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sPosRows,1,0.0);
var _g5 = Image.sPosRows;
(openfl__$Vector_Vector_$Impl_$().default).set(_g5,2,_g5[2] * correction);
}
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sTexCols,0,Image.sBasCols[0] / pixelBounds.width);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sTexCols,2,Image.sBasCols[2] / pixelBounds.width);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sTexCols,1,1.0 - Image.sTexCols[0] - Image.sTexCols[2]);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sTexRows,0,Image.sBasRows[0] / pixelBounds.height);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sTexRows,2,Image.sBasRows[2] / pixelBounds.height);
(openfl__$Vector_Vector_$Impl_$().default).set(Image.sTexRows,1,1.0 - Image.sTexRows[0] - Image.sTexRows[2]);
var numVertices = this.__setupScale9GridAttributes(Image.sPadding.get_left(),Image.sPadding.get_top(),Image.sPosCols,Image.sPosRows,Image.sTexCols,Image.sTexRows);
var numQuads = (Std().default).int(numVertices / 4);
vertexData.set_numVertices(numVertices);
indexData.set_numIndices(0);
var _g6 = 0;
var _g11 = numQuads;
while(_g6 < _g11) {
var i = _g6++;
indexData.addQuad(i * 4,i * 4 + 1,i * 4 + 2,i * 4 + 3);
}
if(numVertices != prevNumVertices) {
var color = prevNumVertices != 0 ? vertexData.getColor(0) : 16777215;
var alpha = prevNumVertices != 0 ? vertexData.getAlpha(0) : 1.0;
vertexData.colorize("color",color,alpha);
}
(starling_utils_Pool().default).putRectangle(textureBounds);
(starling_utils_Pool().default).putRectangle(pixelBounds);
(starling_utils_Pool().default).putRectangle(gridCenter);
(starling_utils_Pool().default).putRectangle(intersection);
this.setRequiresRedraw();
},
__setupScale9GridAttributes: function(startX,startY,posCols,posRows,texCols,texRows) {
var posAttr = "position";
var texAttr = "texCoords";
var row;
var col;
var colWidthPos;
var rowHeightPos;
var colWidthTex;
var rowHeightTex;
var vertexData = this.get_vertexData();
var texture = this.get_texture();
var currentX = startX;
var currentY = startY;
var currentU = 0.0;
var currentV = 0.0;
var vertexID = 0;
var _g = 0;
while(_g < 3) {
var row1 = _g++;
rowHeightPos = posRows[row1];
rowHeightTex = texRows[row1];
if(rowHeightPos > 0) {
var _g1 = 0;
while(_g1 < 3) {
var col1 = _g1++;
colWidthPos = posCols[col1];
colWidthTex = texCols[col1];
if(colWidthPos > 0) {
vertexData.setPoint(vertexID,posAttr,currentX,currentY);
texture.setTexCoords(vertexData,vertexID,texAttr,currentU,currentV);
++vertexID;
vertexData.setPoint(vertexID,posAttr,currentX + colWidthPos,currentY);
texture.setTexCoords(vertexData,vertexID,texAttr,currentU + colWidthTex,currentV);
++vertexID;
vertexData.setPoint(vertexID,posAttr,currentX,currentY + rowHeightPos);
texture.setTexCoords(vertexData,vertexID,texAttr,currentU,currentV + rowHeightTex);
++vertexID;
vertexData.setPoint(vertexID,posAttr,currentX + colWidthPos,currentY + rowHeightPos);
texture.setTexCoords(vertexData,vertexID,texAttr,currentU + colWidthTex,currentV + rowHeightTex);
++vertexID;
currentX += colWidthPos;
}
currentU += colWidthTex;
}
currentY += rowHeightPos;
}
currentX = startX;
currentU = 0.0;
currentV += rowHeightTex;
}
return vertexID;
},
__setupTileGrid: function() {
var texture = this.get_texture();
var frame = texture.get_frame();
var vertexData = this.get_vertexData();
var indexData = this.get_indexData();
var bounds = this.getBounds(this,Image.sBounds);
var prevNumVertices = vertexData.get_numVertices();
var color = prevNumVertices != 0 ? vertexData.getColor(0) : 16777215;
var alpha = prevNumVertices != 0 ? vertexData.getAlpha(0) : 1.0;
var invScaleX = this.get_scaleX() > 0 ? 1.0 / this.get_scaleX() : -1.0 / this.get_scaleX();
var invScaleY = this.get_scaleY() > 0 ? 1.0 / this.get_scaleY() : -1.0 / this.get_scaleY();
var frameWidth = this.__tileGrid.width > 0 ? this.__tileGrid.width : texture.get_frameWidth();
var frameHeight = this.__tileGrid.height > 0 ? this.__tileGrid.height : texture.get_frameHeight();
frameWidth *= invScaleX;
frameHeight *= invScaleY;
var tileX = frame != null ? -frame.x * (frameWidth / frame.width) : 0;
var tileY = frame != null ? -frame.y * (frameHeight / frame.height) : 0;
var tileWidth = texture.get_width() * (frameWidth / texture.get_frameWidth());
var tileHeight = texture.get_height() * (frameHeight / texture.get_frameHeight());
var modX = this.__tileGrid.x * invScaleX % frameWidth;
var modY = this.__tileGrid.y * invScaleY % frameHeight;
if(modX < 0) {
modX += frameWidth;
}
if(modY < 0) {
modY += frameHeight;
}
var startX = modX + tileX;
var startY = modY + tileY;
if(startX > frameWidth - tileWidth) {
startX -= frameWidth;
}
if(startY > frameHeight - tileHeight) {
startY -= frameHeight;
}
var posLeft;
var posRight;
var posTop;
var posBottom;
var texLeft;
var texRight;
var texTop;
var texBottom;
var posAttrName = "position";
var texAttrName = "texCoords";
var currentX;
var currentY = startY;
var vertexID = 0;
indexData.set_numIndices(0);
while(currentY < bounds.height) {
currentX = startX;
while(currentX < bounds.width) {
indexData.addQuad(vertexID,vertexID + 1,vertexID + 2,vertexID + 3);
posLeft = currentX < 0 ? 0 : currentX;
posTop = currentY < 0 ? 0 : currentY;
posRight = currentX + tileWidth > bounds.width ? bounds.width : currentX + tileWidth;
posBottom = currentY + tileHeight > bounds.height ? bounds.height : currentY + tileHeight;
vertexData.setPoint(vertexID,posAttrName,posLeft,posTop);
vertexData.setPoint(vertexID + 1,posAttrName,posRight,posTop);
vertexData.setPoint(vertexID + 2,posAttrName,posLeft,posBottom);
vertexData.setPoint(vertexID + 3,posAttrName,posRight,posBottom);
texLeft = (posLeft - currentX) / tileWidth;
texTop = (posTop - currentY) / tileHeight;
texRight = (posRight - currentX) / tileWidth;
texBottom = (posBottom - currentY) / tileHeight;
texture.setTexCoords(vertexData,vertexID,texAttrName,texLeft,texTop);
texture.setTexCoords(vertexData,vertexID + 1,texAttrName,texRight,texTop);
texture.setTexCoords(vertexData,vertexID + 2,texAttrName,texLeft,texBottom);
texture.setTexCoords(vertexData,vertexID + 3,texAttrName,texRight,texBottom);
currentX += frameWidth;
vertexID += 4;
}
currentY += frameHeight;
}
vertexData.set_numVertices(vertexID);
var _g = prevNumVertices;
var _g1 = vertexID;
while(_g < _g1) {
var i = _g++;
vertexData.setColor(i,"color",color);
vertexData.setAlpha(i,"color",alpha);
}
this.setRequiresRedraw();
}
});
Image.prototype.__class__ = Image.prototype.constructor = $hxClasses["starling.display.Image"] = Image;
// Init
Object.defineProperties(Image.prototype,{ scale9Grid : { get : function () { return this.get_scale9Grid (); }, set : function (v) { return this.set_scale9Grid (v); }}, tileGrid : { get : function () { return this.get_tileGrid (); }, set : function (v) { return this.set_tileGrid (v); }}});
// Statics
Image.automateSetupForTexture = function(texture,onAssign,onRelease) {
var automator = Image.sAutomators.get(texture);
if(automator != null) {
automator.add(onAssign,onRelease);
} else {
var this1 = Image.sAutomators;
var v = new (starling_display_SetupAutomator().default)(onAssign,onRelease);
this1.set(texture,v);
}
}
Image.resetSetupForTexture = function(texture) {
Image.sAutomators.remove(texture);
}
Image.removeSetupForTexture = function(texture,onAssign,onRelease) {
var automator = Image.sAutomators.get(texture);
if(automator != null) {
automator.remove(onAssign,onRelease);
}
}
Image.bindScale9GridToTexture = function(texture,scale9Grid) {
Image.automateSetupForTexture(texture,function(image) {
image.set_scale9Grid(scale9Grid);
},function(image1) {
image1.set_scale9Grid(null);
});
}
Image.bindPivotPointToTexture = function(texture,pivotX,pivotY) {
Image.automateSetupForTexture(texture,function(image) {
image.set_pivotX(pivotX);
image.set_pivotY(pivotY);
},function(image1) {
image1.set_pivotX(image1.set_pivotY(0));
});
}
Image.sAutomators = new (haxe_ds_ObjectMap().default)()
Image.sPadding = new (starling_utils_Padding().default)()
Image.sBounds = new (openfl_geom_Rectangle().default)()
Image.sBasCols = (openfl__$Vector_Vector_$Impl_$().default)._new(3,true)
Image.sBasRows = (openfl__$Vector_Vector_$Impl_$().default)._new(3,true)
Image.sPosCols = (openfl__$Vector_Vector_$Impl_$().default)._new(3,true)
Image.sPosRows = (openfl__$Vector_Vector_$Impl_$().default)._new(3,true)
Image.sTexCols = (openfl__$Vector_Vector_$Impl_$().default)._new(3,true)
Image.sTexRows = (openfl__$Vector_Vector_$Impl_$().default)._new(3,true)
// Export
exports.default = Image;