Question

I have found the following code and I need help with editing it. I am not really familiar with texture rendering.

First of all, init method takes a rect and magnifies only that area? How can I make it more dynamic and magnify only whatever is underneath the magnifying glass?

Secondly, Is it possible to change the shape to circle rather than rectangle? Or Can I use an image as the frame of the magnifying glass?

Here is the code..

Cheers..

.h file

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Magnify : CCNode {

    BOOL active;
    CGRect rect;
    CGFloat magnifyScale;

    CCNode *renderNode;     
    CCRenderTexture *renderTexture;     

}

- (id)initWithNodeToMagnify:(CCNode *)n rect:(CGRect)rectToMagnify scale:(CGFloat)scale;
- (void)enable;
- (void)disable;

.m file

#import "Magnify.h"

@implementation Magnify

- (id)initWithNodeToMagnify:(CCNode *)n rect:(CGRect)rectToMagnify scale:(CGFloat)scale
{
    if (self = [super init]) {

        self.visible = active = NO;
        renderNode = n;
        rect = rectToMagnify;
        magnifyScale = scale;

        renderTexture = [[CCRenderTexture renderTextureWithWidth:rect.size.width height:rect.size.height] retain];
        [self addChild:renderTexture];

    }
    return self;
}


- (void)enable
{
    self.visible = active = YES;
    [self scheduleUpdate];
}

- (void)disable
{
    self.visible = active = NO;
    [self unscheduleUpdate];
}

- (void)drawAreaToTexture
{
    [renderTexture beginWithClear:0.0 g:0.0 b:0.0 a:1.0];
    // shift the renderNode's position to capture exactly the rect we need
    CGPoint originalPosition = renderNode.position;

    renderNode.position = ccpSub(originalPosition, rect.origin);

    // scale the node as we want
    CGFloat originalScale = renderNode.scale;
    renderNode.scale = magnifyScale;

    [renderNode visit];

    // shift renderNode's position back
    renderNode.position = originalPosition;

    // scale back
    renderNode.scale = originalScale;

    [renderTexture end];
}

- (void)update:(ccTime)dt
{
    [self drawAreaToTexture];
}

- (void)dealloc
{
    [renderTexture release];
    [super dealloc];
}

@end
Was it helpful?

Solution

OK, so, as I mentioned above for something like this, one possible answer is to use the CCLens3D class to get the "effect" of magnifying something in a circular manner.

I found using this to be a little tricky because it doesn't seem to work unless it's a child of the top level node of your 'scene'.

Here is some code I use to create a lens that moves around the screen, and then disappears:

    // Create the lens object first.
    //
    CCLens3D *lens = 
       [CCLens3D actionWithPosition:fromPos 
                             radius:50 
                               grid:ccg(50, 50) 
                           duration:2.0];

    // Set the "size" of the lens effect to suit your needs.
    //
    [lens setLensEffect:1.0];

    // In my case, I then move the lens to a new position.  To apply an action on
    // a lens, you need to give the actions to the actionManager in the
    // CCDirector instance.
    //
    CCMoveTo *move = [CCMoveTo actionWithDuration:2.0 position:toPos];

    // I had another action in this array, but this will do.
    //
    CCSequence *seq = [CCSequence actions:move, nil];

    // Now tell the actionManager to move the lens.  This is odd, but it works.
    //
    [[[CCDirector sharedDirector] actionManager] addAction:seq target:lens paused:NO];

    // Now just for some more weirdness, to actually make the lens appear and operate
    // you run it as an action on the node it would normally be a child of.  In my case
    // 'self' is the CCLayer object that is the root of the current scene.
    //
    // Note that the first action is the lens itself, and the second is a special
    // one that stops the lens (which is a "grid" object).
    //
    [self runAction:[CCSequence actions:lens, [CCStopGrid action], nil]];

I imagine that you should be able to stop the grid by running the CCStopGrid action when you want to. In my case it is a programmed thing. In yours it might be when the user lets go of a button.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top