| 1 | # MyView.rb |
|---|
| 2 | # Pixels |
|---|
| 3 | # |
|---|
| 4 | # Created by Emil Tin on 28/07/10. |
|---|
| 5 | # Copyright 2010 __MyCompanyName__. All rights reserved. |
|---|
| 6 | |
|---|
| 7 | class MyView < NSView |
|---|
| 8 | attr_accessor :nsBitmapImageRepObj |
|---|
| 9 | attr_accessor :nsRectFrameRect |
|---|
| 10 | attr_accessor :bitmapData |
|---|
| 11 | |
|---|
| 12 | def initWithFrame pFrameRect |
|---|
| 13 | if super |
|---|
| 14 | @nsRectFrameRect = pFrameRect |
|---|
| 15 | self |
|---|
| 16 | end |
|---|
| 17 | end |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | def awakeFromNib |
|---|
| 21 | @nsBitmapImageRepObj = NSBitmapImageRep.alloc.initWithBitmapDataPlanes nil, |
|---|
| 22 | pixelsWide:@nsRectFrameRect.size.width, |
|---|
| 23 | pixelsHigh:@nsRectFrameRect.size.height, |
|---|
| 24 | bitsPerSample:8, |
|---|
| 25 | samplesPerPixel:3, |
|---|
| 26 | hasAlpha:false, |
|---|
| 27 | isPlanar:false, |
|---|
| 28 | colorSpaceName:"NSCalibratedRGBColorSpace", |
|---|
| 29 | bytesPerRow:0, |
|---|
| 30 | bitsPerPixel:0 |
|---|
| 31 | end |
|---|
| 32 | |
|---|
| 33 | def drawBitmap |
|---|
| 34 | puts @nsBitmapImageRepObj.bitmapData.class # => String |
|---|
| 35 | puts @nsBitmapImageRepObj.bitmapData.size # => 0 |
|---|
| 36 | |
|---|
| 37 | xs = 255.0/200.0 |
|---|
| 38 | ys = 255.0/200.0 |
|---|
| 39 | 0.upto @nsRectFrameRect.size.height do |y| |
|---|
| 40 | 0.upto @nsRectFrameRect.size.width do |x| |
|---|
| 41 | color = [x*xs,y*ys,0] |
|---|
| 42 | @nsBitmapImageRepObj.setPixel color, atX:x, y:y |
|---|
| 43 | end |
|---|
| 44 | end |
|---|
| 45 | setNeedsDisplay true |
|---|
| 46 | end |
|---|
| 47 | |
|---|
| 48 | def drawRect pRect |
|---|
| 49 | drawBitmap |
|---|
| 50 | NSGraphicsContext.saveGraphicsState |
|---|
| 51 | nsBitmapImageRepObj.drawInRect pRect |
|---|
| 52 | NSGraphicsContext.restoreGraphicsState |
|---|
| 53 | end |
|---|
| 54 | |
|---|
| 55 | end |
|---|