| 1 |
@implementation CocoaCanvas : NSImageView |
| 2 |
|
| 3 |
-(id) initWith:(phoenix::Canvas&)canvasReference { |
| 4 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) { |
| 5 |
canvas = &canvasReference; |
| 6 |
[self setEditable:NO]; //disable image drag-and-drop functionality |
| 7 |
NSTrackingArea* area = [[[NSTrackingArea alloc] initWithRect:[self frame] |
| 8 |
options:NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect |
| 9 |
owner:self userInfo:nil |
| 10 |
] autorelease]; |
| 11 |
[self addTrackingArea:area]; |
| 12 |
} |
| 13 |
return self; |
| 14 |
} |
| 15 |
|
| 16 |
-(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender { |
| 17 |
return DropPathsOperation(sender); |
| 18 |
} |
| 19 |
|
| 20 |
-(BOOL) performDragOperation:(id<NSDraggingInfo>)sender { |
| 21 |
lstring paths = DropPaths(sender); |
| 22 |
if(paths.empty()) return NO; |
| 23 |
if(canvas->onDrop) canvas->onDrop(paths); |
| 24 |
return YES; |
| 25 |
} |
| 26 |
|
| 27 |
-(void) mouseButton:(NSEvent*)event down:(BOOL)isDown { |
| 28 |
if(auto& callback = isDown ? canvas->onMousePress : canvas->onMouseRelease) { |
| 29 |
switch([event buttonNumber]) { |
| 30 |
case 0: return callback(phoenix::Mouse::Button::Left); |
| 31 |
case 1: return callback(phoenix::Mouse::Button::Right); |
| 32 |
case 2: return callback(phoenix::Mouse::Button::Middle); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
-(void) mouseExited:(NSEvent*)event { |
| 38 |
if(canvas->onMouseLeave) canvas->onMouseLeave(); |
| 39 |
} |
| 40 |
|
| 41 |
-(void) mouseMove:(NSEvent*)event { |
| 42 |
if([event window] == nil) return; |
| 43 |
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil]; |
| 44 |
if(canvas->onMouseMove) canvas->onMouseMove({location.x, [self frame].size.height - 1 - location.y}); |
| 45 |
} |
| 46 |
|
| 47 |
-(void) mouseDown:(NSEvent*)event { |
| 48 |
[self mouseButton:event down:YES]; |
| 49 |
} |
| 50 |
|
| 51 |
-(void) mouseUp:(NSEvent*)event { |
| 52 |
[self mouseButton:event down:NO]; |
| 53 |
} |
| 54 |
|
| 55 |
-(void) mouseDragged:(NSEvent*)event { |
| 56 |
[self mouseMove:event]; |
| 57 |
} |
| 58 |
|
| 59 |
-(void) rightMouseDown:(NSEvent*)event { |
| 60 |
[self mouseButton:event down:YES]; |
| 61 |
} |
| 62 |
|
| 63 |
-(void) rightMouseUp:(NSEvent*)event { |
| 64 |
[self mouseButton:event down:NO]; |
| 65 |
} |
| 66 |
|
| 67 |
-(void) rightMouseDragged:(NSEvent*)event { |
| 68 |
[self mouseMove:event]; |
| 69 |
} |
| 70 |
|
| 71 |
-(void) otherMouseDown:(NSEvent*)event { |
| 72 |
[self mouseButton:event down:YES]; |
| 73 |
} |
| 74 |
|
| 75 |
-(void) otherMouseUp:(NSEvent*)event { |
| 76 |
[self mouseButton:event down:NO]; |
| 77 |
} |
| 78 |
|
| 79 |
-(void) otherMouseDragged:(NSEvent*)event { |
| 80 |
[self mouseMove:event]; |
| 81 |
} |
| 82 |
|
| 83 |
@end |
| 84 |
|
| 85 |
namespace phoenix { |
| 86 |
|
| 87 |
void pCanvas::setDroppable(bool droppable) { |
| 88 |
@autoreleasepool { |
| 89 |
if(droppable) { |
| 90 |
[cocoaCanvas registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]]; |
| 91 |
} else { |
| 92 |
[cocoaCanvas unregisterDraggedTypes]; |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
void pCanvas::setGeometry(Geometry geometry) { |
| 98 |
if(canvas.state.width == 0 || canvas.state.height == 0) rasterize(); |
| 99 |
|
| 100 |
unsigned width = canvas.state.width; |
| 101 |
unsigned height = canvas.state.height; |
| 102 |
if(width == 0) width = widget.state.geometry.width; |
| 103 |
if(height == 0) height = widget.state.geometry.height; |
| 104 |
|
| 105 |
if(width < geometry.width) { |
| 106 |
geometry.x += (geometry.width - width) / 2; |
| 107 |
geometry.width = width; |
| 108 |
} |
| 109 |
|
| 110 |
if(height < geometry.height) { |
| 111 |
geometry.y += (geometry.height - height) / 2; |
| 112 |
geometry.height = height; |
| 113 |
} |
| 114 |
|
| 115 |
pWidget::setGeometry(geometry); |
| 116 |
} |
| 117 |
|
| 118 |
void pCanvas::setMode(Canvas::Mode mode) { |
| 119 |
rasterize(), redraw(); |
| 120 |
} |
| 121 |
|
| 122 |
void pCanvas::setSize(Size size) { |
| 123 |
rasterize(), redraw(); |
| 124 |
} |
| 125 |
|
| 126 |
void pCanvas::constructor() { |
| 127 |
@autoreleasepool { |
| 128 |
cocoaView = cocoaCanvas = [[CocoaCanvas alloc] initWith:canvas]; |
| 129 |
} |
| 130 |
setSize(canvas.size()); |
| 131 |
} |
| 132 |
|
| 133 |
void pCanvas::destructor() { |
| 134 |
@autoreleasepool { |
| 135 |
[cocoaView release]; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
void pCanvas::rasterize() { |
| 140 |
@autoreleasepool { |
| 141 |
unsigned width = canvas.state.width; |
| 142 |
unsigned height = canvas.state.height; |
| 143 |
if(width == 0) width = widget.state.geometry.width; |
| 144 |
if(height == 0) height = widget.state.geometry.height; |
| 145 |
|
| 146 |
if(width != surfaceWidth || height != surfaceHeight) { |
| 147 |
NSImage* image = [[[NSImage alloc] initWithSize:NSMakeSize(width, height)] autorelease]; |
| 148 |
NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc] |
| 149 |
initWithBitmapDataPlanes:nil |
| 150 |
pixelsWide:width pixelsHigh:height |
| 151 |
bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES |
| 152 |
isPlanar:NO colorSpaceName:NSCalibratedRGBColorSpace |
| 153 |
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat |
| 154 |
bytesPerRow:width * 4 bitsPerPixel:32 |
| 155 |
] autorelease]; |
| 156 |
|
| 157 |
[image addRepresentation:bitmap]; |
| 158 |
[cocoaView setImage:image]; |
| 159 |
|
| 160 |
surfaceWidth = width; |
| 161 |
surfaceHeight = height; |
| 162 |
} |
| 163 |
|
| 164 |
if(NSBitmapImageRep* bitmap = [[[cocoaView image] representations] objectAtIndex:0]) { |
| 165 |
uint32_t* target = (uint32_t*)[bitmap bitmapData]; |
| 166 |
|
| 167 |
if(canvas.state.mode == Canvas::Mode::Color) { |
| 168 |
nall::image image; |
| 169 |
image.allocate(width, height); |
| 170 |
image.fill(canvas.state.color.argb()); |
| 171 |
memcpy(target, image.data, image.size); |
| 172 |
} |
| 173 |
|
| 174 |
if(canvas.state.mode == Canvas::Mode::Gradient) { |
| 175 |
nall::image image; |
| 176 |
image.allocate(width, height); |
| 177 |
image.gradient( |
| 178 |
canvas.state.gradient[0].argb(), canvas.state.gradient[1].argb(), canvas.state.gradient[2].argb(), canvas.state.gradient[3].argb() |
| 179 |
); |
| 180 |
memcpy(target, image.data, image.size); |
| 181 |
} |
| 182 |
|
| 183 |
if(canvas.state.mode == Canvas::Mode::Image) { |
| 184 |
nall::image image = canvas.state.image; |
| 185 |
image.scale(width, height); |
| 186 |
image.transform(0, 32, 255u << 24, 255u << 16, 255u << 8, 255u << 0); |
| 187 |
memcpy(target, image.data, image.size); |
| 188 |
} |
| 189 |
|
| 190 |
if(canvas.state.mode == Canvas::Mode::Data) { |
| 191 |
if(width == canvas.state.width && height == canvas.state.height) { |
| 192 |
memcpy(target, canvas.state.data, width * height * sizeof(uint32_t)); |
| 193 |
} else { |
| 194 |
memset(target, 0x00, width * height * sizeof(uint32_t)); |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
//ARGB -> ABGR transformation |
| 199 |
for(unsigned n = 0; n < width * height; n++) { |
| 200 |
uint32_t color = *target; |
| 201 |
color = (color & 0xff00ff00) | ((color & 0xff0000) >> 16) | ((color & 0x0000ff) << 16); |
| 202 |
*target++ = color; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void pCanvas::redraw() { |
| 209 |
@autoreleasepool { |
| 210 |
[cocoaView setNeedsDisplay:YES]; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
} |