您的位置:首页 > 产品设计 > UI/UE

CCMenuItem解析

2015-09-07 22:20 447 查看


代码:

/** @brief MenuItem base class.
 * 菜单项目基类
 *  Subclass MenuItem (or any subclass) to create your custom MenuItem objects.
 */
class CC_DLL MenuItem : public Node
{
public:
    /** Creates a MenuItem with no target/selector. */
	// 创建一个MenuItem对象
    static MenuItem* create();
    /** Creates a MenuItem with a target/selector. */
    CC_DEPRECATED_ATTRIBUTE static MenuItem* create(Ref *rec, SEL_MenuHandler selector);
    /** Creates a MenuItem with a target/selector. */
    static MenuItem* create(const ccMenuCallback& callback);

    /** Returns the outside box. */
    Rect rect() const;
    /** Activate the item. */
	// 激活项目
    virtual void activate();
    /** The item was selected (not activated), similar to "mouse-over". */
	// 项目是否被选中
    virtual void selected();
    /** The item was unselected. */
	// 取消选中项目
    virtual void unselected();
    /** Returns whether or not the item is enabled. */
	// 返回这个项目是否可用
    virtual bool isEnabled() const;
    /** Enables or disables the item. */
	// 设置这个项目是否可用
    virtual void setEnabled(bool value);
    /** Returns whether or not the item is selected. */
	// 项目是否被选中
    virtual bool isSelected() const;

    /**
     * @js NA
     */
    virtual void onExit() override;

    /** Set the callback to the menu item.
    * @code
    * In js,can contain two params,the second param is jsptr.
    * @endcode
    * @lua NA
    */
	// 设置回调函数
    void setCallback(const ccMenuCallback& callback);
    
    /** Set the target/selector of the menu item.
    * @lua NA
    */
    CC_DEPRECATED_ATTRIBUTE void setTarget(Ref *rec, SEL_MenuHandler selector);

    /**
     * @js NA
     */
	 // 得到描述
    virtual std::string getDescription() const override;
    
CC_CONSTRUCTOR_ACCESS:
    /**
     * @js ctor
     */
    MenuItem()
    : _selected(false)
    , _enabled(false)
	, _callback(nullptr)
    {}
    /**
     * @js NA
     * @lua NA
     */
    virtual ~MenuItem();
    
    /** Initializes a MenuItem with a target/selector.
     * @lua NA
     */
	 // 通过回调函数初始化
    bool initWithCallback(const ccMenuCallback& callback);
    /** Initializes a MenuItem with a target/selector.
     * @js NA
     * @lua NA
     */
    CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Ref *rec, SEL_MenuHandler selector);

protected:
    bool            _selected;
    bool            _enabled;
    // callback
    ccMenuCallback _callback;

private:
    CC_DISALLOW_COPY_AND_ASSIGN(MenuItem);
};

/** @brief An abstract class for "label" MenuItemLabel items.
 Any Node that supports the LabelProtocol protocol can be added.
 Supported nodes:
 - BitmapFontAtlas
 - LabelAtlas
 - LabelTTF
 - Label
 */
 // 
class CC_DLL MenuItemLabel : public MenuItem
{
public:
    /** Creates a MenuItemLabel with a Label, target and selector. */
    CC_DEPRECATED_ATTRIBUTE static MenuItemLabel * create(Node*label, Ref* target, SEL_MenuHandler selector);

    /** Creates a MenuItemLabel with a Label and a callback. */
	// 创建一个MenuItemLabel对象
    static MenuItemLabel * create(Node*label, const ccMenuCallback& callback);

    /** Creates a MenuItemLabel with a Label. Target and selector will be nil. */
    static MenuItemLabel* create(Node *label);

    /** Sets a new string to the inner label. */
	// 设置字符串
    void setString(const std::string& label);

    /** Gets the color that will be used to disable the item. */
	// 得到不可用颜色
    inline const Color3B& getDisabledColor() const { return _disabledColor; };

    /** Sets the color that will be used to disable the item. */
	// 设置不可用颜色
    inline void setDisabledColor(const Color3B& color) { _disabledColor = color; };
    
