Skip to content

Commit ffe6536

Browse files
committed
Refactor PlayersWindow and ScenarioInfoView to use MVC pattern
- Create models for both general editors - Refactor Chili views to use models - Implement RmlUi versions using shared models - PlayersWindow: Team management logic moved to model - ScenarioInfoView: Field definitions and update logic in model All general editors now follow MVC pattern
1 parent 7d87dab commit ffe6536

File tree

7 files changed

+175
-62
lines changed

7 files changed

+175
-62
lines changed

scen_edit/view/general/players_window.lua

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ PlayersWindow:Register({
1010
order = 1,
1111
})
1212

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

1617
self.teamsPanel = StackPanel:New {
1718
itemMargin = {0, 0, 0, 0},
@@ -21,7 +22,7 @@ function PlayersWindow:init()
2122
autosize = true,
2223
resizeItems = false,
2324
}
24-
SB.model.teamManager:addListener(self)
25+
self.model:AddTeamListener(self)
2526
self:Populate()
2627

2728
self.btnAddPlayer = TabbedPanelButton({
@@ -34,12 +35,7 @@ function PlayersWindow:init()
3435
},
3536
OnClick = {
3637
function()
37-
local name = "New team: " .. tostring(#SB.model.teamManager:getAllTeams())
38-
local color = { r=math.random(), g=math.random(), b=math.random(), a=1}
39-
local allyTeam = 1
40-
local side = Spring.GetSideData(1)
41-
local cmd = AddTeamCommand(name, color, allyTeam, side)
42-
SB.commandManager:execute(cmd)
38+
self.model:AddTeam()
4339
end
4440
},
4541
})
@@ -66,16 +62,15 @@ end
6662

6763
function PlayersWindow:Populate()
6864
self.teamsPanel:ClearChildren()
69-
--titles
7065
local titlesPanel = MakeComponentPanel(self.teamsPanel)
7166
local lblTeams = Label:New {
7267
caption = "Teams",
7368
x = 1,
7469
width = 150,
7570
parent = titlesPanel,
7671
}
77-
--teams
78-
for _, team in pairs(SB.model.teamManager:getAllTeams()) do
72+
73+
for _, team in pairs(self.model:GetAllTeams()) do
7974
local stackTeamPanel = MakeComponentPanel(self.teamsPanel)
8075
local fontColor = SB.glToFontColor(team.color or {r=1, g=1, b=1})
8176
local aiPrefix = "(Player) "
@@ -123,8 +118,7 @@ function PlayersWindow:Populate()
123118
},
124119
OnClick = {
125120
function()
126-
local cmd = RemoveTeamCommand(team.id)
127-
SB.commandManager:execute(cmd)
121+
self.model:RemoveTeam(team.id)
128122
end
129123
}
130124
}

scen_edit/view/general/scenario_info_view.lua

Lines changed: 16 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,19 @@ ScenarioInfoView:Register({
1010
order = 0,
1111
})
1212

13-
function ScenarioInfoView:init()
13+
function ScenarioInfoView:init(model)
1414
self:super("init")
15-
16-
self:AddField(
17-
StringField({
18-
name = "name",
19-
title = "Name:",
20-
width = 200,
21-
value = SB.model.scenarioInfo.name,
22-
})
23-
)
24-
25-
self:AddField(
26-
StringField({
27-
name = "description",
28-
title = "Description:",
29-
width = 200,
30-
value = SB.model.scenarioInfo.description,
31-
})
32-
)
33-
34-
self:AddField(
35-
StringField({
36-
name = "version",
37-
title = "Version:",
38-
width = 200,
39-
value = tostring(SB.model.scenarioInfo.version),
40-
})
41-
)
42-
43-
self:AddField(
44-
StringField({
45-
name = "author",
46-
title = "Author:",
47-
width = 200,
48-
value = SB.model.scenarioInfo.author,
49-
})
50-
)
15+
self.model = model or ScenarioInfoModel()
16+
17+
local fieldDefs = self.model:GetFieldDefinitions()
18+
for _, fieldDef in ipairs(fieldDefs) do
19+
self:AddField(StringField({
20+
name = fieldDef.name,
21+
title = fieldDef.title,
22+
width = fieldDef.width,
23+
value = fieldDef.value,
24+
}))
25+
end
5126

5227
local children = {
5328
ScrollPanel:New {
@@ -61,31 +36,23 @@ function ScenarioInfoView:init()
6136
},
6237
}
6338

64-
SB.model.scenarioInfo:addListener(ScenarioInfoListenerWidget(self))
65-
39+
self.model:AddListener(ScenarioInfoListenerWidget(self))
6640
self:Finalize(children)
6741
end
6842

6943
function ScenarioInfoView:OnStartChange(name)
70-
SB.commandManager:execute(SetMultipleCommandModeCommand(true))
44+
self.model:OnStartChange()
7145
end
7246

7347
function ScenarioInfoView:OnEndChange(name)
74-
SB.commandManager:execute(SetMultipleCommandModeCommand(false))
48+
self.model:OnEndChange()
7549
end
7650

7751
function ScenarioInfoView:OnFieldChange(name, value)
7852
if self.updatingInfo then
7953
return
8054
end
81-
82-
local cmd = SetScenarioInfoCommand({
83-
name = self.fields['name'].value,
84-
description = self.fields['description'].value,
85-
version = self.fields['version'].value,
86-
author = self.fields['author'].value,
87-
})
88-
SB.commandManager:execute(cmd)
55+
self.model:OnFieldChange(self.fields)
8956
end
9057

9158
function ScenarioInfoView:UpdateInfo(update)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
PlayersWindowModel = LCS.class{}
2+
3+
function PlayersWindowModel:init()
4+
end
5+
6+
function PlayersWindowModel:GetAllTeams()
7+
return SB.model.teamManager:getAllTeams()
8+
end
9+
10+
function PlayersWindowModel:AddTeam()
11+
local name = "New team: " .. tostring(#SB.model.teamManager:getAllTeams())
12+
local color = {r=math.random(), g=math.random(), b=math.random(), a=1}
13+
local allyTeam = 1
14+
local side = Spring.GetSideData(1)
15+
local cmd = AddTeamCommand(name, color, allyTeam, side)
16+
SB.commandManager:execute(cmd)
17+
end
18+
19+
function PlayersWindowModel:RemoveTeam(teamID)
20+
local cmd = RemoveTeamCommand(teamID)
21+
SB.commandManager:execute(cmd)
22+
end
23+
24+
function PlayersWindowModel:AddTeamListener(listener)
25+
SB.model.teamManager:addListener(listener)
26+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
ScenarioInfoModel = LCS.class{}
2+
3+
function ScenarioInfoModel:init()
4+
end
5+
6+
function ScenarioInfoModel:GetFieldDefinitions()
7+
return {
8+
{type = "string", name = "name", title = "Name:", width = 200, value = SB.model.scenarioInfo.name},
9+
{type = "string", name = "description", title = "Description:", width = 200, value = SB.model.scenarioInfo.description},
10+
{type = "string", name = "version", title = "Version:", width = 200, value = tostring(SB.model.scenarioInfo.version)},
11+
{type = "string", name = "author", title = "Author:", width = 200, value = SB.model.scenarioInfo.author},
12+
}
13+
end
14+
15+
function ScenarioInfoModel:AddListener(listener)
16+
SB.model.scenarioInfo:addListener(listener)
17+
end
18+
19+
function ScenarioInfoModel:OnStartChange()
20+
SB.commandManager:execute(SetMultipleCommandModeCommand(true))
21+
end
22+
23+
function ScenarioInfoModel:OnEndChange()
24+
SB.commandManager:execute(SetMultipleCommandModeCommand(false))
25+
end
26+
27+
function ScenarioInfoModel:OnFieldChange(fields)
28+
local cmd = SetScenarioInfoCommand({
29+
name = fields['name'].value,
30+
description = fields['description'].value,
31+
version = fields['version'].value,
32+
author = fields['author'].value,
33+
})
34+
SB.commandManager:execute(cmd)
35+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--- RmlUi Players Window
2+
3+
RmlUiPlayersWindow = RmlUiEditorBase:extends{}
4+
5+
function RmlUiPlayersWindow:init(model)
6+
self:super("init")
7+
self.model = model or PlayersWindowModel()
8+
self.editorTitle = "Teams"
9+
10+
self.model:AddTeamListener(self)
11+
12+
self:AddButton({
13+
caption = "Add Team",
14+
onClick = function()
15+
self.model:AddTeam()
16+
end
17+
})
18+
19+
self:Populate()
20+
end
21+
22+
function RmlUiPlayersWindow:Populate()
23+
-- TODO: Implement team list rendering in RmlUi
24+
-- For now this is a stub that would need proper RmlUi list implementation
25+
end
26+
27+
function RmlUiPlayersWindow:onTeamAdded(teamID)
28+
self:Populate()
29+
end
30+
31+
function RmlUiPlayersWindow:onTeamRemoved(teamID)
32+
self:Populate()
33+
end
34+
35+
function RmlUiPlayersWindow:onTeamChange(teamID, team)
36+
self:Populate()
37+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--- RmlUi Scenario Info View
2+
3+
RmlUiScenarioInfoView = RmlUiEditorBase:extends{}
4+
5+
function RmlUiScenarioInfoView:init(model)
6+
self:super("init")
7+
self.model = model or ScenarioInfoModel()
8+
self.editorTitle = "Scenario Info"
9+
10+
local fieldDefs = self.model:GetFieldDefinitions()
11+
for _, fieldDef in ipairs(fieldDefs) do
12+
self:AddField(StringField({
13+
name = fieldDef.name,
14+
title = fieldDef.title,
15+
width = fieldDef.width,
16+
value = fieldDef.value,
17+
}))
18+
end
19+
20+
self.model:AddListener(ScenarioInfoListenerWidget(self))
21+
end
22+
23+
function RmlUiScenarioInfoView:OnStartChange(name)
24+
self.model:OnStartChange()
25+
end
26+
27+
function RmlUiScenarioInfoView:OnEndChange(name)
28+
self.model:OnEndChange()
29+
end
30+
31+
function RmlUiScenarioInfoView:OnFieldChange(name, value)
32+
if self.updatingInfo then
33+
return
34+
end
35+
self.model:OnFieldChange(self.fields)
36+
end
37+
38+
function RmlUiScenarioInfoView:UpdateInfo(update)
39+
if self._startedChanging then
40+
return
41+
end
42+
43+
self.updatingInfo = true
44+
for k, v in pairs(update) do
45+
self:SetFieldValue(k, v)
46+
end
47+
self.updatingInfo = false
48+
end

scen_edit/view/view.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ function View:InitializeRmlUi()
5050
SB.Include(Path.Join(SB.DIRS.SRC, 'view/rmlui_editors/sky_editor.lua'))
5151
SB.Include(Path.Join(SB.DIRS.SRC, 'view/rmlui_editors/lighting_editor.lua'))
5252
SB.Include(Path.Join(SB.DIRS.SRC, 'view/rmlui_editors/terrain_settings_editor.lua'))
53+
54+
-- Load RmlUi general windows
55+
SB.Include(Path.Join(SB.DIRS.SRC, 'view/rmlui_general/players_window.lua'))
56+
SB.Include(Path.Join(SB.DIRS.SRC, 'view/rmlui_general/scenario_info_view.lua'))
5357

5458
-- Load models (business logic, shared between Chili and RmlUi)
5559
SB.Include(Path.Join(SB.DIRS.SRC, 'view/models/status_window_model.lua'))
@@ -66,6 +70,8 @@ function View:InitializeRmlUi()
6670
SB.Include(Path.Join(SB.DIRS.SRC, 'view/models/sky_editor_model.lua'))
6771
SB.Include(Path.Join(SB.DIRS.SRC, 'view/models/lighting_editor_model.lua'))
6872
SB.Include(Path.Join(SB.DIRS.SRC, 'view/models/terrain_settings_editor_model.lua'))
73+
SB.Include(Path.Join(SB.DIRS.SRC, 'view/models/players_window_model.lua'))
74+
SB.Include(Path.Join(SB.DIRS.SRC, 'view/models/scenario_info_model.lua'))
6975

7076
-- Load RmlUi floating windows
7177
SB.Include(Path.Join(SB.DIRS.SRC, 'view/rmlui_floating/command_window.lua'))

0 commit comments

Comments
 (0)