Back to project page DolphinOES.
The source code is released under:
Apache License
If you think the Android project DolphinOES listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/******************************************************************************* * Copyright 2015 See AUTHORS file.//from w w w . ja v a 2 s.c o m * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.sidereal.dolphinoes.behaviors.renderer.texture; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.sidereal.dolphinoes.architecture.DolphinOES; import com.sidereal.dolphinoes.behaviors.renderer.Drawer; import com.sidereal.dolphinoes.behaviors.renderer.DrawerBuilder; import com.sidereal.dolphinoes.behaviors.renderer.Renderer; /** Draws a texture at the specified location. Uses {@link TextureBuilder} for * building. For more complex single-image operations, use {@link SpriteDrawer}. * * * @author Claudiu Bele. */ public class TextureDrawer extends Drawer { // region fields /** Filepath to the current texture found in {@link #texture}. */ private String filePath; /** {@link Texture} to draw. Set in */ private Texture texture; /** Rectangle designating the area in which the texture is drawn. Set in * {@link #updateBoundingRectangle()} which is called in * {@link #setOffsetPosition(Vector2)} or {@link #setSize(Vector2)}. */ private Rectangle boundingRectangle; /** The offset position. Set to (0,0) by default in * {@link TextureBuilder#TextureBuilder(String)}. Signifies the bottom-left * corner of the image. */ private Vector2 offsetPosition; /** Size of the texture. Set to the texture's width by default in * {@link TextureDrawer#TextureDrawer(Renderer, String, String)}. */ private Vector2 size; // endregion fields // region constructors /** Creates a new instance of {@link TextureDrawer}, with the only Mandatory * parameter being the texture file path. * * @param renderer * passed from {@link Renderer#addDrawer(String, DrawerBuilder)}. * @param name * passed from {@link Renderer#addDrawer(String, DrawerBuilder)} * @param filePath * Path to the texture. */ public TextureDrawer(Renderer renderer, String name, String filePath) { super(renderer, name, false); boundingRectangle = new Rectangle(); setTexture(filePath); } // endregion constructors // region methods private void updateBoundingRectangle() { if (offsetPosition == null) return; if (size == null) return; boundingRectangle.set(renderer.object.pos.getX() + offsetPosition.x, renderer.object.pos.getY() + offsetPosition.y, size.x, size.y); } public void setOffsetPosition(Vector2 offsetPosition) { if (offsetPosition == null) throw new NullPointerException( "Passed null Vector2 parameter to TextureDrawer.setOffsetPosition(Vector2)"); this.offsetPosition = offsetPosition; } public void setSize(Vector2 size) { if (size == null) this.size = new Vector2(texture.getWidth(), texture.getHeight()); else this.size = size; } public void setTexture(String filepath) { if (filepath == null) throw new NullPointerException( "Passed null String parameter to TextureDrawer.setTexture(String)"); if(filepath.equals(this.filePath)) return; // asset is not loaded if (!DolphinOES.assets.contains(filepath)) { DolphinOES.assets.load(filepath, Texture.class); DolphinOES.assets.finishLoading(); texture = DolphinOES.assets.get(filepath, Texture.class); } // asset is loaded else { Texture targetTexture = DolphinOES.assets.get( filepath, Texture.class); if (!targetTexture.equals(texture)) { texture = targetTexture; } } this.filePath = filepath; } @Override protected void dispose() { } @Override protected void draw(float delta) { float targetX = renderer.object.pos.getX() + offsetPosition.x; float targetY = renderer.object.pos.getY() + offsetPosition.y; renderer.object.gameBatch.spriteBatch.draw(texture, targetX, targetY, size.x, size.y); } @Override protected boolean isOutOfBounds() { updateBoundingRectangle(); return renderer.object.gameBatch.renderingArea .overlaps(boundingRectangle) == false; } // endregion }