On Dec 30, 2008, at 20:35, Matt Rajca wrote:

I am trying to figure out how to implement a 'Piano Roll' view in Cocoa. Attached is a screenshot of what I'm trying to accomplish. Disregard the keyboard on the left of the window and let's just focus on the area with the colorful rectangles, symbolizing notes.

1. When drawing the light and dark grey rows which make up a background, should I just use a loop with Quartz 2D/Cocoa Drawing calls? 2. When drawing the vertical lines, should I just use a loop with Quartz 2D/Cocoa Drawing calls?

Yes to both. You can choose to draw a line with a 1 pixel wide rectangle (or whatever the line width is) using NSRectFill or one of its friends, or you can choose to construct a NSBezierPath (you can put some or all of the lines in the same path with move-to/line-to combinations) and then stroke the path. There is a subtle difference between the two approaches: when using whole-number coordinate values, NSRectFill in effect pixel-aligns the edges of the line, while NSBezierPath in effect pixel-aligns the center of the line. (Of course, you can get the same result with either approach, by suitably offsetting your coordinates by half a pixel.)

3. Should I put the notes (colorful rectangles) each in a separate view (or Core Animation layer), or is it effective to just draw them directly in the main view (same view in which the rows are drawn)? I am eventually going to want to resize them and change their position by dragging them around.

With the number of note rects your image shows (and presumably there could be a lot more in some music), I'd suggest avoiding subviews. If you want to animate the notes (have them slide around smoothly, or fade in or out) CA layers would probably be the way to go. Otherwise, just draw everything in a single custom view.

4. How would I go about changing the view's width as more notes (rectangles) are added to the view? When enclosed in a NSScrollView, would manipulating the width of the view also hide/show the horizontal scrollbar appropriately?

Something like this:

NSRect noteFrame = noteView.frame; // or [noteView frame], if you prefer that syntax
        noteFrame.size.width = computedViewWidth;
noteView.frame = noteFrame; // or [noteView setFrame: noteFrame], if you prefer that syntax

and make sure you've disabled horizontal autoresizing for your note view (in Interface Builder).

If you've checked the NSScrollView option to automatically hide/show scrollbars (in Interface Builder), changing the note view frame as above will automatically hide or show the horizontal scrollbar.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to