Permalink
Browse files

Added working LMHoneycombView control and sample application.

  • Loading branch information...
1 parent 66382b3 commit d20a2438299b7574d311878dc0df812af734bbbe @mmower committed Aug 1, 2008
Binary file not shown.
Binary file not shown.
Oops, something went wrong.
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.yourcompany.${PRODUCT_NAME:identifier}</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundlePackageType</key>
+ <string>APPL</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+ <key>NSMainNibFile</key>
+ <string>MainMenu</string>
+ <key>NSPrincipalClass</key>
+ <string>NSApplication</string>
+</dict>
+</plist>
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleIdentifier</key>
+ <string>com.lucidmac.${PRODUCT_NAME:identifier}</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundlePackageType</key>
+ <string>FMWK</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+</dict>
+</plist>
View
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>CFBundleDevelopmentRegion</key>
+ <string>English</string>
+ <key>CFBundleExecutable</key>
+ <string>${EXECUTABLE_NAME}</string>
+ <key>CFBundleName</key>
+ <string>${PRODUCT_NAME}</string>
+ <key>CFBundleIconFile</key>
+ <string></string>
+ <key>CFBundleIdentifier</key>
+ <string>com.lucidmac.${PRODUCT_NAME:identifier}</string>
+ <key>CFBundleInfoDictionaryVersion</key>
+ <string>6.0</string>
+ <key>CFBundlePackageType</key>
+ <string>FMWK</string>
+ <key>CFBundleSignature</key>
+ <string>????</string>
+ <key>CFBundleVersion</key>
+ <string>1.0</string>
+ <key>NSPrincipalClass</key>
+ <string></string>
+</dict>
+</plist>
View
No changes.
View
@@ -0,0 +1,31 @@
+LMHoneycombView
+by Matt Mower <self@mattmower.com>
+http://matt.blogs.it/
+
+^Overview
+
+This control presents a honeycomb made up of interlocking columns of regular hexagons ("hex cells") that can be selected using the mouse.
+
+The honeycomb view draws cell data from a dataSource that follows an included protocol and is quite flexile. The cell class provides basic drawing and can be extended to customise the appearance of cells.
+
+I originally wrote this control as part of an application I am building. There was enough interest in how it worked that I spent some time extracting a generic honeycomb view control and re-wrote my application to use it. It's broadly 'good enough'.
+
+This control is released under the MIT license (see attached LICENSE file) and includes work .
+
+^How to use
+
+You'll need to build a working version of the control. This can either be done by building it as a framework and including it on your system, add the sources to your directly to your own application, or (as I do) building it as a private framework in your app. XCode is a bit annoying but you can make this work without too much trouble.
+
+Once you have the framework just add an NSView object to your nib file and set the class to LMHoneycombView. You will then need a dataSource that provides cells to the view. Implement the LMHoneycombMatrix protocol in one of your classes and set this to be the dataSource of the view. The dataSource decides how many columns & rows the view has and the view will then pull LMHexCell objects from the dataSource to fill it.
+
+Selection events are dispatched to both the data model and any delegate of the view and include the selected hex cell. LMHexCell can hold a data object and can be subclassed to offer additional functionality.
+
+^Issues & follow-ups
+
+* The 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.
+
+* 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.
+
+* It shouldn't be too hard to add an Interface Builder palette for the hex view. It shouldn't be too hard but of course the stuff you have to do is a little daunting so this may take a while.
@@ -0,0 +1,19 @@
+//
+// HoneycombController.h
+// HoneycombView
+//
+// Created by Matt Mower on 31/07/2008.
+// Copyright 2008 LucidMac Software. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+#import <HoneycombView/HoneycombView.h>
+
+@interface HoneycombController : NSObject <LMHoneycombMatrix> {
+ IBOutlet LMHoneycombView *honeycombView;
+
+ NSMutableArray *cells;
+}
+
+@end
@@ -0,0 +1,45 @@
+//
+// HoneycombController.m
+// HoneycombView Test App
+//
+// Created by Matt Mower on 31/07/2008.
+// Copyright 2008 LucidMac Software. All rights reserved.
+//
+
+#import "HoneycombController.h"
+
+@implementation HoneycombController
+
+- (id)init {
+ if( self = [super init] ) {
+ cells = [[NSMutableArray alloc] initWithCapacity:204];
+ }
+
+ return self;
+}
+
+- (void)awakeFromNib {
+ for( int col = 0; col < 17; col++ ) {
+ for( int row = 0; row < 12; row++ ) {
+ [cells addObject:[[LMHexCell alloc] initWithColumn:col row:row data:nil]];
+ }
+ }
+}
+
+- (int)hexColumns {
+ return 17;
+}
+
+- (int)hexRows {
+ return 12;
+}
+
+- (LMHexCell *)hexCellAtColumn:(int)column row:(int)row {
+ return [cells objectAtIndex:((column*12)+row)];
+}
+
+- (void)hexCellSelected:(LMHexCell *)cell {
+ NSLog( @"Selected hex at grid reference: %d,%d", [cell column], [cell row] );
+}
+
+@end
Oops, something went wrong.

0 comments on commit d20a243

Please sign in to comment.