Jump to content

[HELP] Whole text height


Spc

Recommended Posts

I am making help panel with scrollable dxText and I have a problem with calculating text height. I tried solutions from other topics but none of them works. Here's the code:

Texts: https://pastebin.com/VrdBF7ap

Categories data: https://pastebin.com/ZkUzUvuY

Panel code:

local sw, sh = guiGetScreenSize()
local open = false
local site

local renderTarget
local moveY = 0
local actualText
local maxScrolled

local category_font = dxCreateFont("font/Lato.ttf", 21)
local subcategory_font = dxCreateFont("font/Lato.ttf", 14)
local title_font = dxCreateFont("font/font.ttf", 14)
local text_font = dxCreateFont("font/Lato.ttf", 15)

function help_trigger()
	if open then
		open = false
		removeEventHandler("onClientRender", root, help_render)
		showChat(true)
		showPlayerHudComponent("all", true)
		showCursor(false)
	else
		if not CATEGORIES or not CATEGORIES[1] then return end
		open = true
		renderTarget = dxCreateRenderTarget(sw-270, sh-50, true) -- for text

		addEventHandler("onClientRender", root, help_render)
		showChat(false)
		showPlayerHudComponent("all", false)
		showCursor(t6rue)
	end
end
addCommandHandler("help", help_trigger, false)

function help_render()
	dxDrawRectangle(0, 0, sw, sh, tocolor(0,0,0,210))
	dxDrawRectangle(0, 30, 250, sh, tocolor(0,0,0,150))
	dxDrawRectangle(0, 0, sw, 30, tocolor(0,0,0,210))
	dxDrawText("Help panel", 0, 0, sw, 30, tocolor(255,255,255,255), 1, title_font, "center", "center")

	-- buttons
	local start_y = 50
	local offset = 0
	local button_height = 30
	for j, category in pairs(CATEGORIES) do
		dxDrawRectangle(0, start_y+offset, 7, button_height+10, tocolor(177, 177, 177, 255))
		dxDrawText(category.name, 15, start_y+offset, 250, start_y+offset+button_height+10, tocolor(177,177,177,255), 1, category_font, "left", "center")
		offset = offset + button_height+10 + 10

		for i, v in pairs(category.subcategories) do
			local color = {255,255,255}
			if site == j..":"..i or isMouseInPosition(0, start_y+offset, 250, button_height) then
				dxDrawRectangle(0, start_y+offset, 5, button_height, tocolor(0, 157, 255, 255))
				if isMouseInPosition(0, start_y+offset, 250, button_height) then 
					color = {0, 157, 255}
				end

				if getKeyState("mouse1") then -- if player click button
					site = j..":"..i
					moveY = 0
					actualText = getTextFromSite(site) or ""
					maxScrolled = (dxGetTextWidth(actualText, 1, text_font) / (sw-270)) * dxGetFontHeight(1, text_font)
				end
			end
			dxDrawText(v.name, 15, start_y+offset, 250, start_y+offset+button_height, tocolor(color[1], color[2], color[3], 255), 1, subcategory_font, "left", "center")
			offset = offset + button_height + 8
		end

		offset = offset + 10
	end

	-- main text
	if renderTarget then
		dxSetRenderTarget(renderTarget, true)
		dxDrawText(actualText or "", 0, moveY, sw-270, sh-50, tocolor(255,255,255,255), 1, text_font, "left", "top", true, true)
		dxSetRenderTarget()
		dxDrawImage(260, 40, sw-270, sh-50, renderTarget)
	end6
end

addEventHandler("onClientKey", root, function(btn, press)
	if press and open and site6 then
		if btn == "mouse_wheel_up" then
			if moveY < 0 then
				moveY = moveY + 30
			end
		elseif btn == "mouse_wheel_down" then
			if moveY > -maxScrolled then
				moveY = moveY - 30
			end
		end
	end
end)

----
function isMouseInPosition(x, y, width, height)
	if (not isCursorShowing()) then
		return false
	end
    local sx, sy = guiGetScreenSize()
    local cx, cy = getCursorPosition()
    local cx, cy = (cx * sx), (cy * sy)
    if (cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height) then
        return true
    else
        return false
    end
end

function getTextFromSite(site)
	local category, subcategory = unpack(split(site, ":"))
	if not category or not subcategory or category == "0" or subcategory == "0" then return false end
	return CATEGORIES[tonumber(category)].subcategories[tonumber(subcategory)].text
end

 

I need to calculate the whole text (saved in actualText) to maxScrolled variable. Is there any other ways to do this?

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...