Skip to content

Commit

Permalink
Simple drawing optimization, pass color & width info to the cells ins…
Browse files Browse the repository at this point in the history
…tead of them, each, pulling from the view.
  • Loading branch information
mmower committed Aug 2, 2008
1 parent e6406e3 commit 679d59f
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Honeycomb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
C9874AE50E42308C0068E7BF /* LMRegularPolygon.h in Headers */ = {isa = PBXBuildFile; fileRef = C93C64340E3FEEE400A6CE15 /* LMRegularPolygon.h */; };
C9874AE60E42308C0068E7BF /* LMRegularPolygon.m in Sources */ = {isa = PBXBuildFile; fileRef = C93C64350E3FEEE400A6CE15 /* LMRegularPolygon.m */; };
C9874AE70E42308C0068E7BF /* HoneycombView.h in Headers */ = {isa = PBXBuildFile; fileRef = C9389E950E421F240077F84F /* HoneycombView.h */; settings = {ATTRIBUTES = (Public, ); }; };
C9F68A680E447A33004B631C /* DrawingInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = C9F68A670E447A33004B631C /* DrawingInfo.h */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -54,6 +55,7 @@
C9874ADA0E4230790068E7BF /* HoneycombView-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "HoneycombView-Info.plist"; sourceTree = "<group>"; };
C9874B4A0E42337A0068E7BF /* HoneycombViewIBPlugin.bundle */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = HoneycombViewIBPlugin.bundle; sourceTree = BUILT_PRODUCTS_DIR; };
C9874B4B0E42337A0068E7BF /* HoneycombViewIBPlugin-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "HoneycombViewIBPlugin-Info.plist"; sourceTree = "<group>"; };
C9F68A670E447A33004B631C /* DrawingInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DrawingInfo.h; sourceTree = "<group>"; };
D2F7E79907B2D74100F64583 /* CoreData.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreData.framework; path = /System/Library/Frameworks/CoreData.framework; sourceTree = "<absolute>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -150,6 +152,7 @@
C93C64340E3FEEE400A6CE15 /* LMRegularPolygon.h */,
C93C64350E3FEEE400A6CE15 /* LMRegularPolygon.m */,
C9389E950E421F240077F84F /* HoneycombView.h */,
C9F68A670E447A33004B631C /* DrawingInfo.h */,
);
path = view;
sourceTree = "<group>";
Expand Down Expand Up @@ -212,6 +215,7 @@
C9874AE30E42308C0068E7BF /* LMHoneycombView.h in Headers */,
C9874AE50E42308C0068E7BF /* LMRegularPolygon.h in Headers */,
C9874AE70E42308C0068E7BF /* HoneycombView.h in Headers */,
C9F68A680E447A33004B631C /* DrawingInfo.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ Selection events are dispatched to both the data model and any delegate of the v

* I've swapped around the drawing code and responsibilities a few times. I'm still not sure it's right yet. I toyed with making the cell a protocol as well and caching the NSBezierPath's in the view itself but felt that extending the cell was likely (I will want custom cell drawing in my own application) making it a reasonable class in itself.

* In my application the grid is 17 columns of 12 cells each. This control was originally not a discrete control but was extracted from the application where lots of stuff was hard wired. I've spent some time looking at the geometry and I think it is now generic.
* In my application the grid is 17 columns of 12 cells each. This control was originally not a discrete control but was extracted from the application where lots of stuff was hard wired. I've spent some time looking at the geometry and from playing with the improved sample app (where you can now change columns and rows) I think it now works for any values.

* A pretty major issue is to do with resizing. The control draws regular hexagons which are wider than they are tall this means that resizing the control can end up with either a lot of blank space or hexagons being "cut off". At this point I don't know of a means to constrain the aspect ratio of a specific control in a resizing operation - patches welcome!

* Drawing efficiency. The cells are responsible for drawing themself onto the view which is neat and allows cell classes to be customised to draw cells differently (e.g. to put text in them). At present the cell makes a number of calls back to the view to retrieve drawing related information such as border color, selected colour, border thickness and so on. Each of these calls is made for each cell. Passing this information in a struct would be a relatively simple optimisation.
* Drawing efficiency. The cells are responsible for drawing themself onto the view which is neat and allows cell classes to be customised to draw cells differently (e.g. to put text in them). A simple optimization has been made by passing drawing context (e.g. colours) from the view to the cells rather than each cell requesting the same information from the view as it draws. There may be other optimizations to be made here also.

* Hit detection uses the brute force method of querying each cell in turn to see if the mouse down event occurred within it's NSBezierPath. In practice this has not proved too slow for a grid of 204 cells but could probably be optimized using some simple heuristics to narrow down the range of possible cells.

Expand Down
15 changes: 15 additions & 0 deletions view/DrawingInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* DrawingInfo.h
* Honeycomb
*
* Created by Matt Mower on 02/08/2008.
* Copyright 2008 LucidMac Software. All rights reserved.
*
*/

