starling-framework
Version:
A fast, productive library for 2D cross-platform development.
166 lines (150 loc) • 5.95 kB
JavaScript
// Class: starling.display.Quad
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_display_Mesh() {return require("./../../starling/display/Mesh");}
function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));}
function starling_utils_RectangleUtil() {return require("./../../starling/utils/RectangleUtil");}
function openfl_geom_Vector3D() {return $import(require("openfl/geom/Vector3D"));}
function openfl_geom_Matrix() {return $import(require("openfl/geom/Matrix"));}
function openfl_geom_Matrix3D() {return $import(require("openfl/geom/Matrix3D"));}
function starling_rendering_VertexData() {return require("./../../starling/rendering/VertexData");}
function starling_styles_MeshStyle() {return require("./../../starling/styles/MeshStyle");}
function starling_rendering_IndexData() {return require("./../../starling/rendering/IndexData");}
function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");}
function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));}
// Constructor
var Quad = function(width,height,color) {
if(color == null) {
color = 16777215;
}
this.__bounds = new (openfl_geom_Rectangle().default)(0,0,width,height);
var vertexData = new (starling_rendering_VertexData().default)((starling_styles_MeshStyle().default).VERTEX_FORMAT,4);
var indexData = new (starling_rendering_IndexData().default)(6);
(starling_display_Mesh().default).call(this,vertexData,indexData);
if(width == 0.0 || height == 0.0) {
throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid size: width and height must not be zero"));
}
this.__setupVertices();
this.set_color(color);
}
// Meta
Quad.__name__ = ["starling","display","Quad"];
Quad.__super__ = (starling_display_Mesh().default);
Quad.prototype = $extend((starling_display_Mesh().default).prototype, {
__setupVertices: function() {
var posAttr = "position";
var texAttr = "texCoords";
var texture = this.get_style().get_texture();
var vertexData = this.get_vertexData();
var indexData = this.get_indexData();
indexData.set_numIndices(0);
indexData.addQuad(0,1,2,3);
if(vertexData.get_numVertices() != 4) {
vertexData.set_numVertices(4);
vertexData.trim();
}
if(texture != null) {
texture.setupVertexPositions(vertexData,0,"position",this.__bounds);
texture.setupTextureCoordinates(vertexData,0,texAttr);
} else {
vertexData.setPoint(0,posAttr,this.__bounds.get_left(),this.__bounds.get_top());
vertexData.setPoint(1,posAttr,this.__bounds.get_right(),this.__bounds.get_top());
vertexData.setPoint(2,posAttr,this.__bounds.get_left(),this.__bounds.get_bottom());
vertexData.setPoint(3,posAttr,this.__bounds.get_right(),this.__bounds.get_bottom());
vertexData.setPoint(0,texAttr,0.0,0.0);
vertexData.setPoint(1,texAttr,1.0,0.0);
vertexData.setPoint(2,texAttr,0.0,1.0);
vertexData.setPoint(3,texAttr,1.0,1.0);
}
this.setRequiresRedraw();
},
getBounds: function(targetSpace,out) {
if(out == null) {
out = new (openfl_geom_Rectangle().default)();
}
if(targetSpace == this) {
out.copyFrom(this.__bounds);
} else if(targetSpace == this.get_parent() && !this.get_isRotated()) {
var scaleX = this.get_scaleX();
var scaleY = this.get_scaleY();
out.setTo(this.get_x() - this.get_pivotX() * scaleX,this.get_y() - this.get_pivotY() * scaleY,this.__bounds.width * scaleX,this.__bounds.height * scaleY);
if(scaleX < 0) {
out.width *= -1;
out.x -= out.width;
}
if(scaleY < 0) {
out.height *= -1;
out.y -= out.height;
}
} else if(this.get_is3D() && this.get_stage() != null) {
this.get_stage().getCameraPosition(targetSpace,Quad.sPoint3D);
this.getTransformationMatrix3D(targetSpace,Quad.sMatrix3D);
(starling_utils_RectangleUtil().default).getBoundsProjected(this.__bounds,Quad.sMatrix3D,Quad.sPoint3D,out);
} else {
this.getTransformationMatrix(targetSpace,Quad.sMatrix);
(starling_utils_RectangleUtil().default).getBounds(this.__bounds,Quad.sMatrix,out);
}
return out;
},
hitTest: function(localPoint) {
if(!this.get_visible() || !this.get_touchable() || !this.hitTestMask(localPoint)) {
return null;
} else if(this.__bounds.containsPoint(localPoint)) {
return this;
} else {
return null;
}
},
readjustSize: function(width,height) {
if(height == null) {
height = -1;
}
if(width == null) {
width = -1;
}
if(width <= 0) {
if(this.get_texture() != null) {
width = this.get_texture().get_frameWidth();
} else {
width = this.__bounds.width;
}
}
if(height <= 0) {
if(this.get_texture() != null) {
height = this.get_texture().get_frameHeight();
} else {
height = this.__bounds.height;
}
}
if(width != this.__bounds.width || height != this.__bounds.height) {
this.__bounds.setTo(0,0,width,height);
this.__setupVertices();
}
},
set_texture: function(value) {
if(value != this.get_texture()) {
(starling_display_Mesh().default).prototype.set_texture.call(this,value);
this.__setupVertices();
}
return value;
}
});
Quad.prototype.__class__ = Quad.prototype.constructor = $hxClasses["starling.display.Quad"] = Quad;
// Init
// Statics
Quad.fromTexture = function(texture) {
var quad = new Quad(100,100);
quad.set_texture(texture);
quad.readjustSize();
return quad;
}
Quad.sPoint3D = new (openfl_geom_Vector3D().default)()
Quad.sMatrix = new (openfl_geom_Matrix().default)()
Quad.sMatrix3D = new (openfl_geom_Matrix3D().default)()
// Export
exports.default = Quad;