Skip to content

Commit

Permalink
Add save route button in export frame + fix export string format
Browse files Browse the repository at this point in the history
  • Loading branch information
Neogeekmo committed Apr 17, 2024
1 parent a13ec4a commit a7dd2fc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
21 changes: 20 additions & 1 deletion frames/exportRoute.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ function AprRC.export:Show()
dropdown:SetFullWidth(true)
local routeList = {}
local defaultIndex = nil
local selectedRouteName = ''
for index, route in ipairs(AprRCData.Routes) do
if index == 1 then
selectedRouteName = route.name
end
routeList[index] = route.name
if AprRCData.CurrentRoute and route.name == AprRCData.CurrentRoute.name then
defaultIndex = index
selectedRouteName = route.name
end
end
dropdown:SetList(routeList)
Expand Down Expand Up @@ -49,6 +54,20 @@ function AprRC.export:Show()
end)
frame:AddChild(btnExportELT)

local btnSave = AceGUI:Create("Button")
btnSave:SetText("Save Route")
btnSave:SetWidth(200)
btnSave:SetCallback("OnClick", function()
local routeText = editbox:GetText()
local newStepRouteTable = AprRC:stringToTable(routeText)
local newRoute = { name = selectedRouteName, steps = newStepRouteTable }
AprRC:UpdateRouteByName(selectedRouteName, newRoute)
if AprRCData.CurrentRoute.name == selectedRouteName then
AprRCData.CurrentRoute = newRoute
end
end)
frame:AddChild(btnSave)

-- local btnDelete = AceGUI:Create("Button")
-- btnDelete:SetText("Delete this route")
-- btnDelete:SetWidth(200)
Expand All @@ -68,9 +87,9 @@ function AprRC.export:Show()

dropdown:SetCallback("OnValueChanged", function(widget, event, index)
local selectedRoute = AprRCData.Routes[index]
-- defaultIndex = index
if selectedRoute then
editbox:SetText(AprRC:RouteToString(selectedRoute.steps))
selectedRouteName = selectedRoute.name
end
end)
end
25 changes: 23 additions & 2 deletions helper/Utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ local function qpartTableToString(tbl, level)
local itemIndent = string.rep(" ", level + 1)

for k, v in pairs(tbl) do
local keyStr = k .. " = "
local keyStr = ''
if k == "Button" then
keyStr = '["' .. tostring(k) .. '"] = '
else
keyStr = "[" .. k .. "] = "
end
if type(v) == "table" then
str = str .. itemIndent .. keyStr .. AprRC:RouteToString(v, level + 1) .. ",\n"
else
Expand Down Expand Up @@ -120,7 +125,7 @@ function AprRC:RouteToString(tbl, level)
str = str .. itemIndent .. keyStr .. "{}" .. ",\n"
else
local valueStr
if k == "Qpart" or k == "Fillers" then
if k == "Qpart" or k == "Fillers" or k == "Button" then
valueStr = qpartTableToString(v, level + 1)
else
valueStr = self:RouteToString(v, level + 1)
Expand All @@ -136,3 +141,19 @@ function AprRC:RouteToString(tbl, level)
str = str .. indent .. "}"
return str
end

function AprRC:stringToTable(str)
local cleanedStr = str:gsub("[%s\n\r\t]+", "")

local func, err = loadstring("return " .. cleanedStr)
if not func then
AprRC:Debug("Error when converting the string to a table", err)
else
local success, tableResult = pcall(func)
if success then
return tableResult
else
AprRC:Debug("Error when executing the string converted to a table", tableResult)
end
end
end

0 comments on commit a7dd2fc

Please sign in to comment.