robot-js
Version:
Native system automation for node.js
143 lines (105 loc) • 4.31 kB
JavaScript
////////////////////////////////////////////////////////////////////////////////
// -------------------------------------------------------------------------- //
// //
// (C) 2010-2018 Robot Developers //
// See LICENSE for licensing info //
// //
// -------------------------------------------------------------------------- //
////////////////////////////////////////////////////////////////////////////////
"use strict";
//----------------------------------------------------------------------------//
// Exports //
//----------------------------------------------------------------------------//
module.exports = function (robot)
{
//----------------------------------------------------------------------------//
// Constructor Size //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
function Size (aw, ah)
{
// Auto instantiate the Size
if (!(this instanceof Size))
return new Size (aw, ah);
var s = Size.normalize (aw, ah);
this.w = s.w;
this.h = s.h;
}
//----------------------------------------------------------------------------//
// Functions Size //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Size.prototype.isZero = function()
{
return this.w === 0
&& this.h === 0;
};
////////////////////////////////////////////////////////////////////////////////
Size.prototype.isEmpty = function()
{
return this.w === 0
|| this.h === 0;
};
////////////////////////////////////////////////////////////////////////////////
Size.prototype.toPoint = function()
{
return robot.Point
(this.w, this.h);
};
//----------------------------------------------------------------------------//
// Static Size //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Size.normalize = function (aw, ah)
{
if (aw instanceof Size)
return aw;
if (aw === undefined)
return { w: 0, h: 0 };
if (typeof aw === "object" &&
typeof aw.w === "number" &&
typeof aw.h === "number")
return aw;
if (typeof aw === "number")
{
if (typeof ah === "number")
return { w: aw, h: ah };
return { w: aw, h: aw };
}
throw new TypeError
("Invalid arguments");
};
//----------------------------------------------------------------------------//
// Operators Size //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Size.prototype.add = function (aw, ah)
{
var s = Size.normalize (aw, ah);
return Size
(this.w + s.w, this.h + s.h);
};
////////////////////////////////////////////////////////////////////////////////
Size.prototype.sub = function (aw, ah)
{
var s = Size.normalize (aw, ah);
return Size
(this.w - s.w, this.h - s.h);
};
////////////////////////////////////////////////////////////////////////////////
Size.prototype.eq = function (aw, ah)
{
var s = Size.normalize (aw, ah);
return this.w === s.w
&& this.h === s.h;
};
////////////////////////////////////////////////////////////////////////////////
Size.prototype.ne = function (aw, ah)
{
var s = Size.normalize (aw, ah);
return this.w !== s.w
|| this.h !== s.h;
};
////////////////////////////////////////////////////////////////////////////////
return Size;
};