PDA

View Full Version : getViewpoint() and setViewpointCenter()



zuilin
October 26th, 2020, 00:42
It seems to me that image.getViewpoint() and image.setViewpointCenter() use different references for Y. That is, when I getViewpoint() the Y is positive (for example) but to set it the same, I have to setViewpointCenter() with -Y. It's like one uses top-left for Y reference and the other uses bottom-left for Y reference.

Thoughts?

Moon Wizard
October 26th, 2020, 23:51
Is this in FGU or FGC?

JPG

zuilin
October 26th, 2020, 23:53
FGU.

Here are examples of zooming in and zooming out. You can see that I had to negate the Y to make it work.


function zoomIn(image)
local vpX, vpY, vpZoom = image.getViewpoint();
-- for some reason, you have to negative Y
image.setViewpointCenter(vpX, -vpY, vpZoom*1.1);
end

function zoomOut(image)
local vpX, vpY, vpZoom = image.getViewpoint();
-- for some reason, you have to negative Y
image.setViewpointCenter(vpX, -vpY, vpZoom/1.1);
end

Moon Wizard
October 27th, 2020, 00:46
This is a bug in the API. The result of getViewpoint should be Y-positive in down direction like the rest of the API. It is currently returning Y-negative in down direction (which is what Unity uses internally).

I'll get it fixed in the next release build.

Regards,
JPG

zuilin
October 27th, 2020, 00:48
Sounds good. If you don't mind posting back here when it's fixed then I'll fix my extension.

Moon Wizard
October 27th, 2020, 01:05
You can subscribe to EA release updates on this thread.
https://www.fantasygrounds.com/forums/showthread.php?54466-FGU-Early-Access-Updates

Regards,
JPG

zuilin
November 1st, 2020, 16:02
@Moon Wizard - Thanks for the fix.

However, I'm still experiencing madness with this. Here's some code:


local vpX, vpY, vpZoom = image.getViewpoint();
local w, h = image.getImageSize();

Debug.console("Current viewport center after Zoom to Fit but before hitting running code below: ", vpX, " ", vpY);
Debug.console("w and h of image: ", w, " ", h);

local newW = w/2;
local newH = h/2;

Debug.console("NEW w and h for centering image--should be middle point: ", newW, " ", newH);

image.setViewpointCenter(newW, newH, vpZoom);


This should center the image, right? Well, sometimes it does. Sometimes, it doesn't. The doesn't is the problem.

I have an image here that I created in Photoshop and imported into FGU by dragging from Assets into the Image/Maps list to create a new image record. It's 1000px x 1000px at 72dpi. When I hit the "Zoom to Fit" button, it correctly fills the image window. However, when I hit my button to center it, it moves the image so that the bottom right hand corner is in the center of the image window. With some images, it's not the bottom right corner, it's the bottom left that gets centered.

BUT... what's totally weird is that on some images--like the map images from the DMG and other images in modules, the calculations work (newW and newH are the centre of the image) AND my code above correctly centers the image in the viewport, regardless of zoom level, regardless of scroll position.

