Jump to content

dxGetTextWidth and text positioning problem


Dzsozi (h03)

Recommended Posts

Hello!

I would like to make dxDrawTexts draw after each other with a little offset between them, like a horizontal menu thing. But I keep failing, I have been trying to solve this for hours now but I can't get the result I want. What am I doing wrong?

local defaultButtonColor = tocolor(180,180,180,255)

local normal = dxCreateFont("files/fonts/normal.ttf", 24, true, "antialiased")
local buttonSize = 0.5
local marginOffset = 10

local menuButtons = {
	{"SADSADASWQETSADASG", defaultButtonColor, hover = false},
	{"QRWRQW", defaultButtonColor, hover = false},
	{"GDADSSASDFSASD", defaultButtonColor, hover = false},
	{"FHDJRSDAD", defaultButtonColor, hover = false},
}

function draw()
  for i=1, #menuButtons do
      if menuButtons[i] then
          local menuButtonTextWidth = dxGetTextWidth(menuButtons[i][1], buttonSize, normal)
          local menuButtonTextHeight = dxGetRealFontHeight(normal)

          local step = 50 + ((i - 1) * (menuButtonTextWidth + marginOffset)) -- problem is probably here somewhere, I guess

          dxDrawText(menuButtons[i][1], step, 400, 0, 0, menuButtons[i].hover and tocolor(255,255,255,255) or menuButtons[i][2], buttonSize, normal, "left", "top", false, false)
      end
  end
end
addEventHandler("onClientRender", root, draw)

For some reason, this is the result I get:

AMP3JcQ.png

And this is what I would like to have:

qMgJ9cv.png

What is the problem, how should I do it?

Link to comment
-- render

local nextOffset = 0

for i, v in pairs(menu) do
	local textwidth -- get your text width
	local textheight -- get your text height here
	local posX = nextOffset
	local posY -- your Y position
	local endX = posX + textwidth
	local endY = posY + textheight

	-- draw it
	dxDrawText(v.name, posX, posY, endX, endY, ...) -- your next argument bla, bla, bla
	
	-- set the next offset
	nextOffset = nextOffset + textwidth
end

Something like this may work. And also argument 5 and argument 6 should not be 0 value, because you also set the alignment for the text.

See: dxDrawText

Quote

Optional Arguments

  • right: the absolute X coordinate of the right side of the text bounding box. Used for text aligning, clipping and word breaking.
  • bottom: the absolute Y coordinate of the bottom side of the text bounding box. Used for text aligning, clipping and word breaking.

 

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...