Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Trusted Contributor
cjonesy
Posts: 160
Registered: ‎09-13-2012
My Device: 9900
My Carrier: vodafone

Re: Where to begin to pan/zoom with native screen

I'm now very close to solving this (now have max x and y), my confusion is what is too distinguish between a swipe in and out and what should I check to see if the x and y are out of order (meaning that the user has zoomed back to max.

 

I now have it where if i set the pos to 0 after every pinch it will stay in position but will only ever zoom into a certain area.

 

If I don't set position to 0 every time then every time I zoom out the image is slowly being pushed top left out of view. I think this is where nudging would be helpful but what should I be checking to see if nugding needs to take place.

 

 

 

#define MIN_VIEWPORT_SIZEX 128
#define MAX_VIEWPORT_SIZEX 1024

#define MIN_VIEWPORT_SIZEY 128
#define MAX_VIEWPORT_SIZEY 600

case GESTURE_PINCH: {
		gesture_pinch_t* pinch = (gesture_pinch_t*)gesture;
		fprintf(stderr,"Pinch %d, %d", (pinch->last_distance.x - pinch->distance.x), (pinch->last_distance.y - pinch->distance.y));

		int dist_x = pinch->distance.x;
		int dist_y = pinch->distance.y;
		int last_dist_x = pinch->last_distance.x;
		int last_dist_y = pinch->last_distance.y;

		int reldist = sqrt((dist_x)*(dist_x) + (dist_y)*(dist_y));
		int last_reldist = sqrt((last_dist_x)*(last_dist_x) + (last_dist_y)*(last_dist_y));

		if (reldist && last_reldist) {
			viewport_size[0] += (last_reldist - reldist) >> 1;
			viewport_size[1] += (last_reldist - reldist) >> 1;

			/* Size restrictions */
			if (viewport_size[0] < MIN_VIEWPORT_SIZEX) {
				viewport_size[0] = MIN_VIEWPORT_SIZEX;
			} else if (viewport_size[0] > MAX_VIEWPORT_SIZEX) {
				viewport_size[0] = MAX_VIEWPORT_SIZEX;
			}
			if (viewport_size[1] < MIN_VIEWPORT_SIZEY) {
				viewport_size[1] = MIN_VIEWPORT_SIZEY;
			} else if (viewport_size[1] > MAX_VIEWPORT_SIZEY) {
				viewport_size[1] = MAX_VIEWPORT_SIZEY;
			}

			/* Zoom into center of image */
			if (viewport_size[0] > MIN_VIEWPORT_SIZEX && viewport_size[1] > MIN_VIEWPORT_SIZEY &&
					viewport_size[0] < MAX_VIEWPORT_SIZEX && viewport_size[1] < MAX_VIEWPORT_SIZEY) {
				viewport_pos[0] -= (last_reldist - reldist) >> 2;
				viewport_pos[1] -= (last_reldist - reldist) >> 2;
			}

			//viewport_pos[0] = 0;
			//viewport_pos[1] = 0;



		}

 

Please use plain text.
Trusted Contributor
cjonesy
Posts: 160
Registered: ‎09-13-2012
My Device: 9900
My Carrier: vodafone

Re: Where to begin to pan/zoom with native screen

nvm, solved it by just setting x pos to 0 if (viewport_size[0] > MAX_VIEWPORT_SIZEX) is true, thanks for your help Sean.

Please use plain text.
Trusted Contributor
cjonesy
Posts: 160
Registered: ‎09-13-2012
My Device: 9900
My Carrier: vodafone

Re: Where to begin to pan/zoom with native screen

I'm still struggling to accomplish my aim - I'm currently trying to tweak the gestures sample to zoom into the area of the screen which the user has actually zoomed into rather than just the centre of the screen which it does in the sample.

 

If I just set the postion of the screen to 15,15 after the gesture pinch callback then this shows what I need is possible - It zooms in to an area of the screen other than the centre - top left. 

 

However I have just spent hours trying to convert this knowledge into actually making my gesture pinch callback zoom into the specific area. 

 

I have a pinch gesture object but all its variables  seems to be very large so these cant match what part of the screen the user has the fingers on which im trying to find so i can set position to.

 

Can somebody tell me what actually represents the part of the screen (x,y) the user has triggered the pinch event?

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 660
Registered: ‎11-29-2011
My Device: developer
My Carrier: other

Re: Where to begin to pan/zoom with native screen

Well, let's think about this for a minute... talking through it should make the solution obvious...

 

The problem is that when the user pinches anywhere on the screen, that it zooms into the center.

In what situation is this the expected result?  --> when the user pinches in the center of the screen.

So what can you do to deal with the fact that the user pinched off-center?  You can measure how far off-center they pinched, and then shift the zoom-center accordingly.

But what if the co-ordinate spaces don't match?  eg. screen resolution is 1024x600, but only 100x60 is on screen right now.  well,  10% off-center is still 10% off-center in the different co-ordinate space... so just shift things appropriately.

Hey, this sounds a lot like a zoom and a pan at the same time!

 

.....

 

 

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 660
Registered: ‎11-29-2011
My Device: developer
My Carrier: other

Re: Where to begin to pan/zoom with native screen

If you actually take this concept to the next level and require that:

 

any pixel the user touches with a finger will always remain under that finger

 

Then you can derive all of the pan & zoom (and even rotate!!) equations yourself knowing just the starting and ending co-ordinates for those 2 touch points!

Please use plain text.
Trusted Contributor
cjonesy
Posts: 160
Registered: ‎09-13-2012
My Device: 9900
My Carrier: vodafone

Re: Where to begin to pan/zoom with native screen

Thanks Sean, the thing is currently i dont know how to detect the area of the zoom (how far from centre) so then maybe i can figure out what you mention from there - all figures seem to be over 1024 so these cant be cooridinates of the area the pinch occured. What am I missing?

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 660
Registered: ‎11-29-2011
My Device: developer
My Carrier: other

Re: Where to begin to pan/zoom with native screen

fyi, I just dug up the code I wrote to pan & zoom around a photo.  no warranty is implied here, and I don't have any time to look into how it worked, but as far as I recall, it just updated some parameters while another thread was constantly applying the defined blit to the screen.

I think I cheated and just used the 2-dimensional distance between fingers in the pinch gesture to apply to the width parameter, and then ensured the height was computed to maintain aspect ratio.  If you wanted to implement what I mentioned earlier - where the point under each finger should follow that finger - then you would forego this aspect-ratio stuff and just stretch the image accordingly.

 

        case GESTURE_TWO_FINGER_PAN: {
            fprintf(stderr, "TWO FINGER PAN\n");
            if (gBlit.run) {
                gesture_tfpan_t* tfpan = (gesture_tfpan_t*)gesture;
                if (tfpan->last_centroid.x && tfpan->last_centroid.y) {
                    pthread_mutex_lock(&gBlit.mutex);
                    gBlit.src_x += (tfpan->last_centroid.x - tfpan->centroid.x);
                    if (gBlit.src_x < 0) gBlit.src_x = 0;
                    if (gBlit.src_x + gBlit.src_w > photores.height) gBlit.src_x = photores.height - gBlit.src_w;
                    gBlit.src_y += (tfpan->last_centroid.y - tfpan->centroid.y);
                    if (gBlit.src_y < 0) gBlit.src_y = 0;
                    if (gBlit.src_y + gBlit.src_h > photores.width) gBlit.src_y = photores.width - gBlit.src_h;
                    gBlit.post = true;
                    pthread_mutex_unlock(&gBlit.mutex);
                }
            }
            break;
        }
        case GESTURE_PINCH: {
            fprintf(stderr,"PINCH\n");
            if (gBlit.run) {
                gesture_pinch_t* pinch = (gesture_pinch_t*)gesture;
                int dist_x = pinch->distance.x;
                int dist_y = pinch->distance.y;
                int last_dist_x = pinch->last_distance.x;
                int last_dist_y = pinch->last_distance.y;

                int reldist = sqrt((dist_x)*(dist_x) + (dist_y)*(dist_y));
                int last_reldist = sqrt((last_dist_x)*(last_dist_x) + (last_dist_y)*(last_dist_y));

                if (reldist && last_reldist) {
                    pthread_mutex_lock(&gBlit.mutex);
                    gBlit.src_w += (last_reldist - reldist);
                    if (gBlit.src_x < 0) gBlit.src_x = 0;
                    if (gBlit.src_x + gBlit.src_w > photores.height) gBlit.src_x = photores.height - gBlit.src_w;
                    gBlit.src_h = gBlit.src_w * 9 / 16;  // maintain aspect ration when zooming
                    if (gBlit.src_y < 0) gBlit.src_y = 0;
                    if (gBlit.src_y + gBlit.src_h > photores.width) gBlit.src_y = photores.width - gBlit.src_h;
                    gBlit.post = true;
                    pthread_mutex_unlock(&gBlit.mutex);
                }
            }
            break;
        }

 

Please use plain text.
Trusted Contributor
cjonesy
Posts: 160
Registered: ‎09-13-2012
My Device: 9900
My Carrier: vodafone

Re: Where to begin to pan/zoom with native screen

Thanks Sean, that's really nice of you.

 

Hopefully I can accomplish my aim now!

Please use plain text.
BlackBerry Development Advisor
smcveigh
Posts: 660
Registered: ‎11-29-2011
My Device: developer
My Carrier: other

Re: Where to begin to pan/zoom with native screen


cjonesy wrote:

Thanks Sean, the thing is currently i dont know how to detect the area of the zoom (how far from centre) so then maybe i can figure out what you mention from there - all figures seem to be over 1024 so these cant be cooridinates of the area the pinch occured. What am I missing?


gesture_pinch_t looks like it advertises the 2 finger positions as well as the centroid.

https://developer.blackberry.com/native/beta/reference/com.qnx.doc.gestures.lib_ref/topic/structgest...

 

 

 

Please use plain text.
Trusted Contributor
cjonesy
Posts: 160
Registered: ‎09-13-2012
My Device: 9900
My Carrier: vodafone

Re: Where to begin to pan/zoom with native screen

Unfortunatley I have still not got there - I have the image able to zoom in to centre and 2 finger pan around but I want to zoom in to the area the user is interested in, i will then disable pan as that is not what i want.

 

I am trying to attempt both of Seans suggestions - first one - set position of video to the value of the first touch event x and y in handle screen event (tried before and after process gesture function).

 

viewport_pos[0] = (mtouch_event.x);
viewport_pos[1] = (mtouch_event.y);

 

but what do i then do with the position part in gesture callback?

 

if (viewport_size[0] > MIN_VIEWPORT_SIZEX && viewport_size[1] > MIN_VIEWPORT_SIZEY &&
viewport_size[0] < MAX_VIEWPORT_SIZEX && viewport_size[1] < MAX_VIEWPORT_SIZEY) {
viewport_pos[0] -= (last_reldist - reldist) >> 2;
viewport_pos[1] -= (last_reldist - reldist) >> 2;

//viewport_pos[0] = (mtouch_event.x);
//viewport_pos[1] = (mtouch_event.y);

printf("pinch end. \n");
fflush( stdout );

}

 

I have tried everything you can imagine - there is only 1 area in the class that is to do with postion (above) but I cant get anything near a working version where the image is zoomed into the area of my pinch. It just jumps about the screen in a seemingly uncontrolled manner.

 

I 'm pretty sure by my limited understanding it could be just tweaking 2 lines of code that might finish this for me..

 

viewport_pos[0] -= (last_reldist - reldist) >> 2;
viewport_pos[1] -= (last_reldist - reldist) >> 2;

 

but many many hours of trying and I haven't solved it. Maybe tomorrow!

Please use plain text.