UNPKG

@pilotlab/lux-types

Version:

A luxurious user experience framework, developed by your friends at Pilot.

96 lines (70 loc) 3.49 kB
import is from '@pilotlab/lux-is'; import { DataType, IAttribute, Attribute, AttributeCollection, AttributeChangeOptions } from '@pilotlab/lux-attributes'; import IRectangle from './interfaces/iRectangle'; import Point from './point'; export class Rectangle extends Attribute<Rectangle> implements IRectangle { constructor(x?:number, y?:number, width?:number, height?:number) { super('Rectangle', null, DataType.COLLECTION, null, true); this.p_x = this.children.set('x', is.notEmpty(x) ? x : 0, DataType.NUMBER_DOUBLE, AttributeChangeOptions.zero).node; this.p_y = this.children.set('y', is.notEmpty(y) ? y : 0, DataType.NUMBER_DOUBLE, AttributeChangeOptions.zero).node; this.p_width = this.children.set('width', is.notEmpty(width) ? width : 0, DataType.NUMBER_DOUBLE, AttributeChangeOptions.zero).node; this.p_height = this.children.set('height', is.notEmpty(height) ? height : 0, DataType.NUMBER_DOUBLE, AttributeChangeOptions.zero).node; } get copy():Rectangle { return new Rectangle(this.x, this.y, this.width, this.height); } get x():number { return this.p_x.value; } set x(value:number) { this.p_x.set(value, AttributeChangeOptions.saveAndSignal.durationZero); } get left():number { return this.x; } set left(value:number) { this.x = value; } get right():number { return this.x + this.width; } set right(value:number) { this.x = value - this.width; } protected p_x:IAttribute; get y():number { return this.p_y.value; } set y(value:number) { this.p_y.set(value, AttributeChangeOptions.saveAndSignal.durationZero); } get top():number { return this.y; } set top(value:number) { this.y = value; } get bottom():number { return this.y + this.height; } set bottom(value:number) { this.y = value - this.height; } protected p_y:IAttribute; get width():number { return this.p_width.value; } set width(value:number) { this.p_width.set(value, AttributeChangeOptions.saveAndSignal.durationZero); } protected p_width:IAttribute; get height():number { return this.p_height.value; } set height(value:number) { this.p_height.set(value, AttributeChangeOptions.saveAndSignal.durationZero); } protected p_height:IAttribute; get topLeft():Point { return new Point(this.left, this.top); } get topRight():Point { return new Point(this.right, this.top); } get bottomLeft():Point { return new Point(this.left, this.bottom); } get bottomRight():Point { return new Point(this.right, this.bottom); } intersects(rectangle:Rectangle):boolean { return !( rectangle.left > this.right || rectangle.right < this.left || rectangle.top > this.bottom || rectangle.bottom < this.top ); } contains(rectangle:Rectangle):boolean { return ( rectangle.left > this.left && rectangle.right < this.right && rectangle.top > this.top && rectangle.bottom < this.bottom ); } canFit(rectangle:Rectangle) { return ( this.width >= rectangle.width && this.height >= rectangle.height ); } static get empty():Rectangle { let rectangle:Rectangle = new Rectangle(); rectangle.x = null; rectangle.y = null; rectangle.width = null; rectangle.height = null; return rectangle; } } // End of class export default Rectangle;