File size: 6,909 Bytes
b6a38d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
DefineClass.CharacterEntity =
{
	__parents = {"CObject"},
	flags = {gofRealTimeAnim = true},
}

DefineClass.CharacterBody = { __parents = {"CharacterEntity"} }
	DefineClass.CharacterBodyMale = { __parents = {"CharacterBody"} }
	DefineClass.CharacterBodyFemale = { __parents = {"CharacterBody"} }
DefineClass.CharacterHead = { __parents = {"CharacterEntity"} }
	DefineClass.CharacterHeadMale = { __parents = {"CharacterHead"} }
	DefineClass.CharacterHeadFemale = { __parents = {"CharacterHead"} }
DefineClass.CharacterPants = { __parents = {"CharacterEntity"} }
	DefineClass.CharacterPantsMale = { __parents = {"CharacterPants"} }
	DefineClass.CharacterPantsFemale = { __parents = {"CharacterPants"} }
DefineClass.CharacterShirts = { __parents = {"CharacterEntity"} }
	DefineClass.CharacterShirtsMale = { __parents = {"CharacterShirts"} }
	DefineClass.CharacterShirtsFemale = { __parents = {"CharacterShirts"} }
DefineClass.CharacterArmor = { __parents = {"CharacterEntity"} }
	DefineClass.CharacterArmorMale = { __parents = {"CharacterArmor"} }
	DefineClass.CharacterArmorFemale = { __parents = {"CharacterArmor"} }
DefineClass.CharacterHair = { __parents = {"CharacterEntity"} }
	DefineClass.CharacterHairMale = { __parents = {"CharacterHair"} }
	DefineClass.CharacterHairFemale = { __parents = {"CharacterHair"} }
DefineClass.CharacterHat = { __parents = {"CharacterEntity"} }
DefineClass.CharacterChest = { __parents = {"CharacterEntity"} }
	DefineClass.CharacterChestMale = { __parents = {"CharacterChest"} }
	DefineClass.CharacterChestFemale = { __parents = {"CharacterChest"} }
DefineClass.CharacterHip = { __parents = {"CharacterEntity"} }
	DefineClass.CharacterHipMale = { __parents = {"CharacterHip"} }
	DefineClass.CharacterHipFemale = { __parents = {"CharacterHip"} }

local function GetGender(appearance)
	return IsKindOf(g_Classes[appearance.Body], "CharacterBodyMale") and "Male" or "Female"
end

local function GetEntityClassInherits(entity_class, skip_none, filter)
	local inherits = ClassLeafDescendantsList(entity_class, function(class)
		return not table.find(filter, class)
	end)
	
	if not skip_none then
		table.insert(inherits, 1, "")
	end
	
	return inherits
end

function GetCharacterBodyComboItems()
	return GetEntityClassInherits("CharacterBody", "skip none", {"CharacterBodyMale", "CharacterBodyFemale"})
end

function GetCharacterHeadComboItems(appearance)
	return IsKindOf(appearance, "AppearancePreset") and GetEntityClassInherits("CharacterHead" .. GetGender(appearance)) or {}
end

function GetCharacterPantsComboItems(appearance)
	return IsKindOf(appearance, "AppearancePreset") and GetEntityClassInherits("CharacterPants" .. GetGender(appearance)) or {}
end

function GetCharacterShirtComboItems(appearance)
	return IsKindOf(appearance, "AppearancePreset") and GetEntityClassInherits("CharacterShirts" .. GetGender(appearance)) or {}
end

function GetCharacterArmorComboItems(appearance)
	return IsKindOf(appearance, "AppearancePreset") and GetEntityClassInherits("CharacterArmor" .. GetGender(appearance)) or {}
end

function GetCharacterHairComboItems(appearance)
	return IsKindOf(appearance, "AppearancePreset") and GetEntityClassInherits("CharacterHair" .. GetGender(appearance)) or {}
end

function GetCharacterHatComboItems()
	return GetEntityClassInherits("CharacterHat")
end

function GetCharacterChestComboItems(appearance)
	return IsKindOf(appearance, "AppearancePreset") and GetEntityClassInherits("CharacterChest" .. GetGender(appearance)) or {}
end

function GetCharacterHipComboItems(appearance)
	return IsKindOf(appearance, "AppearancePreset") and GetEntityClassInherits("CharacterHip" .. GetGender(appearance)) or {}
end

if FirstLoad then
	AppearanceEditor = false
end

function OpenAppearanceEditor(appearance)
	CreateRealTimeThread(function(appearance)
		if not AppearanceEditor or not IsValid(AppearanceEditor) then
			AppearanceEditor = OpenPresetEditor("AppearancePreset") or false
		end
	end, appearance)
end

function OnMsg.GedOpened(ged_id)
	local gedApp = GedConnections[ged_id]
	if gedApp and gedApp.app_template == "PresetEditor" and gedApp.context and gedApp.context.PresetClass == "AppearancePreset" then
		AppearanceEditor = gedApp
	end
end

function OnMsg.GedClosing(ged_id)
	if AppearanceEditor and AppearanceEditor.ged_id == ged_id then
		if cameraMax.IsActive() then
			cameraTac.Activate(1)
		end
		AppearanceEditor = false
	end	
end

function CloseAppearanceEditor()
	if AppearanceEditor then
		AppearanceEditor:Send("rfnApp", "Exit")
	end
end

local function UpdateAnimationMomentsEditor(appearance)
	local character = GetAnimationMomentsEditorObject()
	if character then
		local speed = character.anim_speed
		local frame = character.Frame
		character:ApplyAppearance(appearance)
		if speed == 0 then
			character:SetFrame(frame)
		end
	end
end

function OnMsg.GedPropertyEdited(ged_id, object, prop_id, old_value)
	if AppearanceEditor and AppearanceEditor.ged_id == ged_id then
		UpdateAnimationMomentsEditor(AppearanceEditor.selected_object.id)
	elseif AreModdingToolsActive() and g_Classes.ModItemAppearancePreset and IsKindOf(object, "ModItemAppearancePreset") then
		UpdateAnimationMomentsEditor(object.id)
	end
end

function OnMsg.GedOnEditorSelect(appearance, selected, ged)
	if selected and AppearanceEditor and AppearanceEditor.ged_id == ged.ged_id then
		UpdateAnimationMomentsEditor(appearance.id)
	end
end

OnMsg.ChangeMapDone = CloseAppearanceEditor

function RefreshApperanceToAllUnits(root, obj, context)
	local appearance = obj.id
	MapForEach("map", "AppearanceObject", function(obj)
		if obj.Appearance == appearance then
			obj:ApplyAppearance(appearance, "force")
		end
	end)
end

DefineClass.AppearanceWeight =
{
	__parents = {"PropertyObject"},
	
	properties =
	{
		{id = "Preset", name = "Preset", editor = "combo", items = PresetsCombo("AppearancePreset"), default = "" },
		{id = "Weight", name = "Weight", editor = "number", default = 1 },
		{id = "ViewInAppearanceEditorBtn", editor = "buttons", buttons = {{name = "View in Appearance Editor", func = "ViewInAppearanceEditor"}}, dont_save = true},
		{id = "ViewInAnimMetadataEditorBtn", editor = "buttons", buttons = {{name = "View in Anim Metadata Editor", func = "ViewInAnimMetadataEditor"}}, dont_save = true},		
		{id = "GameStates", name = "Game States Required",
			editor = "set", three_state = true, default = set(),
			items = function() return GetGameStateFilter() end,
			help = "Map states requirements for the Preset to be choosen.",
		},
	},
	EditorView = Untranslated("AppearanceWeight <u(Preset)> : <Weight>")
}

function AppearanceWeight:ViewInAppearanceEditor(prop_id, ged)
	local appearance = self.Preset
	local preset = AppearancePresets[appearance] or EntitySpecPresets[appearance]
	if preset then
		preset:OpenEditor()
	end
end

function AppearanceWeight:ViewInAnimMetadataEditor(prop_id, ged)
	OpenAnimationMomentsEditor(self.Preset)
end