How do i call a method from C4Workspace by using [object addGesture...]? - c4

What I'm hoping to achieve is to call a method in C4Workspace.m by using:
[shape addGesture:SWIPELEFT name:#"swipeLeft" action:#"leftSwipeMethod"];
I know that this attempts to call a method called "leftSwipeMethod" within the C4Shape class, but in the documentation it also mentions you can call methods from the super class (which is what I think I'm trying to do?).
I checked other questions like this and I'm aware that you're not supposed to do this in normal objective-c... but I wonder if that is the case with C4 as well.
Is there any other way to get the same result or do I have to create a sub class?

Okay, the easiest way to do this is to set up your canvas to listen for the right method that is being called inside the C4Shape (actually, it's from any C4Control so this technique will work for all visual objects.
First, create a shape and add it to the canvas.
Add a gesture to the shape, triggering the appropriate swipe method
Tell the canvas to listen for a notification from the shape
Do something (i.e. change the shape's color) when the canvas hears the notification
The following code sets up the shape:
#implementation C4WorkSpace {
C4Shape *s;
}
-(void)setup {
s = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
s.center = self.canvas.center;
[s addGesture:SWIPELEFT name:#"leftSwipeGesture" action:#"swipedLeft"];
[self.canvas addShape:s];
[self listenFor:#"swipedLeft" fromObject:s andRunMethod:#"randomColor"];
}
-(void)randomColor {
s.fillColor = [UIColor colorWithRed:[C4Math randomInt:100]/100.0f
green:[C4Math randomInt:100]/100.0f
blue:[C4Math randomInt:100]/100.0f
alpha:1.0f];
}
#end
However, this is hard-coded... A nicer, more dynamic way of doing this is to listen from a lot of objects and have a randomColor: method that also accepts the notification so that you can pull out the shape that's doing the notifying.
#implementation C4WorkSpace {
C4Shape *s1, *s2;
}
-(void)setup {
s1 = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
[s1 addGesture:SWIPELEFT name:#"leftSwipeGesture" action:#"swipedLeft"];
s2 = [C4Shape rect:CGRectMake(0, 0, 192, 96)];
[s2 addGesture:SWIPELEFT name:#"left" action:#"swipedLeft"];
s1.center = CGPointMake(self.canvas.center.x, self.canvas.center.y - s1.height * 1.25);
s2.center = CGPointMake(self.canvas.center.x, self.canvas.center.y + s2.height * 0.25);
NSArray *shapes = #[s1,s2];
[self.canvas addObjects:shapes];
[self listenFor:#"swipedLeft" fromObjects:shapes andRunMethod:#"randomColor:"];
}
-(void)randomColor:(NSNotification *)notification {
C4Shape *shape = (C4Shape *)notification.object;
shape.fillColor = [UIColor colorWithRed:[C4Math randomInt:100]/100.0f
green:[C4Math randomInt:100]/100.0f
blue:[C4Math randomInt:100]/100.0f
alpha:1.0f];
}
#end
Things to note in the second example:
First, to accept a notification, the method being run has to have the format:
-(void)randomColor:(NSNotification *)notification {}
Second, to trigger this the method name you use in listenFor has to have a : like so:
#"randomColor:"
Third, you grab the object that just received a swipe gesture by pulling it from the notification it sent:
C4Shape *shape = (C4Shape *)notification.object;

Related

How to find the clicked button in the action method

When I have a dialog and multiple buttons have the same click-callback (action method) I mostly want to know which button has been pressed. How do I do that?
Example:
class ButtonDialog : UIFrame{
void button_pressed(object self){
// how do I get this button
TagGroup pressed_button = ?;
result("The button " + pressed_button + " is pressed.\n");
}
object init(object self){
TagGroup dlg, dlg_items, button1, button2;
dlg = DLGCreateDialog("Press a button", dlg_items);
button1 = DLGCreatePushButton("Button 1", "button_pressed");
button1.DLGIdentifier("button1");
dlg_items.DLGAddElement(button1);
button2 = DLGCreatePushButton("Button 2", "button_pressed");
button2.DLGIdentifier("button2");
dlg_items.DLGAddElement(button2);
self.super.init(dlg);
return self;
}
}
object dialog = alloc(ButtonDialog).init();
dialog.pose();
In my current program I have multiple rows created from a TagGroup. Each row has multiple buttons doing the same thing but for their specific row. Therefore I need to know which button it is to get the row to modify. Also the length of the TagGroup and therefore the row count is not fixed. So I cannot create button1_pressed, button2_pressed, ... functions except with doing some weird stuff with code evaluation on the fly which I want to avoid if possible.
The fact that you cannot pass an argument in the simple call-back of a push-button is a bit of a bummer. The easiest solution (albeit not elegant) is to use simple-one-line-callback methods which are unique but themselves call a generalized method as in the code below.
Of course mile7 stated that the number of buttons isn't fixed at compile time, which is an issue here. But unless the (potential) number of buttons is legion, this approach is still the easiest and cleanest, and as each "hard coded" call-back is only one line with a very systemic change, it should be fairly trivial to use Notepad++ or similar to provide an extensive enough set of such calls. (It doesn't hurt if some of them are actually never used.)
class ButtonDialog : UIFrame{
void button_pressed(object self, string buttonID){
// how do I get this button
TagGroup pressed_button = self.LookUpElement(buttonID);
if ( pressed_button.TagGroupIsValid() )
result("The button " + buttonID + " is pressed.\n");
else
result("The button " + buttonID + " was not found!\n");
}
// Need to be done for each button
void button_pressed_0(object self) { self.button_pressed("button0"); }
void button_pressed_1(object self) { self.button_pressed("button1"); }
object init(object self){
TagGroup dlg, dlg_items
dlg = DLGCreateDialog("Press a button", dlg_items);
number nButtons = 2
for( number n=0; n<nButtons; n++ ){
TagGroup button = DLGCreatePushButton("Button " + n , "button_pressed_" + n);
button.DLGIdentifier("button" + n);
dlg_items.DLGAddElement(button);
}
self.super.init(dlg);
return self;
}
}
object dialog = alloc(ButtonDialog).init();
dialog.pose();
So I kind of found an answer which I can live with, but I'm still hoping for better results.
My current idea is to use DualStateBevelButtons. If a button gets clicked, the state changes and the callback is executed. Then all buttons are checked if they have a changed state. If so, this is the clicked button and the state is reset.
The very very big downside of this solution is, that there are only buttons with images allowed, no text is possible. So this is not really the general solution to work with.
rgbimage button_img = RGBImage("button-image", 4, 16, 16);
button_img = max(0, 255 - iradius / 8 * 255);
class ButtonDialog : UIFrame{
TagGroup buttons;
void button_pressed(object self){
for(number i = 0; i < buttons.TagGroupCountTags(); i++){
TagGroup button;
if(buttons.TagGroupGetIndexedTagAsTagGroup(i, button)){
if(button.DLGGetValue() == 1){
// this button is toggled so it is clicked
string identifier;
button.DLGGetIdentifier(identifier);
result("Button " + i + " (" + identifier + ") is clicked.\n");
// reset button state
button.DLGBevelButtonOn(0);
// do not continue searching, found pressed button already
break;
}
}
}
}
object init(object self){
TagGroup dlg, dlg_items, button1, button2;
dlg = DLGCreateDialog("Press a button", dlg_items);
buttons = NewTagList();
button1 = DLGCreateDualStateBevelButton("button1", button_img, button_img, "button_pressed");
buttons.TagGroupInsertTagAsTagGroup(infinity(), button1);
dlg_items.DLGAddElement(button1);
button2 = DLGCreateDualStateBevelButton("button2", button_img, button_img, "button_pressed");
buttons.TagGroupInsertTagAsTagGroup(infinity(), button2);
dlg_items.DLGAddElement(button2);
self.super.init(dlg);
return self;
}
}
object dialog = alloc(ButtonDialog).init();
dialog.pose();

recursion in typescript gets undefined

I'm using a canvas object inside my component to generate a chart. In order for it animate i'm calling the method recursively. I keep getting an error saying that the method is not defined. Not sure how I need to structure it.
any assistance appreciated.
// Animate function
protected animate(draw_to) {
// Clear off the canvas
this.ctx.clearRect(0, 0, this.width, this.height);
// Start over
this.ctx.beginPath();
// arc(x, y, radius, startAngle, endAngle, anticlockwise)
// Re-draw from the very beginning each time so there isn't tiny line spaces between each section (the browser paint rendering will probably be smoother too)
this.ctx.arc(this.x, this.y, this.radius, this.start, draw_to, false);
// Draw
this.ctx.stroke();
// Increment percent
this.curr++;
// Animate until end
if (this.curr < this.finish + 1) {
// Recursive repeat this function until the end is reached
requestAnimationFrame(function () {
error happens here >>> this.animate(this.circum * this.curr / 100 + this.start);
});
}
}
You need to use an arrow function to keep the same context in the function you give to requestAnimationFrame.
requestAnimationFrame(() => {
error happens here >>> this.animate(this.circum * this.curr / 100 + this.start);
});
Another option is:
requestAnimationFrame(this.animate.bind(this, this.circum * this.curr / 100 + this.start));
You are passing a reference to this.animate which is already bound to the correct this along with the parameters.

View-based NSOutlineView without NIB?

NSOutlineView is a subclass of NSTableView. And currently, NSTableView supports two implementations.
Cell-based.
View-based.
To make OSX 10.8 Finder style side bar (with automatic gray Icon styling), need to use view-based table view with source-list highlight style.
With NIBs, this is typical job. Nothing hard. (see SidebarDemo) But I want to avoid any NIBs or Interface Builder. I want make the side bar purely programmatically.
In this case, I have big problem. AFAIK, there's no way to supply prototype view for specific cell. When I open .xib file, I see <tableColumn> is containing <prototypeCellViews>. And this specifies what view will be used for the column. I can't find how to set this programmatically using public API.
As a workaround, I tried to make cell manually using -[NSTableView makeViewWithIdentifier:owner:] and -[NSTableView viewAtColumn:row:makeIfNecessary:], but none of them returns view instance. I created a NSTableCellView, but it doesn't have image-view and text-field instances. And I also tried to set them, but the fields are marked as assign so the instances deallocated immediately. I tried to keep it by forcing retaining them, but it doesn't work. NSTableView doesn't manage them, so I am sure that table view don't like my implementation.
I believe there's a property to set this prototype-view for a column. But I can't find them. Where can I find the property and make system-default NSOutlineView with source-list style programmatically?
If you follow the example in SidebarDemo, they use a subclass of NSTableCellView for the detail rows. In order to emulate the InterfaceBuilder mojo, you can hook everything together in the constructor. The rest is the same as the demo (see outlineView:viewForTableColumn:item:).
#interface SCTableCellView : NSTableCellView
#end
#implementation SCTableCellView
- (id)initWithFrame:(NSRect)frameRect {
self = [super initWithFrame:frameRect];
[self setAutoresizingMask:NSViewWidthSizable];
NSImageView* iv = [[NSImageView alloc] initWithFrame:NSMakeRect(0, 6, 16, 16)];
NSTextField* tf = [[NSTextField alloc] initWithFrame:NSMakeRect(21, 6, 200, 14)];
NSButton* btn = [[NSButton alloc] initWithFrame:NSMakeRect(0, 3, 16, 16)];
[iv setImageScaling:NSImageScaleProportionallyUpOrDown];
[iv setImageAlignment:NSImageAlignCenter];
[tf setBordered:NO];
[tf setDrawsBackground:NO];
[[btn cell] setControlSize:NSSmallControlSize];
[[btn cell] setBezelStyle:NSInlineBezelStyle];
[[btn cell] setButtonType:NSMomentaryPushInButton];
[[btn cell] setFont:[NSFont boldSystemFontOfSize:10]];
[[btn cell] setAlignment:NSCenterTextAlignment];
[self setImageView:iv];
[self setTextField:tf];
[self addSubview:iv];
[self addSubview:tf];
[self addSubview:btn];
return self;
}
- (NSButton*)button {
return [[self subviews] objectAtIndex:2];
}
- (void)viewWillDraw {
[super viewWillDraw];
NSButton* btn = [self button];
...
Here's #jeberle's code re-written in Swift 4 (five years later!):
class ProgrammaticTableCellView: NSTableCellView {
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.autoresizingMask = .width
let iv: NSImageView = NSImageView(frame: NSMakeRect(0, 6, 16, 16))
let tf: NSTextField = NSTextField(frame: NSMakeRect(21, 6, 200, 14))
let btn: NSButton = NSButton(frame: NSMakeRect(0, 3, 16, 16))
iv.imageScaling = .scaleProportionallyUpOrDown
iv.imageAlignment = .alignCenter
tf.isBordered = false
tf.drawsBackground = false
btn.cell?.controlSize = .small
// btn.bezelStyle = .inline // Deprecated?
btn.cell?.isBezeled = true // Closest property I can find.
// btn.cell?.setButtonType(.momentaryPushIn) // Deprecated?
btn.setButtonType(.momentaryPushIn)
btn.cell?.font = NSFont.boldSystemFont(ofSize: 10)
btn.cell?.alignment = .center
self.imageView = iv
self.textField = tf
self.addSubview(iv)
self.addSubview(tf)
self.addSubview(btn)
}
required init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
var button: NSButton {
get {
return self.subviews[2] as! NSButton
}
}
}
Edit: I found a link (that will inevitably rot away – it was last revised in 2011) to Apple's SidebarDemo that #jeberle based his code on.
In addition to #jeberle 's answer, I need to note something more.
The key to keep the text-field and image-view is adding them as subviews of the NSTableCellView.
Set NSTableView.rowSizeStyle to a proper value (non-Custom which is default value) to make the table-view layout them automatically. Otherwise, you have to layout them completely yourself.
Do not touch frame and autoresizing stuffs if you want to use predefined NSTableViewRowSizeStyle value. Otherwise, the layout might be broken.
You can adjust row-height by providing private func outlineView(outlineView: NSOutlineView, heightOfRowByItem item: AnyObject) -> CGFloat delegate method. Setting NSTableView.rowHeight is not a good idea because it needs NSTableView.rowSizeStyle set to Custom which will turn off cell text/image layout management provided by default.
You can reuse row/cell views by settings NSView.identifier property. (example)

What is the best approach to add existing sprites as child sprite's

I have the following:
A background sprite called "_background"
3 x sprites "C4", D5" and "Hj"
The three sprites are added separately onto the background. I then, at a double click, want to make it possible to drag them all at the same time to another location on the screen while they stay in the same order and position.
The only way i have got it, nearly, to work is with this code:
- (void)tap2TouchesGesture:(UITapGestureRecognizer *)sender {
SKNode *removeNode = [_background childNodeWithName:#"C4"];
CGPoint aPos = removeNode.position;
[removeNode removeFromParent];
SKSpriteNode *topNode = [SKSpriteNode spriteNodeWithImageNamed:#"C4"];
topNode.position = aPos;
topNode.zPosition = 100;
topNode.name = #"C4";
[_background addChild:topNode];
removeNode = [_background childNodeWithName:#"D5"];
[removeNode removeFromParent];
SKSpriteNode *vv = [SKSpriteNode spriteNodeWithImageNamed:#"D5"];
vv.position = CGPointMake(-10, -10);
vv.zPosition = -10;
vv.userInteractionEnabled = NO; // just testing
vv.name = #"D5";
[topNode addChild:vv];
removeNode = [_background childNodeWithName:#"Hj"];
[removeNode removeFromParent];
vv = [SKSpriteNode spriteNodeWithImageNamed:#"Hj"];
vv.position = CGPointMake(-20, -20);
vv.zPosition = -50;
vv.userInteractionEnabled = NO; // just testing
vv.name = #"Hj";
[topNode addChild:vv];
}
After processing the above code i can move the pack of sprites but the current problem is that the parent, C4, do not seem to be on top. The only way of selecting C4 is to click on the part that is outside of any of the other sprites, not included the _background.
I would guess that this is not the best approach to performing this so i would like to ask for some help of how to do this correctly. Also, so i can select the C4 by clicking on the whole sprite.
You mean you want to be able to drag all three sprites simultaneously and synchronously, their position relative to each other always remaining the same?
What I always say in such cases. If you want multiple sprites (or any node) to do something together, then: add a SKNode, put all three sprites in it, drag the node. Bam, super simple!
I now got it to work, i had done a very simple mistake as i tried to move the selected node and not the topNode (container). Unbelievable, i should have seen that, especially after Steffens suggestion :-(
Thanks Steffen & Ben Stahl # Apple SpriteKit forum:-)
However, here is the code that i use for this example to work:
- (void)tap2TouchesGesture:(UITapGestureRecognizer *)sender {
_topNode = [SKNode node];
[_background addChild:_topNode];
SKSpriteNode *vv = [SKSpriteNode spriteNodeWithImageNamed:#"C4"];
[_topNode addChild:vv];
vv = [SKSpriteNode spriteNodeWithImageNamed:#"D5"];
[_topNode addChild:vv];
vv = [SKSpriteNode spriteNodeWithImageNamed:#"Hj"];
[_topNode addChild:vv];
_isThePackSelectedForAction = YES; // sprites are selected
}
- (void)handlePan:(UIPanGestureRecognizer *)sender {
_currentTouchLocationGlobal = [sender locationInView:sender.view];
_currentTouchLocationGlobal = [self convertPointFromView:_currentTouchLocationGlobal];
if (_isThePackSelectedForAction) {
_topNode.position = CGPointMake(_currentTouchLocationGlobal.x, _currentTouchLocationGlobal.y);
} else {
_currentNode.position = CGPointMake(_currentTouchLocationGlobal.x, _currentTouchLocationGlobal.y);
}
}

Redraw NSTableView with new data from file when NSViewController re-loaded?

I have a Mac OS X Document based app that has multiple NSViewControllers that I switch between and each view displays data from plist files in NSTableViews based on the user selections in the previous NSViewController's NSTableView. The problem I have is that I can't figure out what function can be called, every time a NSViewController gets loaded, to read the correct data from a file to display in the NSTableView. For UIViewControllers I used the function family of viewDidLoad, viewWillAppear, but I haven't been able to find the corresponding functions for NSViewController.
Currently I am using awakeFromNib, which works fine, but only the first time the NSViewController gets loaded. I've tried loadView, but that collapses the NSView. I assume that I need to do more setup to use loadView.
I'm using the View Swapping code from Hillegass's book Cocoa Programming for MAC OS X which switches ViewControllers with the following code:
- (void)displayViewController:(ManagingViewController *)vc
curBox: (NSBox *)windowBox
{
// End editing
NSWindow *w = [windowBox window];
BOOL ended = [w makeFirstResponder:w];
if (!ended) {
NSBeep();
return;
}
NSView *v = [vc view];
NSSize currentSize = [[windowBox contentView] frame].size;
NSSize newSize = [v frame].size;
float deltaWidth = newSize.width - currentSize.width;
float deltaHeight = newSize.height - currentSize.height;
NSRect windowFrame = [w frame];
windowFrame.size.height += deltaHeight;
windowFrame.origin.y -= deltaHeight;
windowFrame.size.width += deltaWidth;
[windowBox setContentView:nil];
[w setFrame:windowFrame
display:YES
animate:YES];
[windowBox setContentView:v];
// Put the view controller in the responder chain
[v setNextResponder:vc];
[vc setNextResponder:windowBox];
}
and puts the NSView Controller in the responder chain.
Is there some function I can call to setup the view every time I swap NSViewControllers? Can I check that a NSViewController has become the firstResponder?
This post provided the answer.
I added the following code:
- (void)viewWillLoad {
}
- (void)viewDidLoad {
}
- (void)loadView {
[self viewWillLoad];
[super loadView];
[self viewDidLoad];
}
and at the beginning of displayViewController I added
[vc loadView]

Resources