It's like it only doesn't work with images imported from Assets by hand (I've tried a number of them and none of the work properly with the code above). And it's like it always works with images from modules (I've tried a number of them, too).

And it feels like after hitting Zoom to Fit then running my code above, the viewPort's data isn't updated after the Zoom to Fit--if I look at what the Debug commands above return, sometimes the first Debug shows the center values and sometimes they make no sense. I would have expected that after Zoom to Fit, if an image is 1000x1000, what getViewPort should return is the center, 500 and 500. But only with images imported into FGU by hand, not images from modules.

Is something not being set correctly in the database data of images brought in by manually creating the image records that's causing the code above not to work?

(Sorry about the weird description... it's hard to explain.)

40668

40669

Moon Wizard
November 1st, 2020, 16:12
Viewpoint 0,0 is not the midpoint of an image. The image layers are laid out on an imaginary set of axes.

If you drop an image into the images list or into the image data control, it adds at 0,0 by default. If you drop an image onto the image directly, it adds at whatever x,y coordinate that the mouse is at. 0,0 is only the center of the image if the image is placed at 0,0.

Regards,
JPG

zuilin
November 1st, 2020, 17:09
Viewpoint 0,0 is not the midpoint of an image. The image layers are laid out on an imaginary set of axes.

If you drop an image into the images list or into the image data control, it adds at 0,0 by default. If you drop an image onto the image directly, it adds at whatever x,y coordinate that the mouse is at. 0,0 is only the center of the image if the image is placed at 0,0.

Regards,
JPG

Right, but where is 0,0? Top left? Bottom left?

How would you calculate the centre of an image and place it in the middle of the viewpoint if not h/2 and w/2 then use setViewpointCenter to place at midpoint of h and w at centre of the viewpoint? How does Zoom to Fit do it?

Moon Wizard
November 1st, 2020, 17:19
0,0 is completely relative to how image assets were added. For example, you could have only a single 1000,1000 image placed at 5000,5000. That doesn't change where 0,0 is located. The 0,0 position is not relative to assets; the assets are placed relative to 0,0.

I'm not sure if there is currently a way to calculate the center position of an image. We aren't planning on adding any FGU specific image APIs until we get further past release. For now, you should use the zoomToFit or zoomToFill APIs for image controls (added for FGU) to do that.

Regards,
JPG

zuilin
November 1st, 2020, 17:22
Ok, maybe Zoom to Fill could help me...

zuilin
November 1st, 2020, 17:31
Oh, and thanks again Moon Wizard.

zuilin
November 1st, 2020, 19:03
If you don't mind helping me understand one more thing...

How come this code moves the image within the window? Based on the descriptions of the functions in the wiki, reading the current viewport and then setting the center with the same values should not move the image in the viewport. But it does. Not always, but certainly with any image that I added to the database records myself (eg. dragging from Assets to Maps list).


local vpX, vpY, vpZoom = image.getViewpoint();
image.setViewpointCenter(vpX, vpY, vpZoom);


Wiki says:


getViewpoint

Returns data on the current viewpoint of the user. The viewpoint is determined by the center point of the portion of image being viewed in the control as well as the used zoom level.

Return values

(number)
The X coordinate on the image acting as the center point of the viewpoint(number)
The Y coordinate on the image acting as the center point of the viewpoint(number)
The zoom level being used (a value of 1.0 represents 100% zoom)

and


setViewpointCenter

Sets the active viewpoint of the user to center on the . The image viewpoint is repositioned so that the given coordinates on the image are centered in the control with the specified zoom level active.

Parameters

viewx (number)
The desired horizontal view point center coordinate

viewy (number)
The desired vertical view point center coordinate

zoom (number) [optional]
The zoom level as a decimal number, with 1.0 being 100% zoom

In fact, it does work, but only after I execute it once. The first time it executes after opening the image window, it moves. After that, it stays put each time the code runs. It's like it doesn't know where to start, but once I set the center the first time, it has the right coordinates in the database. It really feels like there is something not set right somewhere in memory or the database or something when the image window is first opened.

Moon Wizard
November 1st, 2020, 20:48
The current view point is defined as the center point of the view plus the zoom scale. When you call get/set, you are just setting the exact same information back into itself.

Regards,
JPG

zuilin
November 1st, 2020, 23:42
The current view point is defined as the center point of the view plus the zoom scale. When you call get/set, you are just setting the exact same information back into itself.

Regards,
JPG

That's my point. When I do that, the image moves. It shouldn't.

Moon Wizard
November 2nd, 2020, 03:10
I just pre-empted one of the toolbar buttons in the CoreRPG image toolbar with the following code:
function onButtonPress()
local ic = window.getImage();
local vX, vY, vZoom = ic.getViewpoint();
ic.setViewpointCenter(vX, vY, vZoom);
Debug.chat("OLD: ", vX, vY, vZoom);
Debug.chat("NEW: ", ic.getViewpoint());
end

With this code on my maps, I do not see any movement when I press the button.
Example:
s'OLD: ' | #1.58316040039063 | #18.5975341796875 | #0.816916465759277
s'NEW: ' | #1.58316040039063 | #18.5975341796875 | #0.816916465759277

Are you sure that you are not changing the values between the get and set at all?

Regards,
JPG

zuilin
November 2nd, 2020, 03:49
Well sonofa. For some reason, now it works perfectly. Nothin' moves. I don't get it. I had restarted FGU a number of times while testing, too, and it was still doing it. But it's not now. Cache? Solar flare?