FXGL
FXGL copied to clipboard
Restore profile functionality + full serialization
This includes generation of profile data, saving and loading. Also needs to consider allowing no-profile games
-
[ ] FXGLMenu, use profile name instead of "./" to save/load data. Perhaps we can add a setting "saveDir" and save there?
-
[ ] FXGLDefaultMenu should be updated too
-
[ ] Input serialization:
override fun save(profile: UserProfile) {
log.debug("Saving data to profile")
val bundle = Bundle("input")
bindings.forEach { bundle.put(it.key.toString(), it.value.toString()) }
bundle.log()
profile.putBundle(bundle)
}
override fun load(profile: UserProfile) {
log.debug("Loading data from profile")
val bundle = profile.getBundle("input")
bundle.log()
for (binding in bindings) {
val action = binding.key
// if binding is not present in bundle, then we added some new binding thru code
// it will be saved on next serialization and will be found in bundle
var triggerName: String? = bundle.get<String>("$action")
if (triggerName == null)
continue
var modifierName = "NONE"
val plusIndex = triggerName.indexOf("+")
if (plusIndex != -1) {
modifierName = triggerName.substring(0, plusIndex)
triggerName = triggerName.substring(plusIndex + 1)
}
// if triggerName was CTRL+A, we end up with:
// triggerName = A
// modifierName = CTRL
try {
val key = KeyCode.getKeyCode(triggerName)
rebind(action, key, InputModifier.valueOf(modifierName))
} catch (ignored: Exception) {
try {
val btn = MouseTrigger.buttonFromString(triggerName)
rebind(action, btn, InputModifier.valueOf(modifierName))
} catch (e: Exception) {
log.warning("Undefined trigger name: " + triggerName)
throw IllegalArgumentException("Corrupt or incompatible user profile: " + e.message)
}
}
}
}
Legacy profile code
// private fun createProfilesDirTask(): IOTask<*> {
// log.debug("Creating profiles dir")
//
// return fs.createDirectoryTask(PROFILES_DIR)
// .then { fs.writeDataTask(Collections.singletonList("This directory contains user profiles."), PROFILES_DIR + "Readme.txt") }
// .onFailure {
// log.warning("Failed to create profiles dir: $it")
// Thread.getDefaultUncaughtExceptionHandler().uncaughtException(Thread.currentThread(), it)
// }
// }
// fun createProfileTask(profileName: String): IOTask<Void> {
// log.debug("Creating profile: $profileName")
// return fs.createDirectoryTask("./$PROFILES_DIR$profileName")
// }
//
// /**
// * A task that reads all profile names.
// */
// fun readProfileNamesTask(): IOTask<List<String>> {
// log.debug("Reading profile names")
// return fs.loadDirectoryNamesTask("./$PROFILES_DIR", recursive = false)
// }
//
// /**
// * Delete profile.
// *
// * @param profileName name of profile to delete
// */
// fun deleteProfileTask(profileName: String): IOTask<Void> {
// log.debug("Deleting profile: $profileName")
// return fs.deleteDirectoryTask("./$PROFILES_DIR$profileName")
// }
//
// fun `Delete profile`() {
// assertTrue(Files.exists(Paths.get("profiles/TestProfileName")))
//
// saveLoadService.deleteProfileTask("TestProfileName").run()
//
// assertFalse(Files.exists(Paths.get("profiles/TestProfileName")))
// }
Double check this is needed:
Bundle fxglServicesBundle = new Bundle("FXGLServices");
fxglServicesBundle.put("globalSoundVolume", 0.5);
fxglServicesBundle.put("fullscreen", false);
fxglServicesBundle.put("globalMusicVolume", 0.5);