Custom NSButton not working from CoreMIDI callback function - cocoa

I'm creating a piano app (for OSX) that has an onscreen keyboard that displays what the user is playing on their synth keyboard. Everything is connected using the CoreMIDI framework. I have created some customs buttons by subclassing NSButton in a class called PianoKey. The class can be seen below:
#import "PianoKey.h"
#implementation PianoKey
//updates the core animation layer whenever it needs to be changed
- (BOOL)wantsUpdateLayer {
return YES;
}
- (void)updateLayer {
//tells you whether or not the button is pressed
if ([self.cell isHighlighted]) {
NSLog(#"BUTTON PRESSED");
self.layer.contents = [NSImage imageNamed:#"buttonpressed.png"];
}
else {
NSLog(#"BUTTON NOT PRESSED");
self.layer.contents = [NSImage imageNamed:#"button.png"];
}
}
#end
The "PianoKeys" are created programmatically. When unpressed, the piano keys are blue and when they are pressed they are pink (just temp colours). The colour switch works fine if I click the buttons on screen, but they are not changing when I try to play through my MIDI keyboard.
Some things to note:
1) The MIDI keyboard works
2) I am successfully getting MIDI data from the keyboard.
a) This MIDI data is passed to a callback function called midiInputCallback as seen below:
[from class AppController.m]
void midiInputCallback(const MIDIPacketList *list, void *procRef, void *srcRef) {
PianoKey *button = (__bridge PianoKey*)procRef;
UInt16 nBytes;
const MIDIPacket *packet = &list->packet[0]; //gets first packet in list
for(unsigned int i = 0; i < list->numPackets; i++) {
nBytes = packet->length; //number of bytes in a packet
handleMIDIStatus(packet, button);
packet = MIDIPacketNext(packet);
}
}
The object button is a reference to the button that I've created programatically as defined in AppController.h as:
#property PianoKey *button; //yes, this is synthesized in AppController.m
b) The callback function calls a bunch of functions that handle the MIDI data. If a note has been detected as being played, this is where I set my custom button to be highlighted...this can be seen in the function below:
void updateKeyboardButtonAfterKeyPressed(PianoKey *button, int key, bool keyOn) {
if(keyOn)
[button highlight:YES];
else
[button highlight:NO];
[button updateLayer];
}
[button updateLayer] calls my updateLayer() method from PianoKey, but it doesn't change the image. Is there something I'm missing? It seems as if the view or window is not being updated, even though my button says that it is "highlighted". Please help! Thanks in advance :)

Just a guess (but a good one...;-) Is the callback function being called on the main thread? If not, that could be the problem - at leas that is the way it works in iOS: the UI (screen drawing) is only updated in the main thread.
Put a log statement or breakpoint in the callback to see if it is getting called. If it is, then the problem is due to this thread issue.
Update:
So tell the button to update itself - but on the main thread (I think I have the syntaxt right - at least for iOS - OSX may vary)
[button performSelectorOnMainThread:#selector(updateLayer)
withObject:nil
waitUntilDone:FALSE];

Related

iOS 8 presentationController determine if really is popover

