Skip to content

Commit

Permalink
prevent the double clicking of the auto-regen thread
Browse files Browse the repository at this point in the history
  • Loading branch information
madhephaestus committed Jun 19, 2023
1 parent 484ff27 commit fbe52c9
Showing 1 changed file with 38 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.neuronrobotics.sdk.util.ThreadUtil;

import javafx.application.Platform;
import javafx.beans.InvalidationListener;
import javafx.beans.property.Property;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXMLLoader;
Expand Down Expand Up @@ -49,7 +51,11 @@ public class CreatureLab extends AbstractBowlerStudioTab implements IOnEngineeri
private CheckBox autoRegen = new CheckBox("Auto-Regen CAD");
Parent root;
private BowlerJInputDevice gameController = null;
CreatureLabControlsTab tab = new CreatureLabControlsTab();;
CreatureLabControlsTab tab = new CreatureLabControlsTab();

private long timeSinceLastUpdate=0;

private HBox radioOptions;;

@Override
public void onTabClosing() {
Expand All @@ -68,11 +74,10 @@ public void initializeUI(BowlerAbstractDevice pm) {
setGraphic(AssetFactory.loadIcon("CreatureLab-Tab.png"));
this.pm = pm;
autoRegen.setSelected(true);

autoRegen.setDisable(true);
autoRegen.setOnAction(event -> {
baseManager.setAutoRegen(autoRegen.isSelected());
if (autoRegen.isSelected()) {
generateCad();
}
regenFromUiEvent();
});
// TODO Auto-generated method stub
setText(pm.getScriptingName());
Expand Down Expand Up @@ -119,6 +124,21 @@ public void initializeUI(BowlerAbstractDevice pm) {
}
}

private void regenFromUiEvent() {
if(System.currentTimeMillis()-timeSinceLastUpdate<500) {
return;
}
timeSinceLastUpdate = System.currentTimeMillis();
if(autoRegen.isSelected()) {
autoRegen.setDisable(true);
if(radioOptions!=null)radioOptions.setDisable(true);
}
baseManager.setAutoRegen(autoRegen.isSelected());
if (autoRegen.isSelected()) {
generateCad();
}
}

private void finishLoading(MobileBase device) {

TreeItem<String> rootItem = null;
Expand Down Expand Up @@ -232,7 +252,7 @@ public void run() {
setCadMode(true);
});

HBox radioOptions = new HBox(10);
radioOptions = new HBox(10);
radioOptions.getChildren().addAll(new Label("Cad"), rb1, rb2, new Label("Config"));

pi = new ProgressIndicator(0);
Expand All @@ -252,6 +272,17 @@ public void run() {
setCadMode(true);// start the UI in config mode
generateCad();

pi.progressProperty().addListener(new ChangeListener<Number>() {

@Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
System.out.println("Progress listener "+newValue);
if(newValue.doubleValue()>0.99) {
autoRegen.setDisable(false);
if(radioOptions!=null)radioOptions.setDisable(false);
}
}
});
}

private boolean hasWalking(MobileBase device) {
Expand All @@ -261,10 +292,7 @@ private boolean hasWalking(MobileBase device) {
private void setCadMode(boolean mode) {
new Thread(() -> {
baseManager.setConfigurationViewerMode(mode);
baseManager.setAutoRegen(autoRegen.isSelected());
if (autoRegen.isSelected()) {
generateCad();
}
regenFromUiEvent();
}).start();

}
Expand Down

0 comments on commit fbe52c9

Please sign in to comment.