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

Adding a progress indicator to CCSlider #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
43 changes: 43 additions & 0 deletions Extensions/CCSlider/CCSlider.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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
Expand All @@ -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
78 changes: 71 additions & 7 deletions Extensions/CCSlider/CCSlider.m
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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
{
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -221,7 +286,6 @@ -(BOOL) ccMouseDown:(NSEvent*)event
return isTouchHandled; // YES for events I handle
}


-(BOOL) ccMouseDragged:(NSEvent*)event
{
CGPoint location = [self locationFromEvent: event];
Expand Down