IJInventoryView.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. NSLog(@"%s", __PRETTY_FUNCTION__);
  170. if (!dragging)
  171. {
  172. NSPoint mouseDownPoint = [mouseDownEvent locationInWindow];
  173. NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil];
  174. int itemIndex = [self itemIndexForPoint:pointInView];
  175. [delegate inventoryView:self selectedItemAtIndex:itemIndex];
  176. }
  177. }
  178. - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
  179. {
  180. return NSDragOperationEvery;
  181. }
  182. //- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint
  183. //{
  184. // NSLog(@"%s", __PRETTY_FUNCTION__);
  185. //}
  186. - (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation
  187. {
  188. NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, operation);
  189. if (operation == NSDragOperationMove)
  190. {
  191. //
  192. }
  193. }
  194. //- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint
  195. //{
  196. // NSLog(@"%s", __PRETTY_FUNCTION__);
  197. //}
  198. #pragma mark -
  199. #pragma mark Drag & Drop: Destination
  200. - (void)moveHighlightToLayerAtIndex:(int)index
  201. {
  202. [self.layer.sublayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  203. CALayer *layer = obj;
  204. if (idx == index)
  205. layer.borderColor = CGColorGetConstantColor(kCGColorWhite);
  206. else
  207. layer.borderColor = CGColorGetConstantColor(kCGColorBlack);
  208. }];
  209. }
  210. - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
  211. {
  212. // TODO: Detect and ignore same slot.
  213. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  214. [self moveHighlightToLayerAtIndex:index];
  215. if ([[sender draggingSource] isKindOfClass:[self class]])
  216. return NSDragOperationMove; // moving between inventories
  217. else
  218. return NSDragOperationCopy; // copying from the item selector, presumably
  219. }
  220. - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
  221. {
  222. // TODO: Detect and ignore same slot.
  223. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  224. [self moveHighlightToLayerAtIndex:index];
  225. if ([[sender draggingSource] isKindOfClass:[self class]])
  226. return NSDragOperationMove; // moving between inventories
  227. else
  228. return NSDragOperationCopy; // copying from the item selector, presumably
  229. }
  230. - (void)draggingExited:(id <NSDraggingInfo>)sender
  231. {
  232. [self moveHighlightToLayerAtIndex:-1];
  233. }
  234. - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
  235. {
  236. return YES;
  237. }
  238. - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
  239. {
  240. NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, sender.draggingSourceOperationMask);
  241. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  242. NSData *itemData = [[sender draggingPasteboard] dataForType:IJPasteboardTypeInventoryItem];
  243. IJInventoryItem *item = [NSKeyedUnarchiver unarchiveObjectWithData:itemData];
  244. [delegate inventoryView:self setItem:item atIndex:index];
  245. return YES;
  246. }
  247. - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
  248. {
  249. [self moveHighlightToLayerAtIndex:-1];
  250. }
  251. @end