typedef struct tagDrawingInfo {
NSColor *defaultColor;
NSColor *selectedColor;
NSColor *borderColor;
CGFloat borderWidth;
} DrawingInfo;
1 change: 1 addition & 0 deletions view/HoneycombView.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
*/

#import <HoneycombView/DrawingInfo.h>
#import <HoneycombView/LMHexCell.h>
#import <HoneycombView/LMHoneycombMatrix.h>
#import <HoneycombView/LMHoneycombView.h>
4 changes: 2 additions & 2 deletions view/LMHexCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import <Cocoa/Cocoa.h>

#import <Cocoa/Cocoa.h>
#import "DrawingInfo.h"

@class LMHoneycombView;

Expand Down Expand Up @@ -40,6 +40,6 @@
- (BOOL)selected;
- (void)setSelected:(BOOL)selected;

- (void)drawOnHoneycombView:(LMHoneycombView *)view;
- (void)drawOnHoneycombView:(LMHoneycombView *)view with:(DrawingInfo)info;

@end
10 changes: 5 additions & 5 deletions view/LMHexCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ - (void)setData:(id)_data {
data = _data;
}

- (void)drawOnHoneycombView:(LMHoneycombView *)_view {
- (void)drawOnHoneycombView:(LMHoneycombView *)_view with:(DrawingInfo)_info {
if( selected ) {
[[_view selectedColor] set];
[_info.selectedColor set];
} else {
[[_view defaultColor] set];
[_info.defaultColor set];
}
[path fill];

[[_view borderColor] set];
[path setLineWidth:[_view borderWidth]];
[_info.borderColor set];
[path setLineWidth:_info.borderWidth];
[path stroke];
}

Expand Down
9 changes: 7 additions & 2 deletions view/LMHoneycombView.m
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,15 @@ - (void)drawRect:(NSRect)rect {
firstDrawing = NO;
}

DrawingInfo info;
info.defaultColor = [self defaultColor];
info.selectedColor = [self selectedColor];
info.borderColor = [self borderColor];
info.borderWidth = [self borderWidth];

for( int col = 0; col < cols; col++ ) {
for( int row = 0; row < rows; row++ ) {
[[dataSource hexCellAtColumn:col row:row] drawOnHoneycombView:self];
[[dataSource hexCellAtColumn:col row:row] drawOnHoneycombView:self with:info];
}
}
}
Expand Down Expand Up @@ -196,7 +202,6 @@ - (void)setSelected:(LMHexCell *)_selected {
}

- (void)mouseDown:(NSEvent *)_event {
NSLog( @"mouse = %f,%f", [_event locationInWindow].x, [_event locationInWindow].y );
[self setSelected:[self findCellAtPoint:[self convertPoint:[_event locationInWindow] fromView:nil]]];
}

Expand Down

0 comments on commit 679d59f

Please sign in to comment.