Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Last active September 25, 2015 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PrimaryFeather/867241 to your computer and use it in GitHub Desktop.
Save PrimaryFeather/867241 to your computer and use it in GitHub Desktop.
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