I'm using the new adaptive "Present As Popover" capability of iOS 8. I wired up a simple segue in the StoryBoard to do the presentation. It works great on an iPhone 6 Plus as it presents the view as a popover and on an iPhone 4s it shows as a full screen view (sheet style).
The problem is when shown as a full screen view, I need to add a "Done" button to the view so dismissViewControllerAnimated can be called. And I don't want to show the "done" button when it's shown as a popover.
I tried looking at the properties of both presentationController and popoverPresentationController, and I can find nothing that tells me if it is actually being shown as a popover.
NSLog( #"View loaded %lx", (long)self.presentationController.adaptivePresentationStyle ); // UIModalPresentationFullScreen
NSLog( #"View loaded %lx", (long)self.presentationController.presentationStyle ); // UIModalPresentationPopover
NSLog( #"View loaded %lx", (long)self.popoverPresentationController.adaptivePresentationStyle ); // UIModalPresentationFullScreen
NSLog( #"View loaded %lx", (long)self.popoverPresentationController.presentationStyle ); // UIModalPresentationPopover
adaptivePresentationStyle always returns UIModalPresentationFullScreen and presentationStyle always returns UIModalPresentationPopover
When looking at the UITraitCollection I did find a trait called "_UITraitNameInteractionModel" which was only set to 1 when it was actually displayed as a Popover. However, Apple doesn't provide direct access to that trait through the traitCollection of popoverPresentationController.
The best way (least smelly) I've found to do this is to use the UIPopoverPresentationControllerDelegate.
• Ensure the presented view controller is set as the UIPopoverPresentationControllerDelegate on the UIPopoverPresentationController being used to manage the presentation. I'm using a Storyboard so set this in prepareForSegue:
segue.destinationViewController.popoverPresentationController.delegate = presentedVC;
• Create a property in the presented view controller to keep track of this state:
#property (nonatomic, assign) BOOL amDisplayedInAPopover;
• And add the following delegate method (or add to your existing delegate method):
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController
{
// This method is only called if we are presented in a popover
self.amDisplayedInAPopover = YES;
}
• And then finally in viewWillAppear: - viewDidLoad: is too early, the delegate prepare method is called between viewDidLoad: and viewWillAppear:
if (self.amDisplayedInAPopover) {
// Hide the offending buttons in whatever manner you do so
self.navigationItem.leftBarButtonItem = nil;
}
Edit: Simpler method!
Just set the delegate (making sure your presentedVC adopts the UIPopoverPresentationControllerDelegate):
segue.destinationViewController.popoverPresentationController.delegate = presentedVC;
And supply the method:
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController
{
// This method is only called if we are presented in a popover
// Hide the offending buttons in whatever manner you do so
self.navigationItem.leftBarButtonItem = nil;
}
I check to see if the popoverPresentationController's arrowDirection is set after the view is laid out. For my purposes, this works well enough and covers the case of popovers on smaller screened devices.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if (popoverPresentationController?.arrowDirection != UIPopoverArrowDirection.Unknown) {
// This view controller is running in a popover
NSLog("I'm running in a Popover")
}
}
How about
if (self.modalPresentationStyle == UIModalPresentationPopover)
It's working for me
The official way to implement this is first remove the Done button from your view controller and second, when adapting to compact embed your view controller in a navigation controller, adding the done button as a navigation item:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.FullScreen
}
func presentationController(controller: UIPresentationController, viewControllerForAdaptivePresentationStyle style: UIModalPresentationStyle) -> UIViewController? {
let navigationController = UINavigationController(rootViewController: controller.presentedViewController)
let btnDone = UIBarButtonItem(title: "Done", style: .Done, target: self, action: "dismiss")
navigationController.topViewController.navigationItem.rightBarButtonItem = btnDone
return navigationController
}
func dismiss() {
self.dismissViewControllerAnimated(true, completion: nil)
}
Full Tutorial
I tested all solutions presented in this post. Sorry, none works correctly in all cases. For example in iPad split view presentation style can change while dragging split view line, so we need specific notification for that.
After few hours of researches i found solution in apple sample (swift):
https://developer.apple.com/library/ios/samplecode/AdaptivePhotos/Introduction/Intro.html#//apple_ref/doc/uid/TP40014636
Here is the same solution in obj-c.
First in prepareForSegue function set the popoverPresentationController delegate. It can be also set in MyViewController "init", but not in "viewDidLoad" (because first willPresentWithAdaptiveStyle is called before viewDidLoad).
MyViewController *controller = [segue destinationViewController];
controller.popoverPresentationController.delegate = (MyViewController *)controller;
Now MyViewController object will receive this notification every time iOS changes presentation style, including first presenting. Here is example implementation which shows/hides "Close" button in navigationController:
- (void)presentationController:(UIPresentationController *)presentationController
willPresentWithAdaptiveStyle:(UIModalPresentationStyle)style
transitionCoordinator:(nullable id<UIViewControllerTransitionCoordinator>)transitionCoordinator {
if (style == UIModalPresentationNone) {
// style set in storyboard not changed (popover), hide close button
self.topViewController.navigationItem.leftBarButtonItem = nil;
} else {
// style changed by iOS (to fullscreen or page sheet), show close button
UIBarButtonItem *closeButton =
[[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:self action:#selector(closeAction)];
self.topViewController.navigationItem.leftBarButtonItem = closeButton;
}
}
- (void)closeAction {
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
The UIPresentationController which manages your view controller is presenting it by setting the modalPresentationStyle to UIModalPresentationPopover.
As per UIViewController reference:
presentingViewController
The view controller that presented this view
controller. (read-only)
modalPresentationStyle
UIModalPresentationPopover: In a horizontally regular environment, a presentation style where the content is displayed in a popover view. The background content is dimmed and taps
outside the popover cause the popover to be dismissed. If you do not
want taps to dismiss the popover, you can assign one or more views to
the passthroughViews property of the associated
UIPopoverPresentationController object, which you can get from the
popoverPresentationController property.
We can therefore determine whether your view controller is inside a popover or presented modally by checking the horizontalSizeClass as follows (I assumed your button is a UIBarButtonItem)
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (self.presentingViewController.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular)
self.navigationItem.leftBarButtonItem = nil; // remove the button
}
The safest place to check this is in viewWillAppear: as otherwise the presentingViewController may be nil.
Solution that works with multitasking
Assign the presenting controller as the popover's delegate
...
controller.popoverPresentationController.delegate = controller;
[self presentViewController:controller animated:YES completion:nil];
Then, in the controller, implement the delegate methods:
- (void)presentationController:(UIPresentationController *)presentationController willPresentWithAdaptiveStyle:(UIModalPresentationStyle)style transitionCoordinator:(id<UIViewControllerTransitionCoordinator>)transitionCoordinator
{
if (style != UIModalPresentationNone)
{
// Exited popover mode
self.navigationItem.leftBarButtonItem = button;
}
}
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController
{
// Entered popover mode
self.navigationItem.leftBarButtonItem = nil;
}
My tricky solution, works perfectly.
In the PopoverViewController's viewDidLoad.
if (self.view.superview!.bounds != UIScreen.main.bounds) {
print("This is a popover!")
}
The idea is simple, A Popover's view size is never equal to the device screen size unless it's not a Popover.

NSTableView not updating on OSX 10.9

I have written a small application, initially under OSX 10.8, that receives MIDI events via MidiEventCallback and puts the data into a NSMutableArray that is a member of a NSTableViewDataSource.
In this video you can see how it worked without any problems on OSX 10.8.
Now I have a new MacBook that runs on OSX 10.9, installed XCode and got all my project files and compiled the application.
Have connected my MIDI controller to my MacBook, started the application and pressing keys on the MIDI controller.
The problem:
The NSTableView is not being updated as it should. If I press my piano keys, no MIDI messages will be displayed and after a couple of seconds I get an exception. Here is a screenshot of what XCode is showing me then:
I stop the application and start it again. Now, when pressing keys, I resize the application window continuously and the NSTableView does update properly, no exception, everything fine.
Same when I press some piano keys a couple of times. Then bring some other application to the front and then my application to the front again. Again, all MIDI messages are displayed properly in my NSTableView.
Any idea what this can be? Is it a OSX 10.9 related issue or could it be with changing to a new MacBook. Am running out of options.
What I have tried is connecting the NSWindow of my application as an IBOutlet to my controller that acts as a NSTableViewDataSource and tried the following calls right after I did [tableView reloadData]
[window update]
[window display]
[window flushWindow]
but nothing helped. Any help highly appreciated.
EDIT
Did some more debugging following the comments and actually was looking at the wrong place.
Here some code for more context:
My controller has a property called MidiEventListener that receives all MIDI events and puts them into eventList.
#interface MidiAidController()
...
#property NSMutableArray *eventList;
#property MidiEventListener* listener;
#end
In the init method of my controller I do the following
_eventList = [[NSMutableArray alloc] init];
MidiEventCallback eventCallback = ^(MidiEvent* midiEvent)
{
[[self eventList] addObject:midiEvent];
[[self tableView] reloadData];
};
...
self.listener = [[MidiEventListener alloc] initWithCallback:eventCallback];
In MidiEventListener, within initWithCallback, the following happens:
result = MIDIInputPortCreate(_midiClient, CFSTR("Input"), midiInputCallback, (__bridge_retained void *)(self), &_inputPort);
Now, let's go over to midiInputCallback:
static void midiInputCallback(const MIDIPacketList* list, void *procRef, void *srcRef)
{
MidiEventListener *midiEventListener = (__bridge MidiEventListener*)procRef;
#autoreleasepool {
const MIDIPacket *packet = &list->packet[0];
for (unsigned int i = 0; i < list->numPackets; i++)
{
MidiEvent* midiEvent = [[MidiEvent alloc] initWithPacket:packet];
midiEventListener.midiEventCallback(midiEvent);
packet = MIDIPacketNext(packet);
}
}
}
That is basically it. The Exception happens at midiEventListener.midiEventCallback(midiEvent);. I always looked at *[tableView reloadData] since that was the line when clicking under Thread 6 - 19__25... (see screenshot above). But when I click on Thread 6 - 20 midiInputCallback then I get this line highlighted.
SOLUTION
Reloading the data has to be done from the main thread:
MidiEventCallback eventCallback = ^(MidiEvent* midiEvent)
{
[[self eventList] addObject:midiEvent];
dispatch_async(dispatch_get_main_queue(), ^(void){[[self tableView] reloadData];});
};
*Thread 6 - 19__25...* and reloadData
call reload only on Main Thread
e.g.
void MyCallback(...) {
dispatch_async(dispatch_get_main_queue(), ^{
...
}
}

Button Crashes App with (id) sender

I am attempting to make a basic game which requires a serious of buttons to control player movement. Keep in mind I am using cocos-2d. My goal is to have the buttons be holdable and move a sprite when held down. The code i am using now looks like this.
CCMenuItemHoldable.h
#interface CCMenuItemSpriteHoldable : CCMenuItemSprite {
bool buttonHeld;
}
#property (readonly, nonatomic) bool buttonHeld;
CCMenuItemHoldable.m
#implementation CCMenuItemSpriteHoldable
#synthesize buttonHeld;
-(void) selected
{
[super selected];
buttonHeld = true;
[self setOpacity:128];
}
-(void) unselected
{
[super unselected];
buttonHeld = false;
[self setOpacity:64];
}
#end
and for the set up of the buttons
rightBtn = [CCMenuItemSpriteHoldable itemFromNormalSprite:[CCSprite spriteWithFile:#"art/hud/right.png"] selectedSprite:[CCSprite spriteWithFile:#"art/hud/right.png"] target:self selector:#selector(rightButtonPressed)];
CCMenu *directionalMenu = [CCMenu menuWithItems:leftBtn, rightBtn, nil];
[directionalMenu alignItemsHorizontallyWithPadding:0];
[directionalMenu setPosition:ccp(110,48)];
[self addChild:directionalMenu];
This all seems to work fine but when i do
-(void)rightButtonPressed:(id) sender
{
if([sender buttonHeld])
targetX = 10;
else{
targetX = 0;
}
}
The crash has been fixed but I am trying to get my sprite to move. In my game tick function I add the value of targetX to the position of the sprite on a timer, still no movement.
Please, always include a crash log when asking questions about crashes.
In your case, I can guess the problem. You are adding this selector:
#selector(rightButtonPressed)
Your method is called
rightButtonPressed:(id)sender
Which would be, as a selector, rightButtonPressed: - note the colon indicating that an argument is passed. Either change the method so it has no argument, or add a colon to the selector when you create the button.
The crash log would be telling you this - it would say "unrecognised selector sent to..." with the name of the receiving class, and the name of the selector.

NSColorPanel blocking mouse up events

I am using a NSColorWell which is set to continuously update. I need to know when the user is done editing the control (mouse up) from the color picker in the color panel.
I installed an event monitor and am successfully receiving mouse down and mouse moved messages, however NSColorPanel appears to block mouse up.
The bottom line is that I want to add the final selected color to my undo stack without all the intermediate colors generated while the user is choosing their selection.
Is there a way of creating a custom NSColorPanel and replacing the shared panel with the thought of overriding its mouseUp and sending a message?
In my research this issue has been broached on a few occasions, however I have not read a successful resolution.
Regards,
- George Lawrence Storm, Keencoyote Invention Services
I discovered that if we observe color keypath of NSColorPanel we get called one extra time on mouse up events. This allows us to ignore action messages from NSColorWell when the left mouse button is down and to get the final color from keypath observer.
In this application delegate example code colorChanged: is a NSColorWell action method.
void* const ColorPanelColorContext = (void*)1001;
#interface AppDelegate()
#property (weak) NSColorWell *updatingColorWell;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel];
[colorPanel addObserver:self forKeyPath:#"color"
options:0 context:ColorPanelColorContext];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if (context == ColorPanelColorContext) {
if (![self isLeftMouseButtonDown]) {
if (self.updatingColorWell) {
NSColorWell *colorWell = self.updatingColorWell;
[colorWell sendAction:[colorWell action] to:[colorWell target]];
}
self.updatingColorWell = nil;
}
}
}
- (IBAction)colorChanged:(id)sender {
if ([self isLeftMouseButtonDown]) {
self.updatingColorWell = sender;
} else {
NSColorWell *colorWell = sender;
[self updateFinalColor:[colorWell color]];
self.updatingColorWell = nil;
}
}
- (void)updateFinalColor:(NSColor*)color {
// Do something with the final color...
}
- (BOOL)isLeftMouseButtonDown {
return ([NSEvent pressedMouseButtons] & 1) == 1;
}
#end
In the Interface Builder, select your color well, and then uncheck the Continuous checkbox in the Attributes Inspector. Additionally, add the following line of code somewhere appropriate like in the applicationDidFinishLaunching: method or awakeFromNib method:
[[NSColorPanel sharedColorPanel] setContinuous:NO];
In other words, both the shared color panel and your color well need to have continuous set to NO in order for this to work properly.
The proper way to do what you want is to use NSUndoManager's -begin/endUndoGrouping. So you'd do something like this
[undoManager beginUndoGrouping];
// ... whatever code you need to show the color picker
// ...then when the color has been chosen
[undoManager endUndoGrouping];
The purpose of undo groups is exactly what you're trying to accomplish - to make all changes into a single undo.

NSButtonCell subclass' button type not working

I subclassed the NSButtonCell class to give a different look to my button. I have the drawing code working fine. It's a rounded button filled with CGGradients to look like the iTunes playback controls. They are not completely the same, but they resemble enough to me.
Now my problem is the button type. I set the button type in the -[PlayerButtonCell initImageCell:] but I'm not getting it working to only draw the pushed in look when the button is pressed. I present you a piece of my code:
//
// PlayerButtonCell.m
// DownTube
//
#import "PlayerButtonCell.h"
#implementation PlayerButtonCell
/* MARK: Init */
- (id)initImageCell:(NSImage *)image {
self = [super initImageCell:image];
if(self != nil) {
[self setImage:image];
[self setButtonType:NSMomentaryPushInButton];
}
return self;
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
NSImage *myImage;
NSBezierPath *myPath;
NSRect myFrame;
NSInteger myState;
myFrame = NSInsetRect(cellFrame , STROKE_WIDTH / 2.0 , STROKE_WIDTH / 2.0);
myImage = [self image];
myState = [self state];
NSLog(#"%d", [self buttonType]);
/* Create bezier path */
{
myPath = [NSBezierPath bezierPathWithOvalInRect:myFrame];
}
/* Fill with background color */
{
/* Fill code here */
}
/* Draw gradient if on */
if(myState == NSOffState) {
/* Code to draw the button when it's not pushed in */
} else {
/* Code to draw the button when it IS pressed */
}
/* Stroke */
{
/* Code to stroke the bezier path's edge. */
}
}
#end
As you can see, I check the pushed in status by the [NSButtonCell state] method. But the button cell acts like a check box, as in NSSwitchButton. This, I do not want. I want it to momentary light.
Hope someone can help me,
ief2
EDIT:
I create a button with this code of it is of any use:
- (NSButton *)makeRefreshButton {
NSButton *myButton;
NSRect myFrame;
NSImage *myIcon;
PlayerButtonCell *myCell;
myIcon = [NSImage imageNamed:kPlayerViewRefreshIconName];
myCell = [[PlayerButtonCell alloc] initImageCell:myIcon];
myFrame.origin = NSMakePoint(CONTENT_PADDING , CONTENT_PADDING);
myFrame.size = CONTROL_PLAYBACK_SIZE;
myButton = [[NSButton alloc] initWithFrame:myFrame];
[myButton setCell:myCell];
[myButton setButtonType:NSMomentaryPushInButton];
[myCell release];
return [myButton autorelease];
}
You're looking at the wrong property. -state is whether a button is checked or not. You want to look at isHighlighted, which tells you whether your button is pressed or not.
Look at a checkbox:
When you click and hold on it, the checkbox is still unchecked, but darkens slightly (702). Then when you release the mouse, it checks (704). Then when you click and hold again, the checked checkbox darkens, but is still checked (705), and only when you release inside, it actually unchecks (701).
So there are two different aspects, highlight and state, which combine to provide one of 4 appearances. Every NSButtonCell advances its state to whatever -nextState returns whenever it is clicked. But some button types show the state, while others don't. Look at any pushbutton (e.g. the "OK" button in a dialog window) and you'll find that behind the scenes its state is changed between on and off, even though it always draws the same way.
It is your drawing code that looks at the -showsStateBy property and only shows the state if that indicates it should.
have you tried [self setButtonType:NSMomentaryLightButton]; in the init?

Resources