Android Open Source - minecraft-connect-4 Mc Selector






From Project

Back to project page minecraft-connect-4.

License

The source code is released under:

MIT License

If you think the Android project minecraft-connect-4 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

/* COPYRIGHT (C) 2014 Aleksandr Belkin. All Rights Reserved. */
package sq.squ1rr.mcc4.layout;
//from w w w  .j a v a2s  . co  m
import android.content.Context;
import android.view.View;

/**
 * Minecraft Select Button.
 * Acts like a list in a button, selects next element when the button is
 * clicked.
 * @author Aleksandr Belkin
 */
public class McSelector extends McButton {
    /** labels to show */
    private String[] labels = null;
    
    /** IDs assigned to labels */
    private int[] ids = null;
    
    /** selected element */
    private int selected = 0;
    
    /** click listener */
    private OnClickListener clickListener = null;
    
    /**
     * Create Button
     * @param context
     */
    public McSelector(Context context) {
        super(context);
        super.setOnClickListener(changeLabel);
    }

    /**
     * Returns index of the selected label
     * @return
     */
    public int getSelectedIndex() {
        return selected;
    }
    
    /**
     * Returns selected ID
     * @return
     */
    public int getSelectedId() {
        return ids[selected];
    }
    
    /**
     * Sets new labels
     * @param _labels
     */
    public void setLabels(String[] _labels) {
        labels = _labels;
        invalidate();
    }
    
    /**
     * Sets IDs associated with labels
     * @param _ids
     */
    public void setIds(int[] _ids) {
        ids = _ids;
    }
    
    /**
     * Sets new labels from string resources and assigns IDs
     */
    public void setLabels(int[] stringIds) {
        String[] labels = new String[stringIds.length];
        for(int i = 0; i < stringIds.length; ++i) {
            labels[i] = getContext().getString(stringIds[i]);
        }
        ids = stringIds;
        setLabels(labels);
    }
    
    /**
     * Selects the label by index
     * @param index
     */
    public void select(int index) {
        selected = index;
        invalidate();
    }
    
    /**
     * Selects the label based on ID
     * @param id
     */
    public void selectId(int id) {
        selected = 0;
        for(int i : ids) {
            if(i == id) break;
            selected++;
        }
        invalidate();
    }
    
    /*
     * (non-Javadoc)
     * @see android.view.View#invalidate()
     */
    @Override
    public void invalidate() {
        if(labels != null && getText() != labels[selected]) {
            setText(labels[selected]);
        }
        super.invalidate();
    }

    /*
     * (non-Javadoc)
     * @see android.view.View#setOnClickListener(android.view.View.OnClickListener)
     */
    @Override
    public void setOnClickListener(OnClickListener _clickListener) {
        clickListener = _clickListener;
    }
    
    /**
     * Increments selection index, calls user listener
     */
    private OnClickListener changeLabel = new OnClickListener() {
        @Override
        public void onClick(View view) {
            selected = (selected + 1) % labels.length;
            if(clickListener != null) clickListener.onClick(view);
            invalidate();
        }
    };
}




Java Source Code List

sq.squ1rr.mcc4.AboutMenu.java
sq.squ1rr.mcc4.BaseActivity.java
sq.squ1rr.mcc4.GameActivity.java
sq.squ1rr.mcc4.MainMenuActivity.java
sq.squ1rr.mcc4.MainMenu.java
sq.squ1rr.mcc4.MenuLayout.java
sq.squ1rr.mcc4.OptionsMenu.java
sq.squ1rr.mcc4.QuickGameMenu.java
sq.squ1rr.mcc4.StatsMenu.java
sq.squ1rr.mcc4.Stats.java
sq.squ1rr.mcc4.ai.Ai.java
sq.squ1rr.mcc4.ai.EasyAi.java
sq.squ1rr.mcc4.ai.HardAi.java
sq.squ1rr.mcc4.ai.NormalAi.java
sq.squ1rr.mcc4.ai.PeacefulAi.java
sq.squ1rr.mcc4.board.BoardDialogue.java
sq.squ1rr.mcc4.board.BoardLogic.java
sq.squ1rr.mcc4.board.BoardRenderer.java
sq.squ1rr.mcc4.board.BoardView.java
sq.squ1rr.mcc4.board.GameBoard.java
sq.squ1rr.mcc4.board.Texture.java
sq.squ1rr.mcc4.gl.Rectangle.java
sq.squ1rr.mcc4.gl.SpriteBatch.java
sq.squ1rr.mcc4.gl.SpriteString.java
sq.squ1rr.mcc4.gl.Sprite.java
sq.squ1rr.mcc4.layout.LayoutManager.java
sq.squ1rr.mcc4.layout.McButton.java
sq.squ1rr.mcc4.layout.McGroup.java
sq.squ1rr.mcc4.layout.McSelector.java
sq.squ1rr.mcc4.layout.McStyle.java
sq.squ1rr.mcc4.layout.McText.java
sq.squ1rr.mcc4.layout.McToggler.java
sq.squ1rr.mcc4.layout.McToken.java
sq.squ1rr.mcc4.rules.GameRules.java
sq.squ1rr.mcc4.rules.Player.java
sq.squ1rr.mcc4.rules.Rule.java
sq.squ1rr.mcc4.util.GlobalConstants.java