Skip to content

Instantly share code, notes, and snippets.

@Ramshandilya
Created December 24, 2013 12:02
Show Gist options
  • Save Ramshandilya/8112314 to your computer and use it in GitHub Desktop.
Save Ramshandilya/8112314 to your computer and use it in GitHub Desktop.
A grid layout menu for Cocos2d using CCMenu
@interface CCMenu (GridLayout)
- (void)alignItemsInGridWithPadding:(CGPoint)padding columns:(NSInteger)columns;
@end
#import "CCMenu+GridLayout.h"
@implementation CCMenu (GridLayout)
- (void)alignItemsInGridWithPadding:(CGPoint)padding columns:(NSInteger)columns
{
CCMenuItem *item = [_children objectAtIndex:0];
CGFloat contentWidth = item.contentSize.width * item.scaleX;
CGFloat contentHeight = item.contentSize.height * item.scaleY;
// set content size
NSInteger count = _children.count;
NSInteger numRows = (count + columns - 1) / columns;
NSInteger numCols = MIN(count, columns);
CGFloat height = contentHeight * numRows + padding.y * (numRows - 1);
CGFloat width = contentWidth * numCols + padding.x * (numCols - 1);
[self setContentSize:CGSizeMake(width, height)];
// layout menu items
NSInteger row = 0;
NSInteger col = 0;
CCARRAY_FOREACH(_children, item) {
CGFloat x = (contentWidth + padding.x) * col + contentWidth * 0.5f;
CGFloat y = height - (contentHeight + padding.y) * row - contentHeight * 0.5f;
[item setPosition:ccp(x, y)];
if(++col == columns) {
col = 0;
row++;
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment