/** * File name: ButtonLevel.java * Version: 1.0 * Date: 14/03/2015 23:10:50 * Author: Sawan * Copyright: Copyright 200X Sawan * * This file is part of Math Attack. * * Math Attack is free software: you can redistribute it * and/or modify it under the terms of the GNU General * Public License as published by the Free Software * Foundation, either version 3 of the License, * or (at your option) any later version. * * Math Attack is distributed in the hope that it will * be useful, but WITHOUT ANY WARRANTY; without even * the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public * License for more details. * * You should have received a copy of the GNU General * Public License along with Math Attack. If not, see * http://www.gnu.org/licenses/. */ package com.sawan.mathattack.buttons; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; import com.badlogic.gdx.scenes.scene2d.utils.Drawable; import com.sawan.mathattack.asset.UIAssets; import com.sawan.mathattack.constants.MAConstants; import com.sawan.mathattack.scene2d.ui.AbstractButton; import com.sawan.mathattack.settings.AppSettings; // TODO: Auto-generated Javadoc /** * The Class ButtonLevel. * * @author Sawan */ public class ButtonLevel extends AbstractButton { // Level stars (Not mandatory) /** The texture star holder. */ private TextureRegion textureStarHolder; /** The texture star. */ private TextureRegion textureStar; /** The number of total stars. */ private int numberOfTotalStars = 1; /** The number of earned stars. */ private int numberOfEarnedStars = 1; /** The star size width. */ private float starSizeWidth; /** The star size height. */ private float starSizeHeight; /** The star pos x start. */ private float starPosXStart = 0; /** The star pos y start. */ private float starPosYStart = 0; /** The star size ratio. */ private float starSizeRatio = 5; // Level Number (Not mandatory) /** The level number. */ private int levelNumber = -999; // Font (For writings and level number) /** The bit map font. */ private BitmapFont bitMapFont; /** * Instantiates a new button level. * * @param up the up * @param down the down */ public ButtonLevel(Drawable up, Drawable down) { super(UIAssets.cartwheel_font, up, down); } /* (non-Javadoc) * @see com.badlogic.gdx.scenes.scene2d.ui.Button#draw(com.badlogic.gdx.graphics.g2d.SpriteBatch, float) */ @Override public void draw(SpriteBatch batch, float parentAlpha) { // If level locked // ################################################################## if (isLockActive && textureLocked != null) { drawLocked(batch); } // If level number set // ################################################################## else if (levelNumber != MAConstants.UNDEFINED && bitMapFont != null) { super.draw(batch, parentAlpha); drawLevelNumber(batch); drawText(batch); drawStars(batch); drawExternalTexture(batch); } // Draw default // ################################################################## else { super.draw(batch, parentAlpha); drawText(batch); drawStars(batch); drawExternalTexture(batch); } } /** * Draw stars. * * @param batch the batch */ private void drawStars(SpriteBatch batch) { if(textureStarHolder != null && textureStar != null){ // Updated start positions float activePosXStart = (getX() + getWidth() / 2) - ((starSizeWidth * numberOfTotalStars) / 2); float activePoxYStart = (getY() + starSizeHeight / 1.3f) - (25f * AppSettings.getWorldPositionYRatio()); // Stars align together next by next (increase for each star) // S as Star // SSS (together aligned due to their widths) float currentPosX = getX(); for(int i = 0; i < numberOfTotalStars; i++){ // Update new star holder positions (for multiple stars) currentPosX = activePosXStart + (starSizeWidth * i); batch.draw(textureStarHolder, currentPosX + starPosXStart, activePoxYStart + starPosYStart,starSizeWidth, starSizeHeight); } for(int j = 0; j < numberOfEarnedStars; j++){ // Update new star position (for multiple stars) currentPosX = activePosXStart + (starSizeWidth * j); batch.draw(textureStar, currentPosX + starPosXStart, activePoxYStart + starPosYStart,starSizeWidth, starSizeHeight); } } } /** * Draw text. * * @param batch the batch */ private void drawText(SpriteBatch batch) { if(isTextActive && bitMapFont != null){ bitMapFont.draw(batch, text, getX() + textPosX, getY() + textPosY); } } /** * Draw locked. * * @param batch the batch */ private void drawLocked(SpriteBatch batch) { batch.draw(textureLocked, getX(), getY(), getWidth(), getHeight()); } /** * Draw external texture. * * @param batch the batch */ private void drawExternalTexture(SpriteBatch batch) { if(isExternalTextureActive && textureExternal != null){ batch.draw(textureExternal, getX() + externalTexturePosX, getY() + externalTexturePosY, externalTextureSizeW, externalTextureSizeH); } } /** * Draw level number. * * @param batch the batch */ private void drawLevelNumber(SpriteBatch batch) { // TODO Set precise position of level number for each game // there i single numbers ( < 10) // there is double numbers (9 >) float singePositionArranger = 2.5f; float doublePositionArranger = 2.8f; if (levelNumber < 10) { bitMapFont.draw(batch, "" + levelNumber, getX() + getWidth() / singePositionArranger, getY() + getHeight() / 1.4f); } else { bitMapFont.draw(batch, "" + levelNumber, getX() + getWidth() / doublePositionArranger, getY() + getHeight() / 1.4f); } } /** * Set level stars (It can be any other object) for level achievements * <p> * EXAMPLE<br> * There are 5 stars maximum can be achieved, and user earned 3 stars. It auto calculates the position and sizes of stars due to * button sizes. However further adjustment can be done (optinal) with "setLevelStarSizeRatio", "setLevelStarPosXStart" and "setLevelStarPosYStart". * !!starHolderTexture and starTexture should be in same sizes!! * * @param starHolderTexture the holder texture for star or other achievement object. * @param starTexture the texture for star or other achievement object. * @param numberOfTotalStars number of total stars * @param numberOfEarnedStars number of earned/achieved stars * */ public void setLevelStars(TextureRegion starHolderTexture, TextureRegion starTexture, int numberOfTotalStars, int numberOfEarnedStars){ textureStarHolder = starHolderTexture; textureStar = starTexture; this.numberOfTotalStars = numberOfTotalStars; this.numberOfEarnedStars = numberOfEarnedStars; // float btnSizeW = getWidth() - (getWidth() / starSizeRatio); float btnSizeH = getHeight() - (getHeight() / starSizeRatio); starSizeWidth = btnSizeW / numberOfTotalStars; starSizeHeight = btnSizeH / numberOfTotalStars; } /** * Get level starts X position. * * @return the level star pos x start */ public float getLevelStarPosXStart() { return starPosXStart; } /** * Set level stars X position. This is being calculated automaticly and centered. Do not suggest to play * width. * * @param starPosXStart the new level star pos x start */ public void setLevelStarPosXStart(float starPosXStart) { this.starPosXStart = starPosXStart; } /** * Get level starts Y position. * * @return the level star pos y start */ public float getLevelStarPosYStart() { return starPosYStart; } /** * Set level stars Y position. This can be used to lift all stars (holders and stars) up or down for * adjusments * * @param starPosYStart the new level star pos y start */ public void setLevelStarPosYStart(float starPosYStart) { this.starPosYStart = starPosYStart; } /** * Get level stars ratio. * * @return the level star size ratio */ public float getLevelStarSizeRatio() { return starSizeRatio; } /** * Set level stars size ratio. This makes stars smaller or bigger (Default is 5). Bigger number makes stars/holders * bigger and vice-versa. * * @param starSizeRatio the new level star size ratio */ public void setLevelStarSizeRatio(float starSizeRatio) { this.starSizeRatio = starSizeRatio; float btnSizeW = getWidth() - (getWidth() / starSizeRatio); float btnSizeH = getHeight() - (getHeight() / starSizeRatio); starSizeWidth = btnSizeW / numberOfTotalStars; starSizeHeight = btnSizeH / numberOfTotalStars; } /** * Get level number. * * @return the level number */ public int getLevelNumber() { return levelNumber; } /** * Set level number and the font to draw that number. * * @param levelNumber the level number * @param font the font */ public void setLevelNumber(int levelNumber, BitmapFont font) { this.levelNumber = levelNumber; bitMapFont = font; } /** * Set level number changes. * * @param levelNumber the new level number change */ public void setLevelNumberChange(int levelNumber){ this.levelNumber = levelNumber; } /** * Get achievement object holder texture. * * @return the texture star holder */ public TextureRegion getTextureStarHolder() { return textureStarHolder; } /** * Set achievement object holder texture. * * @param textureStarHolder the new texture star holder */ public void setTextureStarHolder(TextureRegion textureStarHolder) { this.textureStarHolder = textureStarHolder; } /** * Get achievement object . * * @return the texture star */ public TextureRegion getTextureStar() { return textureStar; } /** * Set achievement object texture. * * @param textureStar the new texture star */ public void setTextureStar(TextureRegion textureStar) { this.textureStar = textureStar; } /** * Get number of total achievement objects. * * @return the number of total stars */ public int getNumberOfTotalStars() { return numberOfTotalStars; } /** * Set number of total achievement object. * * @param numberOfTotalStars the new number of total stars */ public void setNumberOfTotalStars(int numberOfTotalStars) { this.numberOfTotalStars = numberOfTotalStars; } /** * Get number of achievement object. * * @return the number of earned stars */ public int getNumberOfEarnedStars() { return numberOfEarnedStars; } /** * Set number of earned achievement object. This number cannot be higher/lower than numberOfTotalStars, it auto * overrides if is less or more than numberOfTotalStars * * @param numberOfEarnedStars the new number of earned stars */ public void setNumberOfEarnedStars(int numberOfEarnedStars) { if(numberOfEarnedStars > numberOfTotalStars){ numberOfEarnedStars = numberOfTotalStars; } else{ this.numberOfEarnedStars = numberOfEarnedStars; } } }