    /** Gets the label that is rendered. */
	// 得到Label
    inline Node* getLabel() const { return _label; };
    
    /** Sets the label that is rendered. */
	// 设置Label
    void setLabel(Node* node);
    
    // Overrides
    virtual void activate() override;
    virtual void selected() override;
    virtual void unselected() override;
    virtual void setEnabled(bool enabled) override;
    
CC_CONSTRUCTOR_ACCESS:
    /**
     * @js ctor
     */
    MenuItemLabel()
    : _originalScale(0.0)
    , _label(nullptr)
    {}
    /**
     * @js NA
     * @lua NA
     */
    virtual ~MenuItemLabel();
    
    /** Initializes a MenuItemLabel with a Label, target and selector. */
	// 初始化项目通过label 
    bool initWithLabel(Node* label, const ccMenuCallback& callback);
    
    /** Initializes a MenuItemLabel with a Label, target and selector. */
    CC_DEPRECATED_ATTRIBUTE bool initWithLabel(Node* label, Ref* target, SEL_MenuHandler selector);
    
protected:
    Color3B    _colorBackup;
    float      _originalScale;

    /** The color that will be used to disable the item. */
    Color3B _disabledColor;
    /** Label that is rendered. It can be any Node that implements the LabelProtocol. */
    Node* _label;

private:
    CC_DISALLOW_COPY_AND_ASSIGN(MenuItemLabel);
};

/** @brief A MenuItemAtlasFont.
 Helper class that creates a MenuItemLabel class with a LabelAtlas.
 */
 // 通过LabelAtlas创建一个MenuItemLabel的辅助类
class CC_DLL MenuItemAtlasFont : public MenuItemLabel
{
public:
    /** Creates a menu item from a string and atlas with a target/selector. */
	// 创建一个MenuItemAtlasFont对象
    static MenuItemAtlasFont* create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap);
    /** Creates a menu item from a string and atlas. Use it with MenuItemToggle. */
    CC_DEPRECATED_ATTRIBUTE static MenuItemAtlasFont* create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector);
    /** Creates a menu item from a string and atlas. Use it with MenuItemToggle. */
    static MenuItemAtlasFont* create(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback);
    
CC_CONSTRUCTOR_ACCESS:
    /**
     * @js ctor
     */
    MenuItemAtlasFont(){}
    /**
     * @js NA
     * @lua NA
     */
    virtual ~MenuItemAtlasFont(){}
    
    /** Initializes a menu item from a string and atlas with a target/selector. */
    CC_DEPRECATED_ATTRIBUTE bool initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, Ref* target, SEL_MenuHandler selector);
    
    /** Initializes a menu item from a string and atlas with a target/selector. */
	// 同坐字符串初始化
    bool initWithString(const std::string& value, const std::string& charMapFile, int itemWidth, int itemHeight, char startCharMap, const ccMenuCallback& callback);

private:
    CC_DISALLOW_COPY_AND_ASSIGN(MenuItemAtlasFont);
};

/** @brief A MenuItemFont.
 Helper class that creates a MenuItemLabel class with a Label.
 */
 // 
class CC_DLL MenuItemFont : public MenuItemLabel
{
public:
    /** Creates a menu item from a string without target/selector. To be used with MenuItemToggle. */
	// 创建一个MenuItemFont对象
    static MenuItemFont * create(const std::string& value = "");
    /** Creates a menu item from a string with a target/selector. */
    CC_DEPRECATED_ATTRIBUTE static MenuItemFont * create(const std::string& value, Ref* target, SEL_MenuHandler selector);
    /** Creates a menu item from a string with a target/selector. */
    static MenuItemFont * create(const std::string& value, const ccMenuCallback& callback);

    /** Set default font size. */
	// 设置字体的尺寸
    static void setFontSize(int size);
    /** Get default font size. */
	// 得到字体的尺寸
    static int getFontSize();
    CC_DEPRECATED_ATTRIBUTE static int fontSize() { return MenuItemFont::getFontSize(); };
    /** Set the default font name. */
	// 设置字体的名字
    static void setFontName(const std::string& name);
    /** Get the default font name. */
	// 得到字体的名字
    static const std::string& getFontName();
    CC_DEPRECATED_ATTRIBUTE static const std::string& fontName() { return MenuItemFont::getFontName(); };

