| 1 |
#if defined(Hiro_Canvas) |
| 2 |
|
| 3 |
@implementation CocoaCanvas |
| 4 |
|
| 5 |
-(id) initWith:(hiro::mCanvas&)canvasReference { |
| 6 |
if(self = [super initWithFrame:NSMakeRect(0, 0, 0, 0)]) { |
| 7 |
canvas = &canvasReference; |
| 8 |
NSTrackingArea* area = [[NSTrackingArea alloc] initWithRect:[self frame] |
| 9 |
options:NSTrackingMouseMoved | NSTrackingMouseEnteredAndExited | NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect |
| 10 |
owner:self userInfo:nil |
| 11 |
]; |
| 12 |
[self addTrackingArea:area]; |
| 13 |
} |
| 14 |
return self; |
| 15 |
} |
| 16 |
|
| 17 |
-(void) resetCursorRects { |
| 18 |
if(auto mouseCursor = NSMakeCursor(canvas->mouseCursor())) { |
| 19 |
[self addCursorRect:self.bounds cursor:mouseCursor]; |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
-(NSDragOperation) draggingEntered:(id<NSDraggingInfo>)sender { |
| 24 |
return DropPathsOperation(sender); |
| 25 |
} |
| 26 |
|
| 27 |
-(BOOL) performDragOperation:(id<NSDraggingInfo>)sender { |
| 28 |
auto paths = DropPaths(sender); |
| 29 |
if(paths.empty()) return NO; |
| 30 |
canvas->doDrop(paths); |
| 31 |
return YES; |
| 32 |
} |
| 33 |
|
| 34 |
-(void) mouseButton:(NSEvent*)event down:(BOOL)isDown { |
| 35 |
if(isDown) { |
| 36 |
switch([event buttonNumber]) { |
| 37 |
case 0: return canvas->doMousePress(hiro::Mouse::Button::Left); |
| 38 |
case 1: return canvas->doMousePress(hiro::Mouse::Button::Right); |
| 39 |
case 2: return canvas->doMousePress(hiro::Mouse::Button::Middle); |
| 40 |
} |
| 41 |
} else { |
| 42 |
switch([event buttonNumber]) { |
| 43 |
case 0: return canvas->doMouseRelease(hiro::Mouse::Button::Left); |
| 44 |
case 1: return canvas->doMouseRelease(hiro::Mouse::Button::Right); |
| 45 |
case 2: return canvas->doMouseRelease(hiro::Mouse::Button::Middle); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
-(void) mouseEntered:(NSEvent*)event { |
| 51 |
canvas->doMouseEnter(); |
| 52 |
} |
| 53 |
|
| 54 |
-(void) mouseExited:(NSEvent*)event { |
| 55 |
canvas->doMouseLeave(); |
| 56 |
} |
| 57 |
|
| 58 |
-(void) mouseMove:(NSEvent*)event { |
| 59 |
if([event window] == nil) return; |
| 60 |
NSPoint location = [self convertPoint:[event locationInWindow] fromView:nil]; |
| 61 |
canvas->doMouseMove({(s32)location.x, (s32)([self frame].size.height - 1 - location.y)}); |
| 62 |
} |
| 63 |
|
| 64 |
-(void) mouseDown:(NSEvent*)event { |
| 65 |
[self mouseButton:event down:YES]; |
| 66 |
} |
| 67 |
|
| 68 |
-(void) mouseUp:(NSEvent*)event { |
| 69 |
[self mouseButton:event down:NO]; |
| 70 |
} |
| 71 |
|
| 72 |
-(void) mouseDragged:(NSEvent*)event { |
| 73 |
[self mouseMove:event]; |
| 74 |
} |
| 75 |
|
| 76 |
-(void) rightMouseDown:(NSEvent*)event { |
| 77 |
[self mouseButton:event down:YES]; |
| 78 |
} |
| 79 |
|
| 80 |
-(void) rightMouseUp:(NSEvent*)event { |
| 81 |
[self mouseButton:event down:NO]; |
| 82 |
} |
| 83 |
|
| 84 |
-(void) rightMouseDragged:(NSEvent*)event { |
| 85 |
[self mouseMove:event]; |
| 86 |
} |
| 87 |
|
| 88 |
-(void) otherMouseDown:(NSEvent*)event { |
| 89 |
[self mouseButton:event down:YES]; |
| 90 |
} |
| 91 |
|
| 92 |
-(void) otherMouseUp:(NSEvent*)event { |
| 93 |
[self mouseButton:event down:NO]; |
| 94 |
} |
| 95 |
|
| 96 |
-(void) otherMouseDragged:(NSEvent*)event { |
| 97 |
[self mouseMove:event]; |
| 98 |
} |
| 99 |
|
| 100 |
-(void) drawRect:(NSRect)dirtyRect { |
| 101 |
NSRect frame = self.bounds; |
| 102 |
s32 width = frame.size.width; |
| 103 |
s32 height = frame.size.height; |
| 104 |
if(auto icon = canvas->state.icon) { |
| 105 |
NSRect alignedFrame = NSMakeRect(canvas->state.alignment.horizontal() * (width - (s32)icon.width()), |
| 106 |
(1 - canvas->state.alignment.vertical()) * (height - (s32)icon.height()), |
| 107 |
icon.width(), icon.height()); |
| 108 |
[NSGraphicsContext currentContext].imageInterpolation = NSImageInterpolationNone; |
| 109 |
[NSMakeImage(icon) drawInRect:alignedFrame]; |
| 110 |
} else if(auto& gradient = canvas->state.gradient) { |
| 111 |
auto& colors = gradient.state.colors; |
| 112 |
image fill; |
| 113 |
fill.allocate(width, height); |
| 114 |
fill.gradient(colors[0].value(), colors[1].value(), colors[2].value(), colors[3].value()); |
| 115 |
[NSMakeImage(fill) drawInRect:frame]; |
| 116 |
} else { |
| 117 |
[NSMakeColor(canvas->state.color) set]; |
| 118 |
NSRectFill(frame); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
@end |
| 123 |
|
| 124 |
namespace hiro { |
| 125 |
|
| 126 |
auto pCanvas::construct() -> void { |
| 127 |
cocoaView = cocoaCanvas = [[CocoaCanvas alloc] initWith:self()]; |
| 128 |
pWidget::construct(); |
| 129 |
} |
| 130 |
|
| 131 |
auto pCanvas::destruct() -> void { |
| 132 |
[cocoaView removeFromSuperview]; |
| 133 |
} |
| 134 |
|
| 135 |
auto pCanvas::minimumSize() const -> Size { |
| 136 |
if(auto& icon = state().icon) return {(s32)icon.width(), (s32)icon.height()}; |
| 137 |
return {0, 0}; |
| 138 |
} |
| 139 |
|
| 140 |
auto pCanvas::setAlignment(Alignment) -> void { |
| 141 |
update(); |
| 142 |
} |
| 143 |
|
| 144 |
auto pCanvas::setColor(Color color) -> void { |
| 145 |
update(); |
| 146 |
} |
| 147 |
|
| 148 |
auto pCanvas::setDroppable(bool droppable) -> void { |
| 149 |
if(droppable) { |
| 150 |
[cocoaCanvas registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeFileURL]]; |
| 151 |
} else { |
| 152 |
[cocoaCanvas unregisterDraggedTypes]; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
auto pCanvas::setFocusable(bool focusable) -> void { |
| 157 |
//TODO |
| 158 |
} |
| 159 |
|
| 160 |
auto pCanvas::setGeometry(Geometry geometry) -> void { |
| 161 |
pWidget::setGeometry(geometry); |
| 162 |
update(); |
| 163 |
} |
| 164 |
|
| 165 |
auto pCanvas::setGradient(Gradient gradient) -> void { |
| 166 |
update(); |
| 167 |
} |
| 168 |
|
| 169 |
auto pCanvas::setIcon(const image& icon) -> void { |
| 170 |
update(); |
| 171 |
} |
| 172 |
|
| 173 |
auto pCanvas::update() -> void { |
| 174 |
[cocoaView setNeedsDisplay:YES]; |
| 175 |
} |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
#endif |