Using #selector in RubyMotion - objective-c

How do I translate the following method call from ObjectiveC to RubyMotion syntax:
[self.faceView addGestureRecognizer:[
[UIPinchGestureRecognizer alloc] initWithTarget:self.faceView
action:#selector(pinch:)]];
I got this far:
self.faceView.addGestureRecognizer(
UIPinchGestureRecognizer.alloc.initWithTarget(
self.faceView, action:???))
I understand the #selector(pinch:) indicates a delegation to the receiver object pinch method, but how would I do this in RubyMotion? Maybe using a block?

You should be able to just use a string to specify the selector:
self.faceView.addGestureRecognizer(
UIPinchGestureRecognizer.alloc.initWithTarget(
self.faceView, action:'pinch'))

#gesture = UIPinchGestureRecognizer.alloc.initWithTarget(self.faceView,action:'pinch:')
self.faceView.addGestureRecognizer(#gesture)
def pinch(foo)
end
If you don't want the method handler to take an argument, use action:'pinch' instead. It will then look for a method like this:
def pinch
end
Using an instance var (#gesture = ...) is a good idea here because sometimes gesture recognizers and the GC don't play well together if you don't make the gesture var an instance var of a UIViewController. (In my experience)

Related

Godot - set_fixed_process function

I am trying to understand Godot Game engine, I am following a tutorial, and I wrote a breakout game.
Here is the padding code:
extends KinematicBody2D
func _ready():
set_fixed_process(true)
func _fixed_process(delta):
var y = get_pos().y
var x = get_viewport().get_mouse_pos().x
set_pos(Vector2(x,y))
And I don't understand why I can't use the _fixed_process function without the set_fixed_process function, and what is it's use. What does the function do?
The _fixed_process() function is a callback that is enabled by calling set_fixed_process(true). This is just like how _process() isn't called unless you enabled the callback with set_process().
Both functions are essentially telling Godot that you want to receive those callbacks in your script when the nodes are processed. And then you're just overriding the _fixed_process() function to capture it in your script.

Using with_context in super call turns into infinite call in odoo

In odoo context dictionary is frozen so no one can update it,
for that one solution is also available is calling method using with_context.
ctx = self.env.context.copy()
ctx.update({'additional_parameter' : value})
self.with_context(ctx).methodname()
but when we use the same pattern to call super method then result turns into the infinite call.
ctx = self.env.context.copy()
ctx.update({'additional_parameter' : value})
super(product_product, self).with_context(ctx).create(vals)
Any help will be highly appreciated.
I think you should try something like this at respective line:
super(product_product, self.with_context(ctx)).create(vals)

Trying to switch a direct integer value with a variable in swift

I'm trying to switch out a direct integer with a variable in swift, but for some reason I'm getting this error and I have no idea. The end goal is to get my currentValue (line 76) to replace the 100's on line 41 - could anyone let me know how I could accomplish this without the error? New to swift and having a hard time (background in objective-c, figured something this simple would not stop me in my tracks!)
Full .swift file here: http://pastebin.com/K6UHkNEv
EDIT:
// these values change the number of squares
let _gameView = CGOLView(gridWidth:100, gridHeight:100)
#IBOutlet weak var tileSizeSlider: UISlider!
#IBAction func sliderValueChanged(sender: UISlider) {
var currentValue = Int(sender.value)
print("\(currentValue)")
}
should work as:
// these values change the number of squares
let _gameView = CGOLView(gridWidth:currentValue, gridHeight:currentValue)
#IBOutlet weak var tileSizeSlider: UISlider!
#IBAction func sliderValueChanged(sender: UISlider) {
var currentValue = Int(sender.value)
print("\(currentValue)")
}
instead I get this error:
Use of unresolved identifier 'currentValue'
and if I try to create custom int's and input them:
var gridWidthValue = 50
var gridHeightValue = 50
like this:
let _gameView = CGOLView(gridWidth:gridWidthValue, gridHeight:gridHeightValue)
I get:
'ViewController.Type' does not have a member named 'gridHeightValue'
Any help would be appreciated - thanks stackoverflow community!
David.
currentValue is a local variable to sliderValueChanged.
Instead you should instantiate _gameView in init. Note however, you still won't be able to use currentValue.
If this is a one off sort of thing, you can always make _gameView an optional and then create it when you have adjusted the slider. This is admittedly a little clumsy.
I am not familiar with Conway's Game of Life, but looking at the code, it seems CGOLView's init does some adjustment based on the grid width and height. The reason I am mentioning this is that you could always change the view's frame size, however, you'd then also need to make some other mods to the tileViews for it to look proper.
As to why gridWidthValue/gridHeightValue is not working. Those are properties defined in an instance. Hence you would need to do somethign like self.gridWithValue to reference it. However, you cannot do that when defining the property such as
let _gameView = CGOLView(gridWidth:gridWidthValue, gridHeight:gridHeightValue)
This is also why instantiating _gameView in init is the way to go.
Your problem is that you cannot access the variable currentValue because it is inside of a function. You have to declare that value outside of the function to be able to use it outside of the function.

is there way to check if performSelector:withObject:afterDelay: has been registered?

I whould like to know if there is a way to determine if performSelector:withObject:afterDelay: for the given object has been called (registered to be called). (I could use cancelPreviousPerformRequestsWithTarget:selector:object: and re-call performSelector:withObject:afterDelay:, ok but I'm interested to know if there is the alternative).
Thanks
The best thing to do would be to make sure that the selector being called can be called multiple times safely.
For example, use a flag in the target object to track if the method has already been invoked e.g.
-targetSelector: (id) param
{
if (!hasBeenRun) // hasBeenRun is a boolean intance variable
{
hasBeenRun = true;
// other stuff
}
}

How can my app detect a change to another app's window?

In Cocoa on the Mac, I'd like to detect when a window belonging to another app is moved, resized, or repainted. How can I do this?
You would need to use the Accessibility APIs, which are plain-C, located inside the ApplicationServices framework. For instance:
First you create an application object:
AXUIElementRef app = AXUIElementCreateApplication( targetApplicationProcessID );
Then you get the window from this. You can request the window list and enumerate, or you can get the frontmost window (look in AXAttributeConstants.h for all the attribute names you'd use).
AXUIElementRef frontWindow = NULL;
AXError err = AXUIElementCopyAttributeValue( app, kAXMainWindowAttribute, &frontWindow );
if ( err != kAXErrorSuccess )
// it failed -- maybe no main window (yet)
Now you can request notification via a C callback function when a property of this window changes. This is a four-step process:
First you need a callback function to receive the notifications:
void MyAXObserverCallback( AXObserverRef observer, AXUIElementRef element,
CFStringRef notificationName, void * contextData )
{
// handle the notification appropriately
// when using ObjC, your contextData might be an object, therefore you can do:
SomeObject * obj = (SomeObject *) contextData;
// now do something with obj
}
Next you need an AXObserverRef, which manages the callback routine. This requires the same process ID you used to create the 'app' element above:
AXObserverRef observer = NULL;
AXError err = AXObserverCreate( applicationProcessID, MyObserverCallback, &observer );
if ( err != kAXErrorSuccess )
// handle the error
Having got your observer, the next step is to request notification of certain things. See AXNotificationConstants.h for the full list, but for window changes you'll probably only need these two:
AXObserverAddNotification( observer, frontWindow, kAXMovedNotification, self );
AXObserverAddNotification( observer, frontWindow, kAXResizedNotification, self );
Note that the last parameter there is passing an assumed 'self' object as the contextData. This is not retained, so it's important to call AXObserverRemoveNotification when this object goes away.
Having got your observer and added notification requests, you now want to attach the observer to your runloop so you can be sent these notifications in an asynchronous manner (or indeed at all):
CFRunLoopAddSource( [[NSRunLoop currentRunLoop] getCFRunLoop],
AXObserverGetRunLoopSource(observer),
kCFRunLoopDefaultMode );
AXUIElementRefs are CoreFoundation-style objects, so you need to use CFRelease() to dispose of them cleanly. For cleanliness here, for example, you would use CFRelease(app) once you've obtained the frontWindow element, since you'll no longer need the app.
A note about Garbage-Collection: To keep an AXUIElementRef as a member variable, declare it like so:
__strong AXUIElementRef frontWindow;
This instructs the garbage collector to keep track of this reference to it. When assigning it, for compatibility with GC and non-GC, use this:
frontWindow = (AXUIElementRef) CFMakeCollectable( CFRetain(theElement) );
Further research turned up "Quartz Display Services"
The interesting function for my needs is CGRegisterScreenRefreshCallback.