Cocoaセミナーグラフィックス編@Apple Inc

投稿日:2007/12/20 04:29:55

過去の記事は移行時の問題により、画像が表示されなかったり、レイアウトが崩れたりしている可能性があります。

Cocoaセミナーグラフィックス編 http://developer.apple.com/jp/briefing/cocoa.html というわけで東京にいってきました。 アップル社のなかのアップルマークが撮りたかったけど、携帯充電がギリギリで仕方なくあきらめました。 帰り際に仕事の電話かかってきたから多分正解でした。 セミナー中は、となりのVoluntasさんと二人してPyObjCで書こう大会を開いてたり、XcodeのエディタをつかわずにVimで書いたり。 アウトローな一面をかいま見せていました。まあ前日に今回の内容はやってあったので復習的な感じで楽しんでました。 で、結局PyObjCはCocoaな部分というかGUIな部分というか、必要になるまでObjC的なこと書かなくていいじゃんって結論になった(※1)んですけど、せっかくだからってことで、今回の資料を徹底的にObjCなPyObjCに移植してみました。Pythonの組み込み型はほぼ使ってません。 ※1 NSStringとかNSArrayとかNSDictionaryとかObjCなオブジェクトがPyObjCにはちゃんとあるんですが、これをPythonの組み込み型で代用できてしまうので使う必要ないねって話。ディレクトリあさるのもos.walkしたらいいしって。ちなみにそういうバージョンはすでにVoluntasさんが公開してます。 では、無駄な努力の結晶をどうぞ。変数名がCamelCaseなのは今回の仕様です。 あとメソッド名がやたら長いのとたまに_で終わってるメソッドがあるのはPyObjCの仕様です。 ちなみにInterfaceBuilderでの作業はObjC版とまったく一緒です。 Cocoaセミナーグラフィックス編 資料 http://developer.apple.com/jp/documentation/Cocoa/Seminar/CocoaGraphics/CocoaGraphics.zip [python] #BrowserView.py from objc import YES, NO, NULL, nil from Foundation import * from AppKit import * WIDTH = 200.0 HEIGHT = 200.0 MARGIN = 20.0 class BrowserView(NSView): def initWithFrame_(self, frame): self = super(BrowserView, self).initWithFrame_(frame) if self: self.clickPoint = NSPoint() self.images = NSMutableArray.array() dirPath = NSString.stringWithString_('/Library/Desktop Pictures') subPaths = NSFileManager.defaultManager().subpathsOfDirectoryAtPath_error_(dirPath, NULL) for subPath in subPaths[0]: if subPath.nsstring().pathExtension().isEqualTo_('jpg'): path = dirPath.nsstring().stringByAppendingPathComponent_(subPath) image = NSImage.alloc().initWithContentsOfFile_(path) imageInfo = NSDictionary.dictionaryWithObjectsAndKeys_( path, NSString.stringWithString_('path'), image, NSString.stringWithString_('image'), nil) self.images.addObject_(imageInfo) if self.images.count() >= 10: break return self def isFlipped(self): return YES def mouseDown_(self, event): self.clickPoint = self.convertPoint_fromView_(event.locationInWindow(), nil) self.setNeedsDisplay_(YES) def drawRect_(self, rect): srcRect = NSRect() dstRect = NSRect() dstRect.origin.x = MARGIN dstRect.origin.y = MARGIN dstRect.size.width = WIDTH dstRect.size.height = HEIGHT for imageInfo in self.images: image = imageInfo.objectForKey_(NSString.stringWithString_('image')) srcRect.origin = NSZeroPoint srcRect.size = image.size() if srcRect.size.width > srcRect.size.height: dstRect.size.width = WIDTH dstRect.size.height = (srcRect.size.height / srcRect.size.width) * HEIGHT else: dstRect.size.width = (srcRect.size.width / srcRect.size.height) * HEIGHT dstRect.size.height = HEIGHT image.drawInRect_fromRect_operation_fraction_( dstRect, srcRect, NSCompositeCopy, 1.0) title = imageInfo.objectForKey_(NSString.stringWithString_('path')).lastPathComponent() attrs = NSDictionary.dictionaryWithObjectsAndKeys_( NSFont.systemFontOfSize_(12.0), NSFontAttributeName, NSColor.blackColor(), NSForegroundColorAttributeName, nil) attrStr = NSAttributedString.alloc().initWithString_attributes_(title, attrs) point = NSPoint() point.x = NSMinX(dstRect) point.y = NSMaxY(dstRect) attrStr.drawAtPoint_(point) if NSPointInRect(self.clickPoint, dstRect): NSColor.redColor().set() focusRect = NSInsetRect(dstRect, -1.0, -1.0) bezierPath = NSBezierPath.bezierPathWithRect_(focusRect) bezierPath.setLineWidth_(2.0) bezierPath.stroke() dstRect.origin.x += WIDTH + MARGIN if NSMaxX(dstRect) > rect.size.width: dstRect.origin.x = MARGIN dstRect.origin.y += HEIGHT + MARGIN [/python]
Posted at: 
2007/12/20 04:29:55
0 Comments
0 TrackBacks
Tags: 
Trackback: 
http://blog.monospace.jp/2007/12/20/cocoa-seminar-graphics/trackback/
この日記のはてなブックマーク数

TrackBacks

まだ登録されていません。

Comments

まだ登録されていません。

Add Comment

Add Comment