-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmenu.cpp
More file actions
144 lines (124 loc) · 4.26 KB
/
Copy pathmenu.cpp
File metadata and controls
144 lines (124 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "rubymotion.h"
#include "motion-game.h"
VALUE rb_cMenu = Qnil;
/// @class Menu < Object
/// A Menu is a way to navigate through game options.
/// Menus often contain options like Play, Quit, Settings and About.
/// This is usually in the form of buttons that are pressed.
static VALUE
menu_alloc(VALUE rcv, SEL sel)
{
cocos2d::Menu *menu = cocos2d::Menu::create();
return rb_cocos2d_object_new(menu, rcv);
}
/// @method #align_items_vertically(padding=null)
/// aligns menu items vertically with padding
/// (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
menu_align_items_vertically(VALUE rcv, SEL sel, int argc, VALUE *argv)
{
VALUE padding = Qnil;
rb_scan_args(argc, argv, "01", &padding);
if (padding != Qnil) {
MENU(rcv)->alignItemsVerticallyWithPadding(NUM2LONG(padding));
}
else {
MENU(rcv)->alignItemsVertically();
}
return rcv;
}
/// @method #align_items_horizontally(padding=null)
/// aligns menu items horizontally with padding
/// (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
menu_align_items_horizontally(VALUE rcv, SEL sel, int argc, VALUE *argv)
{
VALUE padding = Qnil;
rb_scan_args(argc, argv, "01", &padding);
if (padding != Qnil) {
MENU(rcv)->alignItemsHorizontallyWithPadding(NUM2LONG(padding));
}
else {
MENU(rcv)->alignItemsHorizontally();
}
return rcv;
}
/// @method #enabled?
/// Whether the menu is enabled. When enabled, a menu can be
/// touched or clicked. By default, a menu is enabled.
/// @return [Boolean] whether the menu is enabled.
static VALUE
menu_enabled(VALUE rcv, SEL sel)
{
return MENU(rcv)->isEnabled() ? Qtrue : Qfalse;
}
/// @method #enabled=(value)
/// Enable or disable a menu item.
/// @param value [Boolean] true if enable an item.
static VALUE
menu_enabled_set(VALUE rcv, SEL sel, VALUE val)
{
MENU(rcv)->setEnabled(RTEST(val));
return val;
}
/// @method #image_item(normal_image, selected_image)
/// Create a menu item with a normal and selected images.
/// @param normal_image [String] normal image name.
/// @param selected_image [String] selected image name.
/// @yield The methods to call when tapped menu.
/// @return [self] the receiver.
static VALUE
menu_image_item(VALUE rcv, SEL sel, VALUE normal_image, VALUE selected_image)
{
VALUE block = rb_current_block();
if (block == Qnil) {
rb_raise(rb_eArgError, "block not given");
}
block = rb_retain(block); // FIXME need release...
cocos2d::MenuItemImage *item = cocos2d::MenuItemImage::create(
RSTRING_PTR(StringValue(normal_image)), RSTRING_PTR(StringValue(selected_image)),
[block](cocos2d::Ref *sender) {
rb_block_call(block, 0, NULL);
});
MENU(rcv)->addChild(item);
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)
{
rb_cMenu = rb_define_class_under(rb_mMC, "Menu", rb_cNode);
// rb_register_cocos2d_object_finalizer(rb_cMenu); removed because rb_cMenu inherits rb_cNode and it already has finalizer.
rb_define_singleton_method(rb_cMenu, "alloc", menu_alloc, 0);
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);
}