Skip to content

Commit

Permalink
SCUserView fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Wilson committed Feb 23, 2010
1 parent cee6637 commit 9f6694e
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 73 deletions.
6 changes: 3 additions & 3 deletions common/Headers/app/SCView.h
Expand Up @@ -810,10 +810,10 @@ class SCUserView : public SCView
bool mClearOnRefresh;
bool mRelativeOrigin;
SCPoint mRealtiveMousePoint;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
CGLayerRef mCGLayer;
#endif

NSImage* mNSImageForLayering; // offscreen image object
NSGraphicsContext *mImageContext;
NSBitmapImageRep* mImageRep;
void mouseAction(PyrSymbol *method, SCPoint where, int modifiers);
};
SCView* NewSCUserView(SCContainerView *inParent, PyrObject* inObj, SCRect inBounds);
Expand Down
4 changes: 2 additions & 2 deletions common/Source/app/SCImage.M
Expand Up @@ -171,7 +171,7 @@ static inline int MultiplyAlpha(const int a, const int r)

#pragma mark -- Graphics Context Utilities
// returns a new rgba bitmap context. you owns the returned context. should be released after
static NSBitmapImageRep* CreateBitmapContextRGBA(const int width, const int height)
NSBitmapImageRep* CreateBitmapContextRGBA(const int width, const int height)
{
return
[[NSBitmapImageRep alloc]
Expand All @@ -192,7 +192,7 @@ static NSBitmapImageRep* CreateBitmapContextRGBA(const int width, const int heig

// creates and return an autoreleased NSBitmapImageRep built from an CIImage or an NSImage
// if image is nil then it will return a zero filled pixel NSBitmapImageRep
static NSBitmapImageRep* BitmapContextRGBAWithImage(const int width, const int height, id image)
NSBitmapImageRep* BitmapContextRGBAWithImage(const int width, const int height, id image)
{
NSGraphicsContext *nsPrevCtx = NULL;
NSBitmapImageRep *bitmap = CreateBitmapContextRGBA(width, height);
Expand Down
170 changes: 102 additions & 68 deletions common/Source/app/SCView.M
Expand Up @@ -4121,92 +4121,126 @@ SCUserView::SCUserView(SCContainerView *inParent, PyrObject* inObj, SCRect inBou
mClearOnRefresh = true;
mRelativeOrigin = false;
mDrawingEnabled = true;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
mCGLayer = NULL;
#endif
mNSImageForLayering = nil;
mImageContext = nil;
}

SCUserView::~SCUserView()
{
if(mCGLayer) CGLayerRelease(mCGLayer);
if( mNSImageForLayering )
[ mNSImageForLayering release ];
if( mImageContext )
[ mImageContext release ];
}


extern NSBitmapImageRep* CreateBitmapContextRGBA(const int width, const int height);
extern NSBitmapImageRep* BitmapContextRGBAWithImage(const int width, const int height, id image);

void SCUserView::draw(SCRect inDamage)
{

SCRect bounds = getDrawBounds();
NSGraphicsContext *lGCtx = NULL;
CGContextRef mainGC = NULL;
SCRect bounds = getDrawBounds();
CGRect cgBounds = SCtoCGRect( bounds );

SCView::draw(bounds); // draw background
if(!mDrawingEnabled) return;
if(bounds.width <= 0 || bounds.height <= 0) return;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
CGContextRef lCGContext;
CGContextRef cgc;
NSGraphicsContext* lGCtx;
if(mRelativeOrigin){
CGSize size = CGSizeMake(bounds.width, bounds.height);
// post("bounds.width: %f, bounds.height: %f\n",bounds.width, bounds.height);
cgc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
if(!mCGLayer)

lGCtx = [ NSGraphicsContext currentContext ];

void *focusStack;

if( !mDrawingEnabled )
return;

if( bounds.width <= 0 || bounds.height <= 0)
return;

if( !mClearOnRefresh )
{
if(
!mNSImageForLayering ||
[ mNSImageForLayering size ].width != bounds.width ||
[ mNSImageForLayering size ].height != bounds.height
)
{
mCGLayer = CGLayerCreateWithContext (cgc, size, NULL);
if(!mCGLayer)
return; // creation failed
CGLayerRetain(mCGLayer);
} else {
//check whether size has changed
CGSize layerSize = CGLayerGetSize(mCGLayer);
if(layerSize.width != size.width || layerSize.height != size.height){
CGLayerRelease(mCGLayer);
mCGLayer = CGLayerCreateWithContext (cgc, size, NULL);
// CGLayerRetain(mCGLayer); // not needed
if( mNSImageForLayering )
[ mNSImageForLayering release ];
if( mImageContext )
[ mImageContext release ];

mImageRep = CreateBitmapContextRGBA((int)bounds.width, (int)bounds.height);

if( ! mImageRep )
{
NSLog(@"Failed creating valid NSBitmapImageRep for SCUserView");
return;
}

[mImageRep autorelease];
mNSImageForLayering = [[NSImage alloc]initWithSize: *(NSSize*)&cgBounds.size];
if( !mNSImageForLayering )
{
NSLog(@"Failed creating valid NSBitmapImageRep for SCUserView");
return;
}

[ mNSImageForLayering addRepresentation: mImageRep];
mImageContext = [NSGraphicsContext graphicsContextWithBitmapImageRep: mImageRep ];

if( !mImageContext )
return;
[ mImageContext retain ];
}
CGRect cgrect = CGRectMake(0, 0, bounds.width, bounds.height);
lGCtx = [NSGraphicsContext currentContext];
lCGContext = CGLayerGetContext(mCGLayer);

NSGraphicsContext *gclayer = [NSGraphicsContext graphicsContextWithGraphicsPort:lCGContext flipped:YES];
[NSGraphicsContext setCurrentContext: gclayer];
if(mClearOnRefresh){
CGContextClearRect(lCGContext, cgrect);
CGContextSetRGBFillColor (lCGContext, 1, 1, 1, 0);
CGContextFillRect (lCGContext, cgrect);
}
CGContextBeginTransparencyLayer(lCGContext, NULL);

[ NSGraphicsContext saveGraphicsState ];

CGContextRef cgc = (CGContextRef)[ mImageContext graphicsPort ];

NSGraphicsContext *gclayer = [ NSGraphicsContext graphicsContextWithGraphicsPort:cgc flipped:YES ];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:mImageRep]];
}
#endif
else
{
mainGC = (CGContextRef)[ lGCtx graphicsPort ];
CGContextSaveGState( mainGC );
CGContextClipToRect( mainGC, cgBounds );

CGContextTranslateCTM(mainGC, bounds.x, bounds.y);

}

sendMessage(s_draw, 0, 0, 0);
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1040
if(mRelativeOrigin){
CGContextEndTransparencyLayer(lCGContext);
CGContextDrawLayerInRect(cgc, SCtoCGRect(bounds), mCGLayer);
[NSGraphicsContext setCurrentContext: lGCtx];

if( !mClearOnRefresh )
{
//[mNSImageForLayering unlockFocus];
//CGContextEndTransparencyLayer( mNSImageForLayering );
[ NSGraphicsContext setCurrentContext: lGCtx ];
[ NSGraphicsContext restoreGraphicsState ];
//[ NSGraphicsContext setCurrentContext: lGCtx ];

[ mNSImageForLayering drawAtPoint:*(NSPoint*)&cgBounds.origin fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.f ];
}
else
{
CGContextRestoreGState( mainGC );
}
#endif
}

void SCUserView::clearDrawing()
{
if(!mCGLayer) return;
SCRect bounds = getDrawBounds();
CGContextRef lCGContext;
CGContextRef cgc;
NSGraphicsContext* lGCtx;
CGRect cgrect = CGRectMake(0,0, bounds.width, bounds.height);
cgc = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
lGCtx = [NSGraphicsContext currentContext];
CGContextSaveGState(cgc);

lCGContext = CGLayerGetContext(mCGLayer);
NSGraphicsContext *gclayer = [NSGraphicsContext graphicsContextWithGraphicsPort:lCGContext flipped:YES];
// [NSGraphicsContext setCurrentContext: gclayer];

CGContextClearRect(lCGContext, cgrect);
CGContextSetRGBFillColor (lCGContext, 1, 1, 1, 0);
CGContextFillRect (lCGContext, cgrect);
CGContextDrawLayerInRect(cgc, SCtoCGRect(bounds), mCGLayer);
CGContextRestoreGState(cgc);
// [NSGraphicsContext setCurrentContext: lGCtx];
if( !mNSImageForLayering )
return;

//SCRect bounds = getDrawBounds();
//CGContextClearRect ( mNSImageForLayering , CGRectMake( 0, 0, bounds.width, bounds.height));
[ mNSImageForLayering lockFocus ];
CGContextRef cgc = (CGContextRef)[[NSGraphicsContext currentContext]graphicsPort];
if(mClearOnRefresh)
CGContextClearRect( cgc, CGRectMake( 0.f, 0.f, [mNSImageForLayering size].width, [mNSImageForLayering size].height ) );
[ mNSImageForLayering unlockFocus ];

}

void SCUserView::mouseAction(PyrSymbol *method, SCPoint where, int modifiers)
Expand Down Expand Up @@ -4430,7 +4464,7 @@ SCAnimationView::SCAnimationView(SCContainerView *inParent, PyrObject* inObj, SC
mRelativeOrigin = false;
mDrawingEnabled = true;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
mCGLayer = NULL;
mNSImageForLayering = NULL;
#endif
mShowInfo = false;
for(int i = 0; i<kLastTimes; i++) {
Expand Down

0 comments on commit 9f6694e

Please sign in to comment.