import { Container } from 'inversify'; import { Bounds } from './bounds'; import { Entity, EntityManager, AbleManager, TransformData, PlaygroundContext } from '../'; import * as chai from 'chai'; import { Rectangle } from '@gedit/math'; const expect = chai.expect; function createContainer(): Container { const child = new Container({defaultScope: 'Singleton'}); child.bind(AbleManager).toSelf(); child.bind(PlaygroundContext).toConstantValue({}); child.bind(EntityManager).toSelf(); return child; } function expectRectangle(target: Rectangle, arr: number[]): void { expect(target.x).to.eql(arr[0]); expect(target.y).to.eql(arr[1]); expect(target.width).to.eql(arr[2]); expect(target.height).to.eql(arr[3]); } const container = createContainer(); function createEntity(): Entity { return container.get(EntityManager).createEntity(Entity); } describe('Playground.utils.bounds', () => { let target: TransformData; beforeEach(() => { target = new TransformData(createEntity()); }); it('get points and bounds', () => { target.origin.x = 0; target.origin.y = 0; expect(Bounds.getCenter(target, target.worldTransform)).to.eql({ x: 50, y: 50 }); expect(Bounds.getTopLeft(target, target.worldTransform)).to.eql({ x: 0, y: 0 }); expect(Bounds.getTopCenter(target, target.worldTransform)).to.eql({ x: 50, y: 0 }); expect(Bounds.getTopRight(target, target.worldTransform)).to.eql({ x: 100, y: 0 }); expect(Bounds.getLeftCenter(target, target.worldTransform)).to.eql({ x: 0, y: 50 }); expect(Bounds.getRightCenter(target, target.worldTransform)).to.eql({ x: 100, y: 50 }); expect(Bounds.getBottomLeft(target, target.worldTransform)).to.eql({ x: 0, y: 100 }); expect(Bounds.getBottomCenter(target, target.worldTransform)).to.eql({ x: 50, y: 100 }); expect(Bounds.getBottomRight(target, target.worldTransform)).to.eql({ x: 100, y: 100 }); expectRectangle(Bounds.getBounds(target, target.worldTransform), [0, 0, 100, 100]); target.origin.x = 0.5; target.origin.y = 0.5; expect(Bounds.getCenter(target, target.worldTransform)).to.eql({ x: 0, y: 0 }); expect(Bounds.getTopLeft(target, target.worldTransform)).to.eql({ x: -50, y: -50 }); expect(Bounds.getTopCenter(target, target.worldTransform)).to.eql({ x: 0, y: -50 }); expect(Bounds.getTopRight(target, target.worldTransform)).to.eql({ x: 50, y: -50 }); expect(Bounds.getLeftCenter(target, target.worldTransform)).to.eql({ x: -50, y: 0 }); expect(Bounds.getRightCenter(target, target.worldTransform)).to.eql({ x: 50, y: 0 }); expect(Bounds.getBottomLeft(target, target.worldTransform)).to.eql({ x: -50, y: 50 }); expect(Bounds.getBottomCenter(target, target.worldTransform)).to.eql({ x: 0, y: 50 }); expect(Bounds.getBottomRight(target, target.worldTransform)).to.eql({ x: 50, y: 50 }); expectRectangle(Bounds.getBounds(target, target.worldTransform), [-50, -50, 100, 100]); }); it('transform position', () => { target.position.x = 10; target.position.y = 10; expect(Bounds.getTopLeft(target)).to.eql({ x: 10, y: 10 }); }); it('transform position with parent', () => { const parent = new TransformData(createEntity()); parent.position.x = 10; parent.position.y = 10; target.setParent(parent); expect(Bounds.getTopLeft(target, target.worldTransform)).to.eql({ x: 10, y: 10 }); target.position.x = 10; target.position.y = 10; expect(Bounds.getTopLeft(target, target.worldTransform)).to.eql({ x: 20, y: 20 }); }); it('transform rotation', () => { // 中心点旋转45度 target.rotation = Math.PI / 4; target.origin.x = 0.5; target.origin.y = 0.5; expectRectangle(Bounds.getBounds(target, target.worldTransform), [-71, -71, 142, 141]); }); it('trasnform scale', () => { target.scale.x = 2; expect(Bounds.getBounds(target, target.worldTransform).width).to.eql(200); }); });