Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to create a MenuItemFont with Menu #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ menu_alloc(VALUE rcv, SEL sel)

/// @method #align_items_vertically(padding=null)
/// aligns menu items vertically with padding
/// (call after adding items via image_item)
/// (call after adding items via image_item or font_item)
/// @param padding [Float] the amount of padding between the items.
/// @return [self] the receiver.
static VALUE
Expand All @@ -37,7 +37,7 @@ menu_align_items_vertically(VALUE rcv, SEL sel, int argc, VALUE *argv)

/// @method #align_items_horizontally(padding=null)
/// aligns menu items horizontally with padding
/// (call after adding items via image_item)
/// (call after adding items via image_item or font_item)
/// @param padding [Float] the amount of padding between the items.
/// @return [self] the receiver.
static VALUE
Expand Down Expand Up @@ -102,6 +102,31 @@ menu_image_item(VALUE rcv, SEL sel, VALUE normal_image, VALUE selected_image)
return rcv;
}

/// @method #font_item(name)
/// Create a menu item with a label.
/// @param name [String] label name.
/// @yield The methods to call when tapped menu.
/// @return [self] the receiver.

static VALUE
menu_font_item(VALUE rcv, SEL sel, VALUE label_name)
{
VALUE block = rb_current_block();
if (block == Qnil) {
rb_raise(rb_eArgError, "block not given");
}
block = rb_retain(block); // FIXME need release...

cocos2d::MenuItemFont *item = cocos2d::MenuItemFont::create(
RSTRING_PTR(StringValue(label_name)),
[block](cocos2d::Ref *sender) {
rb_block_call(block, 0, NULL);
});

MENU(rcv)->addChild(item);
return rcv;
}

extern "C"
void
Init_Menu(void)
Expand All @@ -113,6 +138,7 @@ Init_Menu(void)
rb_define_method(rb_cMenu, "enabled?", menu_enabled, 0);
rb_define_method(rb_cMenu, "enabled=", menu_enabled_set, 1);
rb_define_method(rb_cMenu, "image_item", menu_image_item, 2);
rb_define_method(rb_cMenu, "font_item", menu_font_item, 1);
rb_define_method(rb_cMenu, "align_items_horizontally", menu_align_items_horizontally, -1);
rb_define_method(rb_cMenu, "align_items_vertically", menu_align_items_vertically, -1);
}