Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Textfield y position — Gideros Forum

Textfield y position

gaboritalygaboritaly Member
edited June 2012 in General questions
Hi guys,
Finally I can start learning Gideros .
I started my first example, and I found a little strange thing.
The Textfield doesn't appear in the red box.
I watched in the Gideros tutorials,it uses the similar coordinates system to flash, and I didn't find in the documentation, if the Textfield has a different coordinate.
Any object what I add with addChild, the player view in the correct position.
Anybody know why happened this ?


// CODE

FirstButton = Core.class(Sprite)
function FirstButton:init()

local text = TextField.new(nil, "Change direction")
local shape = Shape.new() -- create the shape

shape:beginPath()
shape:setLineStyle(1,0xddeecc,0.7)
shape:setFillStyle(Shape.SOLID, 0xff0000, 0.5)
shape:lineTo(0,0)
shape:lineTo(text:getWidth()+10,0)
shape:lineTo(text:getWidth()+10,text:getHeight()+10)
shape:lineTo(0,text:getHeight()+10)
shape:lineTo(0,0)
shape:endPath()

print(text:getY(0))

self:addChild(shape)
self:addChild(text)

end

local rotate_btn = FirstButton.new()
local countNum = 1
local orientations ={Application.PORTRAIT,Application.PORTRAIT_UPSIDE_DOWN,Application.LANDSCAPE_LEFT,Application.LANDSCAPE_RIGHT}

local rotateScreen = function ()

countNum = countNum + 1
if countNum > #orientations then
countNum = 1
end
application:setOrientation(orientations[countNum])

end


rotate_btn:addEventListener(Event.MOUSE_DOWN, rotateScreen)
stage:addChild(rotate_btn)

or

Thanks a lot for any suggestion.
NG

Comments

  • Hm, that does work differently than I'd expect, you can set the Y to the height of the textfield however. I'm guessing the coordinates are the point at which the font draws (the point always being at the bottom left).

    Also, just some small things you may want to look out for/find easier:
    Instead of doing the last shape:lineTo(0,0) you can do shape:closePath() and it will automatically draw a line to the originating point. I also usually start with shape:moveTo() instead of lineTo() for the first point (not sure if it makes a difference).

    And your event listener will fire whenever someone clicks anywhere on the screen right now. All sprites will receive any touch/mouse event, so you need to add a parameter to your rotateScreen function and test the point like so:
    local function rotateScreen(e)
       if self:hitTestPoint(e.x,e.y) then
          --your code
       end
    end
  • evsevs Member
    @gaboritaly

    Put this (where the print statement is) to compensate for the way that text is positioned
     
    text:setPosition(5, shape:getHeight() / 2 + text:getHeight() / 2)

    cheers

    evs
  • I noticed in Gideros, all Font classes have the bottom left alignment.
    Thanks all, for the code optimization and for the good explanation.
    Have a nice day

    NG


  • It is not Bottom Left Alignment, it is due to the way that fonts are handled in GiderosStudio, have a read on http://en.wikipedia.org/wiki/Baseline_(typography)
    It seems that it is bottom left.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • For that I didn't understand why viewed the descent part of the letter.
    Thank you for clarify ;)
  • atilimatilim Maintainer
    edited June 2012
    Hi,

    Also there is an undocumented function Sprite:getBounds(targetSprite). It returns the exact bounds as x, y, width and height which appears in another coordinate system (targetSprite's coordinate system).

    For example, you can use Sprite:getBounds function to get untransformed local bounds as:
    local x, y, width, height = sprite:getBounds(sprite)
    or get the bounds in stage's coordinate system as:
    local x, y, width, height = sprite:getBounds(stage)
    And here is an example that gets and draws the bounds of 2 text fields:
    -- example 1
    do
        local text = TextField.new(nil, "ABCDEFG abcdefg")
        text:setPosition(20, 30)
        stage:addChild(text)
     
        local x, y, width, height = text:getBounds(text)
     
        local shape = Shape.new()
        shape:setLineStyle(1, 0x306090)
        shape:moveTo(x, y)
        shape:lineTo(x + width, y)
        shape:lineTo(x + width, y + height)
        shape:lineTo(x, y + height)
        shape:closePath()
        shape:endPath()
     
        text:addChild(shape)
    end
     
     
    -- example 2
    do
        local text = TextField.new(nil, "Abg")
        text:setPosition(20, 200)
        text:setScale(16)
        stage:addChild(text)
     
        local x, y, width, height = text:getBounds(stage)
     
        local shape = Shape.new()
        shape:setLineStyle(1, 0x306090)
        shape:moveTo(x, y)
        shape:lineTo(x + width, y)
        shape:lineTo(x + width, y + height)
        shape:lineTo(x, y + height)
        shape:closePath()
        shape:endPath()
     
        stage:addChild(shape)
    end
    The documentation of this function will be available with the next version.
  • Thanks Atilim, for advanced notification !
Sign In or Register to comment.