Drag & drop working! Removed outline view, placing item picker in main window.
This commit is contained in:
parent
f791d3e403
commit
a0a158af27
File diff suppressed because it is too large
Load Diff
@ -8,6 +8,8 @@
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
@protocol IJInventoryViewDelegate;
|
||||
@class IJInventoryItem;
|
||||
|
||||
@interface IJInventoryView : NSView {
|
||||
int rows;
|
||||
@ -16,9 +18,18 @@
|
||||
NSEvent *mouseDownEvent;
|
||||
|
||||
NSArray *items;
|
||||
|
||||
id<IJInventoryViewDelegate> delegate;
|
||||
}
|
||||
@property (nonatomic, assign) id<IJInventoryViewDelegate> delegate;
|
||||
|
||||
- (void)setRows:(int)numberOfRows columns:(int)numberOfColumns;
|
||||
- (void)setItems:(NSArray *)theItems;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@protocol IJInventoryViewDelegate <NSObject>
|
||||
- (void)inventoryView:(IJInventoryView *)inventoryView removeItemAtIndex:(int)itemIndex;
|
||||
- (void)inventoryView:(IJInventoryView *)inventoryView setItem:(IJInventoryItem *)item atIndex:(int)itemIndex;
|
||||
@end
|
||||
|
||||
@ -16,6 +16,8 @@ const static CGFloat cellOffset = 40;
|
||||
|
||||
@implementation IJInventoryView
|
||||
|
||||
@synthesize delegate;
|
||||
|
||||
- (id)initWithFrame:(NSRect)frameRect
|
||||
{
|
||||
if (self = [super initWithFrame:frameRect])
|
||||
@ -82,6 +84,7 @@ const static CGFloat cellOffset = 40;
|
||||
|
||||
- (void)setItems:(NSArray *)theItems
|
||||
{
|
||||
NSLog(@"%s", __PRETTY_FUNCTION__);
|
||||
[items autorelease];
|
||||
[theItems retain];
|
||||
items = theItems;
|
||||
@ -103,11 +106,6 @@ const static CGFloat cellOffset = 40;
|
||||
return index;
|
||||
}
|
||||
|
||||
- (IJInventoryItem *)itemAtPoint:(NSPoint)point
|
||||
{
|
||||
return [items objectAtIndex:[self itemIndexForPoint:point]];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Drag & Drop: Source
|
||||
|
||||
@ -128,7 +126,8 @@ const static CGFloat cellOffset = 40;
|
||||
|
||||
// Find the IJInventoryItem:
|
||||
NSPoint pointInView = [self convertPoint:mouseDownPoint fromView:nil];
|
||||
IJInventoryItem *item = [self itemAtPoint:pointInView];
|
||||
int itemIndex = [self itemIndexForPoint:pointInView];
|
||||
IJInventoryItem *item = [items objectAtIndex:itemIndex];
|
||||
if (item.itemId == 0)
|
||||
return; // can't drag nothing
|
||||
|
||||
@ -141,9 +140,12 @@ const static CGFloat cellOffset = 40;
|
||||
|
||||
NSImage *image = item.image;
|
||||
|
||||
// Now clear out item:
|
||||
[delegate inventoryView:self removeItemAtIndex:itemIndex];
|
||||
|
||||
NSPoint dragPoint = NSMakePoint(pointInView.x - image.size.width*0.5, pointInView.y - image.size.height*0.5);
|
||||
|
||||
[self dragImage:item.image
|
||||
[self dragImage:image
|
||||
at:dragPoint
|
||||
offset:NSZeroSize
|
||||
event:mouseDownEvent
|
||||
@ -164,6 +166,11 @@ const static CGFloat cellOffset = 40;
|
||||
- (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation
|
||||
{
|
||||
NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, operation);
|
||||
|
||||
if (operation == NSDragOperationMove)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
//- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint
|
||||
//{
|
||||
@ -218,6 +225,14 @@ const static CGFloat cellOffset = 40;
|
||||
}
|
||||
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
|
||||
{
|
||||
NSLog(@"%s operation=%d", __PRETTY_FUNCTION__, sender.draggingSourceOperationMask);
|
||||
|
||||
int index = [self itemIndexForPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
|
||||
|
||||
NSData *itemData = [[sender draggingPasteboard] dataForType:IJPasteboardTypeInventoryItem];
|
||||
IJInventoryItem *item = [NSKeyedUnarchiver unarchiveObjectWithData:itemData];
|
||||
|
||||
[delegate inventoryView:self setItem:item atIndex:index];
|
||||
return YES;
|
||||
}
|
||||
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
|
||||
|
||||
@ -7,11 +7,12 @@
|
||||
//
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import "IJInventoryView.h"
|
||||
|
||||
@class IJInventoryView;
|
||||
@class IJMinecraftLevel;
|
||||
|
||||
@interface IJInventoryWindowController : NSWindowController <NSCollectionViewDelegate> {
|
||||
@interface IJInventoryWindowController : NSWindowController <NSCollectionViewDelegate, IJInventoryViewDelegate> {
|
||||
IJMinecraftLevel *level;
|
||||
NSArray *inventory;
|
||||
|
||||
|
||||
@ -31,6 +31,9 @@
|
||||
[inventoryView setRows:3 columns:9];
|
||||
[quickView setRows:1 columns:9];
|
||||
[armorView setRows:4 columns:1];
|
||||
inventoryView.delegate = self;
|
||||
quickView.delegate = self;
|
||||
armorView.delegate = self;
|
||||
}
|
||||
|
||||
- (void)dealloc
|
||||
@ -232,150 +235,59 @@
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Inventory Outline View
|
||||
#pragma mark IJInventoryViewDelegate
|
||||
|
||||
- (id)outlineView:(NSOutlineView *)theOutlineView child:(NSInteger)index ofItem:(id)item
|
||||
- (NSMutableArray *)itemArrayForInventoryView:(IJInventoryView *)theInventoryView slotOffset:(int*)slotOffset
|
||||
{
|
||||
if (item == nil)
|
||||
if (theInventoryView == inventoryView)
|
||||
{
|
||||
return [rootItems objectAtIndex:index];
|
||||
*slotOffset = IJInventorySlotNormalFirst;
|
||||
return inventoryItem;
|
||||
}
|
||||
else
|
||||
else if (theInventoryView == quickView)
|
||||
{
|
||||
return [item objectAtIndex:index];
|
||||
*slotOffset = IJInventorySlotQuickFirst;
|
||||
return quickItem;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (BOOL)outlineView:(NSOutlineView *)theOutlineView isItemExpandable:(id)item
|
||||
{
|
||||
return item == nil || [rootItems containsObject:item];
|
||||
}
|
||||
|
||||
- (NSInteger)outlineView:(NSOutlineView *)theOutlineView numberOfChildrenOfItem:(id)item
|
||||
{
|
||||
if (item == nil)
|
||||
else if (theInventoryView == armorView)
|
||||
{
|
||||
return 3;
|
||||
*slotOffset = IJInventorySlotArmorFirst;
|
||||
return armorItem;
|
||||
}
|
||||
else if ([rootItems containsObject:item])
|
||||
{
|
||||
return [(NSArray *)item count];
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
- (id)outlineView:(NSOutlineView *)theOutlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
|
||||
{
|
||||
if ([rootItems containsObject:item])
|
||||
{
|
||||
if ([tableColumn.identifier isEqual:@"slot"])
|
||||
{
|
||||
if (item == armorItem)
|
||||
return @"Armor";
|
||||
else if (item == quickItem)
|
||||
return @"Quick Inventory";
|
||||
else if (item == inventoryItem)
|
||||
return @"Inventory";
|
||||
}
|
||||
else
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
}
|
||||
|
||||
IJInventoryItem *invItem = item;
|
||||
|
||||
if ([tableColumn.identifier isEqual:@"slot"])
|
||||
{
|
||||
return [NSString stringWithFormat:@"%d", invItem.slot];
|
||||
}
|
||||
else if ([tableColumn.identifier isEqual:@"id"])
|
||||
{
|
||||
if (invItem.itemId)
|
||||
return [NSNumber numberWithShort:invItem.itemId];
|
||||
else
|
||||
return nil;
|
||||
}
|
||||
else if ([tableColumn.identifier isEqual:@"item"])
|
||||
{
|
||||
if (invItem.itemId)
|
||||
return invItem.itemName;
|
||||
else
|
||||
return @"";
|
||||
}
|
||||
else if ([tableColumn.identifier isEqual:@"count"])
|
||||
{
|
||||
if (invItem.count)
|
||||
return [NSNumber numberWithUnsignedChar:invItem.count];
|
||||
else
|
||||
return nil;
|
||||
}
|
||||
else if ([tableColumn.identifier isEqual:@"damage"])
|
||||
{
|
||||
if (invItem.damage)
|
||||
return [NSNumber numberWithShort:invItem.damage];
|
||||
else
|
||||
return nil;
|
||||
}
|
||||
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (void)outlineView:(NSOutlineView *)theOutlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
|
||||
- (void)inventoryView:(IJInventoryView *)theInventoryView removeItemAtIndex:(int)itemIndex
|
||||
{
|
||||
IJInventoryItem *invItem = item;
|
||||
if ([tableColumn.identifier isEqual:@"id"])
|
||||
int slotOffset = 0;
|
||||
NSMutableArray *itemArray = [self itemArrayForInventoryView:theInventoryView slotOffset:&slotOffset];
|
||||
|
||||
if (itemArray)
|
||||
{
|
||||
invItem.itemId = [object shortValue];
|
||||
[self markDirty];
|
||||
}
|
||||
else if ([tableColumn.identifier isEqual:@"count"])
|
||||
{
|
||||
invItem.count = [object unsignedCharValue];
|
||||
if (invItem.count > 64)
|
||||
invItem.count = 64;
|
||||
[self markDirty];
|
||||
}
|
||||
else if ([tableColumn.identifier isEqual:@"damage"])
|
||||
{
|
||||
invItem.damage = [object shortValue];
|
||||
[self markDirty];
|
||||
IJInventoryItem *item = [IJInventoryItem emptyItemWithSlot:slotOffset + itemIndex];
|
||||
[itemArray replaceObjectAtIndex:itemIndex withObject:item];
|
||||
[theInventoryView setItems:itemArray];
|
||||
}
|
||||
[self markDirty];
|
||||
}
|
||||
|
||||
- (BOOL)outlineView:(NSOutlineView *)theOutlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
|
||||
- (void)inventoryView:(IJInventoryView *)theInventoryView setItem:(IJInventoryItem *)item atIndex:(int)itemIndex
|
||||
{
|
||||
if ([rootItems containsObject:item])
|
||||
return NO;
|
||||
else if ([tableColumn.identifier isEqual:@"item"])
|
||||
int slotOffset = 0;
|
||||
NSMutableArray *itemArray = [self itemArrayForInventoryView:theInventoryView slotOffset:&slotOffset];
|
||||
|
||||
if (itemArray)
|
||||
{
|
||||
IJInventoryItem *invItem = item;
|
||||
[[IJItemPickerWindowController sharedController] showPickerWithInitialItemId:invItem.itemId completionBlock:^(uint16_t itemId) {
|
||||
invItem.itemId = itemId;
|
||||
[outlineView reloadItem:item];
|
||||
[self markDirty];
|
||||
}];
|
||||
return NO;
|
||||
}
|
||||
else
|
||||
{
|
||||
return [tableColumn.identifier isEqual:@"slot"] == NO;
|
||||
[itemArray replaceObjectAtIndex:itemIndex withObject:item];
|
||||
item.slot = slotOffset + itemIndex;
|
||||
[theInventoryView setItems:itemArray];
|
||||
}
|
||||
[self markDirty];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark NSCollectionViewDelegate
|
||||
#pragma mark Item Picker
|
||||
|
||||
|
||||
- (BOOL)collectionView:(NSCollectionView *)collectionView writeItemsAtIndexes:(NSIndexSet *)indexes toPasteboard:(NSPasteboard *)pasteboard
|
||||
{
|
||||
NSLog(@"%s", __PRETTY_FUNCTION__);
|
||||
[pasteboard declareTypes:[NSArray arrayWithObjects:@"net.adampreble.insidejob.item", nil] owner:self];
|
||||
return YES;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user