IJInventoryView.m 8.0 KB

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