Differential D4182 Diff 18332 ps/trunk/binaries/data/mods/public/gui/gamesettings/attributes/Daytime.js
Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/binaries/data/mods/public/gui/gamesettings/attributes/Daytime.js
GameSettings.prototype.Attributes.Daytime = class Daytime extends GameSetting | GameSettings.prototype.Attributes.Daytime = class Daytime extends GameSetting | ||||
{ | { | ||||
init() | init() | ||||
{ | { | ||||
this.data = undefined; | this.setDataValueHelper(undefined, undefined); | ||||
this.value = undefined; | |||||
this.settings.map.watch(() => this.onMapChange(), ["map"]); | this.settings.map.watch(() => this.onMapChange(), ["map"]); | ||||
} | } | ||||
toInitAttributes(attribs) | toInitAttributes(attribs) | ||||
{ | { | ||||
if (this.value) | if (this.value) | ||||
attribs.settings.Daytime = this.value; | attribs.settings.Daytime = this.value; | ||||
} | } | ||||
fromInitAttributes(attribs) | fromInitAttributes(attribs) | ||||
{ | { | ||||
if (!this.getLegacySetting(attribs, "Daytime")) | if (!this.getLegacySetting(attribs, "Daytime")) | ||||
this.setValue(undefined); | this.setValue(undefined); | ||||
else | else | ||||
this.setValue(this.getLegacySetting(attribs, "Daytime")); | this.setValue(this.getLegacySetting(attribs, "Daytime")); | ||||
} | } | ||||
onMapChange() | onMapChange() | ||||
{ | { | ||||
let mapData = this.settings.map.data; | let mapData = this.settings.map.data; | ||||
if (!mapData || !mapData.settings || !mapData.settings.Daytime) | if (!mapData || !mapData.settings || !mapData.settings.Daytime) | ||||
{ | { | ||||
this.value = undefined; | this.setDataValueHelper(undefined, undefined); | ||||
this.data = undefined; | |||||
return; | return; | ||||
} | } | ||||
// TODO: validation | // TODO: validation | ||||
this.data = mapData.settings.Daytime; | this.setDataValueHelper(mapData.settings.Daytime, "random"); | ||||
this.value = "random"; | |||||
} | } | ||||
setValue(val) | setValue(val) | ||||
{ | { | ||||
// TODO: more validation. | // TODO: more validation. | ||||
if (this.data) | if (this.data) | ||||
this.value = val || "random"; | this.value = val || "random"; | ||||
else | else | ||||
this.value = undefined; | this.value = undefined; | ||||
} | } | ||||
pickRandomItems() | pickRandomItems() | ||||
{ | { | ||||
// If the map is random, we need to wait until it is selected. | // If the map is random, we need to wait until it is selected. | ||||
if (this.settings.map.map === "random") | if (this.settings.map.map === "random") | ||||
return true; | return true; | ||||
if (this.value !== "random") | if (this.value !== "random") | ||||
return false; | return false; | ||||
this.value = pickRandom(this.data).Id; | this.value = pickRandom(this.data).Id; | ||||
return true; | return true; | ||||
} | } | ||||
/** | |||||
* Helper function to ensure this.data and this.value | |||||
* are assigned in the correct order to prevent | |||||
* crashes in the renderer. | |||||
* @param {object} data - The day time option data. | |||||
* @param {string} value - The option's key. | |||||
*/ | |||||
setDataValueHelper(data, value) | |||||
{ | |||||
this.data = data; | |||||
this.value = value; | |||||
} | |||||
}; | }; |
Wildfire Games · Phabricator