Newer
Older
lostmynuts / shared / js / Engine / Display / UniformScaleGraphic.js
Engine.UniformScaleGraphic = class extends Engine.Drawable
{

	constructor(width /*int*/, height /*int*/, allowUpScale /*float*/, graphicName)
	{
		super();

		if (width === undefined) width = 128;
		if (height === undefined) height = 128;
		if (allowUpScale === undefined) allowUpScale = 1;

		this.contents = new Engine.Drawable();
		this.origWidth = 128;
		this.origHeight = 128;
		this.rect = new PIXI.Rectangle();
		this.allowUpScale = 1;
		this._maxWidth = width;
		this._maxHeight = height;
		this.hasContents = false;
		this.frame = 0;
		this.debug = null;
		this.isUniformScaleGraphic = true;
		
		this.allowUpScale = allowUpScale;
		this.setSize(width, height, allowUpScale);

		this.contents.onGraphicLoaded.addListener(this, this.graphicLoaded);
		if (graphicName !== undefined)
		{
			this.setGraphicByName(graphicName);
		}
	}


	get maxWidth() { return this._maxWidth; }
	set maxWidth(value)
	{
		this.setSize(value, this._maxHeight);
	}

	get maxHeight() { return this._maxHeight; }
	set maxHeight(value)
	{
		this.setSize(this._maxWidth, value);
	}


	clear()
	{
		this.contents.clear();
		this.removeChild(this.contents);
		this.hasContents = false;
	}

	setGraphicByID(graphic_id /*int*/, frame /*int*/)
	{
		if (frame === undefined) frame = 1;

		this.frame = frame;
		var def = Engine.GraphicFactoryInstance.getGraphicDefByID(graphic_id);
		if (def)
		{
			this.setGraphicByName(def.name);
		}
	}

	setGraphicByName(graphicName /*string*/, frame /*int*/)
	{
		if (frame === undefined) frame = 1;

		this.clear();
		if (graphicName == null || graphicName == "") return;
		
		this.frame = frame;
		
		this.addChild(this.contents);
		this.contents.loadFromGraphicName(graphicName, this.maxWidth, this.maxHeight);
	}

	graphicLoaded(obj /*Drawable*/)
	{
		this.contents.gotoAndStop(this.frame);
		this.contentsUpdated();
		this.contents.graphicData.setTextureMode(PIXI.SCALE_MODES.LINEAR);
	}

	contentsUpdated()
	{
		this.contents.x = 0;
		this.contents.y = 0;
		this.contents.scale.x = this.contents.scale.y = 1;
		this.rect = this.contents.getLocalBounds();
		this.origWidth = Math.max(1, this.rect.width);
		this.origHeight = Math.max(1, this.rect.height);
		this.setSize(this.maxWidth, this.maxHeight, this.allowUpScale);
	}

	setSize(maxWidth /*int*/, maxHeight /*int*/, allowUpScale /*float*/, setUnrounded /*bool*/)
	{
		if (allowUpScale === undefined) allowUpScale = 1;
		if (setUnrounded === undefined) setUnrounded = false;

		this.allowUpScale = allowUpScale;
		
		this._maxWidth = maxWidth;
		this._maxHeight = maxHeight;
		
		this.contents.scale.x = 1;
		this.contents.scale.y = 1;
		
		var maxScale = 1;
		if (this.origWidth > maxWidth || this.origHeight > maxHeight)
		{
			maxScale = Math.min(maxWidth / this.origWidth, maxHeight / this.origHeight);
		}
		else if (allowUpScale > 1)
		{
			maxScale = Math.min(maxWidth / this.origWidth, maxHeight / this.origHeight);
			maxScale = Math.min(this.allowUpScale, maxScale);
		}
		
		this.contents.scale.x = maxScale;
		this.contents.scale.y = maxScale;
		
		//center graphic?
		this.contents.x = 0;
		this.contents.y = 0;

		var newWidth = this.origWidth * maxScale;
		var newHeight = this.origHeight * maxScale;
		
		var r = new PIXI.Rectangle(this.rect.x, this.rect.y, this.rect.width, this.rect.height);
		r.x *= maxScale;
		r.y *= maxScale;
		r.width *= maxScale;
		r.height *= maxScale;
		
		if(this.setUnrounded)
		{
			this.contents.setXYUnrounded(((maxWidth - r.width) / 2) - r.x, ((maxHeight - r.height) / 2) - r.y);
		}
		else
		{
			this.contents.x = ((maxWidth - r.width) / 2) - r.x;
			this.contents.y = ((maxHeight - r.height) / 2) - r.y;
		}

		this.hitArea = new PIXI.Rectangle(0, 0, maxWidth, maxHeight);
	}

	setWidth(width /*int*/, allowUpScale /*float*/, setXYUnrounded /*bool*/)
	{
		if (allowUpScale === undefined) allowUpScale = 1;
		if (setXYUnrounded === undefined) setXYUnrounded = false;

		this.contents.scale.x = contents.scale.y = 1;
		this.origWidth = Math.max(1, this.contents.width);
		this.origHeight = Math.max(1, this.contents.height);
		
		var ratio = this.contents.height / this.contents.width;
		var height = Math.floor(width * ratio);
		this.setSize(width, height, allowUpScale, setXYUnrounded);
	}

	setHeight(height /*int*/, allowUpScale /*float*/, setXYUnrounded /*bool*/)
	{
		if (allowUpScale === undefined) allowUpScale = 1;
		if (setXYUnrounded === undefined) setXYUnrounded = false;
		
		this.contents.scale.x = this.contents.scale.y = 1;
		this.origWidth = Math.max(1, this.contents.width);
		this.origHeight = Math.max(1, this.contents.height);
		
		var ratio = this.contents.width / this.contents.height;
		var width = Math.floor(height * ratio);
		this.setSize(width, height, allowUpScale, setXYUnrounded);
	}
}