Advertisement
gotlag

Rename Radar control.lua

Jul 9th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.29 KB | None | 0 0
  1. script.on_init(function()
  2.     for _, player in pairs(game.players) do
  3.         gui_initialize(player)
  4.     end
  5. end)
  6.  
  7. script.on_event(defines.events.on_player_created, function(event)
  8.     gui_initialize(game.players[event.player_index])
  9. end)
  10.  
  11. script.on_event(defines.events.on_gui_click, function(event)
  12.     if event.element.name == "rt_minimize_button" then
  13.         gui_functions(game.players[event.player_index])
  14.         --gui_initialize(game.players[event.player_index])
  15.     elseif event.element.name == "train_rename_button" then
  16.         train_rename(game.players[event.player_index])
  17.     end
  18. end)
  19.  
  20. script.on_event("train-rename", function(event)
  21.   if game.players[event.player_index].selected then
  22.     train_rename(game.players[event.player_index])
  23.   end
  24. end)
  25.  
  26. --create the minimize button when the game starts or when joining a server
  27. function gui_initialize(player)
  28.     if player.gui.top.rt_minimize_button then
  29.         player.gui.top.rt_minimize_button.destroy()
  30.     end
  31.     if (not player.gui.top.rt_minimize_button) then
  32.         player.gui.top.add{type = "button", name = "rt_minimize_button", caption = "RT", style = "rt_minimize_button_style"}
  33.     end
  34. end
  35.  
  36. --minimize or maximize the rename GUI
  37. function gui_functions(player)
  38.     if player.gui.left.train_rename_frame then
  39.         player.gui.left.train_rename_frame.destroy()
  40.     else
  41.         frame = player.gui.left.add{type ="frame", name ="train_rename_frame"}
  42.         frame.add{type = "label", name = "train_rename_label", caption = "Train name:"}
  43.         frame.add{type = "textfield", name = "train_rename_textbox"}
  44.         frame.add{type = "button", name = "train_rename_button", caption = "Set"}
  45.     end
  46. end
  47.  
  48. --rename train or building depending on open gui
  49. function train_rename(player)
  50.     -- local entity_with_open_gui = player.opened
  51.     local entity_with_open_gui = player.selected
  52.   if player.gui.left.train_rename_frame then
  53.     local new_name = player.gui.left.train_rename_frame.train_rename_textbox.text
  54.    
  55.     if entity_with_open_gui == nil then
  56.       player.print("You must open a locomotive or cargo wagon then press this button.")
  57.      
  58.     --rename all locomotives in a train if a locomotive or cargo wagon is open and display a confirmation message
  59.     elseif (entity_with_open_gui.type == "locomotive") or (entity_with_open_gui.type == "cargo-wagon") then
  60.       for _, renameme in pairs(entity_with_open_gui.train.locomotives.front_movers) do
  61.         renameme.backer_name = new_name
  62.       end
  63.       for _, renameme in pairs(entity_with_open_gui.train.locomotives.back_movers) do
  64.         renameme.backer_name = new_name
  65.       end
  66.       player.print("All locomotives in this train were renamed: " .. new_name)
  67.      
  68.     --rename assemling machine, furnace, rocket silo, lab, train stop or roboport if any is open and display a confirmation message
  69.     elseif (entity_with_open_gui.type == "assembling-machine") or (entity_with_open_gui.type == "furnace") or (entity_with_open_gui.type == "rocket-silo") or (entity_with_open_gui.type == "radar") or (entity_with_open_gui.type == "lab") or (entity_with_open_gui.type == "train-stop") or (entity_with_open_gui.type == "roboport") then
  70.       entity_with_open_gui.backer_name = new_name
  71.       player.print("Building renamed: " .. new_name)
  72.      
  73.     else
  74.       player.print("You must open a locomotive or cargo wagon then press this button.")
  75.      
  76.     end
  77.   end
  78. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement