Jump to content

guiCreateStaticImage and URL


Recommended Posts

  • Moderators

Just to let you know I'm on it, but that's pretty huge because I need to reimplement some basic gui functions (gui(Set/Get)Size, gui(Set/Get)Position etc). Because the only way to do it is to create a texture from image datas and draw it using dxDrawImage.

So yeah check this topic later. :wink:

EDIT:

You then save these data to some file using fileCreate, fileWrite and fileClose and then you can use that to display the image.

Ah yeah, didn't think about that ! Well I had the idea to store it as a file but thought I would need to reload the resource to get it on the client-side. But not needed at all using ur way. Gonna try it right now.

Edited by Guest
Link to comment
Just to let you know I'm on it, but that's pretty huge because I need to reimplement some basic gui functions (gui(Set/Get)Size, gui(Set/Get)Position etc). Because the only way to do it is to create a texture from image datas and draw it using dxDrawImage.

So yeah check this topic later. :wink:

That's not true, you can save the data to some file and then display it using guiCreateStaticImage, I do it that way myself.

Link to comment
  • Moderators

Yeah I didn't see your messages when I posted, because I had that tab already opened and it has no reply yet. (It didn't even warned me about new replies)

Ok so here is my script based on the MIKI785 idea:

Pre-requisites:

As we can't create a static image without giving it an existing image, you need to download this image and put in in your resource folder:

http://www.mediafire.com/view/ujgfu7wlt ... oading.png

And add it to your meta.xml:

<file src="imageloading.png" /> 

server:

addEvent("onClientImageRequest", true) 
addEventHandler("onClientImageRequest", root, 
    function onClientImageRequest( url, id ) 
        fetchRemote( 
            url, 
            function ( datas, errno, player, url ) 
                triggerClientEvent( player, "onClientGotImageResponse", player, url, datas, errno ) 
            end, 
            "", 
            false, 
            client, 
            url 
        ) 
    end 
) 

client:

local _loadingImage = "imageloading.png" 
local _waitingForImage = {} 
local _imgsOnCache = {} 
  
function isUrl( str ) 
    return string.sub(str, 1, 4) == "http" 
end 
  
function getExtension( str ) 
    return string.match(str, "([^%.]+)$") 
end 
  
local _guiCreateStaticImage = guiCreateStaticImage 
function guiCreateStaticImage( x, y, width, height, path, relative, parent ) 
    if not isUrl(path) then return _guiCreateStaticImage( x, y, width, height, path, relative, parent ) end 
    local img = _guiCreateStaticImage( x, y, width, height, _loadingImage, relative, parent ) 
    setRemoteImage(img, path) 
    return img 
end 
  
local _guiStaticImageLoadImage = guiStaticImageLoadImage 
function guiStaticImageLoadImage( elem, path ) 
    if not isUrl(path) then return _guiStaticImageLoadImage( elem, path ) end 
    return setRemoteImage( elem, path ) 
end 
  
function generateTmpFilename( img, path ) 
    local str = tostring(img).."_"..tostring(getTickCount()).."."..tostring(getExtension(path)) 
    str = string.gsub(str, "userdata: ", "") 
    return str 
end 
  
function setRemoteImage( img, path ) 
    local currentPath = getElementData(img, "_url") 
    if path ~= currentPath then 
        setElementData(img, "_url", path) 
        setElementData(img, "_tmpfile", generateTmpFilename(img, path)) 
        table.insert(_waitingForImage, img) 
        return triggerServerEvent("onClientImageRequest", localPlayer, path) 
    end 
end 
  
addEvent("onClientGotImageResponse", true) 
function onClientGotImageResponse( path, datas, errno ) 
    local img, index = nil, nil 
    for k, simage in ipairs (_waitingForImage) do 
        if getElementData(simage, "_url") == path then 
            img = simage 
            index = k 
            break 
        end 
    end 
    if img and isElement(img) then 
        local filename = getElementData(img, "_tmpfile") 
        imageWriteToFile( filename, datas ) 
        _guiStaticImageLoadImage(img, filename) 
        table.remove(_waitingForImage, index) 
    end 
end 
addEventHandler("onClientGotImageResponse", root, onClientGotImageResponse) 
  
function imageWriteToFile( filename, datas ) 
    local file = fileExists(filename) and fileOpen(filename) or fileCreate(filename) 
    if file then 
        fileWrite(file, datas) 
        fileClose(file) 
        table.insert(_imgsOnCache, filename) 
    end 
end 
  
function cleanUp () 
    for k, filename in ipairs(_imgsOnCache) do 
        fileDelete( filename ) 
    end 
end 
addEventHandler("onClientResourceStop", resourceRoot, cleanUp) 

So you can now use guiCreateStaticImage and guiStaticImageLoadImage with an url as path.

Link to comment

you don't name functions if used inside addEventHandler...

It would be like this then:

function ( url, id ) 
fetchRemote( 
url, 
function ( datas, errno, player, url ) 
triggerClientEvent( player, "onClientGotImageResponse", player, url, datas, errno ) 
end, 
"", 
false, 
client, 
url 
) 
end 
) 

Please use lua tags.

Btw. the way you wrote it, everything on a new line is just horrible... but if you like it.. :roll:

Link to comment
  • Moderators
you don't name functions if used inside addEventHandler...

Omg ! I just wanted to make it all in one just before posting it, but forgot to make the handler function an anonymous one. *facepalm*

You didn't apply the MIKI785's fix correctly

This is what he mean't

server:

addEvent("onClientImageRequest", true) 
addEventHandler("onClientImageRequest", root, 
    function ( url, id ) 
        fetchRemote( 
            url, 
            function ( datas, errno, player, url ) 
                triggerClientEvent( player, "onClientGotImageResponse", player, url, datas, errno ) 
            end, 
            "", 
            false, 
            client, 
            url 
        ) 
    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...