UNPKG

starling-framework

Version:

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

293 lines (276 loc) 9.44 kB
// Class: starling.utils.RectangleUtil 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; function openfl_geom_Point() {return $import(require("openfl/geom/Point"));} function openfl_geom_Vector3D() {return $import(require("openfl/geom/Vector3D"));} function openfl__$Vector_Vector_$Impl_$() {return require("./../../openfl/_Vector/Vector_Impl_");} function openfl_geom_Rectangle() {return $import(require("openfl/geom/Rectangle"));} function starling_utils_ScaleMode() {return require("./../../starling/utils/ScaleMode");} function js__$Boot_HaxeError() {return require("./../../js/_Boot/HaxeError");} function openfl_errors_ArgumentError() {return $import(require("openfl/errors/ArgumentError"));} function starling_utils_MatrixUtil() {return require("./../../starling/utils/MatrixUtil");} function starling_utils_MathUtil() {return require("./../../starling/utils/MathUtil");} // Constructor var RectangleUtil = function(){} // Meta RectangleUtil.__name__ = "starling.utils.RectangleUtil"; RectangleUtil.__isInterface__ = false; RectangleUtil.prototype = { }; RectangleUtil.prototype.__class__ = RectangleUtil.prototype.constructor = $hxClasses["starling.utils.RectangleUtil"] = RectangleUtil; // Init // Statics RectangleUtil.intersect = function(rect1,rect2,out) { if(out == null) { out = new (openfl_geom_Rectangle().default)(); } var left = rect1.x > rect2.x ? rect1.x : rect2.x; var right = rect1.get_right() < rect2.get_right() ? rect1.get_right() : rect2.get_right(); var top = rect1.y > rect2.y ? rect1.y : rect2.y; var bottom = rect1.get_bottom() < rect2.get_bottom() ? rect1.get_bottom() : rect2.get_bottom(); if(left > right || top > bottom) { out.setEmpty(); } else { out.setTo(left,top,right - left,bottom - top); } return out; } RectangleUtil.fit = function(rectangle,into,scaleMode,pixelPerfect,out) { if(pixelPerfect == null) { pixelPerfect = false; } if(scaleMode == null) { scaleMode = "showAll"; } if(!(starling_utils_ScaleMode().default).isValid(scaleMode)) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("Invalid scaleMode: " + scaleMode)); } if(out == null) { out = new (openfl_geom_Rectangle().default)(); } var width = rectangle.width; var height = rectangle.height; var factorX = into.width / width; var factorY = into.height / height; var factor = 1.0; if(scaleMode == "showAll") { factor = factorX < factorY ? factorX : factorY; if(pixelPerfect) { factor = RectangleUtil.nextSuitableScaleFactor(factor,false); } } else if(scaleMode == "noBorder") { factor = factorX > factorY ? factorX : factorY; if(pixelPerfect) { factor = RectangleUtil.nextSuitableScaleFactor(factor,true); } } width *= factor; height *= factor; out.setTo(into.x + (into.width - width) / 2,into.y + (into.height - height) / 2,width,height); return out; } RectangleUtil.nextSuitableScaleFactor = function(factor,up) { var divisor = 1.0; if(up) { if(factor >= 0.5) { return Math.ceil(factor); } else { while(1.0 / (divisor + 1) > factor) ++divisor; } } else if(factor >= 1.0) { return Math.floor(factor); } else { while(1.0 / divisor > factor) ++divisor; } return 1.0 / divisor; } RectangleUtil.normalize = function(rect) { if(rect.width < 0) { rect.width = -rect.width; rect.x -= rect.width; } if(rect.height < 0) { rect.height = -rect.height; rect.y -= rect.height; } } RectangleUtil.extend = function(rect,left,right,top,bottom) { if(bottom == null) { bottom = 0; } if(top == null) { top = 0; } if(right == null) { right = 0; } if(left == null) { left = 0; } rect.x -= left; rect.y -= top; rect.width += left + right; rect.height += top + bottom; } RectangleUtil.extendToWholePixels = function(rect,scaleFactor) { if(scaleFactor == null) { scaleFactor = 1; } var left = Math.floor(rect.x * scaleFactor) / scaleFactor; var top = Math.floor(rect.y * scaleFactor) / scaleFactor; var right = Math.ceil(rect.get_right() * scaleFactor) / scaleFactor; var bottom = Math.ceil(rect.get_bottom() * scaleFactor) / scaleFactor; rect.setTo(left,top,right - left,bottom - top); } RectangleUtil.getBounds = function(rectangle,matrix,out) { if(out == null) { out = new (openfl_geom_Rectangle().default)(); } var minX = 1.79e+308; var maxX = -1.79e+308; var minY = 1.79e+308; var maxY = -1.79e+308; var positions = RectangleUtil.getPositions(rectangle,RectangleUtil.sPositions); (starling_utils_MatrixUtil().default).transformCoords(matrix,positions[0].x,positions[0].y,RectangleUtil.sPoint); if(minX > RectangleUtil.sPoint.x) { minX = RectangleUtil.sPoint.x; } if(maxX < RectangleUtil.sPoint.x) { maxX = RectangleUtil.sPoint.x; } if(minY > RectangleUtil.sPoint.y) { minY = RectangleUtil.sPoint.y; } if(maxY < RectangleUtil.sPoint.y) { maxY = RectangleUtil.sPoint.y; } (starling_utils_MatrixUtil().default).transformCoords(matrix,positions[1].x,positions[1].y,RectangleUtil.sPoint); if(minX > RectangleUtil.sPoint.x) { minX = RectangleUtil.sPoint.x; } if(maxX < RectangleUtil.sPoint.x) { maxX = RectangleUtil.sPoint.x; } if(minY > RectangleUtil.sPoint.y) { minY = RectangleUtil.sPoint.y; } if(maxY < RectangleUtil.sPoint.y) { maxY = RectangleUtil.sPoint.y; } (starling_utils_MatrixUtil().default).transformCoords(matrix,positions[2].x,positions[2].y,RectangleUtil.sPoint); if(minX > RectangleUtil.sPoint.x) { minX = RectangleUtil.sPoint.x; } if(maxX < RectangleUtil.sPoint.x) { maxX = RectangleUtil.sPoint.x; } if(minY > RectangleUtil.sPoint.y) { minY = RectangleUtil.sPoint.y; } if(maxY < RectangleUtil.sPoint.y) { maxY = RectangleUtil.sPoint.y; } (starling_utils_MatrixUtil().default).transformCoords(matrix,positions[3].x,positions[3].y,RectangleUtil.sPoint); if(minX > RectangleUtil.sPoint.x) { minX = RectangleUtil.sPoint.x; } if(maxX < RectangleUtil.sPoint.x) { maxX = RectangleUtil.sPoint.x; } if(minY > RectangleUtil.sPoint.y) { minY = RectangleUtil.sPoint.y; } if(maxY < RectangleUtil.sPoint.y) { maxY = RectangleUtil.sPoint.y; } out.setTo(minX,minY,maxX - minX,maxY - minY); return out; } RectangleUtil.getBoundsProjected = function(rectangle,matrix,camPos,out) { if(out == null) { out = new (openfl_geom_Rectangle().default)(); } if(camPos == null) { throw new (js__$Boot_HaxeError().default)(new (openfl_errors_ArgumentError().default)("camPos must not be null")); } var minX = 1.79e+308; var maxX = -1.79e+308; var minY = 1.79e+308; var maxY = -1.79e+308; var positions = RectangleUtil.getPositions(rectangle,RectangleUtil.sPositions); var _g = 0; while(_g < 4) { var i = _g++; var position = positions[i]; if(matrix != null) { (starling_utils_MatrixUtil().default).transformCoords3D(matrix,position.x,position.y,0,RectangleUtil.sPoint3D); } else { RectangleUtil.sPoint3D.setTo(position.x,position.y,0); } (starling_utils_MathUtil().default).intersectLineWithXYPlane(camPos,RectangleUtil.sPoint3D,RectangleUtil.sPoint); if(minX > RectangleUtil.sPoint.x) { minX = RectangleUtil.sPoint.x; } if(maxX < RectangleUtil.sPoint.x) { maxX = RectangleUtil.sPoint.x; } if(minY > RectangleUtil.sPoint.y) { minY = RectangleUtil.sPoint.y; } if(maxY < RectangleUtil.sPoint.y) { maxY = RectangleUtil.sPoint.y; } } out.setTo(minX,minY,maxX - minX,maxY - minY); return out; } RectangleUtil.getPositions = function(rectangle,out) { if(out == null) { out = (openfl__$Vector_Vector_$Impl_$().default)._new(4,true); } if(out[0] == null) { (openfl__$Vector_Vector_$Impl_$().default).set(out,0,new (openfl_geom_Point().default)()); } if(out[1] == null) { (openfl__$Vector_Vector_$Impl_$().default).set(out,1,new (openfl_geom_Point().default)()); } if(out[2] == null) { (openfl__$Vector_Vector_$Impl_$().default).set(out,2,new (openfl_geom_Point().default)()); } if(out[3] == null) { (openfl__$Vector_Vector_$Impl_$().default).set(out,3,new (openfl_geom_Point().default)()); } out[0].x = rectangle.get_left(); out[0].y = rectangle.get_top(); out[1].x = rectangle.get_right(); out[1].y = rectangle.get_top(); out[2].x = rectangle.get_left(); out[2].y = rectangle.get_bottom(); out[3].x = rectangle.get_right(); out[3].y = rectangle.get_bottom(); return out; } RectangleUtil.compare = function(r1,r2,e) { if(e == null) { e = 0.0001; } if(r1 == null) { return r2 == null; } else if(r2 == null) { return false; } else if(r1.x > r2.x - e && r1.x < r2.x + e && r1.y > r2.y - e && r1.y < r2.y + e && r1.width > r2.width - e && r1.width < r2.width + e && r1.height > r2.height - e) { return r1.height < r2.height + e; } else { return false; } } RectangleUtil.sPoint = new (openfl_geom_Point().default)() RectangleUtil.sPoint3D = new (openfl_geom_Vector3D().default)() RectangleUtil.sPositions = (openfl__$Vector_Vector_$Impl_$().default)._new(null,null,[new (openfl_geom_Point().default)(),new (openfl_geom_Point().default)(),new (openfl_geom_Point().default)(),new (openfl_geom_Point().default)()]) // Export exports.default = RectangleUtil;