iTunes like 'diamond' progress indicator
iTunes like 'diamond' progress indicator
- Subject: iTunes like 'diamond' progress indicator
- From: Nick Sayer <email@hidden>
- Date: Tue, 17 Feb 2004 09:45:39 -0800
I don't suppose this will be terribly interesting, but I thought I'd share.
I wanted an NSProgressIndicator that looked like the one in iTunes - a
rectangle with a little diamond mark that travels through it.
So this is what I came up with. My prooject is Java/Cocoa, but this
ought to be downright trivial to duplicate in Objective C. For one
thing, you don't have to duplicate the constructors like you do in Java. :-)
Obviously, the meat is in drawRect.
Interestingly, I found I also had to override setDoubleValue to force it
to redraw when the value changed. Not sure why.
As a special bonus, you can hide it by making it indeterminate.
/* DiamondProgressIndicator */
import com.apple.cocoa.foundation.*;
import com.apple.cocoa.application.*;
public class DiamondProgressIndicator extends NSProgressIndicator {
public DiamondProgressIndicator() {
super();
}
public DiamondProgressIndicator(NSRect rect) {
super(rect);
}
// If you don't declare and super() these two constructors, very bad things happen.
protected DiamondProgressIndicator(boolean a, int b) {
super(a,b);
}
protected DiamondProgressIndicator(NSCoder a, long b) {
super(a,b);
}
// The size factor is how much smaller the diamond is than the height of the bounds.
// It looks a little better not being full size.
private static float sizeFactor = 0.2F;
public void drawRect(NSRect aRect) {
if (this.isIndeterminate())
return;
NSRect bounds = this.bounds();
NSBezierPath p = NSBezierPath.bezierPathWithRect(bounds);
//NSColor.colorWithCalibratedRGB(0.9F, 0.9F, 0.8F, 1.0F).set();
//p.fill();
NSColor.blackColor().set();
p.stroke();
float height = bounds.height();
float where = (float)(this.doubleValue() - this.minValue());
if (where > this.maxValue())
where = (float)this.maxValue();
else if (where < this.minValue())
where = (float)this.minValue();
where /= (maxValue() - minValue()); // make it a fractional value
where *= bounds.width();
float whereX = where + bounds.x();
p = new NSBezierPath();
p.moveToPoint(new NSPoint(whereX, bounds.maxY() - (sizeFactor * bounds.height()) ));
p.lineToPoint(new NSPoint(whereX + (bounds.height() / 2.0F) - (sizeFactor * bounds.height()), bounds.midY()));
p.lineToPoint(new NSPoint(whereX, bounds.y() + (sizeFactor * bounds.height())));
p.lineToPoint(new NSPoint(whereX - (bounds.height() / 2.0F) + (sizeFactor * bounds.height()), bounds.midY()));
p.closePath();
p.fill();
}
public void setDoubleValue(double v) {
super.setDoubleValue(v);
this.setNeedsDisplay(true);
}
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.