PT-BR/SvgSetDocumentXML: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Example update)
Line 22: Line 22:


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
-- Isso também pode ser um arquivo, com o diretório do arquivo fornecido no svgCreate
-- Isso também pode ser um arquivo, com o caminho fornecido para svgCreate
local rawSvgData = [[
local rawSvgData = [[
     <svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
     <svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
Line 29: Line 29:
]]
]]


local svg
local svgs = {}


function init()
local function render(svg)
     -- Cria um círculo em SVG, usando os dados, usando os dados brutos XML acima.
     if (not isElement(svg)) or (getElementType(svg) ~= "svg") then
    svg = svgCreate(500, 500, rawSvgData, function(didLoad)
         removeEventHandler("onClientRender", root, svgs[svg].handler)
         if (not didLoad) then -- Se o SVG falhou em carregar, essa função não continuará.
        svgs[svg] = nil
            return
    end
        end


         onSvgLoad(svg)
    local width, height = svgGetSize(svg)
    dxDrawImage(0, 0, width, height, svg, 0, 0, 0, tocolor(255, 255, 255), false)
end
 
local function onUpdate(svg)
    -- Se esta for a primeira atualização, adicione svg à nossa tabela e comece a desenhá-la
    if (not svgs[svg]) then
         svgs[svg] = {
            state = true,
            handler = function()
                render(svg)
            end
        }
 
        addEventHandler("onClientRender", root, svgs[svg].handler)
    end
 
    iprint("Textura SVG atualizada.", svg, getTickCount())
end
 
local function init()
    -- Cria um SVG contendo um círculo, usando os dados XML brutos acima
    local mySvg = svgCreate(500, 500, rawSvgData, onUpdate)
 
    -- Vincule uma chave para criar um nó filho SVG rect, que acionará o retorno de chamada onUpdate
    bindKey("F2", "down", function()
        addSVGRectNode(mySvg)
     end)
     end)
end
end
addEventHandler("onClientResourceStart", resourceRoot, init)
addEventHandler("onClientResourceStart", resourceRoot, init)


 
-- Adiciona um nó reto ao SVG com cor, tamanho e posição aleatórios
-- Implementando a função de callback para carregar o SVG, em seguida para manipular o documento XML subjacente.
function addSVGRectNode(svg)
function onSvgLoad(svg)
     -- Obter o documento XML do nosso SVG
     -- Obter o documento XML do nosso SVG
     local svgXML = svgGetDocumentXML(svg)
     local svgXML = svgGetDocumentXML(svg)
      
      
     -- Adicionar um retangulo, posicionando no centro do documento
     -- Adicione um nó SVG reto, posicionado no centro do documento
     local rect = xmlCreateChild(svgXML, "rect")
     local rect = xmlCreateChild(svgXML, "rect")
     xmlNodeSetAttribute(rect, "cx", "25%")
 
     xmlNodeSetAttribute(rect, "cy", "25%")
    local size = math.random(0, 50)
     xmlNodeSetAttribute(rect, "width", "50%")
    local r, g, b = math.random(10, 99), math.random(10, 99), math.random(10, 99)
     xmlNodeSetAttribute(rect, "height", "50%")
 
     xmlNodeSetAttribute(rect, "fill", "#00749b")
     xmlNodeSetAttribute(rect, "x", (size / 2) .. "%")
     xmlNodeSetAttribute(rect, "y", (size / 2) .. "%")
     xmlNodeSetAttribute(rect, "width", size .. "%")
     xmlNodeSetAttribute(rect, "height", size .. "%")
     xmlNodeSetAttribute(rect, "fill", "#" .. r .. g .. b)
      
      
     -- Aplicar o XML para o SVG, e começar a desenhar usando a função de callback
     -- Aplique nosso XML ao SVG e comece a desenhar via callback
     return svgSetDocumentXML(svg, svgXML, function()
     svgSetDocumentXML(svg, svgXML)
        addEventHandler("onClientRender", root, render)
    end)
end
 
function render()
    local width, height = svgGetSize(svg)
    dxDrawImage(0, 0, width, height, svg, 0, 0, 0, tocolor(255, 255, 255), false)
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 01:32, 9 April 2022

Template:BR/Funcao cliente Define o documento XML subjacente de um elemento SVG.

Sintaxe

bool svgSetDocumentXML ( svg svgElement, xmlnode xmlDocument [, function callback ( bool didLoad ) ] )

Argumentos Necessários

  • svgElement: Um elemento svg que você deseja definir o documento XML.
  • xmlDocument: Um xmlnode contendo os dados para serem definidos no documento SVG.

Argumentos Opcionais

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • callback: Uma função de callback chamada na criação do documento SVG e na criação da textura, útil para saber se o elemento SVG está carregado.

Retornos

  • Retornatrue se for bem sucedido, false caso dê errado.

Exemplo

Esse exemplo mostra como carregar um SVG por dados brutos (ou arquivo), manipulando um documento subjacente e desenhá-lo com dxDrawImage via onClientRender

IMPORTANTE: Dependendo da sua implementação, o uso de uma função de callback será necessário para garantir que a textura SVG e o documento XML estejam carregados.

-- Isso também pode ser um arquivo, com o caminho fornecido para svgCreate
local rawSvgData = [[
    <svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg">
        <circle cx="250" cy="250" r="250" fill="#0fc0fc" />
    </svg>
]]

local svgs = {}

local function render(svg)
    if (not isElement(svg)) or (getElementType(svg) ~= "svg") then
        removeEventHandler("onClientRender", root, svgs[svg].handler)
        svgs[svg] = nil
    end

    local width, height = svgGetSize(svg)
    dxDrawImage(0, 0, width, height, svg, 0, 0, 0, tocolor(255, 255, 255), false)
end

local function onUpdate(svg)
    -- Se esta for a primeira atualização, adicione svg à nossa tabela e comece a desenhá-la
    if (not svgs[svg]) then
        svgs[svg] = {
            state = true,
            handler = function()
                render(svg)
            end
        }

        addEventHandler("onClientRender", root, svgs[svg].handler)
    end

    iprint("Textura SVG atualizada.", svg, getTickCount())
end

local function init()
    -- Cria um SVG contendo um círculo, usando os dados XML brutos acima
    local mySvg = svgCreate(500, 500, rawSvgData, onUpdate)

    -- Vincule uma chave para criar um nó filho SVG rect, que acionará o retorno de chamada onUpdate
    bindKey("F2", "down", function()
        addSVGRectNode(mySvg)
    end)
end
addEventHandler("onClientResourceStart", resourceRoot, init)

-- Adiciona um nó reto ao SVG com cor, tamanho e posição aleatórios
function addSVGRectNode(svg)
    -- Obter o documento XML do nosso SVG
    local svgXML = svgGetDocumentXML(svg)
    
    -- Adicione um nó SVG reto, posicionado no centro do documento
    local rect = xmlCreateChild(svgXML, "rect")

    local size = math.random(0, 50)
    local r, g, b = math.random(10, 99), math.random(10, 99), math.random(10, 99)

    xmlNodeSetAttribute(rect, "x", (size / 2) .. "%")
    xmlNodeSetAttribute(rect, "y", (size / 2) .. "%")
    xmlNodeSetAttribute(rect, "width", size .. "%")
    xmlNodeSetAttribute(rect, "height", size .. "%")
    xmlNodeSetAttribute(rect, "fill", "#" .. r .. g .. b)
    
    -- Aplique nosso XML ao SVG e comece a desenhar via callback
    svgSetDocumentXML(svg, svgXML)
end

Requisitos

Minimum server version n/a
Minimum client version 1.5.8-9.20979

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version client="1.5.8-9.20979" />

Veja também