[macruby-changes] [1911] MacRuby/branches/experimental/lib/hotcocoa/graphics

source_changes at macosforge.org source_changes at macosforge.org
Sat Jun 20 15:17:23 PDT 2009


Revision: 1911
          http://trac.macosforge.org/projects/ruby/changeset/1911
Author:   lsansonetti at apple.com
Date:     2009-06-20 15:17:22 -0700 (Sat, 20 Jun 2009)
Log Message:
-----------
optimized a few code paths of HotCocoa::Graphics

Modified Paths:
--------------
    MacRuby/branches/experimental/lib/hotcocoa/graphics/canvas.rb
    MacRuby/branches/experimental/lib/hotcocoa/graphics/color.rb
    MacRuby/branches/experimental/lib/hotcocoa/graphics/elements/particle.rb
    MacRuby/branches/experimental/lib/hotcocoa/graphics/path.rb

Modified: MacRuby/branches/experimental/lib/hotcocoa/graphics/canvas.rb
===================================================================
--- MacRuby/branches/experimental/lib/hotcocoa/graphics/canvas.rb	2009-06-20 12:14:56 UTC (rev 1910)
+++ MacRuby/branches/experimental/lib/hotcocoa/graphics/canvas.rb	2009-06-20 22:17:22 UTC (rev 1911)
@@ -259,7 +259,7 @@
         x = x - w / 2
         y = y - h / 2
       end
-      CGContextAddRect(@ctx, [x, y, w, h])
+      CGContextAddRect(@ctx, NSMakeRect(x, y, w, h))
       CGContextDrawPath(@ctx, KCGPathFillStroke)
     end
   
@@ -270,7 +270,7 @@
         x = x - w / 2
         y = y - w / 2
       end
-      CGContextAddEllipseInRect(@ctx, [x, y, w, h])
+      CGContextAddEllipseInRect(@ctx, NSMakeRect(x, y, w, h))
       CGContextDrawPath(@ctx, KCGPathFillStroke) # apply fill and stroke
     end
   
@@ -294,14 +294,14 @@
     def radial(gradient, sx=@width/2, sy=@height/2, er=@width/2, ex=sx, ey=sy, sr=0.0)
       #options = KCGGradientDrawsBeforeStartLocation
       #options = KCGGradientDrawsAfterEndLocation
-      CGContextDrawRadialGradient(@ctx, gradient.gradient, [sx, sy], sr, [ex, ey], er, gradient.pre + gradient.post)
+      CGContextDrawRadialGradient(@ctx, gradient.gradient, NSMakePoint(sx, sy), sr, NSMakePoint(ex, ey), er, gradient.pre + gradient.post)
     end
   
     # draw an axial(linear) gradient starting at sx,sy and ending at ex,ey
     def gradient(gradient=Gradient.new, start_x=@width/2, start_y=0, end_x=@width/2, end_y=@height)
       #options = KCGGradientDrawsBeforeStartLocation
       #options = KCGGradientDrawsAfterEndLocation
-      CGContextDrawLinearGradient(@ctx, gradient.gradient, [start_x, start_y], [end_x, end_y], gradient.pre + gradient.post)
+      CGContextDrawLinearGradient(@ctx, gradient.gradient, NSMakePoint(start_x, start_y), NSMakePoint(end_x, end_y), gradient.pre + gradient.post)
     end
 
     # draw a cartesian coordinate grid for reference
@@ -339,7 +339,7 @@
   
     # draw a line starting at x1,y1 and ending at x2,y2
     def line(x1, y1, x2, y2)
-      CGContextAddLines(@ctx, [[x1, y1], [x2, y2]], 2)
+      CGContextAddLines(@ctx, [NSPoint.new(x1, y1), NSPoint.new(x2, y2)], 2)
       CGContextDrawPath(@ctx, KCGPathStroke) # apply stroke
       endpath
       

Modified: MacRuby/branches/experimental/lib/hotcocoa/graphics/color.rb
===================================================================
--- MacRuby/branches/experimental/lib/hotcocoa/graphics/color.rb	2009-06-20 12:14:56 UTC (rev 1910)
+++ MacRuby/branches/experimental/lib/hotcocoa/graphics/color.rb	2009-06-20 22:17:22 UTC (rev 1911)
@@ -194,7 +194,13 @@
       [330, 298], [345, 329],
       [360, 0  ]
     ]
-      
+
+    COLORNAMES.each_key do |name|
+      (class << self; self; end).define_method(name) do
+        named(name)
+      end
+    end
+  
     # create a color with the specified name
     def self.named(name)
       if COLORNAMES[name]
@@ -236,11 +242,6 @@
       nearest
     end
   
-    # if the method name is not recognized, try creating a color with that name
-    def self.method_missing(name, *args)
-      Color.named(name.to_s.downcase)
-    end
-  
     # return a copy of this color
     def copy
       Color.new(r, g, b, a)

Modified: MacRuby/branches/experimental/lib/hotcocoa/graphics/elements/particle.rb
===================================================================
--- MacRuby/branches/experimental/lib/hotcocoa/graphics/elements/particle.rb	2009-06-20 12:14:56 UTC (rev 1910)
+++ MacRuby/branches/experimental/lib/hotcocoa/graphics/elements/particle.rb	2009-06-20 22:17:22 UTC (rev 1911)
@@ -36,7 +36,7 @@
       @velocity_y=velocity_y
     
       # append the point to the array
-      @points = [[@x, @y]]
+      @points = [NSPoint.new(@x, @y)]
       @stroke = Color.white
     end
   
@@ -56,7 +56,7 @@
 
       # draw a line from the old position to the new
       #CANVAS.line(@previous_x, at previous_y, at x, at y);
-      @points.push([@x, @y])
+      @points.push(NSPoint.new(@x, @y))
     
       # grow old
       @age += 1
@@ -72,4 +72,4 @@
     end
   
   end
-end
\ No newline at end of file
+end

Modified: MacRuby/branches/experimental/lib/hotcocoa/graphics/path.rb
===================================================================
--- MacRuby/branches/experimental/lib/hotcocoa/graphics/path.rb	2009-06-20 12:14:56 UTC (rev 1910)
+++ MacRuby/branches/experimental/lib/hotcocoa/graphics/path.rb	2009-06-20 22:17:22 UTC (rev 1911)
@@ -132,8 +132,7 @@
   
     # return the current point
     def currentpoint
-      point = CGPathGetCurrentPoint(@path)
-      [point.x, point.y]
+      CGPathGetCurrentPoint(@path)
     end
   
     # true if the path contains the current point # doesn't work?
@@ -206,7 +205,7 @@
   
     # draw a line from x1,x2 to x2,y2
     def line(x1, y1, x2, y2)
-      CGPathAddLines(@path, @transform, [[x1, y1], [x2, y2]])
+      CGPathAddLines(@path, @transform, [NSMakePoint(x1, y1), NSMakePoint(x2, y2)])
       self
     end
 
@@ -323,4 +322,4 @@
   
 
   end
-end
\ No newline at end of file
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090620/37d98278/attachment.html>


More information about the macruby-changes mailing list