drawFont anchoring position and align

  • #1, by stroncisMonday, 08. May 2017, 19:12 3 years ago
    Hi, it would be great to have drawFont selectable anchoring. At this moment it's by default Left Top. And it's complicated (to say the least) to place dynamic text at the bottom right corner of the window. Bottom of text area is quite easy to compensate "lineCount*(fontSize + lineSpacing)", but right corner is wildly variable.

    So, aligns (left, center, right) or anchors would be very handy. Both would be better, because text area have anchoring points and you can align inside as you wish. But doubt if so many users would use both options.

    Proposal examples:
    "drawFont(text, x, y, alpha, anchor)" with anchors as integer 1-9, align matrix
    "drawFont(text, x, y, alpha, align)" align as integer 1-3, left, center, right

    Why:
    Now, for any new text item, which is aligned right, i need to create and call different function each time "graphics.addDrawFunc("selectedText_01()", 0) ", "graphics.addDrawFunc("selectedText_02()", 0) " and in each i need to manually define position.

    My idea is to call same "graphics.addDrawFunc("selectedItem('Something smelly')", 0)" and pass different item names. That way i have just one "selectedItem(name)" function for all my selected item messages, without worrying about position manual recalculation each time (and what a headache, if you will change font size in later development stage).


    P.S.: did i missed something or there's another way?

    Newbie

    42 Posts


  • #2, by SimonSWednesday, 10. May 2017, 16:58 3 years ago
    Hi,

    I won't add anchoring to the call, since it's done in a few calls.
    I'd recommend to not add thousands of calls to the draw list, better write a simple system for that (untested).

    local fontTable = {}

    function addText(x,y,text,anchor)
    table.insert(fontTable,{x=x,y=y,text=text,anchor=anchor})
    end

    function textDraw()
    <span>for i=1..#fontTable do
    local x = fontTable[i].x
    local dimX = graphics.fontDimension(fontTable[i].text).x
    if fontTable[i].anchor == 1 then
    x = x - dimX / 2
    elseif fontTable[i].anchor == 2 then
    x = x - dimX / 2
    end
    graphics.drawFont(fontTable[i].text,x,fontTable[i].y,1.0)
    end
    end

    graphics.addDrawFunc("textDraw()",0)</span>
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    Thread Captain

    1352 Posts

  • #3, by sebastianWednesday, 10. May 2017, 17:29 3 years ago
    anchor 1,2 do the same but generally it works.
    The for i=1..#fontTable do needs a "," instead of ".." though^^


    Thread Captain

    2336 Posts

  • #4, by afrlmeWednesday, 10. May 2017, 18:36 3 years ago
    anchor 1,2 do the same but generally it works.
    The for i=1..#fontTable do needs a "," instead of ".." though^^


    Pretty sure <span> tags aren't required either. @Calli can you fix you code blocks please? - unfortunately there's no actual @ tagging system, so he'll probably never see my message.

    Imperator

    6816 Posts

  • #5, by stroncisThursday, 11. May 2017, 03:04 3 years ago
    Thank You SimonS! I somehow overlooked fontDimension in reference, that's what I need. And thank for your efforts writing that system, it's perfect example. By the way, fontDimension in description doesn't have return type. I figured out though, that it have X and Y measurements.

    Newbie

    42 Posts

Write post