com.github.czyzby.autumn.mvc.component.preferences.dto.Preference Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gdx-autumn-mvc Show documentation
Show all versions of gdx-autumn-mvc Show documentation
MVC framework based on LibGDX using Autumn for components management and LML as view templates.
package com.github.czyzby.autumn.mvc.component.preferences.dto;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.github.czyzby.lml.parser.action.ActorConsumer;
/** Wraps around a single preference, allowing to manage it.
*
* @author MJ
*
* @param type of stored preference.
* @see com.github.czyzby.autumn.mvc.stereotype.preference.Property */
public interface Preference {
/** @param name name of this preference.
* @param preferences contain a value mapped to the chosen preference's key, although it is not validated.
* @throws Exception if unable to read preferences. If an exception is thrown, {@link #getDefault()} will be used as
* preference value. */
void read(String name, Preferences preferences) throws Exception;
/** @return default preference value. Used if preference is absent in the preferences file. */
Type getDefault();
/** @param actor used to set up the preference. Has preference setting action attached.
* @return preference value extracted from the actor. */
Type extractFromActor(Actor actor);
/** @return current preference value. */
Type get();
/** @param preference will become current preference value. */
void set(Type preference);
/** @param name name of this preference.
* @param preferences should store the preference. */
void save(String name, Preferences preferences);
/** Returns current preference value.
*
* @author MJ
*
* @param type of the preference. */
public static class PreferenceGetter implements ActorConsumer {
private final Preference preference;
public PreferenceGetter(final Preference preference) {
this.preference = preference;
}
@Override
public Type consume(final Object actor) {
return preference.get();
}
}
/** Sets and returns current preference value.
*
* @author MJ
*
* @param type of the preference. */
public static class PreferenceSetter implements ActorConsumer {
private final Preference preference;
public PreferenceSetter(final Preference preference) {
this.preference = preference;
}
@Override
public Type consume(final Actor actor) {
preference.set(preference.extractFromActor(actor));
return preference.get();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy