Hi guys, I've been using cocos2d for a while (still learning) but I've just created a class that I needed which is:
A menu item that uses a Label (text) as well as Images so that you can use normal image, selected image, and disabled image. This is so you can create buttons with labels dynamically instead of making a 1000 buttons in photoshop by just changing its text.
If you observe, the text label actually moves down by 1 pixel when you press the menu item, that's meant to happen, to simulate the button being pressed.
The class is created as a cross between CCMenuItemLabel and CCMenuItemImage. It is in fact a child of the CCMenuItemImage and I've added the label capability within.
Maybe this has been created already but I couldn't find it. I hope people can benefit from this :) It works really well for me.
CCMenuItemLabelAndImage.h
/*
* CCMenuItemLabelAndImage
*
* Copyright (c) 2011 Jamorn Horathai
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface CCMenuItemLabelAndImage : CCMenuItemImage {
CCNode<CCLabelProtocol, CCRGBAProtocol> *label_;
ccColor3B colorBackup;
ccColor3B disabledColor_;
}
/** the color that will be used to disable the item */
@property (nonatomic,readwrite) ccColor3B disabledColor;
/** Label that is rendered. It can be any CCNode that implements the CCLabelProtocol */
@property (nonatomic,readwrite,assign) CCNode<CCLabelProtocol, CCRGBAProtocol>* label;
/** creates a menu item with a label and a normal and selected image*/
+ (id) itemWithLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage:(NSString*)value selectedImage:(NSString*) value2;
/** creates a menu item with a label and a normal and selected image with target/selector */
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 target:(id) r selector:(SEL) s;
/** creates a menu item with a label and a normal,selected and disabled image */
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage: (NSString*) value3;
/** creates a menu item with a label and a normal,selected and disabled image with target/selector */
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage:(NSString*) value3 target:(id) r selector:(SEL) s;
/** initializes a menu item with a label and a normal, selected and disabled image with target/selector */
-(id) initFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*) value selectedImage:(NSString*)value2 disabledImage:(NSString*) value3 target:(id) r selector:(SEL) s;
#if NS_BLOCKS_AVAILABLE
/** creates a menu item with a label and a normal and selected image with a block.
The block will be "copied".
*/
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 block:(void(^)(id sender))block;
/** creates a menu item with a label and a normal,selected and disabled image with a block.
The block will be "copied".
*/
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage:(NSString*) value3 block:(void(^)(id sender))block;
/** initializes a menu item with a label and a normal, selected and disabled image with a block.
The block will be "copied".
*/
-(id) initFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*) value selectedImage:(NSString*)value2 disabledImage:(NSString*) value3 block:(void(^)(id sender))block;
#endif
/** sets a new string to the inner label */
-(void) setString:(NSString*)label;
/** Enable or disabled the CCMenuItemFont
@warning setIsEnabled changes the RGB color of the font
*/
-(void) setIsEnabled: (BOOL)enabled;
@end
CCMenuItemLabelAndImage.m
/*
* CCMenuItemLabelAndImage
*
* Copyright (c) 2011 Jamorn Horathai
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#import "CCMenuItemLabelAndImage.h"
@interface CCMenuItemLabelAndImage()
- (void) repositionLabel;
@end
@implementation CCMenuItemLabelAndImage
@synthesize disabledColor = disabledColor_;
+ (id) itemWithLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage:(NSString*)value selectedImage:(NSString*) value2
{
return [self itemFromLabel:label normalImage:value selectedImage:value2 disabledImage:nil target:nil selector:nil];
}
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 target:(id) r selector:(SEL) s
{
return [self itemFromLabel:label normalImage:value selectedImage:value2 disabledImage:nil target:r selector:s];
}
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage: (NSString*) value3
{
return [[[self alloc] initFromLabel:label normalImage:value selectedImage:value2 disabledImage:value3 target:nil selector:nil] autorelease];
}
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage:(NSString*) value3 target:(id) r selector:(SEL) s
{
return [[[self alloc] initFromLabel:label normalImage:value selectedImage:value2 disabledImage:value3 target:r selector:s] autorelease];
}
-(id) initFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*) value selectedImage:(NSString*)value2 disabledImage:(NSString*) value3 target:(id) r selector:(SEL) s
{
if ((self = [super initFromNormalImage:value selectedImage:value2 disabledImage:value3 target:r selector:s])) {
colorBackup = ccWHITE;
disabledColor_ = ccWHITE;
self.label = label;
}
return self;
}
#if NS_BLOCKS_AVAILABLE
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 block:(void(^)(id sender))block
{
return [self itemFromLabel:label normalImage:value selectedImage:value2 disabledImage:nil block:block];
}
+(id) itemFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*)value selectedImage:(NSString*) value2 disabledImage:(NSString*) value3 block:(void(^)(id sender))block
{
return [[[self alloc] initFromLabel:label normalImage:value selectedImage:value2 disabledImage:value3 block:block] autorelease];
}
-(id) initFromLabel:(CCNode<CCLabelProtocol,CCRGBAProtocol>*)label normalImage: (NSString*) value selectedImage:(NSString*)value2 disabledImage:(NSString*) value3 block:(void(^)(id sender))block
{
block_ = [block copy];
return [self initFromLabel:label normalImage:value selectedImage:value2 disabledImage:value3 target:block_ selector:@selector(ccCallbackBlockWithSender:)];
}
#endif // NS_BLOCKS_AVAILABLE
-(CCNode<CCLabelProtocol, CCRGBAProtocol>*) label
{
return label_;
}
-(void) setLabel:(CCNode<CCLabelProtocol, CCRGBAProtocol>*) label
{
if( label != label_ ) {
[self removeChild:label_ cleanup:YES];
[self addChild:label];
label_ = label;
[self repositionLabel];
}
}
-(void) setString:(NSString *)string
{
[label_ setString:string];
[self repositionLabel];
}
-(void) setPosition:(CGPoint)position
{
[super setPosition:position];
[self repositionLabel];
}
-(void) repositionLabel
{
label_.position = ccp(normalImage_.position.x + normalImage_.contentSize.width/2, normalImage_.position.y + normalImage_.contentSize.height/2);
}
-(void) selected
{
// subclass to change the default action
if(isEnabled_) {
[super selected];
// Move the label down 1 point to look like the button's pressed.
label_.position = ccp(label_.position.x, label_.position.y-1);
}
}
-(void) unselected
{
// subclass to change the default action
if(isEnabled_) {
[super unselected];
// Move the label back up
label_.position = ccp(label_.position.x, label_.position.y+1);
}
}
-(void) setIsEnabled: (BOOL)enabled
{
if( isEnabled_ != enabled ) {
if(enabled == NO) {
colorBackup = [label_ color];
[label_ setColor: disabledColor_];
}
else
[label_ setColor:colorBackup];
}
[super setIsEnabled:enabled];
}
- (void) setOpacity: (GLubyte)opacity
{
[label_ setOpacity:opacity];
}
-(GLubyte) opacity
{
return [label_ opacity];
}
-(void) setColor:(ccColor3B)color
{
[label_ setColor:color];
}
-(ccColor3B) color
{
return [label_ color];
}
@end