|
|
@ -12,7 +12,7 @@ import mightypork.gamecore.util.files.config.PropertyManager; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Static application config file accessor |
|
|
|
* Static application configuration |
|
|
|
* |
|
|
|
* |
|
|
|
* @author MightyPork |
|
|
|
* @author MightyPork |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -31,9 +31,19 @@ public class Config { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static class KeyOpts { |
|
|
|
public static class KeyOpts { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private KeyOpts() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void add(String cfgKey, String dataString) |
|
|
|
public void add(String cfgKey, String dataString) |
|
|
|
{ |
|
|
|
{ |
|
|
|
KeyProperty kprop = new KeyProperty(prefixKey(cfgKey), KeyStroke.createFromDataString(dataString), null); |
|
|
|
add(cfgKey, dataString, null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void add(String cfgKey, String dataString, String comment) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
KeyProperty kprop = new KeyProperty(prefixKey(cfgKey), KeyStroke.createFromDataString(dataString), comment); |
|
|
|
strokes.put(prefixKey(cfgKey), kprop); |
|
|
|
strokes.put(prefixKey(cfgKey), kprop); |
|
|
|
cfg.putProperty(kprop); |
|
|
|
cfg.putProperty(kprop); |
|
|
|
} |
|
|
|
} |
|
|
@ -55,8 +65,7 @@ public class Config { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static class KeyProperty extends Property<KeyStroke> { |
|
|
|
public static class KeyProperty extends Property<KeyStroke> { |
|
|
|
|
|
|
|
|
|
|
|
public KeyProperty(String key, KeyStroke defaultValue, String comment) |
|
|
|
public KeyProperty(String key, KeyStroke defaultValue, String comment) { |
|
|
|
{ |
|
|
|
|
|
|
|
super(key, defaultValue, comment); |
|
|
|
super(key, defaultValue, comment); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -95,52 +104,94 @@ public class Config { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static Config.KeyOpts keyOpts = new Config.KeyOpts(); |
|
|
|
public static Config.KeyOpts keyOpts = new Config.KeyOpts(); |
|
|
|
public static Map<String, Config.KeyProperty> strokes = new HashMap<>(); |
|
|
|
public static Map<String, KeyProperty> strokes = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
private static PropertyManager cfg; |
|
|
|
private static PropertyManager cfg; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Initialize property manger for a file |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param file config file |
|
|
|
|
|
|
|
* @param comment file comment |
|
|
|
|
|
|
|
*/ |
|
|
|
public static void init(File file, String comment) |
|
|
|
public static void init(File file, String comment) |
|
|
|
{ |
|
|
|
{ |
|
|
|
cfg = new PropertyManager(file, comment); |
|
|
|
cfg = new PropertyManager(file, comment); |
|
|
|
|
|
|
|
cfg.cfgNewlineBeforeComments(false); |
|
|
|
|
|
|
|
cfg.cfgSeparateSections(true); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void registerKeys(KeySetup layout) |
|
|
|
/** |
|
|
|
|
|
|
|
* populate config with keys from a key setup |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param keys key setup to add |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static void registerKeys(KeySetup keys) |
|
|
|
{ |
|
|
|
{ |
|
|
|
layout.addKeys(keyOpts); |
|
|
|
keys.addKeys(Config.keyOpts); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
public static void registerOptions(ConfigSetup cfgl) |
|
|
|
* Populate config with options from a config setup |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param conf config setup to add |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public static void registerOptions(ConfigSetup conf) |
|
|
|
{ |
|
|
|
{ |
|
|
|
cfgl.addOptions(cfg); |
|
|
|
conf.addOptions(Config.cfg); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Load config from file |
|
|
|
|
|
|
|
*/ |
|
|
|
public static void load() |
|
|
|
public static void load() |
|
|
|
{ |
|
|
|
{ |
|
|
|
cfg.load(); |
|
|
|
cfg.load(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Save config to file |
|
|
|
|
|
|
|
*/ |
|
|
|
public static void save() |
|
|
|
public static void save() |
|
|
|
{ |
|
|
|
{ |
|
|
|
cfg.save(); |
|
|
|
cfg.save(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Get an option for key |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param key |
|
|
|
|
|
|
|
* @return option value |
|
|
|
|
|
|
|
*/ |
|
|
|
public static <T> T getOption(String key) |
|
|
|
public static <T> T getOption(String key) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
if (cfg.getProperty(key) == null) { |
|
|
|
|
|
|
|
throw new IllegalArgumentException("No such property: " + key); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return cfg.getValue(key); |
|
|
|
return cfg.getValue(key); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set option to a value |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param key option key |
|
|
|
|
|
|
|
* @param value value to set |
|
|
|
|
|
|
|
*/ |
|
|
|
public static <T> void setOption(String key, T value) |
|
|
|
public static <T> void setOption(String key, T value) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
if (cfg.getProperty(key) == null) { |
|
|
|
|
|
|
|
throw new IllegalArgumentException("No such property: " + key); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
cfg.setValue(key, value); |
|
|
|
cfg.setValue(key, value); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -151,18 +202,36 @@ public class Config { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Get keystroke for name |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param cfgKey stroke identifier in config file |
|
|
|
|
|
|
|
* @return the stroke |
|
|
|
|
|
|
|
*/ |
|
|
|
public static KeyStroke getKey(String cfgKey) |
|
|
|
public static KeyStroke getKey(String cfgKey) |
|
|
|
{ |
|
|
|
{ |
|
|
|
final Config.KeyProperty kp = strokes.get(prefixKey(cfgKey)); |
|
|
|
final KeyProperty kp = strokes.get(prefixKey(cfgKey)); |
|
|
|
if (kp == null) throw new IllegalArgumentException("No such stroke: " + cfgKey); |
|
|
|
if (kp == null) { |
|
|
|
|
|
|
|
throw new IllegalArgumentException("No such stroke: " + cfgKey); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return kp.getValue(); |
|
|
|
return kp.getValue(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Set a keystroke for name |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param cfgKey stroke identifier in config file |
|
|
|
|
|
|
|
* @param key stroke key |
|
|
|
|
|
|
|
* @param mod stroke modifiers |
|
|
|
|
|
|
|
*/ |
|
|
|
public static void setKey(String cfgKey, int key, int mod) |
|
|
|
public static void setKey(String cfgKey, int key, int mod) |
|
|
|
{ |
|
|
|
{ |
|
|
|
final Config.KeyProperty kp = strokes.get(prefixKey(cfgKey)); |
|
|
|
final Config.KeyProperty kp = strokes.get(prefixKey(cfgKey)); |
|
|
|
if (kp == null) throw new IllegalArgumentException("No such stroke: " + cfgKey); |
|
|
|
if (kp == null) { |
|
|
|
|
|
|
|
throw new IllegalArgumentException("No such stroke: " + cfgKey); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
kp.getValue().setTo(key, mod); |
|
|
|
kp.getValue().setTo(key, mod); |
|
|
|
} |
|
|
|
} |
|
|
|