Skip to content

Commit fa1bccf

Browse files
committed
Refactor editor_body.lua to use unified setModifier/getModifier
1 parent 4f34ab4 commit fa1bccf

1 file changed

Lines changed: 20 additions & 43 deletions

File tree

‎internal/gm-unit/editor_body.lua‎

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,22 @@ function Editor_Body_Modifier:recalculateBodySize()
3838
self.target_unit.body.size_info.size_cur = new_size
3939
end
4040

41-
function Editor_Body_Modifier:setPartModifier(indexList, value)
42-
for _, index in ipairs(indexList) do
43-
self.target_unit.appearance.bp_modifiers[index] = tonumber(value)
41+
function Editor_Body_Modifier:getModifier(index)
42+
if self.partChoice.type == "part" then
43+
return self.target_unit.appearance.bp_modifiers[index]
44+
else
45+
return self.target_unit.appearance.body_modifiers[index]
4446
end
45-
46-
-- Update the unit's portrait
47-
self.target_unit.flags4.portrait_must_be_refreshed = true
48-
-- Update the world texture
49-
self.target_unit.flags4.any_texture_must_be_refreshed = true
50-
51-
self:recalculateBodySize()
52-
53-
self:updateChoices()
5447
end
5548

56-
function Editor_Body_Modifier:setBodyModifier(modifierIndex, value)
57-
self.target_unit.appearance.body_modifiers[modifierIndex] = tonumber(value)
49+
function Editor_Body_Modifier:setModifier(index_data, value)
50+
if self.partChoice.type == "part" then
51+
for _, idx in ipairs(index_data) do
52+
self.target_unit.appearance.bp_modifiers[idx] = tonumber(value)
53+
end
54+
else
55+
self.target_unit.appearance.body_modifiers[index_data] = tonumber(value)
56+
end
5857

5958
-- Update the unit's portrait
6059
self.target_unit.flags4.portrait_must_be_refreshed = true
@@ -73,12 +72,7 @@ function Editor_Body_Modifier:selected(index, selected)
7372
nil,
7473
tostring(selected.value),
7574
function(newValue)
76-
local value = tonumber(newValue)
77-
if self.partChoice.type == "part" then
78-
self:setPartModifier(selected.modifier.idx, value)
79-
else -- Body
80-
self:setBodyModifier(selected.modifier.index, value)
81-
end
75+
self:setModifier(selected.modifier.idx or selected.modifier.index, tonumber(newValue))
8276
end,
8377
nil,nil
8478
)
@@ -105,11 +99,7 @@ function Editor_Body_Modifier:random()
10599
local value = min + roll
106100

107101
-- Set the modifier to the new value
108-
if self.partChoice.type == "part" then
109-
self:setPartModifier(selected.modifier.idx, value)
110-
else
111-
self:setBodyModifier(selected.modifier.index, value)
112-
end
102+
self:setModifier(selected.modifier.idx or selected.modifier.index, value)
113103
end
114104

115105
function Editor_Body_Modifier:step(amount)
@@ -126,40 +116,27 @@ function Editor_Body_Modifier:step(amount)
126116

127117
-- Now determine what range the modifier currently falls into
128118
local currentValue = selected.value
129-
local rangeIndex
119+
local rangeIndex = #ranges
130120

131121
for index, value in ipairs(ranges) do
132-
if ranges[index+1] then -- There's still a next entry
133-
if currentValue < ranges[index+1] then -- The current value is less than the next entry
134-
rangeIndex = index
135-
break
136-
end
137-
else -- This is the last entry
122+
if ranges[index+1] and currentValue < ranges[index+1] then
138123
rangeIndex = index
124+
break
139125
end
140126
end
141127

142128
-- Finally, move the modifier's value up / down in range tiers based on given amount
143129
local newTier = math.min(#ranges, math.max(1, rangeIndex + amount)) -- Clamp values to not go beyond bounds of ranges
144130
local newValue = ranges[newTier]
145131

146-
if self.partChoice.type == "part" then
147-
self:setPartModifier(selected.modifier.idx, newValue)
148-
else
149-
self:setBodyModifier(selected.modifier.index, newValue)
150-
end
132+
self:setModifier(selected.modifier.idx or selected.modifier.index, newValue)
151133
end
152134

153135
function Editor_Body_Modifier:updateChoices()
154136
local choices = {}
155137

156138
for index, modifier in ipairs(self.partChoice.modifiers) do
157-
local currentValue
158-
if self.partChoice.type == "part" then
159-
currentValue = self.target_unit.appearance.bp_modifiers[modifier.idx[1]]
160-
else -- Body
161-
currentValue = self.target_unit.appearance.body_modifiers[modifier.index]
162-
end
139+
local currentValue = self:getModifier(modifier.idx and modifier.idx[1] or modifier.index)
163140
table.insert(choices, {text = self:beautifyString(df.appearance_modifier_type[modifier.entry.modifier.type]) .. ": " .. currentValue, value = currentValue, modifier = modifier})
164141
end
165142

0 commit comments

Comments
 (0)