Search in sources :

Example 1 with DefaultBindings

use of org.terasology.input.DefaultBindings in project Terasology by MovingBlocks.

the class BindingScraper method main.

/**
 * @param args (ignored)
 * @throws Exception if the module environment cannot be loaded
 */
public static void main(String[] args) throws Exception {
    ModuleManager moduleManager = ModuleManagerFactory.create();
    // Holds normal input mappings where there is only one key
    Multimap<InputCategory, String> categories = ArrayListMultimap.create();
    Multimap<String, Input> keys = ArrayListMultimap.create();
    Map<String, String> desc = new HashMap<>();
    for (Class<?> holdingType : moduleManager.getEnvironment().getTypesAnnotatedWith(InputCategory.class)) {
        InputCategory inputCategory = holdingType.getAnnotation(InputCategory.class);
        categories.put(inputCategory, null);
        for (String button : inputCategory.ordering()) {
            categories.put(inputCategory, button);
        }
    }
    for (Class<?> buttonEvent : moduleManager.getEnvironment().getTypesAnnotatedWith(RegisterBindButton.class)) {
        DefaultBinding defBinding = buttonEvent.getAnnotation(DefaultBinding.class);
        RegisterBindButton info = buttonEvent.getAnnotation(RegisterBindButton.class);
        String cat = info.category();
        String id = "engine:" + info.id();
        desc.put(id, info.description());
        if (cat.isEmpty()) {
            InputCategory inputCategory = findEntry(categories, id);
            if (inputCategory == null) {
                System.out.println("Invalid category for: " + info.id());
            }
        } else {
            InputCategory inputCategory = findCategory(categories, cat);
            if (inputCategory != null) {
                categories.put(inputCategory, id);
            } else {
                System.out.println("Invalid category for: " + info.id());
            }
        }
        if (defBinding != null) {
            // This handles bindings with just one key
            Input input = defBinding.type().getInput(defBinding.id());
            keys.put(id, input);
        } else {
            // See if there is a multi-mapping for this button
            DefaultBindings multiBinding = buttonEvent.getAnnotation(DefaultBindings.class);
            // Annotation math magic. We're expecting a DefaultBindings containing one DefaultBinding pair
            if (multiBinding != null && multiBinding.value().length == 2) {
                DefaultBinding[] bindings = multiBinding.value();
                Input primary = bindings[0].type().getInput(bindings[0].id());
                Input secondary = bindings[1].type().getInput(bindings[1].id());
                keys.put(id, primary);
                keys.put(id, secondary);
            }
        }
    }
    for (InputCategory row : categories.keySet()) {
        System.out.println("# " + row.displayName());
        categories.get(row).stream().filter(entry -> entry != null).forEach(entry -> System.out.println(desc.get(entry) + ": " + keys.get(entry)));
    }
}
Also used : ArrayListMultimap(com.google.common.collect.ArrayListMultimap) InputCategory(org.terasology.input.InputCategory) ModuleManagerFactory(org.terasology.testUtil.ModuleManagerFactory) DefaultBinding(org.terasology.input.DefaultBinding) RegisterBindButton(org.terasology.input.RegisterBindButton) DefaultBindings(org.terasology.input.DefaultBindings) Map(java.util.Map) Input(org.terasology.input.Input) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) ModuleManager(org.terasology.engine.module.ModuleManager) DefaultBinding(org.terasology.input.DefaultBinding) HashMap(java.util.HashMap) RegisterBindButton(org.terasology.input.RegisterBindButton) ModuleManager(org.terasology.engine.module.ModuleManager) Input(org.terasology.input.Input) InputCategory(org.terasology.input.InputCategory) DefaultBindings(org.terasology.input.DefaultBindings)

Aggregations

ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Multimap (com.google.common.collect.Multimap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ModuleManager (org.terasology.engine.module.ModuleManager)1 DefaultBinding (org.terasology.input.DefaultBinding)1 DefaultBindings (org.terasology.input.DefaultBindings)1 Input (org.terasology.input.Input)1 InputCategory (org.terasology.input.InputCategory)1 RegisterBindButton (org.terasology.input.RegisterBindButton)1 ModuleManagerFactory (org.terasology.testUtil.ModuleManagerFactory)1