IJInventoryView.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. if (invert)
  54. y = rows - 1 - y;
  55. return NSMakePoint(x * cellOffset, self.bounds.size.height - y * cellOffset);
  56. }
  57. - (void)setRows:(int)numberOfRows columns:(int)numberOfColumns invert:(BOOL)inv
  58. {
  59. invert = inv;
  60. CALayer *layer = [CALayer layer];
  61. layer.bounds = NSRectToCGRect(self.bounds);
  62. layer.anchorPoint = CGPointZero;
  63. layer.position = CGPointZero; //CGPointMake(NSMidX(self.bounds), NSMidY(self.bounds));
  64. layer.geometryFlipped = YES;
  65. [self setLayer:layer];
  66. [self setWantsLayer:YES];
  67. rows = numberOfRows;
  68. cols = numberOfColumns;
  69. // reset the layers
  70. for (CALayer *layer in self.layer.sublayers)
  71. {
  72. [layer removeFromSuperlayer];
  73. }
  74. for (int y = 0; y < rows; y++)
  75. {
  76. for (int x = 0; x < cols; x++)
  77. {
  78. CALayer *layer = [CALayer layer];
  79. layer.anchorPoint = CGPointZero;
  80. if (invert)
  81. layer.position = CGPointMake(x * cellOffset, (rows - 1 - y) * cellOffset);
  82. else
  83. layer.position = CGPointMake(x * cellOffset, y * cellOffset);
  84. layer.bounds = CGRectMake(0, 0, cellSize, cellSize);
  85. layer.borderWidth = 1.0;
  86. layer.borderColor = [self borderColor];
  87. layer.backgroundColor = [[NSColor colorWithCalibratedWhite:0.7 alpha:1.0] CGColor];
  88. layer.cornerRadius = 2.0;
  89. CALayer *imageLayer = [CALayer layer];
  90. imageLayer.position = CGPointMake(cellSize/2.0, cellSize/2.0);
  91. imageLayer.bounds = CGRectMake(0, 0, 32, 32);
  92. [layer addSublayer:imageLayer];
  93. CATextLayer *textLayer = [CATextLayer layer];
  94. textLayer.bounds = CGRectMake(0, 0, cellSize-2, 18);
  95. textLayer.position = CGPointMake(cellSize/2.0, cellSize/2.0 + 18/2 + 1);
  96. textLayer.foregroundColor = CGColorGetConstantColor(kCGColorWhite);
  97. textLayer.fontSize = 18;
  98. textLayer.shadowOpacity = 1.0;
  99. textLayer.shadowRadius = 0.5;
  100. textLayer.shadowOffset = CGSizeMake(0, 1);
  101. textLayer.alignmentMode = @"right";
  102. [layer addSublayer:textLayer];
  103. [self.layer addSublayer:layer];
  104. }
  105. }
  106. }
  107. - (void)reloadItemAtIndex:(int)itemIndex
  108. {
  109. IJInventoryItem *item = [items objectAtIndex:itemIndex];
  110. CALayer *layer = [self.layer.sublayers objectAtIndex:itemIndex];
  111. CALayer *imageLayer = [layer.sublayers objectAtIndex:0];
  112. imageLayer.contents = item.image;
  113. CATextLayer *textLayer = [layer.sublayers objectAtIndex:1];
  114. if (item.count <= 1) // for 1 and 0, show no number.
  115. textLayer.string = @"";
  116. else
  117. textLayer.string = [NSString stringWithFormat:@"%d", item.count];
  118. }
  119. - (void)setItems:(NSArray *)theItems
  120. {
  121. [items autorelease];
  122. [theItems retain];
  123. items = theItems;
  124. for (int i = 0; i < items.count; i++)
  125. [self reloadItemAtIndex:i];
  126. }
  127. - (int)itemIndexForPoint:(NSPoint)point
  128. {
  129. point.y = self.bounds.size.height - point.y;
  130. point.x /= cellOffset;
  131. point.y /= cellOffset;
  132. if (invert)
  133. point.y = rows - 1 - floor(point.y);
  134. int index = floor(point.y) * cols + floor(point.x); // flip y
  135. return index;
  136. }
  137. #pragma mark -
  138. #pragma mark Drag & Drop: Source
  139. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
  140. {
  141. // NSLog(@"%s", __PRETTY_FUNCTION__);
  142. // if (propertiesWindow) // take the first mouse while the properties window is up.
  143. // return YES;
  144. // else
  145. // return NO;
  146. // the above doesn't work since the window has already been dismissed by the time we get here
  147. return YES;
  148. }
  149. - (void)mouseDown:(NSEvent *)theEvent
  150. {
  151. [theEvent retain];
  152. [mouseDownEvent release];
  153. mouseDownEvent = theEvent;
  154. dragging = NO;
  155. }
  156. - (void)mouseDragged:(NSEvent *)theEvent
  157. {
  158. NSPoint mouseDownPoint = [mouseDownEvent locationInWindow];
  159. NSPoint mouseDragPoint = [theEvent locationInWindow];
  160. float dragDistance = hypot(mouseDownPoint.x - mouseDragPoint.x, mouseDownPoint.y - mouseDragPoint.y);
  161. if (dragDistance < 3)
  162. return;
  163. dragging = YES;
  164. // Find the IJInventoryItem:
  165. NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil];
  166. int itemIndex = [self itemIndexForPoint:pointInView];
  167. IJInventoryItem *item = [items objectAtIndex:itemIndex];
  168. if (item.itemId == 0)
  169. return; // can't drag nothing
  170. NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
  171. [pasteboard declareTypes:[NSArray arrayWithObjects:IJPasteboardTypeInventoryItem, nil] owner:nil];
  172. [pasteboard setData:[NSKeyedArchiver archivedDataWithRootObject:item]
  173. forType:IJPasteboardTypeInventoryItem];
  174. NSImage *image = item.image;
  175. // Now clear out item, if the option key isn't down (option for copy):
  176. if (([theEvent modifierFlags] & NSAlternateKeyMask) == 0)
  177. [delegate inventoryView:self removeItemAtIndex:itemIndex];
  178. NSPoint dragPoint = NSMakePoint(pointInView.x - image.size.width*0.5, pointInView.y - image.size.height*0.5);
  179. [self dragImage:image
  180. at:dragPoint
  181. offset:NSZeroSize
  182. event:mouseDownEvent
  183. pasteboard:pasteboard
  184. source:self
  185. slideBack:NO];
  186. }
  187. - (void)mouseUp:(NSEvent *)theEvent
  188. {
  189. if (!dragging)
  190. {
  191. NSPoint mouseDownPoint = [mouseDownEvent locationInWindow];
  192. NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil];
  193. int itemIndex = [self itemIndexForPoint:pointInView];
  194. [delegate inventoryView:self selectedItemAtIndex:itemIndex];
  195. }
  196. }
  197. - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
  198. {
  199. return NSDragOperationEvery;
  200. }
  201. //- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint
  202. //{
  203. // NSLog(@"%s", __PRETTY_FUNCTION__);
  204. //}
  205. - (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation
  206. {
  207. NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, operation);
  208. if (operation == NSDragOperationMove)
  209. {
  210. //
  211. }
  212. }
  213. //- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint
  214. //{
  215. // NSLog(@"%s", __PRETTY_FUNCTION__);
  216. //}
  217. #pragma mark -
  218. #pragma mark Drag & Drop: Destination
  219. - (void)moveHighlightToLayerAtIndex:(int)index
  220. {
  221. [self.layer.sublayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  222. CALayer *layer = obj;
  223. if (idx == index)
  224. layer.borderColor = [self highlightedBorderColor];
  225. else
  226. layer.borderColor = [self borderColor];
  227. }];
  228. }
  229. - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
  230. {
  231. // TODO: Detect and ignore same slot.
  232. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  233. [self moveHighlightToLayerAtIndex:index];
  234. if ([[sender draggingSource] isKindOfClass:[self class]])
  235. return NSDragOperationMove; // moving between inventories
  236. else
  237. return NSDragOperationCopy; // copying from the item selector, presumably
  238. }
  239. - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
  240. {
  241. // TODO: Detect and ignore same slot.
  242. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  243. [self moveHighlightToLayerAtIndex:index];
  244. if ([[sender draggingSource] isKindOfClass:[self class]])
  245. return NSDragOperationMove; // moving between inventories
  246. else
  247. return NSDragOperationCopy; // copying from the item selector, presumably
  248. }
  249. - (void)draggingExited:(id <NSDraggingInfo>)sender
  250. {
  251. [self moveHighlightToLayerAtIndex:-1];
  252. }
  253. - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
  254. {
  255. return YES;
  256. }
  257. - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
  258. {
  259. //NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, sender.draggingSourceOperationMask);
  260. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  261. NSData *itemData = [[sender draggingPasteboard] dataForType:IJPasteboardTypeInventoryItem];
  262. IJInventoryItem *item = [NSKeyedUnarchiver unarchiveObjectWithData:itemData];
  263. IJInventoryItem *existingItem = [items objectAtIndex:index];
  264. if (existingItem.itemId == item.itemId)
  265. {
  266. item.count = MIN(64, item.count + existingItem.count);
  267. }
  268. [delegate inventoryView:self setItem:item atIndex:index];
  269. return YES;
  270. }
  271. - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
  272. {
  273. [self moveHighlightToLayerAtIndex:-1];
  274. }
  275. @end