Skip to content

Commit

Permalink
Added functionality for changing fillType in GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
scfmod committed Jan 3, 2022
1 parent b2e57e6 commit 1bb908e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 11 deletions.
36 changes: 25 additions & 11 deletions classes/TerraFarm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ end
function TerraFarm:onReady()
self:registerCustomFillType()
self:updateFillTypeData()

TerraFarmFillTypes:init()
end

function TerraFarm:setPaintLayer(name, textIndex)
Expand All @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -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')
Expand Down
51 changes: 51 additions & 0 deletions classes/TerraFarmFillTypes.lua
Original file line number Diff line number Diff line change
@@ -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<number, string>
TerraFarmFillTypes.TYPES_LIST = {}

-- For GUI
---@type table<string, number>
TerraFarmFillTypes.NAME_TO_INDEX = {}

-- For GUI
---@type table<string, string>
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
15 changes: 15 additions & 0 deletions classes/gui/TerraFarmSettingsMainFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
---@field disableDischarge CheckedOptionElement
---@field disablePaint CheckedOptionElement
---@field paintLayer MultiTextOptionElement
---@field fillType MultiTextOptionElement
---@field dischargePaintLayer MultiTextOptionElement
TerraFarmSettingsMainFrame = {}

Expand All @@ -15,6 +16,7 @@ TerraFarmSettingsMainFrame.CONTROLS = {

'enableGlobal',
'enableDebug',
'fillType',
'disableDischarge',
'paintLayer',
'dischargePaintLayer',
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down
11 changes: 11 additions & 0 deletions xml/TerraFarmSettingsMainFrame.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@

<GuiElement type="text" profile="settingsMenuSubtitle" text=" " />

<GuiElement type="multiTextOption" profile="multiTextOptionSettings" onClick="onClickFillType" onCreate="onCreateFillType" id="fillType" >
<GuiElement type="button" profile="multiTextOptionSettingsLeft" />
<GuiElement type="button" profile="multiTextOptionSettingsRight"/>
<GuiElement type="text" profile="multiTextOptionSettingsText" />
<GuiElement type="text" profile="multiTextOptionSettingsTitle" text="Fill type"/>
<GuiElement type="bitmap" profile="multiTextOptionSettingsBg" />
<GuiElement type="text" profile="multiTextOptionSettingsTooltip" text="Set fill type for TerrFarm machines" />
</GuiElement>

<GuiElement type="text" profile="settingsMenuSubtitle" text=" " />

<GuiElement type="checkedOption" profile="multiTextOptionSettings" onClick="onDisablePaintCheckClick" id="disablePaint">
<GuiElement type="button" profile="multiTextOptionSettingsLeft" />
<GuiElement type="button" profile="multiTextOptionSettingsRight" />
Expand Down

0 comments on commit 1bb908e

Please sign in to comment.