Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Easy way to find the top of the screen? — Gideros Forum

Easy way to find the top of the screen?

NinjadoodleNinjadoodle Member
edited July 2016 in General questions
Hi guys

Is there a class floating around, or an easy way to find the top of the screen, regardless the scaling mode used, or the dimensions of the device. I looked but can't find anything that works.

Basically, I need a ui bar to be at the top of the screen, whether the screen is square or rectangular.

Thank you in advance!

Comments

  • antixantix Member
    edited July 2016
    Scaling causes a load of headaches sometimes. These 2 lines of code taken from SceneManager.lua are fantastic..
    local dx = 2 * math.ceil(application:getLogicalTranslateX() / application:getLogicalScaleX())
    local dy = 2 * math.ceil(application:getLogicalTranslateY() / application:getLogicalScaleY())
    To set your UI element at the very top left corner of the display try
      UIElement:setPosition(-dx, -dy)
    I theory that should work

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • NinjadoodleNinjadoodle Member
    edited July 2016
    @antix - thanks, I've tried, but it's still way off. I think it's setting it to the top of the predefined game size e.g. 320x480, but not to the top of the mobile screen.

    I know of a hard way to do it, but I'm hoping there is something cleaner/simpler.

    If you have any other ideas that would be awesome.
  • antixantix Member
    @Ninjadoodle. I'm using those values to adjust elements in my game outside the area defined by application:getContentWidth() and application:getContentHeight(). What kind of scaling are you using in your project?

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • NinjadoodleNinjadoodle Member
    edited July 2016
    @antix - my logical dimensions are 320x480 (width, height) and my scaling mode is "fit width".

    320x480 is perfect fit for iPhone 4, but on newer devices, my game shows more of the background. With the scaling mode above, it still seems to be sticking to the top of the 480 height.
  • I think I got it! - I'm playing with the code you gave me and I think it's working. I changed a couple of things, Ill post back soon :)

    Thanks heaps!
  • antixantix Member
    edited July 2016 Accepted Answer
    Try this..
    local function fillRect(x, y, w, h, color)
      local rect = Shape.new()
      rect:setFillStyle(Shape.SOLID, color, 1)
      rect:beginPath()
      rect:moveTo(0, 0)
      rect:lineTo(w, 0)
      rect:lineTo(w, h)
      rect:lineTo(0, h)
      rect:closePath()
      rect:endPath()
      rect:setPosition(x, y)
      rect:setClip(0, 0, w, h)
      stage:addChild(rect)
    end
     
    application:setBackgroundColor(0xffffff)
    stage:setOrientation(Stage.PORTRAIT)
     
    application:setScaleMode("pixelPerfect")
     
    WIDTH = application:getContentWidth()
    HEIGHT = application:getContentHeight()
     
    DX = math.ceil(application:getLogicalTranslateX() / application:getLogicalScaleX())
    DY = math.ceil(application:getLogicalTranslateY() / application:getLogicalScaleY())
     
    local element1 = fillRect(0, 0, WIDTH, HEIGHT, 0x7f7f7f, 1)
    local element2 = fillRect(-DX, -DY, WIDTH + DX * 2, 32, 0x7f0000, 1)
    Run this code in the windows player. There should be a white background, with a grey rectangle filling the content dimensions, and then a red rectangle placed at the very top of the devices display.

    Change the resolution of the player (Hardware > Resolution menu in player) and press CTRL + r to restart the test. You will see that no matter what resolution you use the red bar will always be placed at the top left.

    Does this work for you?

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • @antix - thanks heaps man, with your help I got it working :) I just had to adjust a couple of things because of my anchor point etc.

    Thanks again!!

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
  • @Ninjadoodle I am using this in my projects:
     
    --static variable
    local lw = application:getContentWidth()
    local lh = application:getContentHeight()
    _G.MINX = math.floor(-application:getLogicalTranslateX() / application:getLogicalScaleX())
    _G.MINY = math.floor(-application:getLogicalTranslateY() / application:getLogicalScaleY())
    _G.MAXX,_G.MAXY = lw - MINX,lh - MINY;
    _G.W, _G.H = MAXX - MINX, MAXY - MINY;
    _G.CX,_G.CY = MINX + W/2, MINY + H/2

    Likes: antix

    Coming soon
    +1 -1 (+1 / -0 )Share on Facebook
  • antixantix Member
    If only plugins were so easy :D
    +1 -1 (+2 / -0 )Share on Facebook
  • @vitalitymobile - I managed to get it working :) Thanks for the snippet, I'm sure it will help, when I'm cleaning my code up a bit :)

    @antix - If you can't solve it by then, hopefully the coming Gideros release will help with the issues. I wish I could help, but thats way over my head atm. I haven't even tried publishing an app full stop, so I'm dreading the process.
  • simwhisimwhi Member
    @Ninjadoodle. Don't worry, we'll help.

    Likes: antix, Ninjadoodle

    +1 -1 (+2 / -0 )Share on Facebook
  • @simwhi - Hey thanks! The people on this forum are awesome :)

    Likes: antix

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.