-
Notifications
You must be signed in to change notification settings - Fork 1
Getting started: using the library
FloezeTv edited this page Jan 17, 2021
·
2 revisions
This is the same example as in the README.md
in the repository.
It shows you how to use the library with your own InputSource
as defined here.
It has some comments that should explain to you how this library works.
Here is a basic configuration for our InpuSource.
Remember to change package
to your package.
<?xml version='1.1' encoding='UTF-8'?>
<InputConfiguration>
<player num="0">
<configs for="package.ExampleInputSource">
<key name="input1">
<value class="package.ExampleInputSource..Config">
<value>256</value>
</value>
</key>
</configs>
</player>
</InputConfiguration>
import java.io.IOException;
import tv.floeze.Input4J.Input4J;
public class Example {
public static void main(String[] args) throws ClassNotFoundException, IOException {
// Create a new instance of Input4J and load a default configuration from a file (config.xml in same package).
Input4J<String> input4j = new Input4J<String>(Example.class.getResourceAsStream("config.xml"));
// Add a InputSource and name it 'source1'.
input4j.addInputSource("source1", new ExampleInputSource.Builder(1.5d));
// Enable all InputSources.
// If you only want to enable specific InputSources, use input4j.enable("source1", "source2", ...);
input4j.enableAll();
// You can get the current InputMap by calling input4j.update().
// We'll just print the InputMap.
System.out.println("InputMap: " + input4j.update());
// Save current state of inputs, to later configure a new input.
// If you only want to save the inputs on specific InputSources, use input4j.saveInputs("source1", "source2", ...);
input4j.saveInputs();
// Set a new input.
// This will make the InputSources check if something changed since the last call of input4j.saveInputs() and add that input.
input4j.setInput(0, "input2", (short) 128);
// Let's again print the InputMap to see the current state of inputs.
// You should see a newly added value for "input2"
System.out.println("InputMap: " + input4j.update());
// You can save the new config with the added input by calling one of Input4J's save methods.
// We will just print it to the console.
// Normally, you would save this and load it at the start of your application.
System.out.println("Configuration:\n" + input4j.save());
}
}
When running this example the output should look like this:
InputMap: {0={input1=384}}
InputMap: {0={input2=192, input1=384}}
Configuration:
<?xml version='1.1' encoding='UTF-8'?>
<InputConfiguration>
<player num="0">
<configs for="package.ExampleInputSource">
<key name="input2">
<value class="package.ExampleInputSource..Config">
<value>128</value>
</value>
</key>
<key name="input1">
<value class="package.ExampleInputSource..Config">
<value>256</value>
</value>
</key>
</configs>
</player>
</InputConfiguration>
The first line is the InputMap with only the configuration from the loaded config.
The second line is the InputMap with the newly added configuration.
The rest is the new configuration, that would normally be saved and loaded the next time.