    /** Set font size.
     * c++ can not overload static and non-static member functions with the same parameter types.
     * so change the name to setFontSizeObj.
     * @js setFontSize
     * @js NA
     */
	 // 设置字体尺寸对象
    void setFontSizeObj(int size);
    
    /** get font size .
     * @js getFontSize
     * @js NA
     */
	 // 得到字体尺寸对象
    int getFontSizeObj() const;
    CC_DEPRECATED_ATTRIBUTE int fontSizeObj() const { return getFontSizeObj(); };
    
    /**
     * Set the font name .
     * c++ can not overload static and non-static member functions with the same parameter types.
     * so change the name to setFontNameObj.
     * @js setFontName
     * @js NA
     */
	 // 设置子类名字对象
    void setFontNameObj(const std::string& name);

    /** Returns the name of the Font.
     * @js getFontNameObj
     * @js NA
     */
	 // 得到字体名字对象
    const std::string& getFontNameObj() const;

    /** Deprecated Use getFontNameObj() instead.
     * @js NA
     */
    CC_DEPRECATED_ATTRIBUTE const std::string& fontNameObj() const { return getFontNameObj(); }
    
CC_CONSTRUCTOR_ACCESS:
    /**
     * @js ctor
     */
    MenuItemFont();
    /**
     * @js NA
     * @lua NA
     */
    virtual ~MenuItemFont();
    
    /** Initializes a menu item from a string with a target/selector. */
    CC_DEPRECATED_ATTRIBUTE bool initWithString(const std::string& value, Ref* target, SEL_MenuHandler selector);
    
    /** Initializes a menu item from a string with a target/selector. */
	// 初始化
    bool initWithString(const std::string& value, const ccMenuCallback& callback);
    
protected:
    int _fontSize;
    std::string _fontName;

private:
    CC_DISALLOW_COPY_AND_ASSIGN(MenuItemFont);
};

/** @brief MenuItemSprite accepts Node<RGBAProtocol> objects as items.
 The images has 3 different states:
 - unselected image
 - selected image
 - disabled image
 
 @since v0.8.0
 */
 // 精灵菜单项目
class CC_DLL MenuItemSprite : public MenuItem
{
public:
    /** Creates a menu item with a normal, selected and disabled image.*/
	// 创建一个精灵菜单项目
    static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite = nullptr);
    /** Creates a menu item with a normal and selected image with target/selector. */
    CC_DEPRECATED_ATTRIBUTE static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Ref* target, SEL_MenuHandler selector);
    /** Creates a menu item with a normal,selected  and disabled image with target/selector. */
    CC_DEPRECATED_ATTRIBUTE static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Ref* target, SEL_MenuHandler selector);
    /** Creates a menu item with a normal and selected image with a callable object. */
    static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, const ccMenuCallback& callback);
    /** Creates a menu item with a normal,selected  and disabled image with target/selector. */
    static MenuItemSprite * create(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback);

    /** Gets the image used when the item is not selected. */
	// 得到正常的贴图
    inline Node* getNormalImage() const { return _normalImage; };
    
    /** Sets the image used when the item is not selected. */
	// 设置正常的贴图
    void setNormalImage(Node* image);
    
    /** Gets the image used when the item is selected. */
	// 得到选中的贴图
    inline Node* getSelectedImage() const { return _selectedImage; };
    
    /** Sets the image used when the item is selected. */
	// 设置选中的贴图
    void setSelectedImage(Node* image);
    
    /** Gets the image used when the item is disabled. */
	// 得到不可用状态的贴图
    inline Node* getDisabledImage() const { return _disabledImage; };
    
    /** Sets the image used when the item is disabled. */
	// 设置不可用状态的贴图
    void setDisabledImage(Node* image);
    
    /**
     * The item was selected (not activated), similar to "mouse-over".
     @since v0.99.5
     */
	 // 选中
    virtual void selected();

    /** The item was unselected. */
	// 取消选中
    virtual void unselected();

    /** Enables or disables the item. */
	// 设置是否可用
    virtual void setEnabled(bool bEnabled);
    
