BowlerKernel
JogTrainerWidget.java
Go to the documentation of this file.
1 package com.neuronrobotics.sdk.addons.gamepad;
2 
8 import com.neuronrobotics.bowlerstudio.assets.AssetFactory;
9 import com.neuronrobotics.bowlerstudio.assets.ConfigurationDatabase;
10 
11 import javafx.application.Application;
12 import javafx.application.Platform;
13 import javafx.event.ActionEvent;
14 import javafx.event.EventHandler;
15 import javafx.fxml.FXML;
16 import javafx.fxml.FXMLLoader;
17 import javafx.scene.Parent;
18 import javafx.scene.Scene;
19 import javafx.scene.control.Button;
20 import javafx.scene.control.Label;
21 import javafx.scene.control.TextField;
22 import javafx.scene.image.ImageView;
23 import javafx.scene.input.MouseEvent;
24 import javafx.stage.Modality;
25 import javafx.stage.Stage;
26 
27 import java.io.File;
28 import java.net.URL;
29 import java.util.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.ResourceBundle;
33 import java.net.URL;
34 import java.util.ResourceBundle;
35 import javafx.fxml.FXML;
36 import javafx.scene.layout.ColumnConstraints;
37 import javafx.scene.layout.GridPane;
38 import javafx.scene.layout.VBox;
39 
40 public class JogTrainerWidget extends Application implements IGameControlEvent {
41 
42  @FXML // ResourceBundle that was given to the FXMLLoader
43  private ResourceBundle resources;
44 
45  @FXML // fx:id="controllername"
46  private Label controllername; // Value injected by FXMLLoader
47 
48  @FXML // URL location of the FXML file that was given to the FXMLLoader
49  private URL location;
50 
51  @FXML // fx:id="grid"
52  private GridPane grid; // Value injected by FXMLLoader
53  private int mappingIndex = 0;
54  private HashMap<Integer, TextField> fields = new HashMap<>();
55  private String axisWaiting=null;
56  private long timeOfLastAxisSet=0;
57  private ArrayList<String> listOfMappedAxis =new ArrayList<>();
58  private Button save;
59  private Stage primaryStage;
60 
62 
63  @FXML // This method is called by the FXMLLoader when initialization is complete
64  void initialize() {
65  assert grid != null : "fx:id=\"grid\" was not injected: check your FXML file 'jogTrainerWidget.fxml'.";
66  assert gameController != null : "Game controller missing!";
67  //assert primaryStage != null : "Stage missing!";
68  assert controllername!= null: "fx:id=\"grid\" was not injected: check your FXML file 'jogTrainerWidget.fxml'.";
69 
71  save = new Button("Save Mapping");
72  Button reset = new Button("Reset");
73  reset.setOnAction(event -> {
75  for(Integer key:fields.keySet()) {
76  fields.get(key).setText("");
77  }
78  reset();
79  });
80  save.setOnAction(new EventHandler<ActionEvent>() {
81 
82  @Override
83  public void handle(ActionEvent event) {
84  new Thread(()->{
85  List<String> maps = PersistantControllerMap.getDefaultMaps();
86  for (int i = 0; i < maps.size(); i++)
87  ConfigurationDatabase.setObject(gameController.getName(), fields.get(i).getText(), maps.get(i));
89  }).start();
90  if(primaryStage!=null)
91  primaryStage.hide();
92  }
93  });
94 
95  List<String> maps = PersistantControllerMap.getDefaultMaps();
96  int i = 0;
97  System.out.println("There are "+maps.size()+" rows");
98  for (i = 0; i < maps.size(); i++) {
99  String map = maps.get(i);
100 
101  Label name = new Label(map);
102  Label setto = new Label("set to");
103  TextField toBeMapped = new TextField();
106  }
107  if(i!=0)
108  toBeMapped.setDisable(true);
109  grid.add(name, 0, i);
110  grid.add(setto, 1, i);
111  grid.add(toBeMapped, 2, i);
112  fields.put(i, toBeMapped);
113  }
114  grid.add(save, 2, i);
115  grid.add(reset, 1, i);
116  reset();
118  Platform.runLater(() ->fields.get(0).setDisable(true));
120  }else {
122  }
123  }
124 
125 
127  this.gameController = gameController;
128 
129  }
130 
131  @Override
132  public void start(Stage primaryStage) throws Exception {
133  this.primaryStage = primaryStage;
134  File fxmlFIle = AssetFactory.loadFile("layout/jogTrainerWidget.fxml");
135  URL fileURL = fxmlFIle.toURI().toURL();
136  FXMLLoader loader = new FXMLLoader(fileURL);
137  loader.setLocation(fileURL);
138  Parent root;
139  loader.setController(this);
140  // This is needed when loading on MAC
141  loader.setClassLoader(getClass().getClassLoader());
142  root = loader.load();
143  Platform.runLater(() -> {
144  primaryStage.setTitle("Configure the controller");
145 
146  Scene scene = new Scene(root);
147  primaryStage.setScene(scene);
148  primaryStage.initModality(Modality.WINDOW_MODAL);
149  primaryStage.setResizable(true);
150  primaryStage.show();
151 
152  });
153 
154  }
155 
156  public void reset() {
157  listOfMappedAxis.clear();
158  mappingIndex=0;
159  Platform.runLater(() ->fields.get(mappingIndex).setDisable(false));
161  Platform.runLater(() ->controllername.setText(gameController.getName()));
162  save.setDisable(true);
163  }
164 
165 
166 
167  @Override
168  public void onEvent(String name, float value) {
169  //System.out.println(controller);
170  if(axisWaiting!=null) {
171  // waiting for that axis to go back to 0
172  if(axisWaiting.contentEquals(name)) {
173  if(Math.abs(value)>0.0001) {
174  System.out.println("Waiting for value to settle for "+axisWaiting);
175  return;
176  }else {
177  // the axis returned, moving on
178  timeOfLastAxisSet=System.currentTimeMillis();
179  System.out.println("Map done "+axisWaiting);
180  axisWaiting=null;
181  return;
182  }
183  }else {
184  System.out.println("Waiting for value to settle for "+axisWaiting+" got value from "+name);
185  return;
186  }
187  }
188  for(String s:listOfMappedAxis) {
189  if(s.contentEquals(name)) {
190  System.out.println("mapping skipped for "+name);
191  System.out.println(gameController);
192  return;// This axis name is already mapped and will not be mapped again
193  }
194  }
195  for(String s:PersistantControllerMap.getDefaultMaps()) {
196  if(name.contentEquals(s))
197  return;// Do not use maped axis for re-mapping
198  }
199  if(System.currentTimeMillis()-timeOfLastAxisSet<1000) {
200  return;
201  }
202  axisWaiting=name;
203  System.out.println("Adding Axis "+name);
204  listOfMappedAxis.add(name);
205  timeOfLastAxisSet=System.currentTimeMillis();
206  Platform.runLater(() -> {
207  TextField textField = fields.get(mappingIndex);
208  textField.setText(name);
209 
210  textField.setDisable(true);
211  mappingIndex++;
213  save.setDisable(false);
214  gameController.removeListeners(this);
215  }else
216  fields.get(mappingIndex).setDisable(false);
217  });
218 
219  }
220 
221  public static void run(BowlerJInputDevice c) {
222  //System.out.println("Launching Controller mapping");
223  Platform.runLater(new Runnable() {
224  @Override
225  public void run() {
226  //System.out.println("Creating stage");
227  Stage s = new Stage();
228  new Thread() {
229  public void run() {
230  JogTrainerWidget controller = new JogTrainerWidget(c);
231  try {
232  //System.out.println("Loading FXML");
233  controller.start(s);
234  } catch (Exception e) {
235  e.printStackTrace();
236  }
237  }
238  }.start();
239  }
240  });
241 
242  }
243 
244 }
static Object setObject(String paramsKey, String objectKey, Object value)
static String getHardwareAxisFromMappedValue(String controllerName, String mappedValue)
static boolean isMapedAxis(String controllerName, String mappedValue)