/GraphDraw_Nu/nu/main.nu

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