Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 5459


Ignore:
Timestamp:
Apr 13, 2009, 5:02:50 PM (15 years ago)
Author:
bknecht
Message:

fixed problem with GUI in Lua. You should now be able to easily load, show and hide GUIs you created by yourself

File:
1 edited

Legend:

Unmodified
Added
Removed
  • data/media/gui/scripts/loadGUI_2.lua

    r5446 r5459  
    1414local current = nil
    1515local loadedGUIs = {}
    16 local showing
     16-- a bit more complex: GUI is now a class
     17GUI = {}
     18GUI.__index = GUI
     19
     20-- hide function for the GUI
     21GUI.hide = function (self)
     22    self.window:hide()
     23end
     24
     25-- show function for the GUI
     26GUI.show = function (self)
     27    self.window:show()
     28end
     29
     30-- constructor of the GUI
     31GUI.new = function (gui, fname)
     32    local newElement = { window = gui, filename = fname }
     33    setmetatable(newElement, GUI) -- connects new element with class
     34    return newElement
     35end
    1736
    1837datapath = "" -- points to media-folder (set after loading the script)
    19 
    20 -- function to add a reference to list of reference of loaded GUIs
    21 loadedGUIs.addGUI = function (gui)
    22     loadedGUIs[#loadedGUIs+1] = gui
    23 end
    24 
    25 -- function which returns a GUI by name
    26 loadedGUIs.getGUIbyName = function (str)
    27     print("number of GUIs loaded: " .. #loadedGUIs)
    28     for i = 1, #loadedGUIs, 1 do
    29         print("Checking GUI " .. loadedGUIs[i].filename)
    30         if str == loadedGUIs[i].filename then
    31             return loadedGUIs[i]
    32         end
    33     end
    34     return nil
    35 end
    3638
    3739-- loads the GUI with the specified filename
     
    3941function loadGUI(filename)
    4042    -- check if it already exists
    41     print("about to load " .. filename)
    42     print("search for GUI " .. filename)
    43     newlyLoaded = loadedGUIs:getGUIbyName(filename)
    44     if newlyLoaded == nil then
     43    --gui = loadedGUIs:getGUIbyName(filename)
     44    gui = loadedGUIs[filename]
     45    if gui == nil then
    4546        dofile(datapath .. "gui/scripts/" .. filename .. ".lua")
    46         newlyLoaded = winMgr:loadWindowLayout(layoutPath)
    47         newlyLoaded.filename = filename
    48         loadedGUIs:addGUI(newlyLoaded)
     47        win = winMgr:loadWindowLayout(layoutPath)
     48        gui = GUI.new(win, filename)
     49        loadedGUIs[filename] = gui
    4950        -- if there has no GUI been loaded yet, set new GUI as current
    5051        if #loadedGUIs == 1 then
     
    5354        end
    5455        -- hide new GUI as we do not want to show it accidentially
    55         newlyLoaded:hide()
     56        gui:hide()
    5657    end
    57     return newlyLoaded
     58    return gui
    5859end
    5960
     
    6162-- be sure to set the global variable "filename" before calling this function
    6263function showGUI(filename)
    63     print("about to show " .. filename)
    6464    if current == nil or current.filename ~= filename then
    65         print("current not set")
    66         print("search for GUI " .. filename)
    67         current = loadedGUIs.getGUIbyName(filename)
     65        if current ~= nil then
     66        end
     67        current = loadedGUIs[filename]
    6868        if current == nil then
    6969            current = loadGUI(filename)
    7070        end
    71         system:setGUISheet(current)
     71        system:setGUISheet(current.window)
    7272    end
    73     print("Showing " .. filename)
    7473    current:show()
    7574    showing = true
     
    9897
    9998function hideGUI(filename)
    100     print("about to hide " .. filename)
    101     print("search for GUI " .. filename)
    102     current = loadedGUIs.getGUIbyName(filename)
    103     print("current is: " .. current)
     99    current = loadedGUIs[filename]
    104100    if current ~= nil then
    105         print("Hiding " .. filename)
    106101        current:hide()
    107102        showing = false
Note: See TracChangeset for help on using the changeset viewer.