Jump to content

DX Text appear behind dx rectangle


drk

Recommended Posts

Hi.

So, I created a dx rectangles library, it works perfectly, then I created a dx text library for colorcodes. All works perfectly but the dx text appears behind the rectangle, I put postGUI argument to true and don't work. I tried to find the problem, but nothing...

DX Text:

list [ 1 ] = dx:Create ( getPlayerName ( localPlayer ), ( 312 / 800 ) * x, ( 453 / 600 ) * y, ( 161 / 800 ) * x, ( 20 / 600 ) * y, { 255, 255, 255, 255 }, 1, 'default', 'center', 'left', false, false ); 
  

DX Rectangle:

top = dxRectangle:Create ( ( 274 / 800 ) * x, ( 426 / 600 ) * y, ( 278 / 800 ) * x, ( 169 / 600 ) * y, { 0, 0, 0, 210 }, false ); 

DX Text library:

dx = { } 
dx.__index = dx 
local visible = { } 
  
function dx:Create ( Text, X, Y, W, H, Color, Scale, Font, AlignX, AlignY, Clip, Wordbreak ) 
    if ( 
            type ( Text ) == "string" 
        and 
            type ( X ) == "number" 
        and 
            type ( Y ) == "number" 
        and 
            type ( W ) == "number" 
        and 
            type ( H ) == "number" 
        and 
            type ( Scale ) == "number" 
        ) 
    then 
        local text = 
            { 
                eText       = Text; 
                eX          = X; 
                eY          = Y; 
                eW          = W; 
                eH          = H; 
                eColor      = Color; 
                eScale      = Scale; 
                eFont       = Font; 
                eClip       = Clip; 
                eWordbreak  = WordBreak; 
                ePostGUI    = true 
            } 
        setmetatable ( text, { __index = dx } ) 
        visible [ text ] = true; 
        return text 
    end 
end 
  
function dx:Color ( ) 
    return unpack ( self.eColor ) 
end 
  
function dx:SetColor ( r, g, b, a ) 
    self.eColor = { r, g, b, a } 
end 
  
function dx:Text ( text ) 
    self.eText = text 
end 
  
addEventHandler ( 'onClientRender', root, 
    function ( ) 
        for self, _ in pairs ( visible ) do 
            while true do 
                draw ( self.eText, self.eX, self.eY, self.eW, self.eH, tocolor ( unpack ( self.eColor ) ), self.eScale, self.eFont, self.eAlignX, self.eAlignY, self.eClip, self.eWordbreak, self.PostGUI ) 
                break 
            end 
        end 
    end 
) 
  
function draw ( str, ax, ay, bx, by, color, scale, font, left, top, clip, wordbreak, postGUI ) 
    local pat = "(.-)#(%x%x%x%x%x%x)" 
    local s, e, cap, col = str:find(pat, 1) 
    local last = 1 
    while s do 
        if cap == "" and col then color = tocolor( tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), 
        tonumber( "0x"..col:sub( 5, 6 ) ), 255 ) end 
        if s ~= 1 or cap ~= "" then 
            local w = dxGetTextWidth(cap, scale, font) 
            dxDrawText( cap, ax, ay, ax + w, by, color, scale, font ) 
            ax = ax + w 
            color = tocolor( tonumber( "0x"..col:sub( 1, 2 ) ), tonumber( "0x"..col:sub( 3, 4 ) ), tonumber( "0x"..col:sub( 5, 6 ) ), 255 ) 
        end 
        last = e + 1 
        s, e, cap, col = str:find( pat, last ) 
    end 
    if last <= #str then 
        cap = str:sub( last ) 
        local w = dxGetTextWidth( cap, scale, font ) 
        dxDrawText( cap, ax, ay, ax + w, by, color, scale, font, left, top, clip, wordbreak, postGUI ) 
    end 
end 

Edited by Guest
Link to comment

Just use this

CDraw = { } 
 
function TableAssert( t )
    if type( t ) == 'table' then
        for _, v in pairs( t ) do
            if v.value and v.condition then
                assert( v.value == v.condition, v.sMsg )
            elseif v.condition == false then
                assert( v.condition, v.sMsg )
            end
        end
        return true
    end
    return false
end
 
nScreenX, nScreenY    = guiGetScreenSize( )
local tVisible              = { }
 
function CDraw:CreateText( sText, nPosX, nPosY, bRelative, bResizeText )
    TableAssert(
        {
            {
                value = type( sText );
                condition = 'string';
                sMsg = '1 argument is not string!';
            };
            {
                value = type( nPosX );
                condition = 'number';
                sMsg = '2 argument is not number!';
            };
            {
                value = type( nPosY );
                condition = 'number';
                sMsg = '3 argument is not number!';
            };
            {
                value = type( bRelative );
                condition = 'boolean';
                sMsg = '4 argument is not boolean!';
            };
        }
    )
    return setmetatable(
        {
            sText       = sText;
            nScale      = bResizeText and ( nScreenX / nPosX ) * 1.0 or 1.0;
            nPosX       = nPosX;
            nPosY       = nPosY;
            bRelative   = bRelative;
            tMainColor  = { nR = 255, nG = 255, nB = 255, nA = 255 };
            sData       = 'Text';
           
            sMixedFont  = 'default';
            sAlignX     = 'left';
            sAlignY     = 'top';
            bClip       = false;
            bWordBreak  = false;
            bPostGUI    = false;
           
            bResizeText = bResizeText or false;
        },
        {
            __index = CDraw;
        }
    )
end
 