CC_CONSTRUCTOR_ACCESS:
    MenuItemSprite()
    :_normalImage(nullptr)
    ,_selectedImage(nullptr)
    ,_disabledImage(nullptr)
    {}
    
    /** Initializes a menu item with a normal, selected  and disabled image with target/selector. */
    CC_DEPRECATED_ATTRIBUTE bool initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, Ref* target, SEL_MenuHandler selector);
    
    /** Initializes a menu item with a normal, selected  and disabled image with a callable object. */
	// 初始化
    bool initWithNormalSprite(Node* normalSprite, Node* selectedSprite, Node* disabledSprite, const ccMenuCallback& callback);
    
protected:
// 更新贴图的可视状态
    virtual void updateImagesVisibility();

    /** The image used when the item is not selected. */
    Node* _normalImage;
    /** The image used when the item is selected. */
    Node* _selectedImage;
    /** The image used when the item is disabled. */
    Node* _disabledImage;

private:
    CC_DISALLOW_COPY_AND_ASSIGN(MenuItemSprite);
};

/** @brief MenuItemImage accepts images as items.
 The images has 3 different states:
 - unselected image
 - selected image
 - disabled image
 
 For best results try that all images are of the same size.
 */
 // 贴图菜单项目
class CC_DLL MenuItemImage : public MenuItemSprite
{
public:
    /** Creates an MenuItemImage. */
	// 创建一个贴图菜单项目
    static MenuItemImage* create();
    /** Creates a menu item with a normal and selected image.*/
    static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage);
    /** Creates a menu item with a normal,selected  and disabled image.*/
    static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage);
    /** Creates a menu item with a normal and selected image with target/selector. */
    CC_DEPRECATED_ATTRIBUTE static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, Ref* target, SEL_MenuHandler selector);
    /** Creates a menu item with a normal and selected image with a callable object. */
    static MenuItemImage* create(const std::string&normalImage, const std::string&selectedImage, const ccMenuCallback& callback);

    /** Creates a menu item with a normal,selected  and disabled image with target/selector. */
    CC_DEPRECATED_ATTRIBUTE static MenuItemImage* create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector);
    /** Creates a menu item with a normal,selected  and disabled image with a callable object. */
    static MenuItemImage* create(const std::string&normalImage, const std::string&selectedImage, const std::string&disabledImage, const ccMenuCallback& callback);

    /** Sets the sprite frame for the normal image. */
	// 设置正藏的精灵帧
    void setNormalSpriteFrame(SpriteFrame* frame);
    /** Sets the sprite frame for the selected image. */
	// 设置选中状态的精灵帧
    void setSelectedSpriteFrame(SpriteFrame* frame);
    /** Sets the sprite frame for the disabled image. */
	// 设置不可用状态的精灵帧
    void setDisabledSpriteFrame(SpriteFrame* frame);
    
CC_CONSTRUCTOR_ACCESS:
    /**
     * @js ctor
     */
    MenuItemImage(){}
    /**
     * @js NA
     * @lua NA
     */
    virtual ~MenuItemImage(){}
    // 初始化
    bool init();
    
    /** Initializes a menu item with a normal, selected  and disabled image with target/selector. */
    CC_DEPRECATED_ATTRIBUTE bool initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, Ref* target, SEL_MenuHandler selector);
    
    /** Initializes a menu item with a normal, selected  and disabled image with a callable object. */
    bool initWithNormalImage(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, const ccMenuCallback& callback);

private:
    CC_DISALLOW_COPY_AND_ASSIGN(MenuItemImage);
};

/** @brief A MenuItemToggle.
 A simple container class that "toggles" it's inner items.
 The inner items can be any MenuItem.
 */
 // 复选框菜单对象
