Big cleanup pass

This commit is contained in:
Openarl
2016-05-05 22:18:03 +10:00
parent fac48d2503
commit b5c12aad98
15 changed files with 1411 additions and 1347 deletions

View File

@@ -17,19 +17,138 @@ LoadModule("Classes/PassiveTree", launch)
LoadModule("Classes/PassiveSpec", launch)
LoadModule("Classes/PassiveTreeView", launch)
local cfg = { }
local main = { }
main.tooltipLines = { }
function main:Init()
self.modes = { }
self.modes["LIST"] = LoadModule("Modules/BuildList", launch, self)
self.modes["BUILD"] = LoadModule("Modules/Build", launch, self)
self.tree = common.New("PassiveTree")
self.inputEvents = { }
self.tooltipLines = { }
self:SetMode("LIST")
self:LoadSettings()
end
function main:Shutdown()
self:CallMode("Shutdown")
self:SaveSettings()
end
function main:OnFrame()
self.screenW, self.screenH = GetScreenSize()
if self.newMode then
if self.mode then
self:CallMode("Shutdown")
end
self.mode = self.newMode
self.modeArgs = self.newModeArgs
self.newMode = nil
self:CallMode("Init", unpack(self.modeArgs))
end
self:CallMode("OnFrame", self.inputEvents)
wipeTable(self.inputEvents)
end
function main:OnKeyDown(key, doubleClick)
t_insert(self.inputEvents, { type = "KeyDown", key = key, doubleClick = doubleClick })
end
function main:OnKeyUp(key)
t_insert(self.inputEvents, { type = "KeyUp", key = key })
end
function main:OnChar(key)
t_insert(self.inputEvents, { type = "Char", key = key })
end
function main:SetMode(newMode, ...)
self.newMode = newMode
self.newModeArgs = {...}
end
function main:CallMode(func, ...)
local modeTbl = self.modes[self.mode]
if modeTbl[func] then
modeTbl[func](modeTbl, ...)
end
end
function main:LoadSettings()
local setXML, errMsg = common.xml.LoadXMLFile("Settings.xml")
if not setXML then
return true
elseif setXML[1].elem ~= "PathOfBuilding" then
launch:ShowErrMsg("^1Error parsing 'Settings.xml': 'PathOfBuilding' root element missing")
return true
end
for _, node in ipairs(setXML[1]) do
if type(node) == "table" then
if node.elem == "Mode" then
if not node.attrib.mode or not self.modes[node.attrib.mode] then
launch:ShowErrMsg("^1Error parsing 'Settings.xml': Invalid mode attribute in 'Mode' element")
return true
end
local args = { }
for _, child in ipairs(node) do
if type(child) == "table" then
if child.elem == "Arg" then
if child.attrib.number then
t_insert(args, tonumber(child.attrib.number))
elseif child.attrib.string then
t_insert(args, child.attrib.string)
elseif child.attrib.boolean then
t_insert(args, child.attrib.boolean == "true")
end
end
end
end
self:SetMode(node.attrib.mode, unpack(args))
end
end
end
end
function main:SaveSettings()
local setXML = { elem = "PathOfBuilding" }
local mode = { elem = "Mode", attrib = { mode = self.mode } }
for _, val in ipairs(self.modeArgs) do
local child = { elem = "Arg", attrib = {} }
if type(val) == "number" then
child.attrib.number = tostring(val)
elseif type(val) == "boolean" then
child.attrib.boolean = tostring(val)
else
child.attrib.string = tostring(val)
end
t_insert(mode, child)
end
t_insert(setXML, mode)
local res, errMsg = common.xml.SaveXMLFile(setXML, "Settings.xml")
if not res then
launch:ShowErrMsg("Error saving 'Settings.xml': %s", errMsg)
return true
end
end
function main:AddTooltipLine(size, text)
for line in string.gmatch(text .. "\n", "([^\n]*)\n") do
t_insert(self.tooltipLines, { size = size, text = line })
end
end
function main:AddTooltipSeperator(size)
t_insert(self.tooltipLines, { size = size })
end
function main:DrawTooltip(x, y, w, h, viewPort, col, center)
local ttW, ttH = 0, 0
for _, data in ipairs(self.tooltipLines) do
@@ -87,122 +206,4 @@ function main:DrawTooltip(x, y, w, h, viewPort, col, center)
end
end
main.modes = { }
main.modes["LIST"] = LoadModule("Modules/BuildList", launch, cfg, main)
main.modes["BUILD"] = LoadModule("Modules/Build", launch, cfg, main)
function main:SetMode(newMode, ...)
self.newMode = newMode
self.newModeArgs = {...}
end
function main:CallMode(func, ...)
local modeTbl = self.modes[self.mode]
if modeTbl[func] then
modeTbl[func](modeTbl, ...)
end
end
function main:LoadSettings()
local setXML, errMsg = common.xml.LoadXMLFile("Settings.xml")
if not setXML then
return true
elseif setXML[1].elem ~= "PathOfBuilding" then
launch:ShowErrMsg("^1Error parsing 'Settings.xml': 'PathOfBuilding' root element missing")
return true
end
for _, node in ipairs(setXML[1]) do
if type(node) == "table" then
if node.elem == "Mode" then
if not node.attrib.mode or not self.modes[node.attrib.mode] then
launch:ShowErrMsg("^1Error parsing 'Settings.xml': Invalid mode attribute in 'Mode' element")
return true
end
local args = { }
for _, child in ipairs(node) do
if type(child) == "table" then
if child.elem == "Arg" then
if child.attrib.number then
t_insert(args, tonumber(child.attrib.number))
elseif child.attrib.string then
t_insert(args, child.attrib.string)
elseif child.attrib.boolean then
t_insert(args, child.attrib.boolean == "true")
end
end
end
end
self:SetMode(node.attrib.mode, unpack(args))
end
end
end
end
function main:SaveSettings()
local setXML = { elem = "PathOfBuilding" }
local mode = { elem = "Mode", attrib = { mode = self.mode } }
for _, val in ipairs(self.modeArgs) do
local child = { elem = "Arg", attrib = {} }
if type(val) == "number" then
child.attrib.number = tostring(val)
elseif type(val) == "boolean" then
child.attrib.boolean = tostring(val)
else
child.attrib.string = tostring(val)
end
t_insert(mode, child)
end
t_insert(setXML, mode)
local res, errMsg = common.xml.SaveXMLFile(setXML, "Settings.xml")
if not res then
launch:ShowErrMsg("Error saving 'Settings.xml': %s", errMsg)
return true
end
end
function main:OnFrame()
cfg.screenW, cfg.screenH = GetScreenSize()
if self.newMode then
if self.mode then
self:CallMode("Shutdown")
end
self.mode = self.newMode
self.modeArgs = self.newModeArgs
self.newMode = nil
self:CallMode("Init", unpack(self.modeArgs))
end
self:CallMode("OnFrame", self.inputEvents)
wipeTable(self.inputEvents)
end
function main:OnKeyDown(key, doubleClick)
t_insert(self.inputEvents, { type = "KeyDown", key = key, doubleClick = doubleClick })
end
function main:OnKeyUp(key)
t_insert(self.inputEvents, { type = "KeyUp", key = key })
end
function main:OnChar(key)
t_insert(self.inputEvents, { type = "Char", key = key })
end
function main:Init()
self.inputEvents = { }
self.tree = common.New("PassiveTree")
self:SetMode("LIST")
self:LoadSettings()
end
function main:Shutdown()
self:CallMode("Shutdown")
self:SaveSettings()
end
return main