Jump to content

Help Manager


CapY

Recommended Posts

local rootElement = getRootElement() 
local thisResourceRoot = getResourceRootElement(getThisResource()) 
local pagesXml 
  
local wndHelp, wndBlock, tPanel, btnClose 
local tab = {} 
local memo = {} 
local popupQueue = {} 
local Grid = {} 
  
local HELP_KEY = "F9" 
local HELP_COMMAND = "gamehelp" 
local POPUP_TIMEOUT = 15000 --ms 
local FADE_DELTA = 10 --alpha per frame 
local MAX_ALPHA = 10 
  
  
addEvent("doShowHelp", true) 
addEvent("doHideHelp", true) 
addEvent("onHelpShown") 
addEvent("onHelpHidden") 
  
addEventHandler("onClientResourceStart", thisResourceRoot, 
    function () 
        wndHelp  = guiCreateWindow(.2, .2, .6, .6, "Help", true) 
        wndBlock = guiCreateWindow(0, 0, 1, 1, "", true) 
        tPanel   = guiCreateGridList(0.2086 ,0.1629, 0.6000, 0.6000, true, wndHelp)           
        btnClose = guiCreateButton(.4, .92, .2, .08, "Close", true, wndHelp) 
  
        guiSetVisible(wndHelp, false) 
        guiSetVisible(wndBlock, false) 
        
        guiWindowSetSizable(wndHelp, false) 
        guiSetAlpha(wndBlock, 0) 
        
        addEventHandler("onClientGUIClick", btnClose, 
            function() 
                if source == this then 
                    clientToggleHelp(false) 
                end 
            end 
        ) 
        
        pagesXml = xmlLoadFile("seen.xml") 
        if not pagesXml then 
            pagesXml = xmlCreateFile("seen.xml", "seen") 
        end 
        
        for i, resourceRoot in ipairs(getElementsByType("resource")) do --!w 
            local resource = getResourceFromName(getElementID(resourceRoot)) 
            if resource then 
                addHelpTabFromXML(resource) 
            end 
        end 
        
        addCommandHandler(HELP_COMMAND, clientToggleHelp) 
        bindKey(HELP_KEY, "down", clientToggleHelp) 
    end 
) 
  
addEventHandler("onClientResourceStop", thisResourceRoot, 
    function() 
        showCursor(false) 
    end 
) 
  
-- exports 
function showHelp() 
    return clientToggleHelp(true) 
end 
addEventHandler("doShowHelp", rootElement, showHelp) 
  
function hideHelp() 
    return clientToggleHelp(false) 
end 
addEventHandler("doHideHelp", rootElement, hideHelp) 
  
function addHelpTab(resource, showPopup) 
  
    if showPopup == nil then 
        showPopup = true 
    end 
    
    -- block duplicates 
    if tab[resource] then 
        return false 
    end 
    
    local tabtext = getResourceName(resource) 
    
    local helpnode = getResourceConfig(":" .. getResourceName(resource) .. "/help.xml") 
    
    if helpnode then 
    
        local nameattribute = xmlNodeGetAttribute(helpnode, "title"); 
        
        if nameattribute then 
            tabtext = nameattribute; 
        end 
        
    end 
  
    tab[resource] = guiCreateGridList( tabtext , tPanel) 
    
    if showPopup then 
        addHelpPopup(resource) 
    end 
    
    return tab[resource] 
end 
  
function removeHelpTab(resource) 
    if not tab[resource] then 
        return false 
    end 
    
    if memo[resource] then 
        destroyElement(memo[resource]) 
        memo[resource] = nil 
    end 
    
    guiGridListClear(tab[resource], tPanel) 
    tab[resource] = nil 
    
    return true 
end 
addEventHandler("onClientResourceStop", rootElement, removeHelpTab) 
  
--private 
function addHelpTabFromXML(resource) 
    -- block duplicates 
    if tab[resource] then 
        return false 
    end 
        
    local helpnode = getResourceConfig(":"..getResourceName(resource).."/help.xml") 
    if helpnode then 
        local helptext = xmlNodeGetValue(helpnode) 
        local showPopup = not (xmlNodeGetAttribute(helpnode, "popup") == "no") 
        if helptext then 
            guiGridListAddRow(resource, showPopup) 
            memo[resource] = guiCreateMemo(.01800, .00040, .08080, .09960, helptext, true, tab[resource]) 
            guiMemoSetReadOnly(memo[resource], true) 
        end 
    end 
end 
addEventHandler("onClientResourceStart", rootElement, addHelpTabFromXML) 
  
function clientToggleHelp(state) 
    if state ~= true and state ~= false then 
        state = not guiGetVisible(wndHelp) 
    end 
    guiSetVisible(wndHelp, state) 
    guiSetVisible(wndBlock, state) 
    if state == true then 
        triggerEvent("onHelpShown", localPlayer) 
        guiBringToFront(wndBlock) 
        guiBringToFront(wndHelp) 
        showCursor(true) 
    else 
        triggerEvent("onHelpHidden", localPlayer) 
        showCursor(false) 
    end 
    return true 
end 
  
local function fadeIn(wnd) 
    local function raiseAlpha() 
        local newAlpha = guiGetAlpha(wnd) + FADE_DELTA 
        if newAlpha <= MAX_ALPHA then 
            guiSetAlpha(wnd, newAlpha) 
        else 
            removeEventHandler("onClientRender", rootElement, raiseAlpha) 
        end 
    end 
    addEventHandler("onClientRender", rootElement, raiseAlpha) 
end 
  
local function fadeOut(wnd) 
    local function lowerAlpha() 
        local newAlpha = guiGetAlpha(wnd) - FADE_DELTA 
        if newAlpha >= 0 then 
            guiSetAlpha(wnd, newAlpha) 
        else 
            removeEventHandler("onClientRender", rootElement, lowerAlpha) 
            destroyElement(wnd) 
            
            table.remove(popupQueue, 1) 
            if #popupQueue > 0 then 
                showHelpPopup(popupQueue[1]) 
            end 
        end 
    end 
    addEventHandler("onClientRender", rootElement, lowerAlpha) 
end 
  
function addHelpPopup(resource) 
    local xmlContents = xmlNodeGetValue(pagesXml) 
    local seenPages = split(xmlContents, string.byte(',')) 
    local resourceName = getResourceName(resource) 
    for i, page in ipairs(seenPages) do 
        if page == resourceName then 
            return 
        end 
    end 
    xmlNodeSetValue(pagesXml, xmlContents..resourceName..",") 
    xmlSaveFile(pagesXml) 
  
    table.insert(popupQueue, resource) 
    if #popupQueue == 1 then 
        showHelpPopup(resource) 
    end 
end 
  
function showHelpPopup(resource) 
    local screenX, screenY = guiGetScreenSize() 
    local wndPopup = guiCreateWindow(0, screenY - 20, screenX, 0, '', false) --350 
    
    local restitle = getResourceName(resource) 
    local helpnode = getResourceConfig(":" .. getResourceName(resource) .. "/help.xml") 
    
    if helpnode then 
    
        local nameattribute = xmlNodeGetAttribute(helpnode, "title"); 
        
        if nameattribute then 
            restitle = nameattribute; 
        end 
        
    end 
    
    local text = 
        "Help page available for ".. restitle .."! ".. 
        "Press "..HELP_KEY.." or type /"..HELP_COMMAND.." to read it." 
        
    guiSetText(wndPopup, text) 
    guiSetAlpha(wndPopup, 0) 
    guiWindowSetMovable(wndPopup, false) 
    guiWindowSetSizable(wndPopup, false) 
    
    fadeIn(wndPopup) 
    setTimer(fadeOut, POPUP_TIMEOUT, 1, wndPopup) 
end 
  

You forgot to set the gridlist as a child of the main window. I haven't tested this - but try it.

Also, before I get hate mail - I don't care.

Link to comment

How about you stop pointing fingers at each other?

1 - You are BOTH noobs. CapY can't manage to fix a SINGLE argument in his script - it was so painfully obvious. The rest of you are noobs for starting a flame war in the scripting forum for little reason. Take the shit elsewhere.

2 - The script itself sucks.

If ya'll are going to argue about something like this, do it in private. This is a place to ask questions, get answers, and have general discussion about SCRIPTING.

Link to comment
And Qaisjp why you are hiding your name ?

Let the people know for such an asshole : http://imageshack.us/f/820/qaisjpnoob.png/

And this is not good place for a fight , we can get banned .

Qais is hiding his name because he has a right not to release his own personal information, much like you have a right not to have yours released. You can both get banned illegal activies as you have both now violated the Data Protection Act by releasing the names of eachother and/or eachothers facebook accounts, CapY is the worse one off because he did it in retaliation to Qais even though you're both as bad as eachother.

CapY, you didn't even have the decency to hide people's names in that screenshot who have nothing to do with this, if they chose to report that then good luck with that one.

Link to comment
How about you stop pointing fingers at each other?

1 - You are BOTH noobs. CapY can't manage to fix a SINGLE argument in his script - it was so painfully obvious. The rest of you are noobs for starting a flame war in the scripting forum for little reason. Take the :~ elsewhere.

2 - The script itself sucks.

If ya'll are going to argue about something like this, do it in private. This is a place to ask questions, get answers, and have general discussion about SCRIPTING.

1. I am a noob. I admit :)

2. True.

3. haha, i didn't start it..

Link to comment

CapY, instead of asking/spamming the forums asking people to help, why don't you try and figure it out yourself for once. Sit there and think about why it's going wrong, use the wiki for assistance. Try and figure something out on your own for once, if you keep relying on the community you won't learn.

Link to comment
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...