IJInventoryView.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. //
  2. // IJInventoryView.m
  3. // InsideJob
  4. //
  5. // Created by Adam Preble on 10/9/10.
  6. // Copyright 2010 Adam Preble. All rights reserved.
  7. //
  8. #import "IJInventoryView.h"
  9. #import "IJInventoryItem.h"
  10. #import "MAAttachedWindow.h"
  11. #import "NSColor+Additions.h"
  12. #import <QuartzCore/QuartzCore.h>
  13. NSString * const IJPasteboardTypeInventoryItem = @"net.adampreble.insidejob.inventoryitem";
  14. const static CGFloat cellSize = 36;
  15. const static CGFloat cellOffset = 40;
  16. @implementation IJInventoryView
  17. @synthesize delegate;
  18. - (id)initWithFrame:(NSRect)frameRect
  19. {
  20. if (self = [super initWithFrame:frameRect])
  21. {
  22. // Initialization code here.
  23. [self registerForDraggedTypes:[NSArray arrayWithObjects:IJPasteboardTypeInventoryItem, nil]];
  24. }
  25. return self;
  26. }
  27. - (void)dealloc
  28. {
  29. [items release];
  30. [mouseDownEvent release];
  31. [super dealloc];
  32. }
  33. - (void)awakeFromNib
  34. {
  35. }
  36. - (BOOL)acceptsFirstResponder
  37. {
  38. return YES;
  39. }
  40. - (CGColorRef)borderColor
  41. {
  42. return [[NSColor colorWithCalibratedWhite:0.5 alpha:1.0] CGColor];
  43. }
  44. - (CGColorRef)highlightedBorderColor
  45. {
  46. return [[NSColor colorWithCalibratedWhite:0 alpha:1.0] CGColor];
  47. }
  48. // For use by external stuff, since it flips the coordinates and our layer uses flipped geometry.
  49. - (NSPoint)pointForItemAtIndex:(int)index
  50. {
  51. int x = index % cols;
  52. int y = index / cols;
  53. return CGPointMake(x * cellOffset, self.bounds.size.height - y * cellOffset);
  54. }
  55. - (void)setRows:(int)numberOfRows columns:(int)numberOfColumns
  56. {
  57. CALayer *layer = [CALayer layer];
  58. layer.bounds = NSRectToCGRect(self.bounds);
  59. layer.anchorPoint = CGPointZero;
  60. layer.position = CGPointZero; //CGPointMake(NSMidX(self.bounds), NSMidY(self.bounds));
  61. layer.geometryFlipped = YES;
  62. [self setLayer:layer];
  63. [self setWantsLayer:YES];
  64. rows = numberOfRows;
  65. cols = numberOfColumns;
  66. // reset the layers
  67. for (CALayer *layer in self.layer.sublayers)
  68. {
  69. [layer removeFromSuperlayer];
  70. }
  71. for (int y = 0; y < rows; y++)
  72. {
  73. for (int x = 0; x < cols; x++)
  74. {
  75. CALayer *layer = [CALayer layer];
  76. layer.anchorPoint = CGPointZero;
  77. layer.position = CGPointMake(x * cellOffset, y * cellOffset);
  78. layer.bounds = CGRectMake(0, 0, cellSize, cellSize);
  79. layer.borderWidth = 1.0;
  80. layer.borderColor = [self borderColor];
  81. layer.backgroundColor = [[NSColor colorWithCalibratedWhite:0.7 alpha:1.0] CGColor];
  82. layer.cornerRadius = 2.0;
  83. CALayer *imageLayer = [CALayer layer];
  84. imageLayer.position = CGPointMake(cellSize/2.0, cellSize/2.0);
  85. imageLayer.bounds = CGRectMake(0, 0, 32, 32);
  86. [layer addSublayer:imageLayer];
  87. CATextLayer *textLayer = [CATextLayer layer];
  88. textLayer.bounds = CGRectMake(0, 0, cellSize-2, 18);
  89. textLayer.position = CGPointMake(cellSize/2.0, cellSize/2.0 + 18/2 - 1);
  90. textLayer.foregroundColor = CGColorGetConstantColor(kCGColorWhite);
  91. textLayer.fontSize = 18;
  92. textLayer.shadowOpacity = 1.0;
  93. textLayer.shadowRadius = 0.5;
  94. textLayer.shadowOffset = NSMakeSize(0, 1);
  95. textLayer.alignmentMode = @"right";
  96. [layer addSublayer:textLayer];
  97. [self.layer addSublayer:layer];
  98. }
  99. }
  100. }
  101. - (CALayer *)layerAtRow:(int)row column:(int)column
  102. {
  103. return [self.layer.sublayers objectAtIndex:row * cols + column];
  104. }
  105. - (void)reloadItemAtIndex:(int)itemIndex
  106. {
  107. IJInventoryItem *item = [items objectAtIndex:itemIndex];
  108. CALayer *layer = [self.layer.sublayers objectAtIndex:itemIndex];
  109. CALayer *imageLayer = [layer.sublayers objectAtIndex:0];
  110. imageLayer.contents = item.image;
  111. CATextLayer *textLayer = [layer.sublayers objectAtIndex:1];
  112. if (item.count == 0)
  113. textLayer.string = @"";
  114. else
  115. textLayer.string = [NSString stringWithFormat:@"%d", item.count];
  116. }
  117. - (void)setItems:(NSArray *)theItems
  118. {
  119. NSLog(@"%s", __PRETTY_FUNCTION__);
  120. [items autorelease];
  121. [theItems retain];
  122. items = theItems;
  123. for (int i = 0; i < items.count; i++)
  124. [self reloadItemAtIndex:i];
  125. }
  126. - (int)itemIndexForPoint:(NSPoint)point
  127. {
  128. point.y = self.bounds.size.height - point.y;
  129. point.x /= cellOffset;
  130. point.y /= cellOffset;
  131. int index = floor(point.y) * cols + floor(point.x); // flip y
  132. return index;
  133. }
  134. #pragma mark -
  135. #pragma mark Drag & Drop: Source
  136. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
  137. {
  138. // NSLog(@"%s", __PRETTY_FUNCTION__);
  139. // if (propertiesWindow) // take the first mouse while the properties window is up.
  140. // return YES;
  141. // else
  142. // return NO;
  143. // the above doesn't work since the window has already been dismissed by the time we get here
  144. return YES;
  145. }
  146. - (void)mouseDown:(NSEvent *)theEvent
  147. {
  148. [theEvent retain];
  149. [mouseDownEvent release];
  150. mouseDownEvent = theEvent;
  151. dragging = NO;
  152. }
  153. - (void)mouseDragged:(NSEvent *)theEvent
  154. {
  155. NSPoint mouseDownPoint = [mouseDownEvent locationInWindow];
  156. NSPoint mouseDragPoint = [theEvent locationInWindow];
  157. float dragDistance = hypot(mouseDownPoint.x - mouseDragPoint.x, mouseDownPoint.y - mouseDragPoint.y);
  158. if (dragDistance < 3)
  159. return;
  160. dragging = YES;
  161. // Find the IJInventoryItem:
  162. NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil];
  163. int itemIndex = [self itemIndexForPoint:pointInView];
  164. IJInventoryItem *item = [items objectAtIndex:itemIndex];
  165. if (item.itemId == 0)
  166. return; // can't drag nothing
  167. NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
  168. [pasteboard declareTypes:[NSArray arrayWithObjects:IJPasteboardTypeInventoryItem, nil] owner:nil];
  169. [pasteboard setData:[NSKeyedArchiver archivedDataWithRootObject:item]
  170. forType:IJPasteboardTypeInventoryItem];
  171. NSImage *image = item.image;
  172. // Now clear out item:
  173. [delegate inventoryView:self removeItemAtIndex:itemIndex];
  174. NSPoint dragPoint = NSMakePoint(pointInView.x - image.size.width*0.5, pointInView.y - image.size.height*0.5);
  175. [self dragImage:image
  176. at:dragPoint
  177. offset:NSZeroSize
  178. event:mouseDownEvent
  179. pasteboard:pasteboard
  180. source:self
  181. slideBack:NO];
  182. }
  183. - (void)mouseUp:(NSEvent *)theEvent
  184. {
  185. if (!dragging)
  186. {
  187. NSPoint mouseDownPoint = [mouseDownEvent locationInWindow];
  188. NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil];
  189. int itemIndex = [self itemIndexForPoint:pointInView];
  190. [delegate inventoryView:self selectedItemAtIndex:itemIndex];
  191. }
  192. }
  193. - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
  194. {
  195. return NSDragOperationEvery;
  196. }
  197. //- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint
  198. //{
  199. // NSLog(@"%s", __PRETTY_FUNCTION__);
  200. //}
  201. - (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation
  202. {
  203. NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, operation);
  204. if (operation == NSDragOperationMove)
  205. {
  206. //
  207. }
  208. }
  209. //- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint
  210. //{
  211. // NSLog(@"%s", __PRETTY_FUNCTION__);
  212. //}
  213. #pragma mark -
  214. #pragma mark Drag & Drop: Destination
  215. - (void)moveHighlightToLayerAtIndex:(int)index
  216. {
  217. [self.layer.sublayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  218. CALayer *layer = obj;
  219. if (idx == index)
  220. layer.borderColor = [self highlightedBorderColor];
  221. else
  222. layer.borderColor = [self borderColor];
  223. }];
  224. }
  225. - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
  226. {
  227. // TODO: Detect and ignore same slot.
  228. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  229. [self moveHighlightToLayerAtIndex:index];
  230. if ([[sender draggingSource] isKindOfClass:[self class]])
  231. return NSDragOperationMove; // moving between inventories
  232. else
  233. return NSDragOperationCopy; // copying from the item selector, presumably
  234. }
  235. - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
  236. {
  237. // TODO: Detect and ignore same slot.
  238. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  239. [self moveHighlightToLayerAtIndex:index];
  240. if ([[sender draggingSource] isKindOfClass:[self class]])
  241. return NSDragOperationMove; // moving between inventories
  242. else
  243. return NSDragOperationCopy; // copying from the item selector, presumably
  244. }
  245. - (void)draggingExited:(id <NSDraggingInfo>)sender
  246. {
  247. [self moveHighlightToLayerAtIndex:-1];
  248. }
  249. - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
  250. {
  251. return YES;
  252. }
  253. - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
  254. {
  255. NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, sender.draggingSourceOperationMask);
  256. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  257. NSData *itemData = [[sender draggingPasteboard] dataForType:IJPasteboardTypeInventoryItem];
  258. IJInventoryItem *item = [NSKeyedUnarchiver unarchiveObjectWithData:itemData];
  259. [delegate inventoryView:self setItem:item atIndex:index];
  260. return YES;
  261. }
  262. - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
  263. {
  264. [self moveHighlightToLayerAtIndex:-1];
  265. }
  266. @end