D言語からObjective-CとCocoaAPIを使いたい

直線の描画と使用関数の違いによる線の太さの差、単純な文字出力

簡単な文字列の描画と、太さを変えた直線・曲線を表示してみました。

NSBezierPathのSample

サンプルコード

Powered by Prism

line/dub.json

{
    "authors": ["hidhok"],
    "description": "A D/Objective-C Cocoa application.",
    "name": "Line",
    "dflags": ["-L-framework", "-LCocoa"],
    "sourceFiles": ["../cocoa/window",
                    "../cocoa/control",
                    "../cocoa/menus"]
}               
line/source/app.d

/**
 *   line/source/app.d      Version 0.0.7
 */
module line.main;
import core.attribute : optional, selector;
import std.conv : to;
import std.math;
import std.stdio;
import cocoa.window;
import cocoa.menus;

string appName;
string versionNo = "0.0.7";
string helpMessage = "CocoaAPIを使用して Line を、\nD言語で表示してみました。";

void main() {
    auto delegate_ = AppDelegate.alloc.init;
    NSApp.delegate_ = cast(void*) delegate_;
    NSApp.activationPolicy = NSAppActivationPolicy.regular;
    NSApp.run;
}

extern (Objective-C)
class DemoView : NSView {
    override static DemoView alloc() @selector("alloc");
    override DemoView init() @selector("init");
    override DemoView init(NSRect) @selector("initWithFrame:");
    override void drawRect(NSRect r) @selector("drawRect:") {
        void print(string str, CGFloat x, CGFloat y) { 
            str.toNs.drawAtPoint(NSPoint(x, y), null); }

        NSColor.white.setFill;
        NSRectFill(r);
        print("stroke関数の違いによる線の太さの表示と、curveへの影響", 90, 360);
        print("stroke", 140, 340);
        print("strokeLineFromPoint", 230, 340);
        print("curveToPoint", 370, 340);

        NSBezierPath.defaultLineWidth = 0.1;
        for (int i = 0; i < 5; i++) {
            NSBezierPath path = NSBezierPath.bezierPath;
            path.lineWidth(cast(CGFloat)i);
            NSColor.red.stroke;
            path.moveToPoint(NSPoint(20 + 20 * i, 40));
            path.lineToPoint(NSPoint(120 + 20 * i, 320));
            path.stroke;
            print(i.to!string, 117 + i * 20, 320);

            NSColor.green.stroke;	
            path.strokeLine(NSPoint(20 + 20 * (i + 6), 40), 
                            NSPoint(120 + 20 * (i + 6), 320));
            print(i.to!string, 237 + i * 20, 320);

            NSColor.blue.stroke;	
            NSPoint p1 = NSPoint(20 + 20 * (i + 12), 40);
            NSPoint p2 = NSPoint(220, 120);
            NSPoint p3 = NSPoint(500, 150);
            NSPoint p4 = NSPoint(120 + 20 * (i + 12), 320);
            path.moveToPoint(p1);
            path.curveToPoint(p4, p2, p3);
            path.stroke;
            print(i.to!string, 357 + i * 20, 320);
        }
        print("線幅0では default に設定した0.1の線幅が表示されます。", 90, 10);
    }
}

extern (Objective-C)
class AppDelegate : NSObject {
    override static AppDelegate alloc() @selector("alloc");
    override typeof(this) init() @selector("init");
    bool applicationShouldTerminateAfterLastWindowClosed(
            NSNotification notification) @selector(
            "applicationShouldTerminateAfterLastWindowClosed:") {
        return true;
    }
    void applicationDidFinishLaunching(NSNotification notification) 
                @selector("applicationDidFinishLaunching:") {
        buildWindow;
    }
    void aboutPanel() @selector("aboutPanel") {
        dialog(appName, "Version " ~ versionNo);
    }
    void showHelp() @selector("showHelp") {
        dialog("Help", helpMessage);
    }
} 

void buildWindow() {
    NSWindow win;
    appName = NSProcessInfo.processInfo.processName.toDs;
    NSApp.menu = miniMenu(appName);
    NSRect w_rect = NSMakeRect(100, 550, 500, 400);
    immutable w_style = NSWindowStyleMask.titled |
                        NSWindowStyleMask.closable |
                    //  NSWindowStyleMask.resizable |
                        NSWindowStyleMask.miniaturizable;
    win = NSWindow.alloc.init(w_rect, w_style, backing.buffered, false);
    win.title = (appName ~ " (D言語テスト)").toNs;
    DemoView deView = DemoView.alloc.init;
    win.contentView = deView;
    // deView.bgColor = NSColor.white;
    ViewController vCtrler = ViewController.alloc.init;
    vCtrler.view.subview = deView;
    win.makeKeyAndOrderFront = null;
    NSApp.activateIgnoringOtherApps = true;
}

extern (Objective-C) 
class ViewController : NSViewController {
    override static ViewController alloc() @selector("alloc");
    override ViewController init() @selector("init");
    override void loadView() @selector("loadView") {
        printf("loadView\n");
    }
    override void viewDidLoad() @selector("viewDidLoad") {
        printf("viewDidLoad\n");
    }
}               
cocoa/window.d
cocoa/control.d
cocoa/menus.d