package mightypork.gamecore.core.config;
import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.input.Keys;
import mightypork.utils.config.propmgr.Property;
/**
* Key property.
* The stored value must stay the same instance ({@link KeyStroke} is mutable).
* That ensures that bindings based on this keystroke are automatically updated
* when the settings change.
*
* @author Ondřej Hruška (MightyPork)
*/
public class KeyStrokeProperty extends Property {
public KeyStrokeProperty(String key, KeyStroke defaultValue, String comment) {
super(key, defaultValue, comment);
}
@Override
public void fromString(String string)
{
if (string != null) {
// keep the same instance
final int backup_key = value.getKey();
final int backup_mod = value.getMod();
value.fromDataString(string);
if (value.getKey() == Keys.NONE) {
value.setTo(backup_key, backup_mod);
}
}
}
@Override
public String toString()
{
return value.toDataString();
}
@Override
public void setValue(Object value)
{
// keep the same instance
this.value.setTo(((KeyStroke) value).getKey(), ((KeyStroke) value).getMod());
}
}