IJInventoryView.m 9.6 KB

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