Permalink
Browse files

Added ability to re-order colors in palettes by dragging, fixes #66

  • Loading branch information...
mattrajca committed Apr 24, 2012
1 parent 389cbcc commit a9a0941c16f511ff229aae937e1b25b67b31decf
@@ -46,6 +46,7 @@
- (void)removeLastColor;
- (void)replaceColorAtIndex:(NSUInteger)index withColor:(PXColor)color;
+- (void)moveColorAtIndex:(NSUInteger)sourceIndex toIndex:(NSUInteger)targetIndex;
- (PXColor)colorClosestToColor:(PXColor)color;
@@ -269,6 +269,11 @@ - (void)replaceColorAtIndex:(NSUInteger)index withColor:(PXColor)color
#warning TODO: implement
}
+- (void)moveColorAtIndex:(NSUInteger)sourceIndex toIndex:(NSUInteger)targetIndex
+{
+ PXColorArrayMoveColor(_colors, sourceIndex, targetIndex);
+}
+
- (id)copyWithZone:(NSZone *)zone
{
PXPalette *newPalette = [[PXPalette alloc] initWithoutBackgroundColor];
@@ -0,0 +1,10 @@
+//
+// PXInsertionView.h
+// Pixen
+//
+// Copyright 2012 Pixen Project. All rights reserved.
+//
+
+@interface PXInsertionView : NSView
+
+@end
@@ -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
@@ -7,6 +7,8 @@
#import "PXPalette.h"
+@class PXInsertionView;
+
@interface PXPaletteView : NSView
{
@private
@@ -21,6 +23,10 @@
NSMutableSet *_visibleViews;
NSMutableSet *_recycledViews;
+
+ NSUInteger _clickedCelIndex;
+ BOOL _dragging;
+ PXInsertionView *_insertionView;
}
@property (nonatomic, assign) BOOL allowsColorSelection;
@@ -7,6 +7,7 @@
#import "PXPaletteView.h"
+#import "PXInsertionView.h"
#import "PXPaletteColorView.h"
@implementation PXPaletteView
@@ -27,6 +28,8 @@ - (id)initWithFrame:(NSRect)frameRect
controlSize = NSRegularControlSize;
allowsColorSelection = allowsColorModification = YES;
+ _clickedCelIndex = NSNotFound;
+
[self registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeColor]];
}
return self;
@@ -88,9 +91,14 @@ - (void)reload
[self retile];
}
+- (int)celSize
+{
+ return (controlSize == NSRegularControlSize ? 32 : 16);
+}
+
- (void)size
{
- width = (controlSize == NSRegularControlSize ? 32 : 16) + viewMargin;
+ width = [self celSize] + viewMargin;
columns = NSWidth([self bounds]) / width;
rows = palette ? ceilf((float)(([palette colorCount])) / columns) : 0;
@@ -429,31 +437,66 @@ - (NSView *)hitTest:(NSPoint)aPoint
- (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) {
- NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
- NSUInteger index = [self indexOfCelAtPoint:point];
-
- if (index == NSNotFound)
- return;
-
if ([delegate respondsToSelector:@selector(paletteView:modifyColorAtIndex:)])
[delegate paletteView:self modifyColorAtIndex:index];
-
- return;
}
-
- [self activateIndexWithEvent:event];
+ else if ([event clickCount] == 1 && palette.canSave) {
+ _clickedCelIndex = index;
+ }
}
- (void)mouseDragged:(NSEvent *)event
{
- [self activateIndexWithEvent: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
{
- // [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
@@ -84,6 +84,8 @@
C9B1FA5A13D7328D00BB7CF7 /* PXManagePresetsController.h in Headers */ = {isa = PBXBuildFile; fileRef = C9B1FA5813D7328D00BB7CF7 /* PXManagePresetsController.h */; };
C9B1FA5B13D7328D00BB7CF7 /* PXManagePresetsController.m in Sources */ = {isa = PBXBuildFile; fileRef = C9B1FA5913D7328D00BB7CF7 /* PXManagePresetsController.m */; };
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 */; };
C9C7D461151E21F2009BBCA8 /* PXCanvasAnchorView.m in Sources */ = {isa = PBXBuildFile; fileRef = C9C7D45F151E21F2009BBCA8 /* PXCanvasAnchorView.m */; };
C9D58D8B14F0316B00A62C10 /* PXColor.h in Headers */ = {isa = PBXBuildFile; fileRef = C9D58D8914F0316B00A62C10 /* PXColor.h */; };
@@ -630,6 +632,8 @@
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>"; };
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>"; };
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>"; };
@@ -1386,6 +1390,8 @@
EA31F9810CE7CE4800EC481B /* Palette Picker */ = {
isa = PBXGroup;
children = (
+ C9B3A69B1547200F00872BF2 /* PXInsertionView.h */,
+ C9B3A69C1547200F00872BF2 /* PXInsertionView.m */,
EA31F9890CE7CE4800EC481B /* PXPaletteSelector.h */,
EA31F98A0CE7CE4800EC481B /* PXPaletteSelector.m */,
EA31F98B0CE7CE4800EC481B /* PXPaletteView.h */,
@@ -2231,6 +2237,7 @@
C9F1077C152CCB7000FF2FD5 /* PXPreviewControlView.h in Headers */,
C989DF9A15386C3400B055D5 /* GPLWriter.h in Headers */,
C989DF9E15386C3D00B055D5 /* GPLReader.h in Headers */,
+ C9B3A69D1547200F00872BF2 /* PXInsertionView.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -2667,6 +2674,7 @@
C9F1077D152CCB7000FF2FD5 /* PXPreviewControlView.m in Sources */,
C989DF9B15386C3400B055D5 /* GPLWriter.m in Sources */,
C989DF9F15386C3D00B055D5 /* GPLReader.m in Sources */,
+ C9B3A69E1547200F00872BF2 /* PXInsertionView.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

0 comments on commit a9a0941

Please sign in to comment.