Skip to content

Commit

Permalink
WIP moving from IKImageBrowserViews to NSCollectionViews.
Browse files Browse the repository at this point in the history
  • Loading branch information
uliwitness committed Oct 28, 2018
1 parent 409980b commit 9297088
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 99 deletions.
7 changes: 3 additions & 4 deletions Stacksmith/Platforms/Mac/WILDMediaListDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Carlson
};


@class IKImageBrowserView;
@class WILDMediaListDataSource;


Expand All @@ -29,18 +28,18 @@ namespace Carlson
@end


@interface WILDMediaListDataSource : NSObject
@interface WILDMediaListDataSource : NSObject <NSCollectionViewDataSource, NSCollectionViewDelegate>
{
Carlson::CDocument* mDocument; // This is who we get the icons from.
NSMutableArray* mIcons; // Cached lists of icon names/IDs.
IKImageBrowserView* mIconListView; // View in which we show the icons.
NSCollectionView* mIconListView; // View in which we show the icons.
NSTextField* mImagePathField; // Field where we show where the icon comes from.
id<WILDMediaListDataSourceDelegate> mDelegate;
Carlson::TMediaType mMediaType; // Type of media to display, i.e. icons, cursors etc.
}

@property (assign,nonatomic) Carlson::CDocument* document;
@property (retain,nonatomic) IBOutlet IKImageBrowserView* iconListView;
@property (retain,nonatomic) IBOutlet NSCollectionView* iconListView;
@property (retain,nonatomic) IBOutlet NSTextField* imagePathField;
@property (assign,nonatomic) id<WILDMediaListDataSourceDelegate> delegate;
@property (assign,nonatomic) Carlson::TMediaType mediaType;
Expand Down
44 changes: 25 additions & 19 deletions Stacksmith/Platforms/Mac/WILDMediaListDataSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using namespace Carlson;


@interface WILDSimpleImageBrowserItem : NSObject // IKImageBrowserItem
@interface WILDSimpleImageBrowserItem : NSObject
{
NSString* mName;
NSString* mFileName;
Expand Down Expand Up @@ -142,7 +142,7 @@ -(void) dealloc
}


-(void) setIconListView: (IKImageBrowserView*)inIconListView
-(void) setIconListView: (NSCollectionView *)inIconListView
{
if( mIconListView != inIconListView )
{
Expand Down Expand Up @@ -216,8 +216,7 @@ -(void) setSelectedIconID: (ObjectID)theID
{
if( sibi.pictureID == theID )
{
[mIconListView setSelectionIndexes: [NSIndexSet indexSetWithIndex: x] byExtendingSelection: NO];
[mIconListView scrollIndexToVisible: x];
[mIconListView selectItemsAtIndexPaths: [NSIndexPath indexPathWithIndexes: x] scrollPosition: NSCollectionViewScrollPositionCenteredVertically];
break;
}
x++;
Expand All @@ -232,23 +231,33 @@ -(ObjectID) selectedIconID
}


-(NSUInteger) numberOfItemsInImageBrowser: (IKImageBrowserView *)aBrowser
-(NSInteger) collectionView: (NSCollectionView *)collectionView numberOfItemsInSection: (NSInteger)section
{
[self ensureIconListExists];

return [mIcons count];
}


-(id /*IKImageBrowserItem*/) imageBrowser: (IKImageBrowserView *) aBrowser itemAtIndex: (NSUInteger)idx
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{
[self ensureIconListExists];

return [mIcons objectAtIndex: idx];
[mIcons objectAtIndex: [indexPath indexAtPosition: 1]];
}


-(void) collectionView: (NSCollectionView *)collectionView didSelectItemsAtIndexPaths: (NSSet<NSIndexPath *> *)indexPaths
{
[self selectionDidChange];
}


-(void) collectionView: (NSCollectionView *)collectionView didDeselectItemsAtIndexPaths: (NSSet<NSIndexPath *> *)indexPaths
{
[self selectionDidChange];
}


-(void) imageBrowserSelectionDidChange: (IKImageBrowserView *)aBrowser
-(void) selectionDidChange
{
NSInteger selectedIndex = [[mIconListView selectionIndexes] firstIndex];
if( selectedIndex != NSNotFound )
Expand Down Expand Up @@ -283,9 +292,8 @@ -(IBAction) paste: (id)sender
NSString* imgFileURLStr = [NSString stringWithUTF8String: filePath.c_str()];
NSURL* imgFileURL = [NSURL URLWithString: imgFileURLStr];

[theImg lockFocus];
NSBitmapImageRep * bir = [[NSBitmapImageRep alloc] initWithFocusedViewRect: NSMakeRect(0,0,theImg.size.width,theImg.size.height)];
[theImg unlockFocus];
CGImageRef imageRef = [theImg CGImageForProposedRect:NULL context:NULL hints:nil];
NSBitmapImageRep *bir = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
NSData * pngData = [bir representationUsingType: NSBitmapImageFileTypePNG properties: @{}];
[pngData writeToURL: imgFileURL atomically: YES];

Expand Down Expand Up @@ -348,9 +356,8 @@ - (BOOL)performDragOperation: (id <NSDraggingInfo>)sender
NSString* imgFileURLStr = [NSString stringWithUTF8String: filePath.c_str()];
NSURL* imgFileURL = [NSURL URLWithString: imgFileURLStr];

[theImg lockFocus];
NSBitmapImageRep * bir = [[NSBitmapImageRep alloc] initWithFocusedViewRect: NSMakeRect(0,0,theImg.size.width,theImg.size.height)];
[theImg unlockFocus];
CGImageRef imageRef = [theImg CGImageForProposedRect:NULL context:NULL hints:nil];
NSBitmapImageRep *bir = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
NSData * pngData = [bir representationUsingType: NSBitmapImageFileTypePNG properties: @{}];
[pngData writeToURL: imgFileURL atomically: YES];

Expand All @@ -376,9 +383,8 @@ - (BOOL)performDragOperation: (id <NSDraggingInfo>)sender
NSString* imgFileURLStr = [NSString stringWithUTF8String: filePath.c_str()];
NSURL* imgFileURL = [NSURL URLWithString: imgFileURLStr];

[theImg lockFocus];
NSBitmapImageRep * bir = [[NSBitmapImageRep alloc] initWithFocusedViewRect: NSMakeRect(0,0,theImg.size.width,theImg.size.height)];
[theImg unlockFocus];
CGImageRef imageRef = [theImg CGImageForProposedRect:NULL context:NULL hints:nil];
NSBitmapImageRep *bir = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
NSData * pngData = [bir representationUsingType: NSBitmapImageFileTypePNG properties: @{}];
[pngData writeToURL: imgFileURL atomically: YES];

Expand Down
7 changes: 3 additions & 4 deletions Stacksmith/Platforms/Mac/WILDStackCanvasWindowController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ -(void) windowDidLoad

[self reloadData];

[self.stackCanvasView registerForDraggedTypes: [NSImage.imageTypes arrayByAddingObjectsFromArray: @[ NSFilenamesPboardType ]]];
[self.stackCanvasView registerForDraggedTypes: [NSImage.imageTypes arrayByAddingObjectsFromArray: @[ (NSString *)kUTTypeFileURL ]]];
}


Expand Down Expand Up @@ -447,9 +447,8 @@ -(void) addImages: (NSArray<NSImage*>*)images
NSString* imgFileURLStr = [NSString stringWithUTF8String: filePath.c_str()];
NSURL* imgFileURL = [NSURL URLWithString: imgFileURLStr];

[theImg lockFocus];
NSBitmapImageRep * bir = [[[NSBitmapImageRep alloc] initWithFocusedViewRect: NSMakeRect(0,0,theImg.size.width,theImg.size.height)] autorelease];
[theImg unlockFocus];
CGImageRef imageRef = [theImg CGImageForProposedRect:NULL context:NULL hints:nil];
NSBitmapImageRep *bir = [[NSBitmapImageRep alloc] initWithCGImage: imageRef];
NSData * pngData = [bir representationUsingType: NSBitmapImageFileTypePNG properties: @{}];
[pngData writeToURL: imgFileURL atomically: YES];
newIcon.mMediaID = pictureID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@
#import <Cocoa/Cocoa.h>


@class IKImageBrowserView;


@interface WILDTemplateProjectPickerController : NSWindowController
@interface WILDTemplateProjectPickerController : NSWindowController <NSCollectionViewDataSource>
{
NSMutableArray * groups;
NSMutableArray * items;
}

@property (assign,nonatomic) IBOutlet IKImageBrowserView* iconListView;
@property (assign,nonatomic) IBOutlet NSCollectionView* iconListView;
@property (copy,nonatomic) void (^callbackHandler)( NSString* inPickedFilePath );

-(IBAction) doOK: (id)sender;
Expand Down
64 changes: 23 additions & 41 deletions Stacksmith/Platforms/Mac/WILDTemplateProjectPickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
#import "UKHelperMacros.h"


@interface WILDTemplateProjectPickerController ()

@end

@interface WILDSimpleTemplateProjectBrowserItem : NSObject // IKImageBrowserItem
@interface WILDSimpleTemplateProjectBrowserItem : NSObject
{
NSImage* mImage;
NSString* mName;
Expand Down Expand Up @@ -114,32 +110,28 @@ -(void) ensureItemsListExists
[[NSApplication sharedApplication] presentError: err];
}

NSInteger groupCount = 0;
NSInteger itemCount = 0;

for( NSString* currSubfolderSubPath in subfolderpaths )
{
NSString * currSubfolderPath = [templatesPath stringByAppendingPathComponent: currSubfolderSubPath];
NSArray* subpaths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: currSubfolderPath error: &err];

NSUInteger startItemCount = itemCount;

if( !subpaths )
{
continue;
}

NSMutableArray<WILDSimpleTemplateProjectBrowserItem *> *groupContents = [NSMutableArray new];

[groups addObject: @{ @"name": currSubfolderSubPath, @"contents": groupContents } ];

for( NSString* currSubPath in subpaths )
{
NSString * currPath = [currSubfolderPath stringByAppendingPathComponent: currSubPath];
WILDSimpleTemplateProjectBrowserItem * tbi = [[[WILDSimpleTemplateProjectBrowserItem alloc] init] autorelease];
tbi.filename = currPath;
tbi.name = [[currPath lastPathComponent] stringByDeletingPathExtension];
[items addObject: tbi];

itemCount++;
}

if( subpaths )
{
groupCount++;

[groups addObject: @{ IKImageBrowserGroupRangeKey: [NSValue valueWithRange: (NSRange){ startItemCount, itemCount -startItemCount }], IKImageBrowserGroupTitleKey: currSubfolderSubPath, IKImageBrowserGroupStyleKey: @(IKGroupDisclosureStyle) } ];
[groupContents addObject: tbi];
}
}
}
Expand All @@ -156,45 +148,35 @@ -(void) windowDidLoad
}


