Skip to content

Commit 7961063

Browse files
committed
Refactor LightingEditor and TerrainSettingsEditor to use MVC pattern
- Create models for both editors with field definitions and business logic - Extract lighting/atmosphere parameters and map rendering logic to models - LightingEditor: 230 → 135 lines (41% reduction) - TerrainSettingsEditor: 325 → 138 lines (58% reduction) - All 8 map editors now use consistent MVC pattern
1 parent b650cea commit 7961063

File tree

5 files changed

+373
-438
lines changed

5 files changed

+373
-438
lines changed

scen_edit/view/map/lighting_editor.lua

Lines changed: 67 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -10,130 +10,16 @@ LightingEditor:Register({
1010
order = 0,
1111
})
1212

13-
function LightingEditor:init()
13+
function LightingEditor:init(model)
1414
self:super("init")
15+
self.model = model or LightingEditorModel()
1516

16-
local shadowMode = Spring.GetConfigInt("Shadows")
17-
self:AddField(ChoiceField({
18-
name = "shadowMode",
19-
title = "Shadows:",
20-
tooltip = "Shadow mode. Unsynced (and unsaved) setting.",
21-
items = { "Off", "Terrain", "Full" },
22-
width = 200,
23-
}))
24-
if shadowMode == 0 then
25-
self.fields["shadowMode"]:Set("Off")
26-
elseif shadowMode == 1 then
27-
self.fields["shadowMode"]:Set("Full")
28-
elseif shadowMode == 2 then
29-
self.fields["shadowMode"]:Set("Terrain")
17+
local fieldDefs = self.model:GetFieldDefinitions()
18+
for _, fieldDef in ipairs(fieldDefs) do
19+
self:AddFieldFromDef(fieldDef)
3020
end
3121

32-
self:AddField(GroupField({
33-
NumericField({
34-
name = "sunDirX",
35-
title = "Dir X:",
36-
tooltip = "X dir",
37-
value = 0,
38-
step = 0.002,
39-
width = 100,
40-
}),
41-
NumericField({
42-
name = "sunDirY",
43-
title = "Dir Y:",
44-
tooltip = "Y dir",
45-
value = 0,
46-
step = 0.002,
47-
width = 100,
48-
}),
49-
NumericField({
50-
name = "sunDirZ",
51-
title = "Dir Z:",
52-
tooltip = "Z dir",
53-
value = 0,
54-
step = 0.002,
55-
width = 100,
56-
}),
57-
}))
58-
self:AddControl("sun-ground-sep", {
59-
Label:New {
60-
caption = "Sun ground color",
61-
},
62-
Line:New {
63-
x = 150,
64-
}
65-
})
66-
self:AddField(GroupField({
67-
ColorField({
68-
name = "groundDiffuseColor",
69-
title = "Diffuse:",
70-
tooltip = "Ground diffuse color",
71-
width = 140,
72-
format = 'rgb',
73-
}),
74-
ColorField({
75-
name = "groundAmbientColor",
76-
title = "Ambient:",
77-
tooltip = "Ground ambient color",
78-
width = 140,
79-
format = 'rgb',
80-
}),
81-
ColorField({
82-
name = "groundSpecularColor",
83-
title = "Specular:",
84-
tooltip = "Ground specular color",
85-
width = 140,
86-
format = 'rgb',
87-
})
88-
}))
89-
self:AddField(NumericField({
90-
name = "groundShadowDensity",
91-
title = "Shadow density:",
92-
tooltip = "Ground shadow density",
93-
width = 200,
94-
minValue = 0,
95-
maxValue = 1,
96-
}))
97-
self:AddControl("sun-unit-sep", {
98-
Label:New {
99-
caption = "Sun unit color",
100-
},
101-
Line:New {
102-
x = 150,
103-
}
104-
})
105-
self:AddField(GroupField({
106-
ColorField({
107-
name = "unitDiffuseColor",
108-
title = "Diffuse:",
109-
tooltip = "Unit diffuse color",
110-
width = 140,
111-
format = 'rgb',
112-
}),
113-
ColorField({
114-
name = "unitAmbientColor",
115-
title = "Ambient:",
116-
tooltip = "Unit ambient color",
117-
width = 140,
118-
format = 'rgb',
119-
}),
120-
ColorField({
121-
name = "unitSpecularColor",
122-
title = "Specular:",
123-
tooltip = "Unit specular color",
124-
width = 140,
125-
format = 'rgb',
126-
})
127-
}))
128-
self:AddField(NumericField({
129-
name = "modelShadowDensity",
130-
title = "Shadow density:",
131-
tooltip = "Unit shadow density",
132-
width = 200,
133-
minValue = 0,
134-
maxValue = 1,
135-
}))
136-
22+
self.fields["shadowMode"]:Set(self.model:GetInitialShadowMode())
13723
self:UpdateLighting()
13824

13925
local children = {
@@ -149,38 +35,70 @@ function LightingEditor:init()
14935
}
15036

15137
SB.commandManager:addListener(self)
152-
15338
self:Finalize(children)
15439
end
15540

