// Cocoa variant of the "minimal animation" demo originally written for Carbon
// Nibless application 
// By Ingemar 2009
// This is over 120 lines, QD/carbon version is 64 lines, CG/Carbon is 98. That's progress!

program CocoaMinimalAnimation;
{$mode objfpc}
{$modeswitch objectivec1}

uses
	ctypes, MacOSAll, CocoaAll;

// -------------------- View ------------------------

type TestView = objcclass(NSView)
	public
	procedure drawRect (rect: NSRect); override;
	procedure windowWillClose (note: NSNotification); message 'windowWillClose:';
  end;

var
	loop: Real;
	NSApp: NSApplication;

procedure Line(h1,v1, h2, v2: Real);
var
	p1, p2: NSPoint;
begin
	p1.x := h1;
	p1.y := v1;
	p2.x := h2;
	p2.y := v2;
	NSBezierPath.strokeLineFromPoint_toPoint(p1, p2);
end;

procedure TestView.drawRect (rect: NSRect);
var
	cgr: NSRect = (origin:(x: 100; y:100); size:(width: 200; height: 200));
	p: NSBezierPath;
	col: NSColor;
	
	r: NSRect;
	bp: NSBezierPath;
	color: NSColor;
begin
	NSColor.whiteColor.setFill;
	NSRectFill(rect{bounds?});
	NSColor.blueColor.setFill;
	
	loop := loop + 0.1;
	
	// Draw the spinning wheel
	
	p := NSBezierPath.bezierPathWithOvalInRect(cgr);
	col := NSColor.blackColor;
	col.set_;
	p.stroke;

	Line(100, 100, 200 + Trunc(100*sin(loop)), 200 + Trunc(100*cos(loop)));
	Line(200 + Trunc(100*sin(loop)), 200 + Trunc(100*cos(loop)),
		200 + Trunc(100*sin(loop+Pi)), 200 + Trunc(100*cos(loop+Pi)));
	Line(200 + Trunc(100*sin(loop+Pi/2)), 200 + Trunc(100*cos(loop+Pi/2)),
		200 + Trunc(100*sin(loop-Pi/2)), 200 + Trunc(100*cos(loop-Pi/2)));
end;

procedure TestView.windowWillClose (note: NSNotification);
begin
	NSApp.terminate(NSApp);
end;

// -------------------- Timer ------------------------

// Mini-mini class for the timer
type
	TimerController = objcclass(NSObject)
	public
		procedure TimerAction(t: NSTimer); message 'timerFireMethod:';
	end;

var
	gTimer: NSTimer;
	myTimerController: TimerController;
	view: NSView;
	
// Timer!
procedure TimerController.TimerAction(t: NSTimer);
var
	theText: AnsiString;
	CFText: CFStringRef;
begin
	view.setNeedsDisplay_(true);
end;

// ------------------ Main program ---------------------

var
	pool: NSAutoreleasePool;
	window: NSWindow;
	frame: NSRect = (origin:(x: 10; y: 1000); size:(width: 400; height: 400));
	
begin
	pool := NSAutoreleasePool.new;
	NSApp := NSApplication.sharedApplication;
	
	window := NSWindow.alloc;
	window.initWithContentRect_styleMask_backing_defer(frame,
		NSTitledWindowMask or NSClosableWindowMask or NSMiniaturizableWindowMask or NSResizableWindowMask,
		NSBackingStoreBuffered, LongBool(0));
	window.setTitle(NSString(CFSTR('Minimal animation (Cocoa)')));
	
	view := TestView.alloc;
	view.initWithFrame(frame);
	window.setContentView(view);
	window.setDelegate(view);
	window.makeKeyAndOrderFront(nil);

	// Timer
	myTimerController := TimerController.alloc;
	gTimer := NSTimer.alloc;
	gTimer.scheduledTimerWithTimeInterval_target_selector_userInfo_repeats(
			0.02, myTimerController, sel_registerName(PChar('timerFireMethod:')), nil, true);

	// Main loop
	NSApp.run;
	pool.dealloc; // Free;
end.
