From 1bb908ec2a2b69bd276eb18238bd1757ce5bdb3e Mon Sep 17 00:00:00 2001 From: scfmod Date: Mon, 3 Jan 2022 21:54:39 +0100 Subject: [PATCH] Added functionality for changing fillType in GUI --- classes/TerraFarm.lua | 36 ++++++++++----- classes/TerraFarmFillTypes.lua | 51 ++++++++++++++++++++++ classes/gui/TerraFarmSettingsMainFrame.lua | 15 +++++++ script.lua | 2 + xml/TerraFarmSettingsMainFrame.xml | 11 +++++ 5 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 classes/TerraFarmFillTypes.lua diff --git a/classes/TerraFarm.lua b/classes/TerraFarm.lua index 6fae372..bea812c 100644 --- a/classes/TerraFarm.lua +++ b/classes/TerraFarm.lua @@ -104,6 +104,8 @@ end function TerraFarm:onReady() self:registerCustomFillType() self:updateFillTypeData() + + TerraFarmFillTypes:init() end function TerraFarm:setPaintLayer(name, textIndex) @@ -114,6 +116,28 @@ function TerraFarm:setPaintLayer(name, textIndex) end end +function TerraFarm:setFillType(name, typeIndex) + if name then + local fillType = g_fillTypeManager.nameToFillType[name] + if not fillType then + Logging.error('TerraFarm.setFillType: Failed to get fillType data - ' .. tostring(name)) + if name ~= TerraFarmConfig.DEFAULT.fillTypeName then + -- Revert back to default value just in case .. + Logging.info('Reverting to default') + return self:setFillType(TerraFarmConfig.DEFAULT.fillTypeName) + end + return + end + self.config.fillTypeName = name + self.config.fillTypeIndex = g_fillTypeManager.nameToIndex[self.config.fillTypeName] + self.config.fillTypeMassPerLiter = fillType.massPerLiter * 1000 * 1000 + + return true + elseif typeIndex then + self:setFillType(TerraFarmFillTypes.TITLE_TO_NAME[TerraFarmFillTypes.TYPES_LIST[typeIndex]]) + end +end + function TerraFarm:setDischargePaintLayer(name, textIndex) if name then self.config.dischargePaintLayer = name @@ -135,16 +159,7 @@ function TerraFarm:setEnabled(enabled) end function TerraFarm:updateFillTypeData() - local fillType = g_fillTypeManager.nameToFillType[self.config.fillTypeName] - if fillType == nil then - Logging.error('TerraFarm.updateFillTypeData: Failed to get fillType data') - return false - end - - self.config.fillTypeIndex = g_fillTypeManager.nameToIndex[self.config.fillTypeName] - self.config.fillTypeMassPerLiter = fillType.massPerLiter * 1000 * 1000 - - return true + self:setFillType(self.config.fillTypeName) end function TerraFarm:registerCustomFillType() @@ -182,7 +197,6 @@ function TerraFarm:registerCustomFillType() if result then Logging.info('TerraFarm: Successfully registered custom fillType - ' .. name) - DebugUtil.printTableRecursively(result, ' ', 0, 0) if g_fillTypeManager:addFillTypeToCategory(result.index, g_fillTypeManager.nameToCategoryIndex['BULK']) ~= true then Logging.error('Failed to add fillType to BULK category') diff --git a/classes/TerraFarmFillTypes.lua b/classes/TerraFarmFillTypes.lua new file mode 100644 index 0000000..395362c --- /dev/null +++ b/classes/TerraFarmFillTypes.lua @@ -0,0 +1,51 @@ +---@class TerraFarmFillTypes +TerraFarmFillTypes = {} + +TerraFarmFillTypes.SUPPORTED_TYPES_LIST = { + 'STONE', + 'DIRT', + 'ASPHALT', + 'COAL', + 'TAILINGS', + 'CONCRETE', + 'LIMESTONE', + 'GRAVEL', + 'SAND', + 'IRON', + 'PAYDIRT', + 'CEMENT', + 'RIVERSAND', + 'RIVERSANDP', + 'STONEPOWDER', + 'CLAY', +} + +-- For GUI +---@type table +TerraFarmFillTypes.TYPES_LIST = {} + +-- For GUI +---@type table +TerraFarmFillTypes.NAME_TO_INDEX = {} + +-- For GUI +---@type table +TerraFarmFillTypes.TITLE_TO_NAME = {} + + +function TerraFarmFillTypes:init() + for _, name in pairs(self.SUPPORTED_TYPES_LIST) do + self:add(name) + end +end + +function TerraFarmFillTypes:add(name) + local fillType = g_fillTypeManager.nameToFillType[name] + if fillType then + local title = string.lower(name):gsub("^%l", string.upper) + + table.insert(self.TYPES_LIST, title) + self.NAME_TO_INDEX[name] = #self.TYPES_LIST + self.TITLE_TO_NAME[title] = name + end +end \ No newline at end of file diff --git a/classes/gui/TerraFarmSettingsMainFrame.lua b/classes/gui/TerraFarmSettingsMainFrame.lua index 84846fa..d5a9c90 100644 --- a/classes/gui/TerraFarmSettingsMainFrame.lua +++ b/classes/gui/TerraFarmSettingsMainFrame.lua @@ -5,6 +5,7 @@ ---@field disableDischarge CheckedOptionElement ---@field disablePaint CheckedOptionElement ---@field paintLayer MultiTextOptionElement +---@field fillType MultiTextOptionElement ---@field dischargePaintLayer MultiTextOptionElement TerraFarmSettingsMainFrame = {} @@ -15,6 +16,7 @@ TerraFarmSettingsMainFrame.CONTROLS = { 'enableGlobal', 'enableDebug', + 'fillType', 'disableDischarge', 'paintLayer', 'dischargePaintLayer', @@ -57,6 +59,7 @@ function TerraFarmSettingsMainFrame:updateSettings() self.disablePaint:setIsChecked(g_terraFarm.config.terraformDisablePaint) self.paintLayer:setState(TerraFarm.PAINT_LAYERS[g_terraFarm.config.terraformPaintLayer]) self.dischargePaintLayer:setState(TerraFarm.PAINT_LAYERS[g_terraFarm.config.dischargePaintLayer]) + self.fillType:setState(TerraFarmFillTypes.NAME_TO_INDEX[g_terraFarm.config.fillTypeName]) end function TerraFarmSettingsMainFrame:onGlobalCheckClick(state) @@ -75,6 +78,18 @@ function TerraFarmSettingsMainFrame:onDisablePaintCheckClick(state) g_terraFarm.config.terraformDisablePaint = (state == CheckedOptionElement.STATE_CHECKED) end +---@param element MultiTextOptionElement +function TerraFarmSettingsMainFrame:onCreateFillType(element) + element:setTexts(TerraFarmFillTypes.TYPES_LIST) +end + +---@param state number +function TerraFarmSettingsMainFrame:onClickFillType(state) + g_terraFarm:setFillType(nil, state) + self:updateSettings() +end + + ---@param element MultiTextOptionElement function TerraFarmSettingsMainFrame:onCreateDischargePaintLayer(element) element:setTexts(TerraFarm.PAINT_LAYER_TEXTS) diff --git a/script.lua b/script.lua index a3025df..ba231d3 100644 --- a/script.lua +++ b/script.lua @@ -4,6 +4,8 @@ source(modFolder .. 'classes/TerraFarmConfig.lua') source(modFolder .. 'classes/TerraFarm.lua') source(modFolder .. 'classes/debug/TerraFarmDebug.lua') +source(modFolder .. 'classes/TerraFarmFillTypes.lua') + source(modFolder .. 'classes/TerraFarmLandscaping.lua') source(modFolder .. 'classes/events/TerraFarmLandscapingEvent.lua') diff --git a/xml/TerraFarmSettingsMainFrame.xml b/xml/TerraFarmSettingsMainFrame.xml index a241d9d..d327663 100644 --- a/xml/TerraFarmSettingsMainFrame.xml +++ b/xml/TerraFarmSettingsMainFrame.xml @@ -46,6 +46,17 @@ + + + + + + + + + + +