Skip to content

Commit

Permalink
changed storing of profiles:
Browse files Browse the repository at this point in the history
new button "Save as", allows overwriting and applies changes, closes gui
restoring a profile, applies changes and closes gui
  • Loading branch information
Choumiko committed Sep 26, 2015
1 parent 41993ef commit 14ade39
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 5 deletions.
3 changes: 3 additions & 0 deletions control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,12 @@ game.on_event(defines.events.on_gui_click, function(event)
for _,k in pairs(global.removeTicks) do
c = c+#k
end
debugDump("#config "..#global.config[player.name],true)
debugDump("#Remove "..c,true)
elseif element.name == "module-inserter-storage-store" then
gui_store(player)
elseif element.name == "module-inserter-save-as" then
gui_save_as(player)
else
event.element.name:match("(%w+)__([%w%s%-%#%!%$]*)_*([%w%s%-%#%!%$]*)_*(%w*)")
local type, index, slot = string.match(element.name, "module%-inserter%-(%a+)%-(%d+)%-*(%d*)")
Expand Down
58 changes: 54 additions & 4 deletions gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function gui_open_frame(player)
else
global["config-tmp"][player.name][i] = {
from = global["config"][player.name][i].from,
to = global["config"][player.name][i].to
to = util.table.deepcopy(global["config"][player.name][i].to)
}
end
end
Expand Down Expand Up @@ -247,7 +247,20 @@ function gui_open_frame(player)
name = "module-inserter-debug",
caption = "D"
}
else
button_grid.add{type="label", caption=""}
end
button_grid.add{
type = "button",
name = "module-inserter-save-as",
caption = {"module-inserter-config-button-save-as"}
}

local saveText = button_grid.add{
type = "textfield",
name = "module-inserter-save-as-text"
}
saveText.text = global.config[player.name].loaded or ""

storage_frame = player.gui.left.add{
type = "frame",
Expand Down Expand Up @@ -313,7 +326,7 @@ function gui_open_frame(player)
end
end

function gui_save_changes(player)
function gui_save_changes(player, name)
-- Saving changes consists in:
-- 1. copying config-tmp to config
-- 2. removing config-tmp
Expand All @@ -330,12 +343,13 @@ function gui_save_changes(player)
else
global["config"][player.name][i] = {
from = global["config-tmp"][player.name][i].from,
to = global["config-tmp"][player.name][i].to
to = util.table.deepcopy(global["config-tmp"][player.name][i].to)
}
end
end
global["config-tmp"][player.name] = nil
end
global.config[player.name].loaded = name or nil
--saveVar(global, "saved")
local frame = player.gui.left["module-inserter-config-frame"]
local storage_frame = player.gui.left["module-inserter-storage-frame"]
Expand All @@ -353,6 +367,10 @@ function gui_clear_all(player)
local frame = player.gui.left["module-inserter-config-frame"]
if not frame then return end
local ruleset_grid = frame["module-inserter-ruleset-grid"]
global.config[player.name].loaded = nil
local frame = player.gui.left["module-inserter-config-frame"]
frame["module-inserter-button-grid"]["module-inserter-save-as-text"].text = ""

for i = 1, MAX_CONFIG_SIZE do
global["config-tmp"][player.name][i] = { from = "", to = {} }
ruleset_grid["module-inserter-from-" .. i].style = "mi-icon-style"
Expand Down Expand Up @@ -536,6 +554,37 @@ function gui_store(player)
--saveVar(global, "stored")
end

function gui_save_as(player)
global["storage"][player.name] = global["storage"][player.name] or {}
local storage_frame = player.gui.left["module-inserter-storage-frame"]
local frame = player.gui.left["module-inserter-config-frame"]

if not storage_frame or not frame then return end
local textfield = frame["module-inserter-button-grid"]["module-inserter-save-as-text"]
local name = textfield.text
name = string.match(name, "^%s*(.-)%s*$")

if not name or name == "" then
gui_display_message(frame, true, "module-inserter-storage-name-not-set")
return
end
local index = count_keys(global["storage"][player.name]) + 1
if not global["storage"][player.name][name] and index > MAX_STORAGE_SIZE then
gui_display_message(frame, false, "module-inserter-storage-too-long")
return
end

global["storage"][player.name][name] = {}
local i = 0
for i = 1, #global["config-tmp"][player.name] do
global["storage"][player.name][name][i] = {
from = global["config-tmp"][player.name][i].from,
to = util.table.deepcopy(global["config-tmp"][player.name][i].to)
}
end
gui_save_changes(player, name)
end

function gui_restore(player, index)
local frame = player.gui.left["module-inserter-config-frame"]
local storage_frame = player.gui.left["module-inserter-storage-frame"]
Expand All @@ -557,7 +606,7 @@ function gui_restore(player, index)
else
global["config-tmp"][player.name][i] = {
from = global["storage"][player.name][name][i].from,
to = global["storage"][player.name][name][i].to
to = util.table.deepcopy(global["storage"][player.name][name][i].to)
}
end
local style = global["config-tmp"][player.name][i].from ~= "" and "mi-icon-"..global["config-tmp"][player.name][i].from or "mi-icon-style"
Expand All @@ -566,6 +615,7 @@ function gui_restore(player, index)
gui_update_modules(player, i)
end
gui_display_message(storage_frame, true, "---")
gui_save_changes(player, name)
end

function gui_remove(player, index)
Expand Down
1 change: 1 addition & 0 deletions locale/de/de.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module-inserter-config-header-1=Maschine:
module-inserter-config-header-2=Module:
module-inserter-config-button-clear=löschen
module-inserter-config-button-apply=Änderung speichern
module-inserter-config-button-save-as=Speichere als
module-inserter-config-button-clear-all=alles löschen
module-inserter-storage-frame-title=Einstellungen speichern / wiederherstellen
module-inserter-storage-name-label=Name
Expand Down
3 changes: 2 additions & 1 deletion locale/en/en.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module-inserter-config-frame-title=Module insertion configuration
module-inserter-config-header-1=Entity:
module-inserter-config-header-2=Modules:
module-inserter-config-button-apply=Save changes
module-inserter-config-button-save-as=Save as
module-inserter-config-button-clear-all=Clear all
module-inserter-storage-frame-title=Store / restore ruleset
module-inserter-storage-frame-title=Store / restore profile
module-inserter-storage-name-label=Name
module-inserter-storage-store=store
module-inserter-storage-restore=restore
Expand Down

0 comments on commit 14ade39

Please sign in to comment.