Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This Sparrow extension class displays a texture, trimmed to its left side, depending on a ratio. This can be used to create a progress bar or a rest-time display.
#import <Foundation/Foundation.h>
#import "Sparrow.h"
/// An SXGauge displays a texture, trimmed to its left side, depending on a ratio.
/// This can be used to display a progress bar or a time gauge.
@interface SXGauge : SPSprite
/// Indicates how much of the texture is displayed. Range: 0.0f - 1.0f
@property (nonatomic, assign) float ratio;
/// Initializes a gauge with a certain texture
- (id)initWithTexture:(SPTexture*)texture;
/// Factory method.
+ (SXGauge *)gaugeWithTexture:(SPTexture *)texture;
@end
#import "SXGauge.h"
@implementation SXGauge
{
SPImage *_image;
float _ratio;
}
@synthesize ratio = _ratio;
- (id)initWithTexture:(SPTexture*)texture
{
if ((self = [super init]))
{
_ratio = 1.0f;
_image = [SPImage imageWithTexture:texture];
[self addChild:_image];
}
return self;
}
- (id)init
{
return [self initWithTexture:[SPTexture emptyTexture]];
}
- (void)update
{
_image.scaleX = _ratio;
[_image setTexCoordsWithX:_ratio y:0.0f ofVertex:1];
[_image setTexCoordsWithX:_ratio y:1.0f ofVertex:3];
}
- (void)setRatio:(float)value
{
_ratio = MAX(0.0f, MIN(1.0f, value));
[self update];
}
+ (SXGauge *)gaugeWithTexture:(SPTexture *)texture
{
return [[SXGauge alloc] initWithTexture:texture];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.