Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local GUIs = {};
- local currentOpenedGUI = nil;
- local customOpenedGUI = nil;
- function attachGUI (entityName, options)
- GUIs[entityName] = options;
- end
- -- Returns a entity ID from name and position
- function getEntityID(entity)
- return entity.name .. "["..entity.position.x..","..entity.position.y.."]";
- end
- function checkAndSpawnGUI (entity)
- if (entity ~= nil) then
- local options = GUIs[entity.name];
- if (options ~= nil) then
- local entityID = getEntityID(entity)
- local newEntity = { name= game.player.opened.name, position= game.player.opened.position};
- -- TODO: Set variables on new entity so it appears to be the same entity...
- game.player.gui.center.add{type="frame", name=entityID, caption=options.title, direction= "vertical"};
- game.player.gui.center[entityID].add{type="flow", direction="horizontal", name="guiContent" };
- game.player.gui.center[entityID].add{type="flow", direction="horizontal", name="titlebar" };
- game.player.gui.center[entityID].add{type="button", name="close", caption="Close" };
- options["onopen"](game.player.gui.center[entityID]["guiContent"], newEntity);
- customOpenedGUI = {opts=options, guiName=entityID, entity= newEntity};
- game.player.opened.destroy();
- game.createentity(newEntity);
- end
- end
- end
- function recurseFind (GUI, name)
- if (GUI.name == name) then
- return true;
- else
- if (GUI.childrennames ~= nil) then
- for k,v in pairs(GUI.childrennames) do
- if (recurseFind(v, name) == true) then
- return true;
- end
- end
- end
- end
- return false;
- end
- game.onload(function()
- game.onevent(defines.events.ontick, function ()
- if (game.player.opened ~= nil and currentOpenedGUI ~= getEntityID(game.player.opened)) then
- currentOpenedGUI = getEntityID(game.player.opened);
- checkAndSpawnGUI(game.player.opened);
- end
- end);
- game.onevent(defines.events.onguiclick, function (event)
- local element = event.element;
- if customOpenedGUI ~= nil then
- local customGUIWindow = game.player.gui.center[customOpenedGUI.guiName];
- if (element.name == "close") then
- customOpenedGUI.opts.onclose(customGUIWindow["guiContent"], customOpenedGUI.entity);
- customGUIWindow.destroy();
- customGUIWindow = nil;
- currentOpenedGUI = nil;
- else
- local isPartOfCustomGUI = recurseFind(customGUIWindow, element.name);
- if (isPartOfCustomGUI) then
- customOpenedGUI.opts.onclick(element, customOpenedGUI.entity);
- end
- end
- end
- end);
- end);
- attachGUI("stone-furnace", { title = "Stone Furnace",
- onopen = function (gui, entity)
- end,
- onclose = function (gui, entity)
- end,
- onclick = function (guiElement, entity)
- end});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement