File size: 7,858 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248

DefineClass.ParticleBehavior =
{
	__parents = { "ParticleSystemSubItem", "PropertyObject" },
	__hierarchy_cache = true,
	PropEditorCopy = true,

	properties = {
		{ id = "label", category = "Base", name = "Label", editor = "text", dynamic = true, default = "", help = "A help text used to show the meaning of the behavior" },
		{ id = "bins", category = "Base", name = "Bins", editor = "set", items = { "A", "B", "C", "D", "E", "F", "G", "H" } },
		{ id = "time_start", category = "Base", name = "Time Start", editor = "number", scale = "sec", dynamic = true },
		{ id = "time_stop", category = "Base", name = "Time Stop", editor = "number", scale = "sec", dynamic = true },
		{ id = "time_period", category = "Base", name = "Time Period", editor = "number", scale = "sec", dynamic = true },
		{ id = "period_seed", category = "Base", name = "Period Seed", editor = "number", help = "Leave 0 for random. If period_seed, time_start, time_stop, time_period are equal the seed will be equal." },
		{ id = "randomize_period", category = "Base", name = "Randomize Period (%)", editor = "number", dynamic = true },		
		{ id = "world_space", name = "World space", editor = "bool" },
		{ id = "probability", name = "Probability", editor = "number", dynamic = true, help = "The probability of that behavior to be used", min = 1, max = 100 },
	},
	
	active = true,
	flags_label = false,
	bins = set("A"),
	time_start = 0,
	time_stop = -1000,
	time_period = 0,
	period_seed = 0,
	randomize_period = 0,
	EditorName = false,
	EditorView = Untranslated("<FormatNameForGed>"),
	world_space = false,
	probability = 100,
	
	override_props = false,
	override_value = false,
}

function ParticleBehavior:FormatBins()
	local bins = "["
	local items = self:GetPropertyMetadata("bins").items
	for _, item in ipairs(items) do
		if self.bins[item] then
			bins = bins .. item
		else
			bins = bins .. "_"
		end
	end
	bins = bins .. "]"
	return bins	
end

function ParticleBehavior:GetColorForGed()
	return self.active and "75 105 198" or "170 170 170"
end

function ParticleBehavior:OnAfterEditorNew(parent, socket, paste)
	local container = socket:GetParentOfKind("SelectedObject", "ParticleSystemPreset")
	if not container then return end
	
	local idx = table.find(container, self)
	if idx and idx > 1 and not paste then
		local old_item = container[idx - 1]
		self.bins = table.copy(old_item.bins)
	end
	
	if IsKindOf(container, "ParticleSystemPreset") then
		container:RefreshBehaviorUsageIndicators("do_now")
		ParticlesReload(container.id)
		self:EnableDynamicToggle(container:DynamicParams())
	end
end

function ParticleBehavior:OnAfterEditorSwap(parent, socket, idx1, idx2)
	local container = socket:GetParentOfKind("SelectedObject", "ParticleSystemPreset")
	if not container then return end
	if IsKindOf(container, "ParticleSystemPreset") then
		ParticlesReload(container.id)
	end
end

function ParticleBehavior:OnAfterEditorDragAndDrop(parent, socket)
	local container = socket:GetParentOfKind("SelectedObject", "ParticleSystemPreset")
	if not container then return end
	if IsKindOf(container, "ParticleSystemPreset") then
		ParticlesReload(container.id)
	end
end

function ParticleBehavior:OnAfterEditorDelete(parent, socket)
	local container = GetParentTableOfKind(self, "ParticleSystemPreset")
	if not container then return end
	container:RefreshBehaviorUsageIndicators()
	ParticlesReload(container.id)
end

function ParticleBehavior:FormatNameForGed()
	local bins = self:FormatBins()
	local color = self:GetColorForGed()
	local label = ""
	if self.label ~= "" then
		label = "\"" .. self.label .. "\""
	end

	local name = string.format("<color %s>%s %s %s", color, bins, label, self.EditorName or self.class )
	if self.flags_label then
		name = name .. "<right>" .. self.flags_label
	end

	return name
end

local function FilterDynamicParamsForEditor(dynamic_params, editor)
	local available = {}
	for k, v in sorted_pairs(dynamic_params) do	
		if v.type  == editor then
			available[#available + 1] = k
		end
	end
	return available
end

-- Glue code to support editing in both Hedgehog and GED; to be removed

function ParticleBehavior_SwitchParam(root, obj, prop, ...)
	return ParticleBehavior.GedSwitchParam(obj, root, prop, ...)
end

function ParticleBehavior:GedSwitchParam(root, prop, socket)
	local parsys = GetParentTableOfKind(self, "ParticleSystemPreset")
	if parsys then
		self:ToggleProperty(prop, parsys:DynamicParams())
		ObjModified(self)
	end
end

function ParticleBehavior:EnableDynamicToggle(dynamic_params)
	local available_types = {}
	for k, v in sorted_pairs(dynamic_params) do
		available_types[v.type] = true
	end

	for i = 1, #self.properties do
		local prop = self.properties[i]
		if available_types[prop.orig_editor or prop.editor] and prop.dynamic then
			-- create override metadata for this property with different editor and toggle button

			prop.buttons = { {name = "Dynamic", func = "ParticleBehavior_SwitchParam"} }
			
			self.override_props = self.override_props or {}
			self.override_props[prop.id] = table.copy(prop)
			
			local available = FilterDynamicParamsForEditor(dynamic_params, prop.editor)
			self.override_props[prop.id].editor = "combo"
			self.override_props[prop.id].items = available
		else
			-- remove override metadata, value and toggle button
			prop.buttons = nil
			if self.override_props then
				self.override_props[prop.id] = nil
				if next(self.override_props) == nil then
					self.override_props = nil
				end
			end
			if self.override_value and self.override_value[prop.id] then
				self[prop.id] = self.override_value[prop.id]
				self.override_value[prop.id] = nil
				if next(self.override_value) == nil then
					self.override_value = nil
				end
			end
		end
	end
end

function ParticleBehavior:ToggleProperty(prop, dynamic_params)	
	if self.override_value and self.override_value[prop] then
		local value = self.override_value[prop]
		self[prop] = value
		self.override_value[prop] = nil
		if next(self.override_value) == nil then
			self.override_value = nil
		end
	else

		self.override_value = self.override_value or {}
		self.override_value[prop] = self[prop]
		local new_meta = self.override_props[prop]
		self[prop] = new_meta.items[1]
	end
end

function ParticleBehavior:GetProperties()
	if not self.override_props or not self.override_value then
		return self.properties
	end
	
	local props = {}
	for i = 1, #self.properties do
		local prop = self.properties[i]
		props[i] = self.override_value[prop.id] and self.override_props[prop.id] or prop
	end
	return props
end

function ParticleBehavior:__toluacode(indent, pstr, GetPropFunc)
	if not pstr then
		local props = ObjPropertyListToLuaCode(self, indent, GetPropFunc)
		local arr = ArrayToLuaCode(self, indent)
		local stored
		if self.override_value then
			stored = ValueToLuaCode(self.override_value, indent)
		end
		return string.format("PlaceObj('%s', %s, %s, %s)", self.class, props or "nil", arr or "nil", stored or "nil")
	else
		pstr:appendf("PlaceObj('%s', ", self.class)
		if not ObjPropertyListToLuaCode(self, indent, GetPropFunc, pstr) then
			pstr:append("nil")
		end
		pstr:append(", ")
		if not ArrayToLuaCode(self, indent, pstr) then
			pstr:append("nil")
		end
		pstr:append(", ")
		if self.override_value then
			pstr:appendv(self.override_value, indent)
		else
			pstr:append("nil")
		end
		return pstr:append(")")
	end
end

function ParticleBehavior:__fromluacode(props, arr, stored)
	local obj = PropertyObject.__fromluacode(self, props, arr)
	if stored then
		obj.override_value = stored
	end
	return obj
end

function ParticleBehavior:Clone(class)
	local obj = PropertyObject.Clone(self, class)
	if obj:IsKindOf(self.class) and self.override_value and self.override_props then
		obj.override_value = table.copy(self.override_value)
		obj.override_props = table.copy(self.override_props)
	end
	return obj
end