class CC_DLL MenuItemToggle : public MenuItem
{
public:
    /** Creates a menu item from a Array with a target selector.
     * @js NA
     * @lua NA
     */
    CC_DEPRECATED_ATTRIBUTE static MenuItemToggle * createWithTarget(Ref* target, SEL_MenuHandler selector, const Vector<MenuItem*>& menuItems);
    /** Creates a menu item from a list of items with a target/selector.
     * @js NA
     * @lua NA
     */
    CC_DEPRECATED_ATTRIBUTE static MenuItemToggle* createWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, ...)CC_REQUIRES_NULL_TERMINATION;
    
    /**
     *@brief Creates a menu item from a Array with a callable object.
     */
	 // 创建一个复选框菜单项目
    static MenuItemToggle * createWithCallback(const ccMenuCallback& callback, const Vector<MenuItem*>& menuItems);
    /** Creates a menu item from a list of items with a callable object. */
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
    // WP8 in VS2012 does not support nullptr in variable args lists and variadic templates are also not supported.
    typedef MenuItem* M;
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, M m7, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, m7, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, m7, m8, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, m7, m8, m9, NULL); }
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, M m1, M m2, M m3, M m4, M m5, M m6, M m7, M m8, M m9, M m10, std::nullptr_t listEnd) { return createWithCallbackVA(callback, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10,  NULL); }

    // On WP8 for lists longer than 10 items, use createWithArray or variadicCreate with NULL as the last argument.
    static MenuItemToggle* createWithCallbackVA(const ccMenuCallback& callback, M item, ...);
#else
    static MenuItemToggle* createWithCallback(const ccMenuCallback& callback, MenuItem* item, ...) CC_REQUIRES_NULL_TERMINATION;
#endif
    /** Creates a menu item with no target/selector and no items. */
    static MenuItemToggle* create();
    
    /** Creates a menu item with a item. */
    static MenuItemToggle* create(MenuItem *item);
    
    /** Add more menu item. */
	// 添加菜单项目
    void addSubItem(MenuItem *item);
    
    /** Return the selected item. */
	// 得到选中的 项目
    MenuItem* getSelectedItem();
    
    /**
     *@deprecated Use `getSelectedItem` instead.
     */
    CC_DEPRECATED_ATTRIBUTE MenuItem* selectedItem() { return getSelectedItem(); }

    /** Gets the index of the selected item. */
	// 得到选中项目的index
    inline unsigned int getSelectedIndex() const { return _selectedIndex; };
    
    /** Sets the index of the selected item. */
	// 设置选中项目的index
    void setSelectedIndex(unsigned int index);
    
    /** Gets the array that contains the subitems.
     *You can add/remove items in runtime, and you can replace the array with a new one.
     * @since v0.7.2
     * @js NA
     * @lua NA
     */
	 // 得到为选中的项目
    inline const Vector<MenuItem*>& getSubItems() const { return _subItems; };
	
    inline Vector<MenuItem*>& getSubItems() { return _subItems; };

    /** Sets the array that contains the subitems. */
	// 设置为选中的项目
    inline void setSubItems(const Vector<MenuItem*>& items) {
        _subItems = items;
    }
    
    // Overrides
    virtual void activate() override;
    virtual void selected() override;
    virtual void unselected() override;
    virtual void setEnabled(bool var) override;
    
CC_CONSTRUCTOR_ACCESS:
    /**
     * @js ctor
     */
    MenuItemToggle()
    : _selectedIndex(0)
    {}
    /**
     * @js NA
     * @lua NA
     */
    virtual ~MenuItemToggle();
    
    /** Initializes a menu item from a list of items with a target selector.
     * @js NA
     * @lua NA
     */
    CC_DEPRECATED_ATTRIBUTE bool initWithTarget(Ref* target, SEL_MenuHandler selector, MenuItem* item, va_list args);
    
    /** Initializes a menu item from a list of items with a callable object. */
	// 初始化
    bool initWithCallback(const ccMenuCallback& callback, MenuItem* item, va_list args);
    
    /** Initializes a menu item with a item. */
    bool initWithItem(MenuItem *item);

protected:
    /** Returns the selected item. */
    unsigned int _selectedIndex;
    /** Array that contains the subitems. You can add/remove items in runtime, and you can replace the array with a new one.
     @since v0.7.2
     */
    Vector<MenuItem*> _subItems;

private:
    CC_DISALLOW_COPY_AND_ASSIGN(MenuItemToggle);

};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: