Skip to content

Commit

Permalink
Added ability to re-order colors in palettes by dragging, fixes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrajca committed Apr 24, 2012
1 parent 389cbcc commit a9a0941
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 13 deletions.
1 change: 1 addition & 0 deletions Pixel Art Core/Palette/PXPalette.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
- (void)removeLastColor; - (void)removeLastColor;


- (void)replaceColorAtIndex:(NSUInteger)index withColor:(PXColor)color; - (void)replaceColorAtIndex:(NSUInteger)index withColor:(PXColor)color;
- (void)moveColorAtIndex:(NSUInteger)sourceIndex toIndex:(NSUInteger)targetIndex;


- (PXColor)colorClosestToColor:(PXColor)color; - (PXColor)colorClosestToColor:(PXColor)color;


Expand Down
5 changes: 5 additions & 0 deletions Pixel Art Core/Palette/PXPalette.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ - (void)replaceColorAtIndex:(NSUInteger)index withColor:(PXColor)color
#warning TODO: implement #warning TODO: implement
} }


- (void)moveColorAtIndex:(NSUInteger)sourceIndex toIndex:(NSUInteger)targetIndex
{
PXColorArrayMoveColor(_colors, sourceIndex, targetIndex);
}

- (id)copyWithZone:(NSZone *)zone - (id)copyWithZone:(NSZone *)zone
{ {
PXPalette *newPalette = [[PXPalette alloc] initWithoutBackgroundColor]; PXPalette *newPalette = [[PXPalette alloc] initWithoutBackgroundColor];
Expand Down
10 changes: 10 additions & 0 deletions Pixen Application/Palette/Palette Picker/PXInsertionView.h
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// PXInsertionView.h
// Pixen
//
// Copyright 2012 Pixen Project. All rights reserved.
//

@interface PXInsertionView : NSView

@end
18 changes: 18 additions & 0 deletions Pixen Application/Palette/Palette Picker/PXInsertionView.m
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// PXInsertionView.m
// Pixen
//
// Copyright 2012 Pixen Project. All rights reserved.
//

#import "PXInsertionView.h"

@implementation PXInsertionView

- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor blackColor] set];
NSRectFill([self bounds]);
}

@end
6 changes: 6 additions & 0 deletions Pixen Application/Palette/Palette Picker/PXPaletteView.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@


#import "PXPalette.h" #import "PXPalette.h"


@class PXInsertionView;

@interface PXPaletteView : NSView @interface PXPaletteView : NSView
{ {
@private @private
Expand All @@ -21,6 +23,10 @@


NSMutableSet *_visibleViews; NSMutableSet *_visibleViews;
NSMutableSet *_recycledViews; NSMutableSet *_recycledViews;

NSUInteger _clickedCelIndex;
BOOL _dragging;
PXInsertionView *_insertionView;
} }


@property (nonatomic, assign) BOOL allowsColorSelection; @property (nonatomic, assign) BOOL allowsColorSelection;
Expand Down
69 changes: 56 additions & 13 deletions Pixen Application/Palette/Palette Picker/PXPaletteView.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


#import "PXPaletteView.h" #import "PXPaletteView.h"


#import "PXInsertionView.h"
#import "PXPaletteColorView.h" #import "PXPaletteColorView.h"


@implementation PXPaletteView @implementation PXPaletteView
Expand All @@ -27,6 +28,8 @@ - (id)initWithFrame:(NSRect)frameRect
controlSize = NSRegularControlSize; controlSize = NSRegularControlSize;
allowsColorSelection = allowsColorModification = YES; allowsColorSelection = allowsColorModification = YES;


_clickedCelIndex = NSNotFound;

[self registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeColor]]; [self registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeColor]];
} }
return self; return self;
Expand Down Expand Up @@ -88,9 +91,14 @@ - (void)reload
[self retile]; [self retile];
} }


- (int)celSize
{
return (controlSize == NSRegularControlSize ? 32 : 16);
}

