Skip to content

Commit

Permalink
Painting layers in GUI should now show all available layers for each …
Browse files Browse the repository at this point in the history
…specific map
  • Loading branch information
scfmod committed Jan 4, 2022
1 parent 98a2c50 commit 4f39d0d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 50 deletions.
51 changes: 7 additions & 44 deletions classes/TerraFarm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,6 @@ TerraFarm.NAME_TO_MODE = {
['FLATTEN'] = TerraFarm.MODE.FLATTEN,
['PAINT'] = TerraFarm.MODE.PAINT
}
TerraFarm.PAINT_LAYERS = {
['dirt'] = 1,
['gravel'] = 2,
['concrete'] = 3,
['asphalt'] = 4,
['grass'] = 5,
['sand'] = 6,
['animalMud'] = 7,
['plates'] = 8,
['cobblestones'] = 9,
['flagstones'] = 10,
['forestGround'] = 11,
['rock'] = 12
}
TerraFarm.PAINT_LAYER_TEXTS = {
'Dirt',
'Gravel',
'Concrete',
'Asphalt',
'Grass',
'Sand',
'Animal mud',
'Plates',
'Cobblestones',
'Flagstones',
'Forest ground',
'Rock'
}
TerraFarm.TEXT_TO_PAINT_LAYER = {
['Dirt'] = 'dirt',
['Gravel'] = 'gravel',
['Concrete'] = 'concrete',
['Asphalt'] = 'asphalt',
['Grass'] = 'grass',
['Sand'] = 'sand',
['Animal mud'] = 'animalMud',
['Plates'] = 'plates',
['Cobblestones'] = 'cobblestones',
['Flagstones'] = 'flagstones',
['Forest ground'] = 'forestGround',
['Rock'] = 'rock'
}

---@return TerraFarm
function TerraFarm.new()
Expand Down Expand Up @@ -112,7 +70,7 @@ function TerraFarm:setPaintLayer(name, textIndex)
if name then
self.config.terraformPaintLayer = name
elseif textIndex then
self:setPaintLayer(TerraFarm.TEXT_TO_PAINT_LAYER[TerraFarm.PAINT_LAYER_TEXTS[textIndex]])
self:setPaintLayer(TerraFarmGroundTypes:getNameByIndex(textIndex))
end
end

Expand Down Expand Up @@ -142,7 +100,7 @@ function TerraFarm:setDischargePaintLayer(name, textIndex)
if name then
self.config.dischargePaintLayer = name
elseif textIndex then
self:setDischargePaintLayer(TerraFarm.TEXT_TO_PAINT_LAYER[TerraFarm.PAINT_LAYER_TEXTS[textIndex]])
self:setDischargePaintLayer(TerraFarmGroundTypes:getNameByIndex(textIndex))
end
end

Expand All @@ -162,6 +120,11 @@ function TerraFarm:updateFillTypeData()
self:setFillType(self.config.fillTypeName)
end

function TerraFarm:updatePaintLayerData()
self:setPaintLayer(nil, TerraFarmGroundTypes:getIndexByName(self.config.terraformPaintLayer))
self:setDischargePaintLayer(nil, TerraFarmGroundTypes:getIndexByName(self.config.dischargePaintLayer))
end

function TerraFarm:registerCustomFillType()
local name = 'DIRT'

Expand Down
58 changes: 58 additions & 0 deletions classes/TerraFarmGroundTypes.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---@class TerraFarmGroundTypes
---@field items GroundTypeItem[]
TerraFarmGroundTypes = {}

TerraFarmGroundTypes.items = {}
TerraFarmGroundTypes.TYPES_LIST = {}

local function isValidGroundTypeName(name)
return name == string.upper(name)
end

function TerraFarmGroundTypes:init()
for name, layerId in pairs(g_groundTypeManager.terrainLayerMapping) do
if isValidGroundTypeName(name) then
local index = #self.items + 1
local item = {
index = index,
name = name,
layerId = layerId
}
table.insert(self.items, item)
table.insert(self.TYPES_LIST, name)
end
end

g_terraFarm:updatePaintLayerData()
end

function TerraFarmGroundTypes:getIndexByName(name)
for index, item in ipairs(self.items) do
if item.name == name then
return index
end
end
return 1
end

function TerraFarmGroundTypes:getNameByIndex(index)
if self.items[index] ~= nil then
return self.items[index].name
end
end

function TerraFarmGroundTypes:getLayerByName(name)
for _, item in ipairs(self.items) do
if item.name == name then
return item.layerId
end
end

return 0
end

GroundTypeManager.initTerrain = Utils.appendedFunction(GroundTypeManager.initTerrain,
function ()
TerraFarmGroundTypes:init()
end
)
2 changes: 1 addition & 1 deletion classes/events/TerraFarmLandscapingEvent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function TerraFarmLandscapingEvent.new(machineType, mode, object, operation, pos
self.isDischarging = isDischarging or false

if type(paintLayer) == 'string' then
self.paintLayer = g_groundTypeManager:getTerrainLayerByType(paintLayer)
self.paintLayer = TerraFarmGroundTypes:getLayerByName(paintLayer)
end

return self
Expand Down
8 changes: 4 additions & 4 deletions classes/gui/TerraFarmSettingsMainFrame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ function TerraFarmSettingsMainFrame:updateSettings()
self.enableDebug:setIsChecked(g_terraFarm.config.debug)
self.disableDischarge:setIsChecked(g_terraFarm.config.disableDischarge)
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.paintLayer:setState(TerraFarmGroundTypes:getIndexByName(g_terraFarm.config.terraformPaintLayer))
self.dischargePaintLayer:setState(TerraFarmGroundTypes:getIndexByName(g_terraFarm.config.dischargePaintLayer))
self.fillType:setState(TerraFarmFillTypes.NAME_TO_INDEX[g_terraFarm.config.fillTypeName])
end

Expand Down Expand Up @@ -92,7 +92,7 @@ end

---@param element MultiTextOptionElement
function TerraFarmSettingsMainFrame:onCreateDischargePaintLayer(element)
element:setTexts(TerraFarm.PAINT_LAYER_TEXTS)
element:setTexts(TerraFarmGroundTypes.TYPES_LIST)
end

---@param state number
Expand All @@ -103,7 +103,7 @@ end

---@param element MultiTextOptionElement
function TerraFarmSettingsMainFrame:onCreatePaintLayer(element)
element:setTexts(TerraFarm.PAINT_LAYER_TEXTS)
element:setTexts(TerraFarmGroundTypes.TYPES_LIST)
end

---@param state number
Expand Down
8 changes: 7 additions & 1 deletion definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ Position = {
---@field filePath string
---@field actionEvents table
---@field machine TerraFarmMachine
MachineSpec = {}
MachineSpec = {}

---@class GroundTypeItem
---@field index number
---@field name string
---@field layerId number
GroundTypeItem = {}
1 change: 1 addition & 0 deletions script.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ source(modFolder .. 'classes/TerraFarm.lua')
source(modFolder .. 'classes/debug/TerraFarmDebug.lua')

source(modFolder .. 'classes/TerraFarmFillTypes.lua')
source(modFolder .. 'classes/TerraFarmGroundTypes.lua')

source(modFolder .. 'classes/TerraFarmLandscaping.lua')
source(modFolder .. 'classes/events/TerraFarmLandscapingEvent.lua')
Expand Down

0 comments on commit 4f39d0d

Please sign in to comment.