Refactoring of config inner classes
This commit is contained in:
+11
-96
@@ -1,4 +1,4 @@
|
||||
package mightypork.gamecore.core;
|
||||
package mightypork.gamecore.core.config;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
@@ -6,111 +6,21 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import mightypork.gamecore.input.KeyStroke;
|
||||
import mightypork.gamecore.input.Keys;
|
||||
import mightypork.utils.files.config.Property;
|
||||
import mightypork.utils.files.config.PropertyManager;
|
||||
import mightypork.utils.logging.Log;
|
||||
|
||||
|
||||
/**
|
||||
* Static application configuration
|
||||
* Static application configuration; wrapper around {@link PropertyManager}
|
||||
*
|
||||
* @author Ondřej Hruška (MightyPork)
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
/**
|
||||
* Config setup. Used to populate the config file.
|
||||
*/
|
||||
public static interface ConfigSetup {
|
||||
|
||||
void addOptions(PropertyManager prop);
|
||||
}
|
||||
|
||||
/**
|
||||
* Key configurator access
|
||||
*/
|
||||
public static class KeyOpts {
|
||||
|
||||
private KeyOpts()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
public void add(String cfgKey, String dataString)
|
||||
{
|
||||
add(cfgKey, dataString, null);
|
||||
}
|
||||
|
||||
|
||||
public void add(String cfgKey, String dataString, String comment)
|
||||
{
|
||||
final KeyProperty kprop = new KeyProperty(prefixKey(cfgKey), KeyStroke.createFromDataString(dataString), comment);
|
||||
strokes.put(prefixKey(cfgKey), kprop);
|
||||
cfg.putProperty(kprop);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Key configurator
|
||||
*/
|
||||
public static interface KeySetup {
|
||||
|
||||
public void addKeys(KeyOpts keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* Key property.<br>
|
||||
* The stored value must be invariant ({@link KeyStroke} is mutable).
|
||||
*
|
||||
* @author Ondřej Hruška (MightyPork)
|
||||
*/
|
||||
public static class KeyProperty extends Property<KeyStroke> {
|
||||
|
||||
public KeyProperty(String key, KeyStroke defaultValue, String comment)
|
||||
{
|
||||
super(key, defaultValue, comment);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public KeyStroke decode(String string, KeyStroke defval)
|
||||
{
|
||||
if (string != null) {
|
||||
// keep it invariant
|
||||
|
||||
final int backup_key = getValue().getKey();
|
||||
final int backup_mod = getValue().getMod();
|
||||
|
||||
getValue().fromDataString(string);
|
||||
if (getValue().getKey() == Keys.NONE) {
|
||||
getValue().setTo(backup_key, backup_mod);
|
||||
}
|
||||
}
|
||||
|
||||
return getValue();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String encode(KeyStroke value)
|
||||
{
|
||||
return value.toDataString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setValue(Object value)
|
||||
{
|
||||
// keep it invariant
|
||||
getValue().setTo(((KeyStroke) value).getKey(), ((KeyStroke) value).getMod());
|
||||
}
|
||||
}
|
||||
|
||||
public static Config.KeyOpts keyOpts = new Config.KeyOpts();
|
||||
public static KeyOpts keyOpts = new KeyOpts();
|
||||
public static Map<String, KeyProperty> strokes = new HashMap<>();
|
||||
|
||||
private static PropertyManager cfg;
|
||||
static PropertyManager cfg;
|
||||
|
||||
|
||||
/**
|
||||
@@ -204,7 +114,12 @@ public class Config {
|
||||
}
|
||||
|
||||
|
||||
private static String prefixKey(String cfgKey)
|
||||
/**
|
||||
* Add "key." before the given config file key
|
||||
* @param cfgKey config key
|
||||
* @return key. + cfgKey
|
||||
*/
|
||||
static String prefixKey(String cfgKey)
|
||||
{
|
||||
return "key." + cfgKey;
|
||||
}
|
||||
@@ -236,7 +151,7 @@ public class Config {
|
||||
*/
|
||||
public static void setKey(String cfgKey, int key, int mod)
|
||||
{
|
||||
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);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package mightypork.gamecore.core.config;
|
||||
|
||||
import mightypork.utils.files.config.PropertyManager;
|
||||
|
||||
/**
|
||||
* Config setup, class used to populate the config file.
|
||||
*/
|
||||
public interface ConfigSetup {
|
||||
|
||||
void addOptions(PropertyManager prop);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package mightypork.gamecore.core.config;
|
||||
|
||||
import mightypork.gamecore.input.KeyStroke;
|
||||
|
||||
/**
|
||||
* Key options - restricted access to {@link Config} for keys
|
||||
*/
|
||||
public class KeyOpts {
|
||||
|
||||
|
||||
public void add(String cfgKey, String dataString)
|
||||
{
|
||||
add(cfgKey, dataString, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param cfgKey key in config file
|
||||
* @param dataString string representing the keystroke (format for {@link KeyStroke})
|
||||
* @param comment optional comment
|
||||
*/
|
||||
public void add(String cfgKey, String dataString, String comment)
|
||||
{
|
||||
final KeyProperty kprop = new KeyProperty(Config.prefixKey(cfgKey), KeyStroke.createFromDataString(dataString), comment);
|
||||
Config.strokes.put(Config.prefixKey(cfgKey), kprop);
|
||||
Config.cfg.putProperty(kprop);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package mightypork.gamecore.core.config;
|
||||
|
||||
import mightypork.gamecore.input.KeyStroke;
|
||||
import mightypork.gamecore.input.Keys;
|
||||
import mightypork.utils.files.config.Property;
|
||||
|
||||
/**
|
||||
* Key property.<br>
|
||||
* The stored value must be invariant ({@link KeyStroke} is mutable).
|
||||
*
|
||||
* @author Ondřej Hruška (MightyPork)
|
||||
*/
|
||||
public class KeyProperty extends Property<KeyStroke> {
|
||||
|
||||
public KeyProperty(String key, KeyStroke defaultValue, String comment)
|
||||
{
|
||||
super(key, defaultValue, comment);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public KeyStroke decode(String string, KeyStroke defval)
|
||||
{
|
||||
if (string != null) {
|
||||
// keep it invariant
|
||||
|
||||
final int backup_key = getValue().getKey();
|
||||
final int backup_mod = getValue().getMod();
|
||||
|
||||
getValue().fromDataString(string);
|
||||
if (getValue().getKey() == Keys.NONE) {
|
||||
getValue().setTo(backup_key, backup_mod);
|
||||
}
|
||||
}
|
||||
|
||||
return getValue();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String encode(KeyStroke value)
|
||||
{
|
||||
return value.toDataString();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setValue(Object value)
|
||||
{
|
||||
// keep it invariant
|
||||
getValue().setTo(((KeyStroke) value).getKey(), ((KeyStroke) value).getMod());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package mightypork.gamecore.core.config;
|
||||
|
||||
|
||||
/**
|
||||
* Key configurator. Config access restricted to key options.
|
||||
*/
|
||||
public interface KeySetup {
|
||||
|
||||
public void addKeys(KeyOpts keys);
|
||||
}
|
||||
@@ -10,11 +10,11 @@ import java.util.logging.Level;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.Config.ConfigSetup;
|
||||
import mightypork.gamecore.core.Config.KeySetup;
|
||||
import mightypork.gamecore.core.WorkDir;
|
||||
import mightypork.gamecore.core.WorkDir.RouteSetup;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.core.config.ConfigSetup;
|
||||
import mightypork.gamecore.core.config.KeySetup;
|
||||
import mightypork.gamecore.gui.screens.ScreenRegistry;
|
||||
import mightypork.gamecore.gui.screens.impl.CrossfadeOverlay;
|
||||
import mightypork.gamecore.input.InputSystem;
|
||||
@@ -66,7 +66,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
public String lockFile = ".lock";
|
||||
|
||||
private final List<ResourceSetup> resourceLists = new ArrayList<>();
|
||||
private final List<Config.KeySetup> keyLists = new ArrayList<>();
|
||||
private final List<KeySetup> keyLists = new ArrayList<>();
|
||||
private final List<ConfigSetup> configLists = new ArrayList<>();
|
||||
private final List<RouteSetup> routeLists = new ArrayList<>();
|
||||
|
||||
@@ -89,7 +89,7 @@ public abstract class BaseApp implements AppAccess, UncaughtExceptionHandler {
|
||||
}
|
||||
|
||||
|
||||
public void addKeys(Config.KeySetup keys)
|
||||
public void addKeys(KeySetup keys)
|
||||
{
|
||||
keyLists.add(keys);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package mightypork.rogue;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.core.events.MainLoopRequest;
|
||||
import mightypork.gamecore.core.events.ShudownRequest;
|
||||
import mightypork.gamecore.core.events.UserQuitRequest;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mightypork.rogue;
|
||||
|
||||
|
||||
import mightypork.gamecore.core.Config.ConfigSetup;
|
||||
import mightypork.gamecore.core.config.ConfigSetup;
|
||||
import mightypork.utils.files.config.PropertyManager;
|
||||
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package mightypork.rogue;
|
||||
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.Config.KeyOpts;
|
||||
import mightypork.gamecore.core.config.KeyOpts;
|
||||
import mightypork.gamecore.core.config.KeySetup;
|
||||
|
||||
|
||||
public class RogueKeys implements Config.KeySetup {
|
||||
public class RogueKeys implements KeySetup {
|
||||
|
||||
@Override
|
||||
public void addKeys(KeyOpts keys)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mightypork.rogue.screens;
|
||||
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.core.modules.AppAccess;
|
||||
import mightypork.gamecore.gui.Action;
|
||||
import mightypork.gamecore.gui.AlignX;
|
||||
|
||||
@@ -3,7 +3,7 @@ package mightypork.rogue.screens.game;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.gui.Action;
|
||||
import mightypork.gamecore.gui.AlignX;
|
||||
import mightypork.gamecore.gui.components.input.TextButton;
|
||||
|
||||
@@ -3,7 +3,7 @@ package mightypork.rogue.screens.game;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.gui.Action;
|
||||
import mightypork.gamecore.gui.AlignX;
|
||||
import mightypork.gamecore.gui.components.input.TextButton;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mightypork.rogue.screens.game;
|
||||
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.gui.AlignX;
|
||||
import mightypork.gamecore.gui.components.layout.ConstraintLayout;
|
||||
import mightypork.gamecore.gui.components.layout.FlowColumnLayout;
|
||||
|
||||
@@ -3,7 +3,7 @@ package mightypork.rogue.screens.game;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.gui.Action;
|
||||
import mightypork.gamecore.gui.AlignX;
|
||||
import mightypork.gamecore.gui.components.input.TextButton;
|
||||
|
||||
@@ -3,7 +3,7 @@ package mightypork.rogue.screens.game;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.core.events.UserQuitRequest;
|
||||
import mightypork.gamecore.core.modules.AppAccess;
|
||||
import mightypork.gamecore.gui.Action;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mightypork.rogue.screens.menu;
|
||||
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.core.events.UserQuitRequest;
|
||||
import mightypork.gamecore.core.modules.AppAccess;
|
||||
import mightypork.gamecore.gui.Action;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package mightypork.rogue.screens.select_world;
|
||||
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.WorkDir;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.core.modules.AppAccess;
|
||||
import mightypork.gamecore.gui.AlignX;
|
||||
import mightypork.gamecore.gui.components.layout.RowLayout;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package mightypork.rogue.screens.story;
|
||||
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.core.modules.AppAccess;
|
||||
import mightypork.gamecore.gui.Action;
|
||||
import mightypork.gamecore.gui.AlignX;
|
||||
|
||||
@@ -5,7 +5,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import mightypork.gamecore.core.Config;
|
||||
import mightypork.gamecore.core.config.Config;
|
||||
import mightypork.gamecore.input.InputSystem;
|
||||
import mightypork.gamecore.input.KeyBindingPool;
|
||||
import mightypork.gamecore.input.KeyStroke;
|
||||
|
||||
Reference in New Issue
Block a user