UNPKG

@pilotlab/lux-attributes

Version:

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

121 lines (93 loc) 3.96 kB
import { DataType, IAttribute, Attribute, AttributeChangeOptions } from '@pilotlab/lux-attributes'; import IRectangle from './interfaces/iRectangle'; import Point from './point'; import { AttributeChangeActions, IAttributeChangeOptions, IAttributeUpdateTracker } from '@pilotlab/lux-attributes'; import { IAnimationEaseFunction, ISpeed } from '@pilotlab/lux-animation'; export class Rectangle extends Attribute implements IRectangle { constructor(x:number = 0, y:number = 0, width:number = 1, height:number = 1, label?:string) { super('Rectangle', null, DataType.COLLECTION, label); this.p_x = this.children.get('x', x, DataType.NUMBER_DOUBLE, label, AttributeChangeOptions.zero); this.p_y = this.children.get('y', y, DataType.NUMBER_DOUBLE, label, AttributeChangeOptions.zero); this.p_width = this.children.get('width', width, DataType.NUMBER_DOUBLE, label, AttributeChangeOptions.zero); this.p_height = this.children.get('height', height, DataType.NUMBER_DOUBLE, label, AttributeChangeOptions.zero); } 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.save.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.save.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.save.durationZero); } protected p_width:IAttribute; get height():number { return this.p_height.value; } set height(value:number) { this.p_height.set(value, AttributeChangeOptions.save.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 ); } has(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 ); } go( target:IRectangle, durationSpeed?:(number | ISpeed), ease?:IAnimationEaseFunction, repeatCount:number = 0 ):IAttributeUpdateTracker { const options:IAttributeChangeOptions = new AttributeChangeOptions(AttributeChangeActions.SIGNAL_CHANGE, durationSpeed, ease); options.repeatCount = repeatCount; return this.attributes.update(target, options); } 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;