- (void)size - (void)size
{ {
width = (controlSize == NSRegularControlSize ? 32 : 16) + viewMargin; width = [self celSize] + viewMargin;
columns = NSWidth([self bounds]) / width; columns = NSWidth([self bounds]) / width;
rows = palette ? ceilf((float)(([palette colorCount])) / columns) : 0; rows = palette ? ceilf((float)(([palette colorCount])) / columns) : 0;


Expand Down Expand Up @@ -429,31 +437,66 @@ - (NSView *)hitTest:(NSPoint)aPoint


- (void)mouseDown:(NSEvent *)event - (void)mouseDown:(NSEvent *)event
{ {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSUInteger index = [self indexOfCelAtPoint:point];

if (index == NSNotFound)
return;

if ([event clickCount] == 2 && allowsColorModification) { if ([event clickCount] == 2 && allowsColorModification) {
NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
NSUInteger index = [self indexOfCelAtPoint:point];

if (index == NSNotFound)
return;

if ([delegate respondsToSelector:@selector(paletteView:modifyColorAtIndex:)]) if ([delegate respondsToSelector:@selector(paletteView:modifyColorAtIndex:)])
[delegate paletteView:self modifyColorAtIndex:index]; [delegate paletteView:self modifyColorAtIndex:index];

return;
} }

else if ([event clickCount] == 1 && palette.canSave) {
[self activateIndexWithEvent:event]; _clickedCelIndex = index;
}
} }


- (void)mouseDragged:(NSEvent *)event - (void)mouseDragged:(NSEvent *)event
{ {
[self activateIndexWithEvent:event];
[self autoscroll:event]; [self autoscroll:event];

if (_clickedCelIndex != NSNotFound) {
_dragging = YES;

NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
point.x = (int) ((point.x + [self celSize] / 2) / width) * width;
point.y = (int) (point.y / width) * width;

if (!_insertionView) {
_insertionView = [[PXInsertionView alloc] initWithFrame:NSZeroRect];
[self addSubview:_insertionView];
}

_insertionView.frame = NSMakeRect(point.x, point.y, 2.0f, [self celSize] + viewMargin * 2);
}
} }


- (void)mouseUp:(NSEvent *)event - (void)mouseUp:(NSEvent *)event
{ {
// [self activateIndexWithEvent:event]; if (_dragging) {
int row = _insertionView.frame.origin.y / width;
int col = (_insertionView.frame.origin.x + [self celSize] / 2) / width;

int n = row * columns + col;

[palette moveColorAtIndex:_clickedCelIndex toIndex:n];

selectionIndex = NSNotFound;
[self reload];

[_insertionView removeFromSuperview];

[_insertionView release];
_insertionView = nil;

_clickedCelIndex = NSNotFound;
_dragging = NO;

return;
}

[self activateIndexWithEvent:event];
} }


@end @end
8 changes: 8 additions & 0 deletions Pixen.xcodeproj/project.pbxproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
C9B1FA5A13D7328D00BB7CF7 /* PXManagePresetsController.h in Headers */ = {isa = PBXBuildFile; fileRef = C9B1FA5813D7328D00BB7CF7 /* PXManagePresetsController.h */; }; C9B1FA5A13D7328D00BB7CF7 /* PXManagePresetsController.h in Headers */ = {isa = PBXBuildFile; fileRef = C9B1FA5813D7328D00BB7CF7 /* PXManagePresetsController.h */; };
C9B1FA5B13D7328D00BB7CF7 /* PXManagePresetsController.m in Sources */ = {isa = PBXBuildFile; fileRef = C9B1FA5913D7328D00BB7CF7 /* PXManagePresetsController.m */; }; C9B1FA5B13D7328D00BB7CF7 /* PXManagePresetsController.m in Sources */ = {isa = PBXBuildFile; fileRef = C9B1FA5913D7328D00BB7CF7 /* PXManagePresetsController.m */; };
C9B1FA5E13D732FC00BB7CF7 /* PXManagePresetsWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9B1FA5C13D732FC00BB7CF7 /* PXManagePresetsWindow.xib */; }; C9B1FA5E13D732FC00BB7CF7 /* PXManagePresetsWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = C9B1FA5C13D732FC00BB7CF7 /* PXManagePresetsWindow.xib */; };
C9B3A69D1547200F00872BF2 /* PXInsertionView.h in Headers */ = {isa = PBXBuildFile; fileRef = C9B3A69B1547200F00872BF2 /* PXInsertionView.h */; };
C9B3A69E1547200F00872BF2 /* PXInsertionView.m in Sources */ = {isa = PBXBuildFile; fileRef = C9B3A69C1547200F00872BF2 /* PXInsertionView.m */; };
C9C7D460151E21F2009BBCA8 /* PXCanvasAnchorView.h in Headers */ = {isa = PBXBuildFile; fileRef = C9C7D45E151E21F2009BBCA8 /* PXCanvasAnchorView.h */; }; C9C7D460151E21F2009BBCA8 /* PXCanvasAnchorView.h in Headers */ = {isa = PBXBuildFile; fileRef = C9C7D45E151E21F2009BBCA8 /* PXCanvasAnchorView.h */; };
C9C7D461151E21F2009BBCA8 /* PXCanvasAnchorView.m in Sources */ = {isa = PBXBuildFile; fileRef = C9C7D45F151E21F2009BBCA8 /* PXCanvasAnchorView.m */; }; C9C7D461151E21F2009BBCA8 /* PXCanvasAnchorView.m in Sources */ = {isa = PBXBuildFile; fileRef = C9C7D45F151E21F2009BBCA8 /* PXCanvasAnchorView.m */; };
C9D58D8B14F0316B00A62C10 /* PXColor.h in Headers */ = {isa = PBXBuildFile; fileRef = C9D58D8914F0316B00A62C10 /* PXColor.h */; }; C9D58D8B14F0316B00A62C10 /* PXColor.h in Headers */ = {isa = PBXBuildFile; fileRef = C9D58D8914F0316B00A62C10 /* PXColor.h */; };
Expand Down Expand Up @@ -630,6 +632,8 @@
C9B1FA5813D7328D00BB7CF7 /* PXManagePresetsController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXManagePresetsController.h; sourceTree = "<group>"; }; C9B1FA5813D7328D00BB7CF7 /* PXManagePresetsController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXManagePresetsController.h; sourceTree = "<group>"; };
C9B1FA5913D7328D00BB7CF7 /* PXManagePresetsController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXManagePresetsController.m; sourceTree = "<group>"; }; C9B1FA5913D7328D00BB7CF7 /* PXManagePresetsController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXManagePresetsController.m; sourceTree = "<group>"; };
C9B1FA5D13D732FC00BB7CF7 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/PXManagePresetsWindow.xib; sourceTree = "<group>"; }; C9B1FA5D13D732FC00BB7CF7 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/PXManagePresetsWindow.xib; sourceTree = "<group>"; };
C9B3A69B1547200F00872BF2 /* PXInsertionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXInsertionView.h; sourceTree = "<group>"; };
C9B3A69C1547200F00872BF2 /* PXInsertionView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXInsertionView.m; sourceTree = "<group>"; };
C9C7D45E151E21F2009BBCA8 /* PXCanvasAnchorView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXCanvasAnchorView.h; sourceTree = "<group>"; }; C9C7D45E151E21F2009BBCA8 /* PXCanvasAnchorView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXCanvasAnchorView.h; sourceTree = "<group>"; };
C9C7D45F151E21F2009BBCA8 /* PXCanvasAnchorView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXCanvasAnchorView.m; sourceTree = "<group>"; }; C9C7D45F151E21F2009BBCA8 /* PXCanvasAnchorView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PXCanvasAnchorView.m; sourceTree = "<group>"; };
C9D58D8914F0316B00A62C10 /* PXColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXColor.h; sourceTree = "<group>"; }; C9D58D8914F0316B00A62C10 /* PXColor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PXColor.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1386,6 +1390,8 @@
EA31F9810CE7CE4800EC481B /* Palette Picker */ = { EA31F9810CE7CE4800EC481B /* Palette Picker */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
C9B3A69B1547200F00872BF2 /* PXInsertionView.h */,
C9B3A69C1547200F00872BF2 /* PXInsertionView.m */,
EA31F9890CE7CE4800EC481B /* PXPaletteSelector.h */, EA31F9890CE7CE4800EC481B /* PXPaletteSelector.h */,
EA31F98A0CE7CE4800EC481B /* PXPaletteSelector.m */, EA31F98A0CE7CE4800EC481B /* PXPaletteSelector.m */,
EA31F98B0CE7CE4800EC481B /* PXPaletteView.h */, EA31F98B0CE7CE4800EC481B /* PXPaletteView.h */,
Expand Down Expand Up @@ -2231,6 +2237,7 @@
C9F1077C152CCB7000FF2FD5 /* PXPreviewControlView.h in Headers */, C9F1077C152CCB7000FF2FD5 /* PXPreviewControlView.h in Headers */,
C989DF9A15386C3400B055D5 /* GPLWriter.h in Headers */, C989DF9A15386C3400B055D5 /* GPLWriter.h in Headers */,
C989DF9E15386C3D00B055D5 /* GPLReader.h in Headers */, C989DF9E15386C3D00B055D5 /* GPLReader.h in Headers */,
C9B3A69D1547200F00872BF2 /* PXInsertionView.h in Headers */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand Down Expand Up @@ -2667,6 +2674,7 @@
C9F1077D152CCB7000FF2FD5 /* PXPreviewControlView.m in Sources */, C9F1077D152CCB7000FF2FD5 /* PXPreviewControlView.m in Sources */,
C989DF9B15386C3400B055D5 /* GPLWriter.m in Sources */, C989DF9B15386C3400B055D5 /* GPLWriter.m in Sources */,
C989DF9F15386C3D00B055D5 /* GPLReader.m in Sources */, C989DF9F15386C3D00B055D5 /* GPLReader.m in Sources */,
C9B3A69E1547200F00872BF2 /* PXInsertionView.m in Sources */,
); );
runOnlyForDeploymentPostprocessing = 0; runOnlyForDeploymentPostprocessing = 0;
}; };
Expand Down

0 comments on commit a9a0941

Please sign in to comment.