function CDraw:CreateRectangle( nPosX, nPosY, nWidth, nHeight, bRelative )
    TableAssert(
        {
            {
                value = type( nPosX );
                condition = 'number';
                sMsg = '1 argument is not number!';
            };
            {
                value = type( nPosY );
                condition = 'number';
                sMsg = '2 argument is not number!';
            };
            {
                value = type( nWidth );
                condition = 'number';
                sMsg = '3 argument is not number!';
            };
            {
                value = type( nHeight );
                condition = 'number';
                sMsg = '4 argument is not number!';
            };
            {
                value = type( bRelative );
                condition = 'boolean';
                sMsg = '5 argument is not boolean!';
            };
        }
    )
   
    return setmetatable(
        {
            nPosX       = nPosX;
            nPosY       = nPosY;
            nWidth      = nWidth;
            nHeight     = nHeight;
            bRelative   = bRelative;
            tMainColor  = { nR = 255, nG = 255, nB = 255, nA = 255 };
            bPostGUI    = false;
           
            sData       = 'Rectangle';
        },
        {
            __index = CDraw;
        }
    )
end
 
function CDraw:CreateImage( nPosX, nPosY, nWidth, nHeight, sPath, bRelative )
    TableAssert(
        {
            {
                value = type( nPosX );
                condition = 'number';
                sMsg = '1 argument is not number!';
            };
            {
                value = type( nPosY );
                condition = 'number';
                sMsg = '2 argument is not number!';
            };
            {
                value = type( nWidth );
                condition = 'number';
                sMsg = '3 argument is not number!';
            };
            {
                value = type( nHeight );
                condition = 'number';
                sMsg = '4 argument is not number!';
            };
            {
                value = type( sPath );
                condition = 'string';
                sMsg = '5 argument is not string!';
            };
            {
                value = type( bRelative );
                condition = 'boolean';
                sMsg = '6 argument is not boolean!';
            };
            {
                value = fileExists( sPath );
                condition = true;
                sMsg = 'File: \'' .. sPath .. '\' not exists!';
            };
        }
    )
    return setmetatable(
        {
            nPosX                   = nPosX,
            nPosY                   = nPosY;
            nWidth                  = nWidth;
            nHeight                 = nHeight;
            sPath                   = sPath;
            bRelative               = bRelative;
           
            nRotation               = 0;
            nRotationCenterOffsetX  = 0;
            nRotationCenterOffsetY  = 0;
            tMainColor              = { nR = 255, nG = 255, nB = 255, nA = 255 };
            bPostGUI                = false;
           
            sData                   = 'Image';
        },
        {
            __index = CDraw;
        }
   
    )
end
 
function CDraw:Clip( bClip )
    if self.sData == 'Text' then
        if type( bClip ) == 'boolean' then
            self.bClip = bClip
            return true
        end
        return self.bClip
    end
    return false
end
 
function CDraw:WordBreak( bWordBreak )
    if self.sData == 'Text' then
        if type( bWordBreak ) == 'boolean' then
            self.bWordBreak = bWordBreak
            return true
        end
        return self.bWordBreak
    end
    return false
end
 
function CDraw:HorizontalAlign( sAlignX )
    if self.sData == 'Text' then
        if
            type( sAlignX ) == 'string' and
            type( sAlignX ) == 'left'   and
            type( sAlignX ) == 'center' and
            type( sAlignX ) == 'right'
        then
            self.sAlignX = sAlignX
            return true
        end
        return self.sAlignX
    end
    return false
end
 
function CDraw:VerticalAlign( sAlignY )
    if self.sData == 'Text' then
        if
            type( sAlignY ) == 'string' and
            type( sAlignY ) == 'top'    and
            type( sAlignY ) == 'center' and
            type( sAlignY ) == 'bottom'
        then
            self.sAlignY = sAlignY
            return true
        end
        return self.sAlignY
    end
    return false
end
 
function CDraw:Scale( nScale, bConvertToRelative )
    if self.sData == 'Text' then
        if type( nScale ) == 'number' and type( bConvertToRelative ) == 'boolean' then
            self.nScale = bConvertToRelative and ( nScreenX / self.nPosX ) * nScale or nScale
            return true
        end
        return self.nScale
    end
    return false
end
 
function CDraw:Text( sText )
    if self.sData == 'Text' then
        if type( sText ) == 'string' then
            self.sText = sText
           
Edited by Guest
Link to comment
Someone can help?

This is your answer:

1. DX Can never be defined

2. try creating the rectangle before the text

By defined he means you can't have a variable holding DX element, like for GUI elements.

DX drawing's z-index depends on the order of drawing. First draw rectangle, second draw text - text will be on top of rectangle.

Link to comment
DX drawing's z-index depends on the order of drawing. First draw rectangle, second draw text - text will be on top of rectangle.

That's what I did, don't work. If I use simple gui label work :/

To me it looks like the text library doesn't care about the order of creation. Does it use pairs iterator instead of ipairs? Like we said, DX drawings are overlapping by order of creation (it's called z-index or z-order).

Link to comment
If I use ipairs it create only one rectangle, if I use pairs it create all.

That's because the table is not indexed. I recommend using indexed tables when you need something in order or sortable.

Instead of assigning this way:

myTab[ "key" ] = value; 

Do it this way:

table.insert( myTab, value ); 
-- OR 
myTab:insert( value ); 
-- You can specify values location in the table: 
table.insert( myTab, 1, value ); -- will insert value as the first index of the table 

Link to comment

Why don't you draw your own text instead of using that text lib? The problem is that text is being drawn first but after that you draw rectangle on top of it. Events are not triggered in specific order this means if an event gets triggered and you cancel it, this doesn't necessarily mean the event will be cancelled in other resources. Drawing functions need to be used in onClient(Pre)Render events in order to see the drawings at all time. DX lib may draw the text first and then your event is triggered later.. You can try to draw your rectangle in onClientPreRender to be sure it will be drawn before text.

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