Skip to content

Instantly share code, notes, and snippets.

@ShadowBelmolve
Created January 3, 2011 03:01
Show Gist options
  • Save ShadowBelmolve/763073 to your computer and use it in GitHub Desktop.
Save ShadowBelmolve/763073 to your computer and use it in GitHub Desktop.
@import <Foundation/CPObject.j>
@import "PageView.j"
@import "PhotoInspector.j"
@implementation AppController : CPObject
{
}
- (void)applicationDidFinishLaunching:(CPNotification)aNotification
{
var theWindow = [[CPWindow alloc]
initWithContentRect:CGRectMakeZero()
styleMask:CPBorderlessBridgeWindowMask],
contentView = [theWindow contentView];
[contentView setBackgroundColor:[CPColor blackColor]];
[theWindow orderFront:self];
var bounds = [contentView bounds],
pageView = [[PageView alloc] initWithFrame:
CGRectMake(CGRectGetWidth(bounds) / 2
- 200, CGRectGetHeight(bounds) / 2 - 200,
400, 400)];
[pageView setAutoresizingMask: CPViewMinXMargin |
CPViewMaxXMargin |
CPViewMinYMargin |
CPViewMaxYMargin];
[contentView addSubview:pageView];
var label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[label setTextColor:[CPColor whiteColor]];
[label setStringValue:@"Double Click to Edit Photo"];
[label sizeToFit];
[label setFrameOrigin:CGPointMake(CGRectGetWidth(bounds) / 2 -
CGRectGetWidth([label frame]) / 2,
CGRectGetMinY([pageView frame]) -
CGRectGetHeight([label frame]))];
[label setAutoresizingMask: CPViewMinXMargin |
CPViewMaxXMargin |
CPViewMinYMargin |
CPViewMaxYMargin];
[contentView addSubview:label];
}
@end
@import <AppKit/CPView.j>
@implementation PageView : CPView
{
CALayer _rootLayer;
CALayer _borderLayer;
PaneLayer _paneLayer;
}
- (id)initWithFrame:(CGRect)aFrame
{
self = [super initWithFrame:aFrame];
if (self)
{
_rootLayer = [CALayer layer];
[self setWantsLayer:YES];
[self setLayer:_rootLayer];
[_rootLayer setBackgroundColor:
[CPColor whiteColor]];
_paneLayer = [[PaneLayer alloc]
initWithPageView:self];
[_paneLayer setBounds:CGRectMake(0, 0,
400 - 2 * 40, 400. - 2 * 40)];
[_paneLayer setAnchorPoint:CGPointMakeZero()];
[_paneLayer setPosition:CGPointMake(40, 40)];
[_paneLayer setImage:[[CPImage alloc]
initWithContentsOfFile:
@"Resources/sample.jpg"
size:CGSizeMake(500, 430)]];
[_rootLayer addSublayer:_paneLayer];
[_paneLayer setNeedsDisplay];
[_rootLayer setNeedsDisplay];
_borderLayer = [CALayer layer];
[_borderLayer setAnchorPoint:CGPointMakeZero()];
[_borderLayer setBounds:[self bounds]];
[_borderLayer setDelegate:self];
[_rootLayer addSublayer:_borderLayer];
}
return self;
}
- (void)drawLayer:(CALayer)aLayer inContext:(CGContext)aContext
{
CGContextSetFillColor(aContext, [CPColor whiteColor]);
var bounds = [aLayer bounds],
width = CGRectGetWidth(bounds),
height = CGRectGetHeight(bounds);
CGContextFillRect(aContext, CGRectMake(0, 0, width, 40));
CGContextFillRect(aContext, CGRectMake(0, 40,
40, height - 2 * 40));
CGContextFillRect(aContext, CGRectMake(width - 40, 40,
40, height - 2 * 40));
CGContextFillRect(aContext, CGRectMake(0, height - 40,
width, 40));
}
- (void)setEditing:(BOOL)isEditing
{
[_borderLayer setOpacity:isEditing ? 0.5 : 1.0];
}
- (void)mouseDown:(CPEvent)anEvent
{
if ([anEvent clickCount] == 2)
[PhotoInspector inspectPaneLayer:_paneLayer];
}
@end
@implementation PaneLayer : CALayer
{
float _rotationRadians;
float _scale;
CPImage _image;
CALayer _imageLayer;
PageView _pageView;
}
- (id)initWithPageView:(PageView)aPageView
{
self = [super init];
if (self)
{
_pageView = aPageView;
_rotationRadians = 0.0;
_scale = 1.0;
_imageLayer = [CALayer layer];
[_imageLayer setDelegate:self];
[self addSublayer:_imageLayer];
}
return self;
}
- (PageView)pageView
{
return _pageView;
}
- (void)setBounds:(CGRect)aRect
{
[super setBounds:aRect];
[_imageLayer setPosition:
CGPointMake(CGRectGetMidX(aRect),
CGRectGetMidY(aRect))];
}
- (void)setImage:(CPImage)anImage
{
if (_image == anImage)
return;
_image = anImage;
if (_image)
[_imageLayer setBounds:CGRectMake(0.0, 0.0,
[_image size].width, [_image size].height)];
[_imageLayer setNeedsDisplay];
}
- (void)setRotationRadians:(float)radians
{
if (_rotationRadians == radians)
return;
_rotationRadians = radians;
[_imageLayer setAffineTransform:CGAffineTransformScale(
CGAffineTransformMakeRotation(_rotationRadians),
_scale, _scale)];
}
- (void)setScale:(float)aScale
{
if (_scale == aScale)
return;
_scale = aScale;
[_imageLayer setAffineTransform:CGAffineTransformScale(
CGAffineTransformMakeRotation(_rotationRadians),
_scale, _scale)];
}
- (void)drawInContext:(CGContext)aContext
{
CGContextSetFillColor(aContext, [CPColor grayColor]);
CGContextFillRect(aContext, [self bounds]);
}
- (void)imageDidLoad:(CPImage)anImage
{
[_imageLayer setNeedsDisplay];
}
- (void)drawLayer:(CALayer)aLayer inContext:(CGContext)aContext
{
var bounds = [aLayer bounds];
if ([_image loadStatus] !=
CPImageLoadStatusCompleted)
[_image setDelegate:self];
else
CGContextDrawImage(aContext, bounds, _image);
}
- (void)imageDidLoad:(CPImage)anImage
{
[_imageLayer setNeedsDisplay];
}
- (void)setImage:(CPImage)anImage
{
if (_image == anImage)
return;
_image = anImage;
if (_image)
[_imageLayer setBounds:CGRectMake(0.0, 0.0,
[_image size].width, [_image size].height)];
[_imageLayer setNeedsDisplay];
}
- (void)setRotationRadians:(float)radians
{
if (_rotationRadians == radians)
return;
_rotationRadians = radians;
[_imageLayer setAffineTransform:CGAffineTransformScale(
CGAffineTransformMakeRotation(_rotationRadians),
_scale, _scale)];
}
- (void)setScale:(float)aScale
{
if (_scale == aScale)
return;
_scale = aScale;
[_imageLayer setAffineTransform:CGAffineTransformScale(
CGAffineTransformMakeRotation(_rotationRadians),
_scale, _scale)];
}
@end
@import <AppKit/CPPanel.j>
@import <AppKit/CPWindowController.j>
var PhotoInspectorSharedInstance = nil;
@implementation PhotoInspector : CPWindowController
{
CPSlider _scaleSlider;
CPSlider _rotationSlider;
PaneLayer _paneLayer;
}
+ (PhotoInspector)sharedPhotoInspector
{
if (!PhotoInspectorSharedInstance)
PhotoInspectorSharedInstance = [[PhotoInspector alloc] init];
return PhotoInspectorSharedInstance;
}
- (void)setPaneLayer:(PaneLayer)anPaneLayer
{
if (_paneLayer == anPaneLayer)
return;
[[_paneLayer pageView] setEditing:NO];
_paneLayer = anPaneLayer;
var page = [_paneLayer pageView];
[page setEditing:YES];
if (_paneLayer)
{
var frame = [page convertRect:[page bounds] toView:nil],
windowSize = [[self window] frame].size;
[[self window] setFrameOrigin:
CGPointMake(CGRectGetMidX(frame) -
windowSize.width / 2.0, CGRectGetMidY(frame))];
}
}
- (id)init
{
var theWindow = [[CPPanel alloc]
initWithContentRect:CGRectMake(0, 0, 225, 125)
styleMask:CPHUDBackgroundWindowMask | CPClosableWindowMask];
self = [super initWithWindow:theWindow];
if (self)
{
[theWindow setTitle:@"Inspector"];
[theWindow setFloatingPanel:YES];
[theWindow setDelegate:self];
var contentView = [theWindow contentView],
centerX = (CGRectGetWidth([contentView bounds]) - 135) / 2;
scaleSlider = [[CPSlider alloc]
initWithFrame:CGRectMake(centerX, 13, 135, 16)];
[scaleSlider setMinValue:50];
[scaleSlider setMaxValue:150];
[scaleSlider setValue:100];
[scaleSlider setTarget:self];
[scaleSlider setAction:@selector(scale:)];
[contentView addSubview:scaleSlider];
var scaleStartLabel = [self labelWithTitle:"50%"],
scaleEndLabel = [self labelWithTitle:"150%"];
[scaleStartLabel setFrameOrigin:CGPointMake(centerX -
CGRectGetWidth([scaleStartLabel frame]), 10)];
[scaleEndLabel setFrameOrigin:
CGPointMake(CGRectGetMaxX([scaleSlider frame]), 10)];
[contentView addSubview:scaleStartLabel];
[contentView addSubview:scaleEndLabel];
rotationSlider = [[CPSlider alloc]
initWithFrame:CGRectMake(centerX, 43, 135, 16)];
[rotationSlider setMinValue:0];
[rotationSlider setMaxValue:360];
[rotationSlider setValue:0];
[rotationSlider setTarget:self];
[rotationSlider setAction:@selector(rotate:)];
[contentView addSubview:rotationSlider];
var rotationStartLabel = [self labelWithTitle:"0\u00B0"],
rotationEndLabel = [self labelWithTitle:"360\u00B0"];
[rotationStartLabel setFrameOrigin:CGPointMake(centerX -
CGRectGetWidth([rotationStartLabel frame]), 40)];
[rotationEndLabel setFrameOrigin:
CGPointMake(CGRectGetMaxX([rotationSlider frame]), 40)];
[contentView addSubview:rotationStartLabel];
[contentView addSubview:rotationEndLabel];
}
return self;
}
- (CPTextField)labelWithTitle:(CPString)aTitle
{
var label = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[label setStringValue:aTitle];
[label setTextColor:[CPColor whiteColor]];
[label sizeToFit];
return label;
}
- (void)scale:(id)aSender
{
[_paneLayer setScale:[aSender value] / 100.0];
}
- (void)rotate:(id)aSender
{
[_paneLayer setRotationRadians:PI / 180 * [aSender value]];
}
+ (void)inspectPaneLayer:(PaneLayer)anPaneLayer
{
var inspector = [self sharedPhotoInspector];
[inspector setPaneLayer:anPaneLayer];
[inspector showWindow:self];
}
- (void)windowWillClose:(id)aSender
{
[self setPaneLayer:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment