Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.73 KB | None | 0 0
  1.  
  2. local GUIs = {};
  3. local currentOpenedGUI = nil;
  4. local customOpenedGUI = nil;
  5.  
  6. function attachGUI (entityName, options)
  7.     GUIs[entityName] = options;
  8. end
  9.  
  10. -- Returns a entity ID from name and position
  11. function getEntityID(entity)
  12.     return entity.name .. "["..entity.position.x..","..entity.position.y.."]";
  13. end
  14.  
  15. function checkAndSpawnGUI (entity)
  16.     if (entity ~= nil) then
  17.         local options = GUIs[entity.name];
  18.         if (options ~= nil) then
  19.             local entityID = getEntityID(entity)
  20.             local newEntity = { name= game.player.opened.name, position= game.player.opened.position};
  21.  
  22.             -- TODO: Set variables on new entity so it appears to be the same entity...
  23.  
  24.             game.player.gui.center.add{type="frame", name=entityID, caption=options.title, direction= "vertical"};
  25.  
  26.             game.player.gui.center[entityID].add{type="flow", direction="horizontal", name="guiContent" };
  27.  
  28.  
  29.             game.player.gui.center[entityID].add{type="flow", direction="horizontal", name="titlebar" };
  30.  
  31.             game.player.gui.center[entityID].add{type="button", name="close", caption="Close" };
  32.  
  33.             options["onopen"](game.player.gui.center[entityID]["guiContent"], newEntity);
  34.            
  35.             customOpenedGUI = {opts=options, guiName=entityID, entity= newEntity};
  36.  
  37.             game.player.opened.destroy();
  38.             game.createentity(newEntity);
  39.         end
  40.     end
  41. end
  42.  
  43. function recurseFind (GUI, name)
  44.     if (GUI.name == name) then
  45.      return true;
  46.     else
  47.         if (GUI.childrennames ~= nil) then
  48.             for k,v in pairs(GUI.childrennames) do
  49.                 if (recurseFind(v, name) == true) then
  50.                     return true;
  51.                 end
  52.             end
  53.         end
  54.     end
  55.     return false;
  56. end
  57.  
  58. game.onload(function()
  59.     game.onevent(defines.events.ontick, function ()
  60.         if (game.player.opened ~= nil and currentOpenedGUI ~= getEntityID(game.player.opened)) then
  61.             currentOpenedGUI = getEntityID(game.player.opened);
  62.             checkAndSpawnGUI(game.player.opened);
  63.         end
  64.     end);
  65.     game.onevent(defines.events.onguiclick, function (event)
  66.         local element = event.element;
  67.         if customOpenedGUI ~= nil then
  68.             local customGUIWindow = game.player.gui.center[customOpenedGUI.guiName];
  69.             if (element.name == "close") then
  70.                 customOpenedGUI.opts.onclose(customGUIWindow["guiContent"], customOpenedGUI.entity);
  71.                 customGUIWindow.destroy();
  72.                 customGUIWindow = nil;
  73.                 currentOpenedGUI = nil;
  74.             else
  75.                 local isPartOfCustomGUI = recurseFind(customGUIWindow, element.name);
  76.                 if (isPartOfCustomGUI) then
  77.                     customOpenedGUI.opts.onclick(element, customOpenedGUI.entity);
  78.                 end
  79.             end
  80.         end
  81.     end);
  82. end);
  83.  
  84.  
  85.  
  86. attachGUI("stone-furnace", { title = "Stone Furnace",
  87.                              onopen = function (gui, entity)
  88.  
  89.                              end,
  90.                              onclose = function (gui, entity)
  91.  
  92.                              end,
  93.                              onclick = function (guiElement, entity)
  94.  
  95.                              end});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement