Android Open Source - Castle-Invaders Radio Group Button






From Project

Back to project page Castle-Invaders.

License

The source code is released under:

GNU General Public License

If you think the Android project Castle-Invaders listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.noobygames.utils.ui;
/*from w w w  . jav a  2s.c  om*/
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.noobygames.utils.OverlapTester;
import com.noobygames.utils.exceptions.OutOfBoundingException;

/**
 * Group of RadioButtons, which are linked together (only one can be active)
 * 
 * @author Nerzal
 **/
public class RadioGroupButton extends ClickableElement {

  private Array<RadioButton> rB;

  
  /**
   * Constructor
   * 
   * @param position
   *            A rectangle with the relative position on the window and its size
   * @param tex 
   *       The texture
   **/
  public RadioGroupButton(Rectangle position, Texture tex) {
    super(position, tex);
  }

  /**
   * Constructor
   * 
   * @param position
   *            A rectangle with the relative position on the window and its size
   * @param tex 
   *       The texture
   * 
   * @param rB An array of radiobuttons
   *           
   * @throws OutOfBoundingException
   **/
  public RadioGroupButton(Rectangle position, Texture tex,
      Array<RadioButton> rB) {
    super(position, tex);
    try {
      for (RadioButton rb : rB) {
        if (OverlapTester.outOfBounding(this.getPosition(),
            rb.getPosition()))
          throw new OutOfBoundingException();
      }
      this.rB = rB;
    } catch (OutOfBoundingException e) {
      System.out
          .println("One of the Radiobuttons you wanted to add is out of the RadioGroupButtons boundings! Pls try again!");
    }
  }

  /**
   * If a click occured within the boundings of the radiobuttongroup this
   * method checks if a radiobutton was clicked. If a radiobutton was clicked
   * its onClick() method is called and all other buttons are set to "false"
   * 
   * @see RadioButton
   * @param touchPoint
   *            the touchPoint
   **/
  public void onClick(Vector2 touchPoint) {
    RadioButton tmp = null;

    for (RadioButton rb : rB)
      if (OverlapTester.pointInRectangle(rb.getPosition(), touchPoint))
        if (!rb.isClicked()) {
          rb.onClick();
          tmp = rb;
        }

    for (RadioButton rb : rB)
      if (tmp != null)
        if (rb != tmp)
          rb.setClicked(false);
  }

  /**
   * Checks if a radioButton at the given index is clicked
   * 
   * @return true if the rb is clicked else false
   **/
  public boolean isRbClicked(int index) {
    if (!(index > rB.size))
      return this.rB.get(index).isClicked();
    else
      return false;
  }

  @Override
  public void onClick() {

  }

  /**
   * @return the rb
   */
  public Array<RadioButton> getRb() {
    return rB;
  }

  /**
   * @param rb
   *            the rb to set
   */
  public void setRb(Array<RadioButton> rb) {
    this.rB = rb;
  }

  /**
   * Adds a RadioButton to the Group
   * 
   * @param rB
   *            Radiobutton to add
   **/
  public void addRb(RadioButton rB) {
    try {
      if (OverlapTester.outOfBounding(this.getPosition(),
          rB.getPosition()))
        throw new OutOfBoundingException();
      else
        this.rB.add(rB);
    } catch (OutOfBoundingException e) {
      System.out
          .println("One of the Radiobuttons you wanted to add is out of the RadioGroupButtons boundings! Pls try again!");
    }
  }

  /**
   * Removes a specific radiobutton from the group
   * 
   * @param rB
   *            The RadioButton to remove
   * @return true if found&removed else false
   **/
  public boolean removeRb(RadioButton rB) {
    return this.rB.removeValue(rB, true);
  }

  /**
   * @param index
   *            index of the Radiobutton to remove
   * @return returns the button if found&removed else null
   **/
  public RadioButton removeRb(int index) {
    return this.rB.removeIndex(index);
  }

  /** Draws the radiobuttons **/
  public void drawGroup(SpriteBatch batch) {
    for (RadioButton rb : rB)
      rb.drawIt(batch);
  }

}




Java Source Code List

com.noobgygames.castleinvaders.ui.DragonUltiButton.java
com.noobgygames.castleinvaders.ui.ElementSwitcherButton.java
com.noobgygames.castleinvaders.ui.StoreElement.java
com.noobgygames.castleinvaders.ui.TextureElement.java
com.noobygames.castleinvaders.Assets.java
com.noobygames.castleinvaders.CastleInvaders.java
com.noobygames.castleinvaders.DynamicGameObject.java
com.noobygames.castleinvaders.GameLiving.java
com.noobygames.castleinvaders.GameObject.java
com.noobygames.castleinvaders.MainActivity.java
com.noobygames.castleinvaders.Main.java
com.noobygames.castleinvaders.Player.java
com.noobygames.castleinvaders.Projectile.java
com.noobygames.castleinvaders.Settings.java
com.noobygames.castleinvaders.WorldRenderer.java
com.noobygames.castleinvaders.World.java
com.noobygames.castleinvaders.mobs.Croco.java
com.noobygames.castleinvaders.mobs.EarthDragon.java
com.noobygames.castleinvaders.mobs.FireDragon.java
com.noobygames.castleinvaders.mobs.FireTroll.java
com.noobygames.castleinvaders.mobs.GameScreen.java
com.noobygames.castleinvaders.mobs.GreyTroll.java
com.noobygames.castleinvaders.mobs.IceDragon.java
com.noobygames.castleinvaders.mobs.IceTroll.java
com.noobygames.castleinvaders.mobs.Murloc.java
com.noobygames.castleinvaders.mobs.Orc.java
com.noobygames.castleinvaders.mobs.Skeleton.java
com.noobygames.castleinvaders.screens.GameScreen.java
com.noobygames.castleinvaders.screens.MainMenuScreen.java
com.noobygames.castleinvaders.screens.ScoreScreen.java
com.noobygames.castleinvaders.screens.SplashScreen.java
com.noobygames.castleinvaders.screens.StoreScreen.java
com.noobygames.castleinvaders.store.StoreObject.java
com.noobygames.castleinvaders.store.Store.java
com.noobygames.nerzal.castleinvaders.spells.Burning.java
com.noobygames.nerzal.castleinvaders.spells.Freeze.java
com.noobygames.nerzal.castleinvaders.spells.SpellEffect.java
com.noobygames.nerzal.castleinvaders.spells.Spells.java
com.noobygames.utils.ArrayListUtils.java
com.noobygames.utils.ObjectSelectionContainer.java
com.noobygames.utils.OverlapTester.java
com.noobygames.utils.exceptions.OutOfBoundingException.java
com.noobygames.utils.exceptions.SliderOutOfBoundingsException.java
com.noobygames.utils.ui.Button.java
com.noobygames.utils.ui.ClickableElement.java
com.noobygames.utils.ui.DropDownMenu.java
com.noobygames.utils.ui.Element.java
com.noobygames.utils.ui.RadioButton.java
com.noobygames.utils.ui.RadioGroupButton.java
com.noobygames.utils.ui.ScrollableElement.java
com.noobygames.utils.ui.SimpleElement.java
com.noobygames.utils.ui.Slider.java
com.noobygames.utils.ui.Table.java
com.noobygames.utils.ui.TextBox.java
com.noobygames.utils.ui.Text.java
com.noobygames.utils.ui.Window.java