starling-framework
Version:
A fast, productive library for 2D cross-platform development.
149 lines (131 loc) • 4.4 kB
JavaScript
// Class: starling.geom.RoundRectangle
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_geom_ImmutablePolygon() {return require("./../../starling/geom/ImmutablePolygon");}
function starling_rendering_IndexData() {return require("./../../starling/rendering/IndexData");}
// Constructor
var RoundRectangle = function(x,y,width,height,radiusX,radiusY) {
this.__x = x;
this.__y = y;
this.__width = width;
this.__height = height;
this.__radiusX = radiusX;
this.__radiusY = radiusY != radiusY ? radiusX : radiusY;
(starling_geom_ImmutablePolygon().default).call(this,this.getVertices());
}
// Meta
RoundRectangle.__name__ = "starling.geom.RoundRectangle";
RoundRectangle.__isInterface__ = false;
RoundRectangle.__super__ = (starling_geom_ImmutablePolygon().default);
RoundRectangle.prototype = $extend((starling_geom_ImmutablePolygon().default).prototype, {
getNumVerticesPerCorner: function() {
var numVerticesPerCorner = Math.ceil(Math.PI * (this.__radiusX + this.__radiusY) / 16.0);
if(numVerticesPerCorner < 3) {
numVerticesPerCorner = 3;
}
return numVerticesPerCorner;
},
getVertices: function() {
var numVerticesPerCorner = this.getNumVerticesPerCorner();
var numVertices = numVerticesPerCorner * 4;
var vertices = [];
var angleDelta = Math.PI / 2.0 / (numVerticesPerCorner - 1);
var angle = 0.0;
var offsetX = this.__width - this.__radiusX - this.__radiusX;
var offsetY = this.__height - this.__radiusY - this.__radiusY;
var horizontal = true;
var j = 0;
var len = numVerticesPerCorner;
var _g = 0;
while(_g < 4) {
var i = _g++;
while(j < len) {
vertices[j * 2] = offsetX + Math.cos(angle) * this.__radiusX + this.__x + this.__radiusX;
vertices[j * 2 + 1] = offsetY + Math.sin(angle) * this.__radiusY + this.__y + this.__radiusY;
angle += angleDelta;
++j;
}
angle -= angleDelta;
if(horizontal) {
if(offsetX == 0.0) {
offsetX = this.__width - this.__radiusX - this.__radiusX;
} else {
offsetX = 0.0;
}
} else if(offsetY == 0.0) {
offsetY = this.__height - this.__radiusY - this.__radiusY;
} else {
offsetY = 0.0;
}
horizontal = !horizontal;
len += numVerticesPerCorner;
}
return vertices;
},
triangulate: function(indexData,offset) {
if(offset == null) {
offset = 0;
}
var numVerticesPerCorner = this.getNumVerticesPerCorner();
var numVertices = numVerticesPerCorner * 4;
var numTriangles = numVertices - 2;
if(indexData == null) {
indexData = new (starling_rendering_IndexData().default)(numTriangles * 3);
}
var _g = 0;
var _g1 = numTriangles;
while(_g < _g1) {
var i = _g++;
indexData.addTriangle(0,i + 1,i + 2);
}
return indexData;
},
contains: function(x,y) {
if(x < this.__x || y < this.__y || x > this.__x + this.__width || y > this.__y + this.__height) {
return false;
}
if(x >= this.__x + this.__radiusX && x <= this.__x + this.__width - this.__radiusX) {
return true;
}
if(y >= this.__y + this.__radiusY && y <= this.__y + this.__height - this.__radiusY) {
return true;
}
var centerX;
var centerY;
if(x < this.__x + this.__radiusX) {
centerX = this.__x + this.__radiusX;
} else {
centerX = this.__x + this.__width - this.__radiusX;
}
if(y < this.__y + this.__radiusY) {
centerY = this.__y + this.__radiusY;
} else {
centerY = this.__y + this.__height - this.__radiusY;
}
var vx = x - centerX;
var vy = y - centerY;
var a = vx / this.__radiusX;
var b = vy / this.__radiusY;
return a * a + b * b <= 1;
},
get_area: function() {
return this.__width * this.__height - (4.0 - Math.PI) * this.__radiusX * this.__radiusY;
},
get_isConvex: function() {
return true;
},
get_isSimple: function() {
return true;
}
});
RoundRectangle.prototype.__class__ = RoundRectangle.prototype.constructor = $hxClasses["starling.geom.RoundRectangle"] = RoundRectangle;
// Init
// Statics
// Export
exports.default = RoundRectangle;