MenuItem.java :  » UnTagged » quiniandroid » org » cocos2d » menus » Android Open Source

Android Open Source » UnTagged » quiniandroid 
quiniandroid » org » cocos2d » menus » MenuItem.java
package org.cocos2d.menus;

import org.cocos2d.nodes.CocosNode;
import org.cocos2d.types.CCRect;

import java.lang.reflect.Method;

public abstract class MenuItem extends CocosNode {
    public static final int kItemSize = 32;

    static int _fontSize = kItemSize;
    static String fontName = "DroidSans";

    public static final int kCurrentItem = 0xc0c05001;

    public static final int kZoomActionTag = 0xc0c05002;

    protected boolean isEnabled_;
    protected boolean isSelected_;

    protected Object targetCallback;
    protected String selector;

    private Method invocation;

    /**
     * Initializes a menu item with a target/selector
     */
    protected MenuItem(Object rec, String cb) {
        targetCallback = rec;
        selector = cb;

        setAnchorPoint(0.5f, 0.5f);

        invocation = null;
        if (rec != null && cb != null)
            try {
                Class cls = rec.getClass();
                invocation = cls.getMethod(cb);
            } catch (Exception e) {
                // Do nothing
            }

        isEnabled_ = true;
        isSelected_ = false;
    }

    /**
     * Activate the item
     */
    public void activate() {
        if (isEnabled_) {
            if (targetCallback != null & invocation != null) {
                try {
                    invocation.invoke(targetCallback);
                } catch (Exception e) {
                    // Do nothing
                }
            }
        }
    }

    /**
     * The item was selected (not activated), similar to "mouse-over"
     */
    public void selected() {
        isSelected_ = true;
    }

    /**
     * The item was unselected
     */
    public void unselected() {
        isSelected_ = false;
    }

    /**
     * Enable or disabled the MenuItem
     */
    public void setIsEnabled(boolean enabled) {
        isEnabled_ = enabled;
    }


    /**
     * Returns whether or not the MenuItem is enabled
     */
    public boolean isEnabled() {
        return isEnabled_;
    }

    /**
     * Returns the outside box
     */
    public CCRect rect() {
        return CCRect.make(getPositionX() - getWidth() * getAnchorPointX(), getPositionY() -
                getHeight() * getAnchorPointY(),
                getWidth(), getHeight());
    }


}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.