41+
function LightingEditor:AddFieldFromDef(fieldDef)
42+
if fieldDef.type == "choice" then
43+
self:AddField(ChoiceField({
44+
name = fieldDef.name,
45+
title = fieldDef.title,
46+
tooltip = fieldDef.tooltip,
47+
items = fieldDef.items,
48+
width = fieldDef.width,
49+
}))
50+
elseif fieldDef.type == "numeric" then
51+
self:AddField(NumericField({
52+
name = fieldDef.name,
53+
title = fieldDef.title,
54+
tooltip = fieldDef.tooltip,
55+
value = fieldDef.value,
56+
minValue = fieldDef.min,
57+
maxValue = fieldDef.max,
58+
width = fieldDef.width,
59+
}))
60+
elseif fieldDef.type == "separator" then
61+
self:AddControl(fieldDef.name, {
62+
Label:New {
63+
caption = fieldDef.caption,
64+
},
65+
Line:New {
66+
x = 150,
67+
}
68+
})
69+
elseif fieldDef.type == "group" then
70+
local groupFields = {}
71+
for _, subFieldDef in ipairs(fieldDef.fields) do
72+
if subFieldDef.type == "numeric" then
73+
table.insert(groupFields, NumericField({
74+
name = subFieldDef.name,
75+
title = subFieldDef.title,
76+
tooltip = subFieldDef.tooltip,
77+
value = subFieldDef.value,
78+
step = subFieldDef.step,
79+
width = subFieldDef.width,
80+
}))
81+
elseif subFieldDef.type == "color" then
82+
table.insert(groupFields, ColorField({
83+
name = subFieldDef.name,
84+
title = subFieldDef.title,
85+
tooltip = subFieldDef.tooltip,
86+
width = subFieldDef.width,
87+
format = subFieldDef.format,
88+
}))
89+
end
90+
end
91+
self:AddField(GroupField(groupFields))
92+
end
93+
end
94+
15695
function LightingEditor:UpdateLighting()
15796
self.updating = true
15897

159-
local sunDirX, sunDirY, sunDirZ = gl.GetSun()
160-
self:Set("sunDirX", sunDirX)
161-
self:Set("sunDirY", sunDirY)
162-
self:Set("sunDirZ", sunDirZ)
163-
164-
-- Color
165-
local groundDiffuse = {gl.GetSun("diffuse")}
166-
local groundAmbient = {gl.GetSun("ambient")}
167-
local groundSpecular = {gl.GetSun("specular")}
168-
local groundShadowDensity = gl.GetSun("shadowDensity")
169-
170-
self:Set("groundDiffuseColor", groundDiffuse)
171-
self:Set("groundAmbientColor", groundAmbient)
172-
self:Set("groundSpecularColor", groundSpecular)
173-
self:Set("groundShadowDensity", groundShadowDensity)
174-
175-
local unitDiffuse = {gl.GetSun("diffuse", "unit")}
176-
local unitAmbient = {gl.GetSun("ambient", "unit")}
177-
local unitSpecular = {gl.GetSun("specular", "unit")}
178-
local modelShadowDensity = gl.GetSun("shadowDensity", "unit")
179-
180-
self:Set("unitDiffuseColor", unitDiffuse)
181-
self:Set("unitAmbientColor", unitAmbient)
182-
self:Set("unitSpecularColor", unitSpecular)
183-
self:Set("modelShadowDensity", modelShadowDensity)
98+
local params = self.model:UpdateLighting()
99+
for name, value in pairs(params) do
100+
self:Set(name, value)
101+
end
184102

185103
self.updating = false
186104
end
@@ -192,38 +110,17 @@ function LightingEditor:OnCommandExecuted()
192110
end
193111

194112
function LightingEditor:OnStartChange(name)
195-
SB.commandManager:execute(SetMultipleCommandModeCommand(true))
113+
self.model:OnStartChange()
196114
end
197115

198116
function LightingEditor:OnEndChange(name)
199-
SB.commandManager:execute(SetMultipleCommandModeCommand(false))
117+
self.model:OnEndChange()
200118
end
201119

202120
function LightingEditor:OnFieldChange(name, value)
203121
if self.updating then
204122
return
205123
end
206124

207-
if name == "shadowMode" then
208-
if value == "Off" then
209-
Spring.SendCommands("shadows 0")
210-
elseif value == "Terrain" then
211-
Spring.SendCommands("shadows 2")
212-
else
213-
Spring.SendCommands("shadows 1")
214-
end
215-
elseif name == "sunDirX" or name == "sunDirY" or name == "sunDirZ" or name == "sunStartAngle" or name == "sunOrbitTime" or name == "sunDistance" then
216-
value = { dirX = self.fields["sunDirX"].value,
217-
dirY = self.fields["sunDirY"].value,
218-
dirZ = self.fields["sunDirZ"].value,
219-
}
220-
local cmd = SetSunParametersCommand(value)
221-
SB.commandManager:execute(cmd)
222-
223-
elseif name == "groundDiffuseColor" or name == "groundAmbientColor" or name == "groundSpecularColor" or name == "groundShadowDensity" or name == "unitAmbientColor" or name == "unitDiffuseColor" or name == "unitSunColor" or name == "modelShadowDensity" then
224-
local t = {}
225-
t[name] = value
226-
local cmd = SetSunLightingCommand(t)
227-
SB.commandManager:execute(cmd)
228-
end
125+
self.model:OnFieldChange(name, value, self.fields)
229126
end

0 commit comments

Comments
 (0)