Skip to content

Instantly share code, notes, and snippets.

@dbrajkovic
Created March 22, 2011 05:08
Show Gist options
  • Save dbrajkovic/880807 to your computer and use it in GitHub Desktop.
Save dbrajkovic/880807 to your computer and use it in GitHub Desktop.
AppBadgeViewDragType = "AppBadgeViewDragType";
function hypot(x,y) {
var hyp = Math.sqrt(x*x + y*y);
return hyp;
}
@implementation AppBadgeItemView : CPView
{
CPImageView imageView;
CPTextField textField;
CPEvent mouseDownEvent;
}
- (void)setRepresentedObject:(id)anObject
{
if (!imageView)
{
imageView = [[CPImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, 80.0, 80.0)];
[imageView setImageScaling:CPScaleProportionally];
[self addSubview:imageView];
}
if (!textField)
{
textField = [[CPTextField alloc] initWithFrame:CGRectMake(10.0, 96.0, 80.0, 20.0)];
[textField setFont:[CPFont boldSystemFontOfSize:12.0]];
[textField setAlignment:CPCenterTextAlignment];
[textField setValue:CGSizeMake(0.0, 1.0) forThemeAttribute:@"text-shadow-offset"];
[textField setValue:[CPColor colorWithCalibratedWhite:79.0 / 255.0 alpha:1.0] forThemeAttribute:@"text-color"];
[textField setValue:[CPColor colorWithCalibratedWhite:240.0 / 255.0 alpha:1.0] forThemeAttribute:@"text-shadow-color"];
[self addSubview:textField];
}
[imageView setImage:[anObject image]];
[textField setStringValue:[anObject name]];
}
- (void)setSelected:(BOOL)isSelected
{
[self setBackgroundColor:isSelected ? [CPColor blueColor] : nil];
[textField setTextColor:isSelected ? [CPColor whiteColor] : [CPColor blackColor]];
}
- (void)mouseDown:(CPEvent)event
{
mouseDownEvent = event;
}
- (void)mouseDragged:(CPEvent)event
{
var down = [mouseDownEvent locationInWindow];
var drag = [event locationInWindow];
var distance = hypot(down.x - drag.x, down.y - drag.y);
if (distance < 3) {
return;
}
down.x -= [self frame].origin.x + 40;
down.y -= [self frame].size.height - 30;
var pb = [CPPasteboard pasteboardWithName:CPDragPboard];
[self dragImage:[imageView image]
at:down
offset:CGSizeMakeZero()
event:mouseDownEvent
pasteboard:pb
source:self
slideBack:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment