From 44e810c81f88f5d3e4afd1c0dd78b4e1e8d4eec5 Mon Sep 17 00:00:00 2001 From: Ali Hafizji Date: Tue, 15 Nov 2011 18:11:42 +0530 Subject: [PATCH] Adding the ability to include progress in CCSlider. This way the user can specify the progress color and it will automatically expand as the slider moves. --- Extensions/CCSlider/CCSlider.h | 43 +++++++++++++++++++ Extensions/CCSlider/CCSlider.m | 78 +++++++++++++++++++++++++++++++--- 2 files changed, 114 insertions(+), 7 deletions(-) diff --git a/Extensions/CCSlider/CCSlider.h b/Extensions/CCSlider/CCSlider.h index 1424eb9..2ec2950 100644 --- a/Extensions/CCSlider/CCSlider.h +++ b/Extensions/CCSlider/CCSlider.h @@ -44,6 +44,9 @@ static const NSInteger kCCSliderPriority = kCCMenuTouchPriority - 2; // weak links to children CCMenuItem *_thumb; CCSprite *_bg; + CCSprite *_progress; + + CGSize _progressPadding; } /** Current chosen value, min is 0.0f, max is 1.0f. */ @@ -61,6 +64,18 @@ static const NSInteger kCCSliderPriority = kCCMenuTouchPriority - 2; */ +(id) sliderWithBackgroundSprite: (CCSprite *) bgSprite thumbMenuItem: (CCMenuItem *) aThumb; +/** Creates slider with given bg sprite, progress color and thumb. + * + * @see initWithBackgroundSprite: sliderProgressColor sliderPadding thumbMenuItem + */ ++(id) sliderWithBackgroundSprite: (CCSprite *) bgSprite sliderProgressColor:(ccColor4F) progressColor sliderPadding:(CGSize) padding thumbMenuItem: (CCMenuItem *) aThumb; + +/* Creates a slider with background image filename, progress color & thumb image filename. + * + * @see initWithBackgroundFile: sliderProgressColor: sliderPadding: thumbFile: + */ ++ (id) sliderWithBackgroundFile:(NSString *)bgFile sliderProgressColor:(ccColor4F) progressColor sliderPadding:(CGSize) padding thumbFile:(NSString *)thumbFile; + /** Easy init - filenames instead of CCSprite & CCMenuItem. Uses designated init inside. * * @param thumbFile Filename, that is used to create normal & selected images for @@ -80,4 +95,32 @@ static const NSInteger kCCSliderPriority = kCCMenuTouchPriority - 2; */ -(id) initWithBackgroundSprite: (CCSprite *) bgSprite thumbMenuItem: (CCMenuItem *) aThumb; +/** + * @param bgSprite CCSprite, that is used as a background. It's bounding box is used + * to determine max & min x position for a thumb menu item. + * + * @param progressColor ccColor4F, this is used to create a sprite of a color. This will represent + * the progress. + * + * @param padding CGSize, this represents the total internal padding to give for the progress. The padding + * will be relative to the bgSprite + * + * @param aThumb MenuItem that is used as a thumb. Used without CCMenu, so CCMenuItem#activate + * doesn't get called. + */ +- (id) initWithBackgroundSprite: (CCSprite *) bgSprite sliderProgressColor:(ccColor4F) progressColor sliderPadding:(CGSize) padding thumbMenuItem: (CCMenuItem *) aThumb; + +/** + * @param bgFile Filename for background CCSprite. + * + * @param progressColor ccColor4F, this is used to create a sprite of a color. This will represent + * the progress. + * + * @param padding CGSize, this represents the total internal padding to give for the progress. The padding + * will be relative to the bgSprite + * + * @param thumbFile Filename, that is used to create normal & selected images for + * thumbMenuItem. Selected sprite is darker than normal sprite. + */ +- (id) initWithBackgroundFile:(NSString *)bgFile sliderProgressColor:(ccColor4F) progressColor sliderPadding:(CGSize) padding thumbFile:(NSString *)thumbFile; @end diff --git a/Extensions/CCSlider/CCSlider.m b/Extensions/CCSlider/CCSlider.m index 6b7ee6f..2e022b5 100644 --- a/Extensions/CCSlider/CCSlider.m +++ b/Extensions/CCSlider/CCSlider.m @@ -18,7 +18,7 @@ * 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. + * all copi*es 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, @@ -32,6 +32,12 @@ #import "CCSlider.h" +#define kThumbTag 1 + +@interface CCSlider(Private) +- (void) scaleProgressIndicator; +- (CCSprite *)spriteWithColor:(ccColor4F)bgColor textureSize:(CGSize)textureSize; +@end @implementation CCSlider @@ -48,6 +54,28 @@ +(id) sliderWithBackgroundSprite: (CCSprite *) bgSprite thumbMenuItem: (CCMenuIt thumbMenuItem: aThumb ] autorelease ]; } ++ (id) sliderWithBackgroundFile:(NSString *)bgFile sliderProgressColor:(ccColor4F) progressColor sliderPadding:(CGSize) padding thumbFile:(NSString *)thumbFile +{ + return [[self alloc] initWithBackgroundFile:bgFile sliderProgressColor:progressColor sliderPadding:padding thumbFile:thumbFile]; +} + ++(id) sliderWithBackgroundSprite: (CCSprite *) bgSprite sliderProgressColor:(ccColor4F) progressColor sliderPadding:(CGSize) padding thumbMenuItem: (CCMenuItem *) aThumb +{ + return [[[self alloc] initWithBackgroundSprite:bgSprite sliderProgressColor:progressColor sliderPadding:padding thumbMenuItem:aThumb] autorelease]; +} + +- (id) initWithBackgroundFile:(NSString *)bgFile sliderProgressColor:(ccColor4F) progressColor sliderPadding:(CGSize) padding thumbFile:(NSString *)thumbFile +{ + self = [self initWithBackgroundFile:bgFile thumbFile:thumbFile]; + if(self) { + _progressPadding = padding; + _progress = [self spriteWithColor:progressColor textureSize:CGSizeMake(_thumb.position.x, _bg.contentSize.height - padding.height)]; + _progress.position = CGPointMake(_thumb.position.x/2 + padding.width/2, _bg.position.y); + [self addChild:_progress z:1]; + } + return self; +} + // Easy init - (id) initWithBackgroundFile: (NSString *) bgFile thumbFile: (NSString *) thumbFile { @@ -72,6 +100,27 @@ - (id) initWithBackgroundFile: (NSString *) bgFile thumbFile: (NSString *) thumb return nil; } +- (CCSprite *)spriteWithColor:(ccColor4F)bgColor textureSize:(CGSize)textureSize { + + CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize.width height:textureSize.height]; + [rt beginWithClear:bgColor.r g:bgColor.g b:bgColor.b a:bgColor.a]; + [rt end]; + return [CCSprite spriteWithTexture:rt.sprite.texture]; +} + +- (id) initWithBackgroundSprite: (CCSprite *) bgSprite sliderProgressColor:(ccColor4F) progressColor sliderPadding:(CGSize) padding thumbMenuItem: (CCMenuItem *) aThumb +{ + self = [self initWithBackgroundSprite:bgSprite thumbMenuItem:aThumb]; + + if (self) { + _progressPadding = padding; + _progress = [self spriteWithColor:progressColor textureSize:CGSizeMake(_thumb.position.x, _bg.contentSize.height - padding.height)]; + _progress.position = CGPointMake(_thumb.position.x/2 + padding.width/2, _bg.position.y); + [self addChild:_progress z:1]; + } + return self; +} + // Designated init -(id) initWithBackgroundSprite: (CCSprite *) bgSprite thumbMenuItem: (CCMenuItem *) aThumb { @@ -99,8 +148,9 @@ -(id) initWithBackgroundSprite: (CCSprite *) bgSprite thumbMenuItem: (CCMenuItem thumbSize = [_thumb contentSize]; minX = thumbSize.width / 2; maxX = [self contentSize].width - thumbSize.width / 2; - _thumb.position = CGPointMake(minX, [self contentSize].height / 2); - [self addChild:_thumb]; + _thumb.position = CGPointMake(minX, [self contentSize].height / 2); + [_thumb setTag:kThumbTag]; + [self addChild:_thumb z:2]; } return self; } @@ -127,7 +177,21 @@ - (void) setValue:(float) newValue CCMenuItem *thumb = _thumb; CGPoint pos = thumb.position; pos.x = minX + newValue * (maxX - minX); - thumb.position = pos; + thumb.position = pos; + [self scaleProgressIndicator]; +} + +- (void) scaleProgressIndicator +{ + if (_progress) { + if (_progress) { + CCSprite *progress = _progress; + CGSize size = progress.contentSize; + size.width = _thumb.position.x; + progress.scaleX = size.width/progress.contentSize.width; + progress.position = CGPointMake(_thumb.position.x/2 + _progressPadding.width/2, _bg.position.y); + } + } } - (NSInteger) mouseDelegatePriority @@ -182,15 +246,16 @@ -(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event if ((location.x < minX) || (location.x > maxX)) return; - CCSprite *thumb = (CCSprite *)[[self children] objectAtIndex:1]; + CCSprite *thumb = (CCSprite *)[self getChildByTag:kThumbTag]; CGPoint pos = thumb.position; pos.x = location.x; thumb.position = pos; + [self scaleProgressIndicator]; } -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { - CCSprite *thumb = (CCSprite *)[[self children] objectAtIndex:1]; + CCSprite *thumb = (CCSprite *)[self getChildByTag:kThumbTag]; [_thumb unselected]; self.value = (thumb.position.x - minX) / (maxX - minX); } @@ -221,7 +286,6 @@ -(BOOL) ccMouseDown:(NSEvent*)event return isTouchHandled; // YES for events I handle } - -(BOOL) ccMouseDragged:(NSEvent*)event { CGPoint location = [self locationFromEvent: event];