PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/GraphDraw_Nu/nu/main.nu

http://github.com/jsyedidia/nugraphdraw
Unknown | 169 lines | 140 code | 29 blank | 0 comment | 0 complexity | 837557568b543277474e5eca5599d668 MD5 | raw file
  1;;  Created by Jonathan Yedidia on 5/31/08.
  2;;  Copyright 2008. All rights reserved.
  3
  4(import Cocoa)		;; bridgesupport
  5(load "console")	;; interactive console
  6
  7(class GDNode is NSObject
  8     (ivar (id) point)
  9     
 10     (- (id) initWithPoint: (id) aPoint is
 11        (super init)
 12        (set @point aPoint)
 13        self)
 14     
 15     (- (NSPoint) point is
 16        @point))
 17
 18(class GDEdge is NSObject
 19     (ivar (id) sourceNode (id) destinationNode)
 20     (ivar-accessors)
 21     
 22     (- (id) initWithSourceNode: (id) aNode
 23        destinationNode: (id) bNode is
 24        (super init)
 25        (set @sourceNode aNode)
 26        (set @destinationNode bNode)
 27        self))
 28
 29(class GDGraph is NSObject
 30     (ivar (id) nodes (id) edges)
 31     (ivar-accessors)
 32     
 33     (- (id) init is
 34        (super init)
 35        ;; Set the global variable $graph to give easy access to the graph.
 36        (set $graph self)
 37        (set @nodes (array))
 38        (set @edges (array))
 39        self)
 40     
 41     (- (void) addNode: (id) n is
 42        (@nodes << n))
 43     
 44     (- (void) addEdge: (id) e is
 45        (@edges << e)))
 46
 47(class GDGraphView is NSView
 48     (ivar (id) graph (id) backgroundColor (id) nodeColor (id) edgeColor
 49           (int) nodeSize (double) edgeWidth)
 50     (ivar-accessors)
 51     
 52     (- (id) initWithFrame: (NSRect) frameRect is
 53        (set self (super initWithFrame:frameRect))
 54        (if (self)
 55            ;; Set the global variable $view to give easy access to the view.
 56            (set $view self)
 57            (set @backgroundColor (NSColor darkGrayColor))
 58            (set @nodeColor (NSColor redColor))
 59            (set @edgeColor (NSColor whiteColor))
 60            (set @nodeSize 6)
 61            (set @edgeWidth 0.9)
 62            (set b (self bounds))
 63            (self translateOriginToPoint:
 64                  (NSMakePoint (+ (b first) (/ (b third) 2.0))
 65                       (+ (b second) (/ (b fourth) 2.0)))))
 66        self)
 67     
 68     ;; The following methods are here so the user can change aspects of
 69     ;; the graph from the console or script.
 70     (- (void) setGraph: (id) g is
 71        (set @graph g)
 72        (self setNeedsDisplay:YES))
 73     
 74     (- (void) setBackgroundColor: (id) color is
 75        (set @backgroundColor color)
 76        (self setNeedsDisplay:YES))
 77     
 78     (- (void) setNodeColor: (id) color is
 79        (set @nodeColor color)
 80        (self setNeedsDisplay:YES))
 81     
 82     (- (void) setEdgeColor: (id) color is
 83        (set @edgeColor color)
 84        (self setNeedsDisplay:YES))
 85     
 86     (- (void) setNodeSize: (int) size is
 87        (set @nodeSize size)
 88        (self setNeedsDisplay:YES))
 89     
 90     (- (void) setEdgeWidth: (double) width is
 91        (set @edgeWidth width)
 92        (self setNeedsDisplay:YES))
 93     
 94     (- (void) drawRect: (NSRect) rect is
 95        (set b (self bounds))
 96        (@backgroundColor set)
 97        (NSBezierPath fillRect:b)
 98        (self translateOriginToPoint:
 99              (NSMakePoint (+ (b first) (/ (b third) 2.0))
100                   (+ (b second) (/ (b fourth) 2.0))))
101        (@nodeColor set)
102        (set nodes (@graph nodes))
103        (set count (nodes count))
104        (count times: (do (i)
105                          (set n (nodes i))
106                          (set np (n point))
107                          (set r (NSMakeRect (- (np first) (/ @nodeSize 2))
108                                      (- (np second) (/ @nodeSize 2))
109                                      @nodeSize
110                                      @nodeSize))
111                          (NSBezierPath fillRect:r)))
112        
113        (@edgeColor set)
114        (NSBezierPath setDefaultLineWidth:@edgeWidth)
115        (set edges (@graph edges))
116        (set count (edges count))
117        (count times: (do (i)
118                          (set e (edges i))
119                          (set n1 (e sourceNode))
120                          (set np1 (n1 point))
121                          (set n2 (e destinationNode))
122                          (set np2 (n2 point))
123                          (NSBezierPath strokeLineFromPoint:np1 toPoint:np2)))))
124
125(class AppController is NSObject
126     (ivar (id) graphView (id) graph (id) scriptTextView)
127     (ivar-accessors)
128     
129     (- (id) init is
130        (super init)
131        (set $ac self)
132        (set @graph (GDGraph new))
133        self)
134     
135     (- (void) evaluate: (id) sender is
136        (set str (@scriptTextView string))
137        (try
138            (set prs (parse str))
139            (unless prs (throw "No acceptable parse of Nu script. You may be missing a parenthesis."))
140            (eval prs)
141            (@graphView setGraph:@graph)
142            (catch (exception)
143                   (NSRunAlertPanel "Bad script" (exception description)
144				    nil nil nil)))))
145
146(set SHOW_CONSOLE_AT_STARTUP nil)
147
148;; @class ApplicationDelegate
149;; @discussion Methods of this class perform general-purpose tasks 
150;; that are not appropriate methods of any other classes.
151(class ApplicationDelegate is NSObject
152     
153     ;; This method is called after Cocoa has finished its basic application setup.
154     ;; It instantiates application-specific components.
155     ;; In this case, it constructs an interactive Nu console that can 
156	 ;; be activated from the application's Window menu.
157     (- (void) applicationDidFinishLaunching:(id) sender is
158        (set $console ((NuConsoleWindowController alloc) init))
159        (if SHOW_CONSOLE_AT_STARTUP ($console toggleConsole:self))))
160
161;; install the delegate and keep a reference to it since the application won't retain it.
162((NSApplication sharedApplication) setDelegate:(set $delegate ((ApplicationDelegate alloc) init)))
163
164;; this makes the application window take focus when we've 
165;; started it from the terminal (or with nuke)
166((NSApplication sharedApplication) activateIgnoringOtherApps:YES)
167
168;; run the main Cocoa event loop
169(NSApplicationMain 0 nil)