Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to translate a pixel coordinate from silverlight into the corona app?
Started by spacewolf Jun 12 2015 03:49 PM

4 replies to this topic
coordinates translate system content scaling silverlight windows phone wp8
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

spacewolf

[GLOBAL: userInfoPane.html]
spacewolf
  • Contributor

  • 298 posts
  • Enterprise

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!



[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,075 posts
  • Enterprise

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



[TOPIC: post.html]
#3

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

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!



[TOPIC: post.html]
#4

spacewolf

[GLOBAL: userInfoPane.html]
spacewolf
  • Contributor

  • 298 posts
  • Enterprise

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 :) 



[TOPIC: post.html]
#5

Joshua Quick

[GLOBAL: userInfoPane.html]
Joshua Quick
  • Moderator

  • 3,397 posts
  • Corona Staff

Great!  Happy to help!  :)

 

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




[topic_controls]
[/topic_controls]