How to translate a pixel coordinate from silverlight into the corona app?

Hi all,

GOAL:

Determine the corona y coordinate of the bottom of the admob banner ad that is OVERLAYED over the corona draw surface in my windows phone 8 silverlight app.

Problem:

How do I translate between the silverlight app pixel height and the corona pixel height? I know my banner height is 80 pixels in silverlight, and it is positioned at the top of my silverlight app. The banner ad is overlayed OVER the corona draw surface. But 80 pixels high in silverlight will be different in my corona app because of content scaling. I want to determine the corona app y coordinate of the bottom of the banner ad.

My config.lua is like this:

application = { content = { width = 640, height = 960, scale = "letterBox", xAlign = "left", yAlign = "top", } }

Any ideas on how I can calculate this?

Thanks!

https://docs.coronalabs.com/api/library/display/contentScaleY.html

display.contentScaleY will return you a value of how much the content area is scaled.  So your 640x960 content area on a 320x480 screen will be 2.0.  If you flipped it and put a 320x480 content area on a 640x960 it will be 0.5.  You can use that value to adjust your math.

Rob

First, you need to convert the AdMob XAML Ad banner’s height from Silverlight’s coordinate system to pixels.  You can do so in C# as follows…

// Fetch Silverlight's scale factor. double scaleFactor = (double)System.Windows.Application.Current.Host.Content.ScaleFactor; // Convert the ad banner's height to fractional pixels. double fractionalPixelHeight = (adBanner.ActualHeight \* scaleFactor) / 100.0; // Since the above might be fractional, you should round to the nearest pixel. int pixelHeight = (int)(fractionalPixelHeight + 0.5);

Now you just need to send the pixel height to the Lua side.  From there, you should apply Corona’s display.contentScaleY scale factor to calculate what the height is in Corona’s content coordinates.

local contentHeight = pixelHeight \* display.contentScaleY

I hope this helps!

Thanks guys. Joshua, your code works like a charm. The thing I didn’t realize was that silverlight was scaling its pixels. So 80 pixels (which is what is returned by the adControl.ActualContent property in silverlight) is actually scaled by a factor IN silverlight as you stated.

Problem solved :) 

Great!  Happy to help!  :slight_smile:

And in case you didn’t know, different resolution WP8 devices will have different Silverlight scale factors.  You can easily test this via the WP8 emulator.  You’ll want to test it with WVGA, WXGA, and 720P devices.

One thing to note though is that a 1080P device will report the same resolution and scale as a 720P device.  It kind of works like it does on iPhone 6+, where your app thinks it running under a different resolution but the OS scales it to fit the higher resolution.  You can find more info about this here…

   https://msdn.microsoft.com/en-us/library/windows/apps/jj206974(v=vs.105).aspx#BKMK_Supportedresolutions

https://docs.coronalabs.com/api/library/display/contentScaleY.html

display.contentScaleY will return you a value of how much the content area is scaled.  So your 640x960 content area on a 320x480 screen will be 2.0.  If you flipped it and put a 320x480 content area on a 640x960 it will be 0.5.  You can use that value to adjust your math.

Rob

First, you need to convert the AdMob XAML Ad banner’s height from Silverlight’s coordinate system to pixels.  You can do so in C# as follows…

// Fetch Silverlight's scale factor. double scaleFactor = (double)System.Windows.Application.Current.Host.Content.ScaleFactor; // Convert the ad banner's height to fractional pixels. double fractionalPixelHeight = (adBanner.ActualHeight \* scaleFactor) / 100.0; // Since the above might be fractional, you should round to the nearest pixel. int pixelHeight = (int)(fractionalPixelHeight + 0.5);

Now you just need to send the pixel height to the Lua side.  From there, you should apply Corona’s display.contentScaleY scale factor to calculate what the height is in Corona’s content coordinates.

local contentHeight = pixelHeight \* display.contentScaleY

I hope this helps!

Thanks guys. Joshua, your code works like a charm. The thing I didn’t realize was that silverlight was scaling its pixels. So 80 pixels (which is what is returned by the adControl.ActualContent property in silverlight) is actually scaled by a factor IN silverlight as you stated.

Problem solved :) 

Great!  Happy to help!  :slight_smile:

And in case you didn’t know, different resolution WP8 devices will have different Silverlight scale factors.  You can easily test this via the WP8 emulator.  You’ll want to test it with WVGA, WXGA, and 720P devices.

One thing to note though is that a 1080P device will report the same resolution and scale as a 720P device.  It kind of works like it does on iPhone 6+, where your app thinks it running under a different resolution but the OS scales it to fit the higher resolution.  You can find more info about this here…

   https://msdn.microsoft.com/en-us/library/windows/apps/jj206974(v=vs.105).aspx#BKMK_Supportedresolutions