IJInventoryView.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. NSLog(@"%s", __PRETTY_FUNCTION__);
  122. [items autorelease];
  123. [theItems retain];
  124. items = theItems;
  125. for (int i = 0; i < items.count; i++)
  126. [self reloadItemAtIndex:i];
  127. }
  128. - (int)itemIndexForPoint:(NSPoint)point
  129. {
  130. point.y = self.bounds.size.height - point.y;
  131. point.x /= cellOffset;
  132. point.y /= cellOffset;
  133. if (invert)
  134. point.y = rows - 1 - floor(point.y);
  135. int index = floor(point.y) * cols + floor(point.x); // flip y
  136. return index;
  137. }
  138. #pragma mark -
  139. #pragma mark Drag & Drop: Source
  140. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
  141. {
  142. // NSLog(@"%s", __PRETTY_FUNCTION__);
  143. // if (propertiesWindow) // take the first mouse while the properties window is up.
  144. // return YES;
  145. // else
  146. // return NO;
  147. // the above doesn't work since the window has already been dismissed by the time we get here
  148. return YES;
  149. }
  150. - (void)mouseDown:(NSEvent *)theEvent
  151. {
  152. [theEvent retain];
  153. [mouseDownEvent release];
  154. mouseDownEvent = theEvent;
  155. dragging = NO;
  156. }
  157. - (void)mouseDragged:(NSEvent *)theEvent
  158. {
  159. NSPoint mouseDownPoint = [mouseDownEvent locationInWindow];
  160. NSPoint mouseDragPoint = [theEvent locationInWindow];
  161. float dragDistance = hypot(mouseDownPoint.x - mouseDragPoint.x, mouseDownPoint.y - mouseDragPoint.y);
  162. if (dragDistance < 3)
  163. return;
  164. dragging = YES;
  165. // Find the IJInventoryItem:
  166. NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil];
  167. int itemIndex = [self itemIndexForPoint:pointInView];
  168. IJInventoryItem *item = [items objectAtIndex:itemIndex];
  169. if (item.itemId == 0)
  170. return; // can't drag nothing
  171. NSPasteboard *pasteboard = [NSPasteboard pasteboardWithName:NSDragPboard];
  172. [pasteboard declareTypes:[NSArray arrayWithObjects:IJPasteboardTypeInventoryItem, nil] owner:nil];
  173. [pasteboard setData:[NSKeyedArchiver archivedDataWithRootObject:item]
  174. forType:IJPasteboardTypeInventoryItem];
  175. NSImage *image = item.image;
  176. // Now clear out item, if the option key isn't down (option for copy):
  177. if (([theEvent modifierFlags] & NSAlternateKeyMask) == 0)
  178. [delegate inventoryView:self removeItemAtIndex:itemIndex];
  179. NSPoint dragPoint = NSMakePoint(pointInView.x - image.size.width*0.5, pointInView.y - image.size.height*0.5);
  180. [self dragImage:image
  181. at:dragPoint
  182. offset:NSZeroSize
  183. event:mouseDownEvent
  184. pasteboard:pasteboard
  185. source:self
  186. slideBack:NO];
  187. }
  188. - (void)mouseUp:(NSEvent *)theEvent
  189. {
  190. if (!dragging)
  191. {
  192. NSPoint mouseDownPoint = [mouseDownEvent locationInWindow];
  193. NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil];
  194. int itemIndex = [self itemIndexForPoint:pointInView];
  195. [delegate inventoryView:self selectedItemAtIndex:itemIndex];
  196. }
  197. }
  198. - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal
  199. {
  200. return NSDragOperationEvery;
  201. }
  202. //- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint
  203. //{
  204. // NSLog(@"%s", __PRETTY_FUNCTION__);
  205. //}
  206. - (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation
  207. {
  208. NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, operation);
  209. if (operation == NSDragOperationMove)
  210. {
  211. //
  212. }
  213. }
  214. //- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint
  215. //{
  216. // NSLog(@"%s", __PRETTY_FUNCTION__);
  217. //}
  218. #pragma mark -
  219. #pragma mark Drag & Drop: Destination
  220. - (void)moveHighlightToLayerAtIndex:(int)index
  221. {
  222. [self.layer.sublayers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  223. CALayer *layer = obj;
  224. if (idx == index)
  225. layer.borderColor = [self highlightedBorderColor];
  226. else
  227. layer.borderColor = [self borderColor];
  228. }];
  229. }
  230. - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
  231. {
  232. // TODO: Detect and ignore same slot.
  233. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  234. [self moveHighlightToLayerAtIndex:index];
  235. if ([[sender draggingSource] isKindOfClass:[self class]])
  236. return NSDragOperationMove; // moving between inventories
  237. else
  238. return NSDragOperationCopy; // copying from the item selector, presumably
  239. }
  240. - (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
  241. {
  242. // TODO: Detect and ignore same slot.
  243. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  244. [self moveHighlightToLayerAtIndex:index];
  245. if ([[sender draggingSource] isKindOfClass:[self class]])
  246. return NSDragOperationMove; // moving between inventories
  247. else
  248. return NSDragOperationCopy; // copying from the item selector, presumably
  249. }
  250. - (void)draggingExited:(id <NSDraggingInfo>)sender
  251. {
  252. [self moveHighlightToLayerAtIndex:-1];
  253. }
  254. - (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
  255. {
  256. return YES;
  257. }
  258. - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
  259. {
  260. NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, sender.draggingSourceOperationMask);
  261. int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
  262. NSData *itemData = [[sender draggingPasteboard] dataForType:IJPasteboardTypeInventoryItem];
  263. IJInventoryItem *item = [NSKeyedUnarchiver unarchiveObjectWithData:itemData];
  264. IJInventoryItem *existingItem = [items objectAtIndex:index];
  265. if (existingItem.itemId == item.itemId)
  266. {
  267. item.count = MIN(64, item.count + existingItem.count);
  268. }
  269. [delegate inventoryView:self setItem:item atIndex:index];
  270. return YES;
  271. }
  272. - (void)concludeDragOperation:(id <NSDraggingInfo>)sender
  273. {
  274. [self moveHighlightToLayerAtIndex:-1];
  275. }
  276. @end