-(NSUInteger) numberOfItemsInImageBrowser: (IKImageBrowserView *)aBrowser
- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
[self ensureItemsListExists];

return [items count];
NSDictionary *group = groups[ section ];
return [(NSArray *)group[@"contents"] count];
}


-(id /*IKImageBrowserItem*/) imageBrowser: (IKImageBrowserView *) aBrowser itemAtIndex: (NSUInteger)idx
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath
{
[self ensureItemsListExists];

return [items objectAtIndex: idx];
NSDictionary *group = groups[ [indexPath indexAtPosition:0] ];

NSCollectionViewItem *theItem = [collectionView makeItemWithIdentifier: @"StandardItem" forIndexPath: indexPath];

theItem.representedObject = group[@"contents"][ [indexPath indexAtPosition: 1] ];

return theItem;
}


- (NSUInteger) numberOfGroupsInImageBrowser:(IKImageBrowserView *) aBrowser
- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView
{
return groups.count;
}


/*!
@method imageBrowser:groupAtIndex:
@abstract Returns the group at index 'index'
@discussion A group is defined by a dictionay. Keys for this dictionary are defined below.
*/
- (NSDictionary *) imageBrowser:(IKImageBrowserView *) aBrowser groupAtIndex:(NSUInteger) index
{
return groups[index];
}


-(void) imageBrowserSelectionDidChange: (IKImageBrowserView *)aBrowser
{

}


-(IBAction) doOK: (id)sender
{
[self close];
Expand Down
Loading

0 comments on commit 9297088

Please sign in to comment.