is it possible to use a CALayer for a Xojo canvas ? I am not familiar with CALayer so I just
tried a simple thing but Xojo crashes.
const CocoaLib = “cocoa”
declare sub setLayer lib CocoaLib selector “setLayer:” (ob_id As Ptr, p as ptr)
declare sub setNeedsDisplay lib CocoaLib selector “setNeedsDisplay” (ob_id As Ptr)
declare function layer lib CocoaLib selector “layer” (ob_id As Ptr) As Ptr
declare function NSClassFromString lib CocoaLib (cls as CFStringRef) as ptr
declare sub setWantsLayer lib CocoaLib selector “setWantsLayer:” (ob_id As Ptr, b as Boolean)
declare sub setBackgroundColor lib CocoaLib selector “setBackgroundColor:” (obj_id as Ptr, aColor as Ptr)
dim c as Color = Color.black
dim col() as double = Array(c.Red/255,c.Green/255,c.Blue/255,(255-c.Alpha)/255)
dim cgcm As new xojo.Core.MutableMemoryBlock((col.Ubound+1)4)
for i as integer=0 to col.Ubound
cgcm.SingleValue(i4) = col(i)
next
dim viewRef as ptr = ptr(me.Handle)
dim layerRef as ptr =layer(NSClassFromString(“CALayer”))
self.theLayer = layerRef
setLayer(viewRef ,layerRef)
setWantsLayer(viewRef, true)
setBackgroundColor(layerRef, cgcm.Data)
setNeedsDisplay(layerRef)
Thanks Will. I haven’t seen the conversion from NSColor to CGColor. It works now!.
const CocoaLib = “cocoa”
declare sub setLayer lib CocoaLib selector “setLayer:” (ob_id As Ptr, p as ptr)
declare sub setNeedsDisplay lib CocoaLib selector “setNeedsDisplay” (ob_id As Ptr)
declare function layer lib CocoaLib selector “layer” (ob_id As Ptr) As Ptr
declare function NSClassFromString lib CocoaLib (cls as CFStringRef) as ptr
declare function blueColor lib CocoaLib selector “blueColor” (obj as ptr) as ptr
declare function CGColor lib CocoaLib selector “CGColor” (obj as ptr) as ptr
declare sub setWantsLayer lib CocoaLib selector “setWantsLayer:” (ob_id As Ptr, b as Boolean)
declare sub setBackgroundColor lib CocoaLib selector “setBackgroundColor:” (obj_id as Ptr, aColor as Ptr)
dim c as ptr = CGColor( blueColor( NSClassFromString(“NSColor”)))
dim viewRef as ptr = ptr(me.Handle)
dim layerRef as ptr =layer(NSClassFromString(“CALayer”))
self.theLayer = layerRef
setLayer(viewRef ,layerRef)
setWantsLayer(viewRef, true)
setBackgroundColor(layerRef, c)
setNeedsDisplay(layerRef)
After spending some time, I could figure how CALayers work. The are possibilities are huge.
However, I am wondering how to reset layers and have the original canvas paint() event work again.
code project enclosed.
[quote=241029:@Rob Egal]However, I am wondering how to reset layers and have the original canvas paint() event work again.
[/quote]
A hint without knowing its value: Add the debugdescription API method to your NSView/Canvas and investigate its values when you change layer-related properties. Maybe wantsUpdateLayer needs to be overridden with false when you try to reset the view to CGContext based drawing again. As its not calling the paint event, it looks like the drawRect: still forwards to layer based drawing.
From a SWIFT point of view… I have found that you need to remove the layers from the SuperView… or REPLACE the layer based on its index (which can change as you add/delete layers). I found this when adding a Gradient layer, and trying to change the Gradient later on in the program. The solution was to NAME the Gradient layer, and when attempting to change it, I had to determine if a layer existing with that name, remove it, and insert the new Gradient.
I too thought about “DebugDescription”, but unless XOJO sets that value for you, you might find it to be NIL
I was looking at “Description” which is NIL
but DebugDescription is not a simple string…it seems to be a “description” of all the attributes of the layer, so I’d say that is NOT a good thing to track layer identification with
Using project CALayer1, change the code in PushButton2 to this
declare sub setWantsLayer lib "Cocoa" selector "setWantsLayer:" (ob_id As Ptr, b as Boolean)
setWantsLayer(Ptr(Canvas1.Handle), false)
For me Canvas1 turns solid black, the Paint event is drawing.
[quote=241122:@Will Shank]Using project CALayer1, change the code in PushButton2 to this
declare sub setWantsLayer lib "Cocoa" selector "setWantsLayer:" (ob_id As Ptr, b as Boolean)
setWantsLayer(Ptr(Canvas1.Handle), false)
For me Canvas1 turns solid black, the Paint event is drawing.[/quote]
Yepp it works. I’ve found the trouble maker…
// action - in the Button
dim viewRef as ptr = ptr(me.Handle)
setWantsLayer(viewRef, false)
I should subclass the canvas and put the stuff in there.