Jump to content

GUI Tooltips


Tails

Recommended Posts

  • MTA Team

I made a resource solution, that means a single resource handles the rendering for every gui element, which has a tooltip element data.

Example:

ErtmIUX.png

Result:

wxZaM1H.pnggPlw42s.png

Code:

  
-- Settings 
local font_face = "arial" 
local font_widthscale = 1 
local font_heightscale = 1 
local font_color = tocolor(255, 255, 255) 
local background_color = tocolor(0, 0, 0, 200) 
local hovertime = 500 
local cursor_offsetx = 15 
local cursor_offsety = -10 
local padding_top = 5 
local padding_right = 5 
local padding_bottom = 5 
local padding_left = 5 
  
-- Variables 
local screenWidth, screenHeight = guiGetScreenSize() 
local height = dxGetFontHeight(font_heightscale, font_face) 
local tooltip = false 
local width = 0 
local hoverstart = 0 
  
addEventHandler("onClientRender", root, 
    function () 
        if (tooltip and (isCursorShowing() or isConsoleActive() or isChatBoxInputActive())) then 
            if (hoverstart + hovertime < getTickCount()) then 
                local x, y = getCursorPosition() 
                x, y = screenWidth * x, screenHeight * y 
                x, y = x + cursor_offsetx, y + cursor_offsety 
                dxDrawRectangle(x, y, width + padding_left + padding_right, height + padding_top + padding_bottom, background_color, true) 
                dxDrawText(tooltip, x, y, x + width + padding_left + padding_right, y + height + padding_top + padding_bottom, font_color, font_widthscale, font_heightscale, font_face, "center", "center", false, false, true, true, true) 
            end 
        end 
    end 
) 
  
addEventHandler("onClientMouseEnter", root, 
    function () 
        local text = getElementData(source, "tooltip") 
        if (type(text) == "string" and #text > 0) then 
            tooltip = text 
            local withoutcolor = text:gsub("#%x%x%x%x%x%x", "") 
            width = dxGetTextWidth(withoutcolor, font_widthscale, font_face) 
  
            if (#withoutcolor == 0) then 
                tooltip = false 
                return 
            end 
  
            hoverstart = getTickCount() 
        end 
    end 
) 
  
addEventHandler("onClientMouseLeave", root, 
    function () 
        tooltip = false 
    end 
) 
  

Link to comment
I made a resource solution, that means a single resource handles the rendering for every gui element, which has a tooltip element data.

Example:

ErtmIUX.png

Result:

wxZaM1H.pnggPlw42s.png

Code:

  
-- Settings 
local font_face = "arial" 
local font_widthscale = 1 
local font_heightscale = 1 
local font_color = tocolor(255, 255, 255) 
local background_color = tocolor(0, 0, 0, 200) 
local hovertime = 500 
local cursor_offsetx = 15 
local cursor_offsety = -10 
local padding_top = 5 
local padding_right = 5 
local padding_bottom = 5 
local padding_left = 5 
  
-- Variables 
local screenWidth, screenHeight = guiGetScreenSize() 
local height = dxGetFontHeight(font_heightscale, font_face) 
local tooltip = false 
local width = 0 
local hoverstart = 0 
  
addEventHandler("onClientRender", root, 
    function () 
        if (tooltip and (isCursorShowing() or isConsoleActive() or isChatBoxInputActive())) then 
            if (hoverstart + hovertime < getTickCount()) then 
                local x, y = getCursorPosition() 
                x, y = screenWidth * x, screenHeight * y 
                x, y = x + cursor_offsetx, y + cursor_offsety 
                dxDrawRectangle(x, y, width + padding_left + padding_right, height + padding_top + padding_bottom, background_color, true) 
                dxDrawText(tooltip, x, y, x + width + padding_left + padding_right, y + height + padding_top + padding_bottom, font_color, font_widthscale, font_heightscale, font_face, "center", "center", false, false, true, true, true) 
            end 
        end 
    end 
) 
  
addEventHandler("onClientMouseEnter", root, 
    function () 
        local text = getElementData(source, "tooltip") 
        if (type(text) == "string" and #text > 0) then 
            tooltip = text 
            local withoutcolor = text:gsub("#%x%x%x%x%x%x", "") 
            width = dxGetTextWidth(withoutcolor, font_widthscale, font_face) 
  
            if (#withoutcolor == 0) then 
                tooltip = false 
                return 
            end 
  
            hoverstart = getTickCount() 
        end 
    end 
) 
  
addEventHandler("onClientMouseLeave", root, 
    function () 
        tooltip = false 
    end 
) 
  

Why don't you set the element data sync to false otherwise this element data will be synced with server side which is not needed?

setElementData(element,string,value,false) 

Edit: or the element data doesn't exist server side because the element is client side?

Link to comment

Well, the description of the resource says its all clearly..

Assign any string in "tooltip-text" key as custom data to your GUI element with setElementData() and let the magic do the magic. Simple example:

my_btn = guiCreateButton( ... ) 
setElementData( my_btn, "tooltip-text", "This is my fancy button", false ) 

Make sure 'tooltips' resource is started. Hover your mouse cursor over the button and you will see a bubble with text on top of it.

Link to comment
Oh my bad, I meant the tooltip resource from the official map editor. Do you know how to use that one?
addEventHandler( "onClientResourceStart", resourceRoot, 
    function() 
        -- Create(x, y, text, foregroundColor, backgroundColor) 
        local BGColor = tocolor(140, 140, 240) 
        local FGColor = tocolor(0, 0, 0) 
        tip = Create(200, 350, "Your tooltip text here", FGColor, BGColor) 
        FadeIn(tip) -- Show the tip 
    end 
) 

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