RubyCocoaでスクリーンセーバを作る
MacOSでは
ScreenSaverView
クラスを実装して.saverという名前のbundleに格納すればスクリーンセーバになる。
RubyCocoaでスクリーンセーバを書く方法を
こちらに解説しておいた。
#import <Cocoa/Cocoa.h>
@interface Pi : NSObject
- (double)calc:(id)param;
@end
@implementation Pi
- (double)calc:(id)param
{
// すごい方法でπを計算
return 3.14159265358979;
}
@end
Xcodeを使ってこれからPi.frameworkを作成し、
/Library/Frameworksとかに置いておけば
以下のように簡単にRubyから呼び出すことができる。
require 'osx/cocoa'
include OSX
NSBundle.bundleWithPath("/Library/Frameworks/Pi.framework").load
ns_import :Pi
class Pi < NSObject
addRubyMethod_withType('calc', 'v@:@')
end
pi = Pi.alloc.init
puts pi.calc(nil)
自前のCプログラムをすぐにRubyから使えるので便利である。
def add_checksum(isbn)
v = 0
(0..8).each { |i|
v += isbn[i,1].to_i * (i+1)
}
v %= 11
checksum = (v == 10 ? 'X' : v.to_s)
isbn + checksum
end
現在(2008/1)のKahr氏のシステムは、 ソースコードの構成やライブラリの引数がBurks氏の記事とは異なっているが、 必要そうなものを全部まとめてframework化して以下のようにコードを少し変えると 無事動かすことができた。
require 'osx/cocoa'
include OSX
NSBundle.bundleWithPath("/Library/Frameworks/AppleRemote.framework").load
ns_import :AppleRemote
class AppleRemoteDelegate < NSObject
addRubyMethod_withType('sendRemoteButtonEvent:pressedDown:remoteControl:', 'v@:ii@')
def sendRemoteButtonEvent_pressedDown_remoteControl(buttonIdentifier, pressDown, remoteControl)
puts "button #{buttonIdentifier}, pressed #{pressDown}"
end
end
appleRemoteDelegate = AppleRemoteDelegate.alloc.init
appleRemote = AppleRemote.alloc.initWithDelegate(appleRemoteDelegate)
appleRemote.startListening self
NSApplication.sharedApplication.run
-framework GLUT -framework OpenGL