修改配置类的代码逻辑:

设立一个全局的类(GlobalConfig)专门存放相关的配置类,以静态变量的方式访问这些配置实例。
而不是像以前那样一个动态变量传来传去,眼都看花了...
main
lensfrex 3 years ago
parent 1a091588f5
commit 58d2dbbe62
Signed by: lensfrex
GPG Key ID: 0F69A0A2FBEE98A0
  1. 4
      Dogename/dogename.iml
  2. 52
      Dogename/src/main/java/me/lensferno/dogename/configs/ConfigLoader.java
  3. 6
      Dogename/src/main/java/me/lensferno/dogename/configs/GlobalConfig.java
  4. 40
      Dogename/src/main/java/me/lensferno/dogename/configs/MainConfig.java
  5. 52
      Dogename/src/main/java/me/lensferno/dogename/controllers/MainInterfaceController.java
  6. 29
      Dogename/src/main/java/me/lensferno/dogename/controllers/MiniPaneController.java
  7. 62
      Dogename/src/main/java/me/lensferno/dogename/controllers/SettingsPaneController.java
  8. 17
      Dogename/src/main/java/me/lensferno/dogename/controllers/VoiceSettingsPaneController.java
  9. 25
      Dogename/src/main/java/me/lensferno/dogename/select/Selector.java
  10. 24
      Dogename/src/main/java/me/lensferno/dogename/select/core/Worker.java
  11. 22
      Dogename/src/main/java/me/lensferno/dogename/voice/VoicePlayer.java
  12. 22
      Dogename/src/main/resources/me/lensferno/dogename/FXMLs/SettingsPane.fxml

@ -19,5 +19,9 @@
<orderEntry type="library" name="Maven: org.json:json:20160810" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-simple:1.7.25" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.soundlibs:mp3spi:1.9.5.4" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.soundlibs:jlayer:1.0.1.4" level="project" />
<orderEntry type="library" name="Maven: junit:junit:3.8.2" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.soundlibs:tritonus-share:0.3.7.4" level="project" />
</component>
</module>

@ -20,8 +20,6 @@ import java.nio.charset.StandardCharsets;
public class ConfigLoader {
private final String mainConfigLocation = FilePath.toSpecificPathForm("files/Config.json");
private final String voiceConfigLocation = FilePath.toSpecificPathForm("files/VoiceConfig.json");
//ConfigValuesBean config;
private MainConfig mainConfig;
public String getMainConfigLocation() {
return mainConfigLocation;
@ -31,11 +29,7 @@ public class ConfigLoader {
return voiceConfigLocation;
}
public MainConfig getMainConfig() {
return mainConfig;
}
public MainConfig readConfigFromFile(String fileLocation) {
public void readMainConfig(String fileLocation) {
//property属性应该要自定义一个json适配器才能解析出来
Gson gson = new GsonBuilder()
.registerTypeAdapter(SimpleBooleanProperty.class, new BooleanPropertyAdapter())
@ -52,34 +46,29 @@ public class ConfigLoader {
if (!configFile.exists()) {
configFile.getParentFile().mkdirs();
configFile.createNewFile();
mainConfig = new MainConfig();
GlobalConfig.mainConfig = new MainConfig();
writeMainConfigToFile(mainConfigLocation);
return mainConfig;
return;
}
InputStream inputStream = new FileInputStream(configFile);
configJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
inputStream.close();
mainConfig = gson.fromJson(configJson, MainConfig.class);
GlobalConfig.mainConfig = gson.fromJson(configJson, MainConfig.class);
if (mainConfig == null) {
mainConfig = new MainConfig();
if (GlobalConfig.mainConfig == null) {
GlobalConfig.mainConfig = new MainConfig();
writeMainConfigToFile(mainConfigLocation);
return mainConfig;
}
} catch (Exception e) {
System.out.println("Error to load config file:" + e + "\nUse Default config.");
mainConfig = new MainConfig();
GlobalConfig.mainConfig = new MainConfig();
writeMainConfigToFile(mainConfigLocation);
return mainConfig;
}
return this.mainConfig;
}
public VoiceConfig readVoiceConfigFromFile(String fileLocation) {
public void loadVoiceConfig(String fileLocation) {
//property属性应该要自定义一个json适配器才能解析出来
Gson gson = new GsonBuilder()
.registerTypeAdapter(SimpleBooleanProperty.class, new BooleanPropertyAdapter())
@ -97,31 +86,26 @@ public class ConfigLoader {
configFile.getParentFile().mkdirs();
configFile.createNewFile();
VoicePlayer.voiceConfig = new VoiceConfig();
GlobalConfig.voiceConfig = new VoiceConfig();
writeVoiceConfigToFile(voiceConfigLocation);
return VoicePlayer.voiceConfig;
return;
}
InputStream inputStream = new FileInputStream(configFile);
configJson = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
inputStream.close();
VoicePlayer.voiceConfig = gson.fromJson(configJson, VoiceConfig.class);
GlobalConfig.voiceConfig = gson.fromJson(configJson, VoiceConfig.class);
if (VoicePlayer.voiceConfig == null) {
VoicePlayer.voiceConfig = new VoiceConfig();
if (GlobalConfig.voiceConfig == null) {
GlobalConfig.voiceConfig = new VoiceConfig();
writeVoiceConfigToFile(voiceConfigLocation);
return VoicePlayer.voiceConfig;
}
} catch (Exception e) {
System.out.println("Error to load voice config file:" + e + "\nUse Default voice config.");
VoicePlayer.voiceConfig = new VoiceConfig();
GlobalConfig.voiceConfig = new VoiceConfig();
writeVoiceConfigToFile(voiceConfigLocation);
return VoicePlayer.voiceConfig;
}
return VoicePlayer.voiceConfig;
}
private String toJSON(MainConfig config) {
@ -154,10 +138,10 @@ public class ConfigLoader {
outputFile.createNewFile();
}
OutputStream stream = new FileOutputStream(outputFile);
IOUtils.write(toJSON(this.mainConfig).getBytes(StandardCharsets.UTF_8), stream);
IOUtils.write(toJSON(GlobalConfig.mainConfig).getBytes(StandardCharsets.UTF_8), stream);
OutputStream voiceConfigFileStream = new FileOutputStream(voiceConfigFile);
IOUtils.write(getConfigJson(VoicePlayer.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream);
IOUtils.write(getConfigJson(GlobalConfig.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream);
} catch (Exception e) {
System.out.println("Error in writing all config:" + e);
@ -174,7 +158,7 @@ public class ConfigLoader {
}
OutputStream stream = new FileOutputStream(outputFile);
IOUtils.write(toJSON(this.mainConfig).getBytes(StandardCharsets.UTF_8), stream);
IOUtils.write(toJSON(GlobalConfig.mainConfig).getBytes(StandardCharsets.UTF_8), stream);
} catch (Exception e) {
System.out.println("Error in writing main config:" + e);
@ -191,7 +175,7 @@ public class ConfigLoader {
outputFile.createNewFile();
}
OutputStream voiceConfigFileStream = new FileOutputStream(voiceConfigFile);
IOUtils.write(getConfigJson(VoicePlayer.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream);
IOUtils.write(getConfigJson(GlobalConfig.voiceConfig).getBytes(StandardCharsets.UTF_8), voiceConfigFileStream);
} catch (Exception e) {
System.out.println("Error in writing voice config:" + e);

@ -0,0 +1,6 @@
package me.lensferno.dogename.configs;
public class GlobalConfig {
public static VoiceConfig voiceConfig = null;
public static MainConfig mainConfig = null;
}

@ -8,9 +8,7 @@ import javafx.beans.property.SimpleStringProperty;
public class MainConfig {
// ---------------------- Default values ---------------------------------------------------------
@Expose
public static final boolean DEFAULT_NAME_CHOOSE = true;
public static final int METHOD_NAME = 0; // 名字挑选法
public static final int METHOD_NUMBER = 1; // 数字挑选法
public static final int DEFAULT_MAX_TOTAL_COUNT = 120; // 默认轮回次数:120
@ -21,13 +19,10 @@ public class MainConfig {
public static final boolean DEFAULT_NEW_ALGO = true; // 默认使用新算法"Java sec random"
public static final boolean DEFAULT_VOICE_PLAY = true; // 默认使用语音播报
public static final boolean DEFAULT_SHOW_SAYING = true;
private final int currentVersion = 3;
// ----------------------Properties----------------------------------------------------------------
private final SimpleBooleanProperty nameChoose;
private final SimpleBooleanProperty randomCount; // 挑选次数是否随机
private final SimpleBooleanProperty passSelectedResult; // 是否忽略已经被点过的名字/数字
private final SimpleBooleanProperty ignoreSelectedResult; // 是否忽略已经被点过的名字/数字
private final SimpleIntegerProperty chooseMethod; // 挑选方式: 0->名字挑选法 1->数字挑选法
private final SimpleIntegerProperty maxTotalCount; // 挑选轮回次数
@ -47,10 +42,9 @@ public class MainConfig {
// -------------------------- 初始化 --------------------------------------------------------------
public MainConfig() {
randomCount = new SimpleBooleanProperty(DEFAULT_RANDOM_TIMES);
passSelectedResult = new SimpleBooleanProperty(DEFAULT_IGNORE_PAST);
ignoreSelectedResult = new SimpleBooleanProperty(DEFAULT_IGNORE_PAST);
chooseMethod = new SimpleIntegerProperty(METHOD_NAME);
nameChoose = new SimpleBooleanProperty(DEFAULT_NAME_CHOOSE);
maxTotalCount = new SimpleIntegerProperty(DEFAULT_MAX_TOTAL_COUNT);
@ -68,19 +62,6 @@ public class MainConfig {
}
// -------------------------- Getters and Setters ---------------------------------------------
public boolean getNameChoose() {
return nameChoose.get();
}
public void setNameChoose(boolean nameChoose) {
this.nameChoose.set(nameChoose);
}
public SimpleBooleanProperty nameChooseProperty() {
return nameChoose;
}
public boolean getRandomCount() {
return randomCount.get();
}
@ -93,16 +74,16 @@ public class MainConfig {
return randomCount;
}
public boolean getPassSelectedResult() {
return passSelectedResult.get();
public boolean getIgnoreSelectedResult() {
return ignoreSelectedResult.get();
}
public void setPassSelectedResult(boolean passSelectedResult) {
this.passSelectedResult.set(passSelectedResult);
public void setIgnoreSelectedResult(boolean ignoreSelectedResult) {
this.ignoreSelectedResult.set(ignoreSelectedResult);
}
public SimpleBooleanProperty passSelectedResultProperty() {
return passSelectedResult;
public SimpleBooleanProperty ignoreSelectedResultProperty() {
return ignoreSelectedResult;
}
public int getChooseMethod() {
@ -212,9 +193,4 @@ public class MainConfig {
public SimpleBooleanProperty showSayingProperty() {
return showSaying;
}
public int getCurrentConfigVersion() {
return currentVersion;
}
}

@ -16,17 +16,15 @@ import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import me.lensferno.dogename.configs.ConfigLoader;
import me.lensferno.dogename.configs.GlobalConfig;
import me.lensferno.dogename.configs.MainConfig;
import me.lensferno.dogename.configs.VoiceConfig;
import me.lensferno.dogename.data.Data;
import me.lensferno.dogename.data.History;
import me.lensferno.dogename.select.Selector;
import me.lensferno.dogename.utils.DialogMaker;
import me.lensferno.dogename.utils.FilePath;
import me.lensferno.dogename.utils.ocr.OcrTool;
import me.lensferno.dogename.voice.VoicePlayer;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
@ -38,7 +36,6 @@ public final class MainInterfaceController {
OcrTool ocrTool = null;
History history = new History();
MainConfig mainConfig;
Random random = new Random();
Data data = new Data();
Selector selector = new Selector();
@ -72,14 +69,15 @@ public final class MainInterfaceController {
history.loadHistory();
}
private final ToggleGroup toggleGroup = new ToggleGroup();
public void bindProperties() {
nameChoose.selectedProperty().bindBidirectional(mainConfig.nameChooseProperty());
numbChoose.selectedProperty().bind(mainConfig.nameChooseProperty().not());
numbChoose.selectedProperty().unbind();
nameChoose.setToggleGroup(toggleGroup);
numbChoose.setToggleGroup(toggleGroup);
mainConfig.nameChooseProperty().addListener((observable, oldValue, newValue) -> {
mainConfig.setChooseMethod(mainConfig.getNameChoose() ? MainConfig.METHOD_NAME : MainConfig.METHOD_NUMBER);
toggleGroup.selectToggle(GlobalConfig.mainConfig.getChooseMethod() == MainConfig.METHOD_NAME ? nameChoose : numbChoose);
toggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) -> {
GlobalConfig.mainConfig.setChooseMethod(newValue == nameChoose ? MainConfig.METHOD_NAME : MainConfig.METHOD_NUMBER);
System.out.println("change!"+(newValue == nameChoose));
});
}
@ -88,8 +86,8 @@ public final class MainInterfaceController {
}
public void setUpConfig(ConfigLoader configLoader) {
mainConfig = configLoader.readConfigFromFile(FilePath.toSpecificPathForm("files/Config.json"));
VoicePlayer.voiceConfig = configLoader.readVoiceConfigFromFile(FilePath.toSpecificPathForm("files/VoiceConfig.json"));
configLoader.readMainConfig(FilePath.toSpecificPathForm("files/Config.json"));
configLoader.loadVoiceConfig(FilePath.toSpecificPathForm("files/VoiceConfig.json"));
}
@FXML
@ -116,9 +114,8 @@ public final class MainInterfaceController {
return;
}
NumberSettingsPaneController numberSettingsPaneController = new NumberSettingsPaneController(data);
numberSettingsPaneController.bindProperties(mainConfig);
numberSettingsPaneController.bindProperties(GlobalConfig.mainConfig);
new DialogMaker(rootPane).createDialogWithOneBtn("调整数字", numberSettingsPaneController);
}
@FXML
@ -138,7 +135,7 @@ public final class MainInterfaceController {
miniStage.initStyle(StageStyle.UNDECORATED);
MiniPaneController miniPaneController = loader.getController();
miniPaneController.setBase(data, mainConfig, selector);
miniPaneController.setBase(data, selector);
Stage currentStage = (Stage) anPaiBtn.getScene().getWindow();
miniPaneController.setOldStage(currentStage);
@ -154,11 +151,10 @@ public final class MainInterfaceController {
@FXML
void showSettings(ActionEvent event) {
SettingsPaneController settingsPaneController = new SettingsPaneController();
settingsPaneController.setToggleGroup();
settingsPaneController.bindProperties(mainConfig);
settingsPaneController.bindProperties();
settingsPaneController.setRootPane(rootPane);
@ -177,7 +173,7 @@ public final class MainInterfaceController {
public void init() {
selector.initialVariable(mainConfig, data, history, upperLabel.textProperty(), downLabel.textProperty());
selector.initialVariable(data, history, upperLabel.textProperty(), downLabel.textProperty());
selector.addStoppedEventListener((observableValue, oldValue, stop) -> {
if (stop) {
anPaiBtn.setText("安排一下");
@ -200,11 +196,11 @@ public final class MainInterfaceController {
return;
}
if (mainConfig.getRandomCount()) {
mainConfig.setMaxTotalCount(100 + random.nextInt(151));
if (GlobalConfig.mainConfig.getRandomCount()) {
GlobalConfig.mainConfig.setMaxTotalCount(100 + random.nextInt(151));
}
if (mainConfig.getNameChoose()) {
if (GlobalConfig.mainConfig.getChooseMethod() == MainConfig.METHOD_NAME) {
runNameMode();
} else {
runNumberMode();
@ -225,9 +221,9 @@ public final class MainInterfaceController {
return;
}
if (data.compareNameIgnoreList() && mainConfig.getPassSelectedResult()) {
if (data.compareNameIgnoreList() && GlobalConfig.mainConfig.getIgnoreSelectedResult()) {
if (mainConfig.getEqualMode()) {
if (GlobalConfig.mainConfig.getEqualMode()) {
new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部名字都被点完啦!\n要把名字的忽略列表重置吗?", e -> data.clearNameIgnoreList());
} else {
new DialogMaker(rootPane).createMessageDialog("啊?", "全部名字都被点完啦!\n请多添加几个名字 或 点击“机会均等”的“重置”按钮。");
@ -244,16 +240,16 @@ public final class MainInterfaceController {
try {
int minNumber = Integer.parseInt(mainConfig.getMinNumber());
int maxNumber = Integer.parseInt(mainConfig.getMaxNumber());
int minNumber = Integer.parseInt(GlobalConfig.mainConfig.getMinNumber());
int maxNumber = Integer.parseInt(GlobalConfig.mainConfig.getMaxNumber());
if (maxNumber - minNumber <= 0) {
new DialogMaker(rootPane).createMessageDialog("嗯哼?", "数字要前小后大啊~");
return;
}
if (data.getNumberIgnoreListSize() >= (maxNumber - minNumber + 1) && mainConfig.getPassSelectedResult()) {
if (mainConfig.getEqualMode()) {
if (data.getNumberIgnoreListSize() >= (maxNumber - minNumber + 1) && GlobalConfig.mainConfig.getIgnoreSelectedResult()) {
if (GlobalConfig.mainConfig.getEqualMode()) {
new DialogMaker(rootPane).createDialogWithOKAndCancel("啊?", "全部数字都被点完啦!\n要把数字的忽略列表重置吗?", e -> data.clearNumberIgnoreList());
} else {
new DialogMaker(rootPane).createMessageDialog("啊?", "全部数字都被点完啦!\n请扩大数字范围 或 点击“机会均等”的“重置”按钮。");
@ -280,6 +276,6 @@ public final class MainInterfaceController {
}
public MainConfig getMainConfig() {
return mainConfig;
return GlobalConfig.mainConfig;
}
}

@ -10,6 +10,7 @@ import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.TouchEvent;
import javafx.stage.Stage;
import me.lensferno.dogename.configs.GlobalConfig;
import me.lensferno.dogename.configs.MainConfig;
import me.lensferno.dogename.controllers.WindowListeners.MoveWindowByMouse;
import me.lensferno.dogename.controllers.WindowListeners.MoveWindowByTouch;
@ -32,13 +33,8 @@ public class MiniPaneController {
private JFXButton miniModeBtn;
private final Random random = new Random();
private Data data;
private MainConfig mainConfig;
private Selector selector = new Selector();
public Stage getOldStage() {
return oldStage;
}
public void setOldStage(Stage oldStage) {
this.oldStage = oldStage;
}
@ -51,19 +47,16 @@ public class MiniPaneController {
this.currentStage = currentStage;
}
public void setBase(Data data, MainConfig mainConfig, Selector selector) {
public void setBase(Data data, Selector selector) {
this.data = data;
this.mainConfig = mainConfig;
this.selector = selector;
this.selector.setLabelTexts(chosenNameLabel.textProperty());
this.oldTextProperties = oldTextProperties;
}
@FXML
void recoverMode(ActionEvent event) {
this.oldStage.setOnShown((e) -> {
selector.setLabelTexts(oldTextProperties);
});
// todo: 待修复:界面恢复后label无法正常显示名字
this.oldStage.setOnShown((e) -> selector.setLabelTexts(oldTextProperties));
this.oldStage.show();
currentStage.close();
@ -100,11 +93,11 @@ public class MiniPaneController {
return;
}
if (mainConfig.getRandomCount()) {
mainConfig.setMaxTotalCount(100 + random.nextInt(151));
if (GlobalConfig.mainConfig.getRandomCount()) {
GlobalConfig.mainConfig.setMaxTotalCount(100 + random.nextInt(151));
}
if (mainConfig.getNameChoose()) {
if (GlobalConfig.mainConfig.getChooseMethod() == MainConfig.METHOD_NAME) {
runNameMode();
} else {
runNumberMode();
@ -118,7 +111,7 @@ public class MiniPaneController {
return;
}
if (data.compareNameIgnoreList() && mainConfig.getPassSelectedResult()) {
if (data.compareNameIgnoreList() && GlobalConfig.mainConfig.getIgnoreSelectedResult()) {
return;
}
@ -131,14 +124,14 @@ public class MiniPaneController {
try {
int minNumber = Integer.parseInt(mainConfig.getMinNumber());
int maxNumber = Integer.parseInt(mainConfig.getMaxNumber());
int minNumber = Integer.parseInt(GlobalConfig.mainConfig.getMinNumber());
int maxNumber = Integer.parseInt(GlobalConfig.mainConfig.getMaxNumber());
if (maxNumber - minNumber <= 0) {
return;
}
if (data.getNumberIgnoreListSize() >= (maxNumber - minNumber + 1) && mainConfig.getPassSelectedResult()) {
if (data.getNumberIgnoreListSize() >= (maxNumber - minNumber + 1) && GlobalConfig.mainConfig.getIgnoreSelectedResult()) {
return;
}

@ -9,15 +9,12 @@ import javafx.fxml.FXMLLoader;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import me.lensferno.dogename.configs.MainConfig;
import me.lensferno.dogename.configs.VoiceConfig;
import me.lensferno.dogename.configs.GlobalConfig;
import me.lensferno.dogename.data.Data;
import me.lensferno.dogename.utils.DialogMaker;
import me.lensferno.dogename.voice.VoicePlayer;
public class SettingsPaneController extends VBox {
MainConfig mainConfig;
Pane rootPane;
Data data;
@ -34,9 +31,9 @@ public class SettingsPaneController extends VBox {
@FXML
private JFXCheckBox equalModeBtn;
@FXML
private JFXRadioButton ignoreOnce;
private JFXRadioButton ignoreSelectedResultBtn;
@FXML
private JFXRadioButton chooseOnce;
private JFXRadioButton notIgnoreSelectedResultBtn;
@FXML
private JFXRadioButton randomTimes;
@FXML
@ -53,47 +50,48 @@ public class SettingsPaneController extends VBox {
}
}
public void setMainConfig(MainConfig mainConfig) {
this.mainConfig = mainConfig;
}
public void setRootPane(Pane rootPane) {
this.rootPane = rootPane;
}
public void bindProperties(MainConfig mainConfig) {
setMainConfig(mainConfig);
ignoreOnce.selectedProperty().bindBidirectional(mainConfig.passSelectedResultProperty());
chooseOnce.setSelected(!mainConfig.getPassSelectedResult());
private final ToggleGroup chooseRuleToggleGroup = new ToggleGroup();
private final ToggleGroup chooseCountRuleToggleGroup = new ToggleGroup();
randomTimes.selectedProperty().bindBidirectional(mainConfig.randomCountProperty());
fixedTimes.setSelected(!mainConfig.getRandomCount());
public void bindProperties() {
ignoreSelectedResultBtn.setToggleGroup(chooseRuleToggleGroup);
notIgnoreSelectedResultBtn.setToggleGroup(chooseRuleToggleGroup);
chooseRuleToggleGroup.selectToggle(GlobalConfig.mainConfig.getIgnoreSelectedResult() ? ignoreSelectedResultBtn : notIgnoreSelectedResultBtn);
chooseRuleToggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) ->
GlobalConfig.mainConfig.setIgnoreSelectedResult(newValue == ignoreSelectedResultBtn));
equalModeBtn.selectedProperty().bindBidirectional(mainConfig.equalModeProperty());
GlobalConfig.mainConfig.ignoreSelectedResultProperty().addListener((observable, oldValue, isIgnorePast) -> {
if (!isIgnorePast) {
//如果“忽略被点过的名字”选项被取消后就把机会均等模式的按钮给取消掉
equalModeBtn.setSelected(false);
}
});
newAlgoBtn.selectedProperty().bindBidirectional(mainConfig.secureRandomProperty());
randomTimes.setToggleGroup(chooseCountRuleToggleGroup);
fixedTimes.setToggleGroup(chooseCountRuleToggleGroup);
chooseCountRuleToggleGroup.selectToggle(GlobalConfig.mainConfig.getRandomCount() ? randomTimes : fixedTimes);
chooseCountRuleToggleGroup.selectedToggleProperty().addListener((observable, oldValue, newValue) ->
GlobalConfig.mainConfig.setRandomCount(newValue == randomTimes));
voicePlayBtn.selectedProperty().bindBidirectional(mainConfig.voicePlayProperty());
newAlgoBtn.selectedProperty().bindBidirectional(GlobalConfig.mainConfig.secureRandomProperty());
cycleTimesBar.valueProperty().bindBidirectional(mainConfig.maxTotalCountProperty());
voicePlayBtn.selectedProperty().bindBidirectional(GlobalConfig.mainConfig.voicePlayProperty());
speedBar.valueProperty().bindBidirectional(mainConfig.speedProperty());
cycleTimesBar.valueProperty().bindBidirectional(GlobalConfig.mainConfig.maxTotalCountProperty());
showSayingBtn.selectedProperty().bindBidirectional(mainConfig.showSayingProperty());
speedBar.valueProperty().bindBidirectional(GlobalConfig.mainConfig.speedProperty());
mainConfig.passSelectedResultProperty().addListener((observable, oldValue, isIgnorePast) -> {
if (!isIgnorePast) {
//如果 忽略被点过的名字 被取消后就把机会均等模式的按钮给取消掉
equalModeBtn.setSelected(false);
}
});
showSayingBtn.selectedProperty().bindBidirectional(GlobalConfig.mainConfig.showSayingProperty());
}
public void setToggleGroup() {
ToggleGroup pastGroup = new ToggleGroup();
chooseOnce.setToggleGroup(pastGroup);
ignoreOnce.setToggleGroup(pastGroup);
notIgnoreSelectedResultBtn.setToggleGroup(pastGroup);
ignoreSelectedResultBtn.setToggleGroup(pastGroup);
ToggleGroup fixedTimesGroup = new ToggleGroup();
randomTimes.setToggleGroup(fixedTimesGroup);
@ -123,7 +121,7 @@ public class SettingsPaneController extends VBox {
@FXML
void equalBtnAction(ActionEvent event) {
if (!mainConfig.getPassSelectedResult()) {
if (!GlobalConfig.mainConfig.getIgnoreSelectedResult()) {
equalModeBtn.setSelected(false);
new DialogMaker(rootPane).createMessageDialog("且慢",
"无法在“概率均分”的模式下使用,如需使用请在“人人有份”模式下启用。");

@ -12,6 +12,7 @@ import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import me.lensferno.dogename.configs.GlobalConfig;
import me.lensferno.dogename.configs.VoiceConfig;
import me.lensferno.dogename.utils.DialogMaker;
import me.lensferno.dogename.voice.VoicePlayer;
@ -56,7 +57,7 @@ public class VoiceSettingsPaneController extends VBox {
}
if (speakerList.isEmpty()) {
speakerList.addAll(
"度小宇=1", "度小美=0", "度逍遥=3", "度丫丫=4"
"度小宇", "度小美", "度逍遥", "度丫丫"
// 非高级API用户,这些精品音频库用不了(等把“高级设置”弄出来再说吧...)
/*, "度博文=106", "度小童=110", "度小萌=111", "度米朵=103", "度小娇=5"*/);
}
@ -74,20 +75,20 @@ public class VoiceSettingsPaneController extends VBox {
audioFormatMap.put(VoiceConfig.AUDIO_FORMAT_WAV, wavFormatButton);
audioFormatMap.put(VoiceConfig.AUDIO_FORMAT_MP3, mp3FormatButton);
audioFormatToggleGroup.selectToggle(audioFormatMap.get(VoicePlayer.voiceConfig.getAudioFormat()));
audioFormatToggleGroup.selectToggle(audioFormatMap.get(GlobalConfig.voiceConfig.getAudioFormat()));
audioFormatToggleGroup.selectedToggleProperty().addListener((observable, oldValue, selectBtn) ->
VoicePlayer.voiceConfig.setAudioFormat(selectBtn.equals(wavFormatButton) ? VoiceConfig.AUDIO_FORMAT_WAV : VoiceConfig.AUDIO_FORMAT_MP3));
GlobalConfig.voiceConfig.setAudioFormat(selectBtn.equals(wavFormatButton) ? VoiceConfig.AUDIO_FORMAT_WAV : VoiceConfig.AUDIO_FORMAT_MP3));
speakerSelectBar.getSelectionModel().select(VoicePlayer.voiceConfig.getSpeaker());
speakerSelectBar.getSelectionModel().select(GlobalConfig.voiceConfig.getSpeaker());
speakerSelectBar.valueProperty().addListener((observable, oldValue, newValue) -> {
int selectedSpeakerIndex = speakerSelectBar.getSelectionModel().getSelectedIndex();
VoicePlayer.voiceConfig.setSpeakerIdString(speakers[selectedSpeakerIndex]);
VoicePlayer.voiceConfig.setSpeaker(selectedSpeakerIndex);
GlobalConfig.voiceConfig.setSpeakerIdString(speakers[selectedSpeakerIndex]);
GlobalConfig.voiceConfig.setSpeaker(selectedSpeakerIndex);
});
voiceSpeedBar.valueProperty().bindBidirectional(VoicePlayer.voiceConfig.speedProperty());
intonationBar.valueProperty().bindBidirectional(VoicePlayer.voiceConfig.intonationProperty());
voiceSpeedBar.valueProperty().bindBidirectional(GlobalConfig.voiceConfig.speedProperty());
intonationBar.valueProperty().bindBidirectional(GlobalConfig.voiceConfig.intonationProperty());
}
@FXML

@ -4,6 +4,7 @@ import javafx.animation.AnimationTimer;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import me.lensferno.dogename.configs.GlobalConfig;
import me.lensferno.dogename.configs.MainConfig;
import me.lensferno.dogename.configs.VoiceConfig;
import me.lensferno.dogename.data.Data;
@ -24,10 +25,9 @@ public final class Selector {
public void initialVoicePlayer() {
TokenManager tokenManager = new TokenManager();
VoiceConfig voiceConfig = new VoiceConfig();
tokenManager.init();
if (tokenManager.getTokenStatus() == TokenManager.TOKEN_OK) {
voicePlayer = new VoicePlayer(tokenManager.getToken(), voiceConfig);
voicePlayer = new VoicePlayer(tokenManager.getToken());
}
}
@ -43,8 +43,8 @@ public final class Selector {
processor.stopProcess();
}
public void initialVariable(MainConfig config, Data data, History history, StringProperty... labelTexts) {
processor.initialVariable(config, voicePlayer, data, history, labelTexts);
public void initialVariable(Data data, History history, StringProperty... labelTexts) {
processor.initialVariable(voicePlayer, data, history, labelTexts);
}
public boolean isWorkerRunning() {
@ -61,13 +61,10 @@ public final class Selector {
// ---------------------------------------------------
static class Processor extends AnimationTimer {
MainConfig config = null;
private Worker coreWorker;
protected void initialVariable(MainConfig config, VoicePlayer voicePlayer, Data data, History history, StringProperty... labelTexts) {
coreWorker = new Worker(labelTexts, config, data, history, voicePlayer);
this.config = config;
protected void initialVariable(VoicePlayer voicePlayer, Data data, History history, StringProperty... labelTexts) {
coreWorker = new Worker(labelTexts, data, history, voicePlayer);
}
protected void setWorkerLabelTexts(StringProperty... labelTexts) {
@ -75,8 +72,8 @@ public final class Selector {
}
protected void updateNewValue() {
coreWorker.setSpeed(100 - config.getSpeed());
coreWorker.setMaxTotalCount(config.getMaxTotalCount());
coreWorker.setSpeed(100 - GlobalConfig.mainConfig.getSpeed());
coreWorker.setMaxTotalCount(GlobalConfig.mainConfig.getMaxTotalCount());
}
protected SimpleBooleanProperty stoppedIndicatorProperty() {
@ -100,12 +97,10 @@ public final class Selector {
}
public void setNumberRange() {
int minNumber = Integer.parseInt(config.getMinNumber());
int maxNumber = Integer.parseInt(config.getMaxNumber());
int minNumber = Integer.parseInt(GlobalConfig.mainConfig.getMinNumber());
int maxNumber = Integer.parseInt(GlobalConfig.mainConfig.getMaxNumber());
coreWorker.setNumberRange(minNumber, maxNumber);
}
}
}

@ -2,8 +2,8 @@ package me.lensferno.dogename.select.core;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.StringProperty;
import me.lensferno.dogename.configs.GlobalConfig;
import me.lensferno.dogename.configs.MainConfig;
import me.lensferno.dogename.configs.VoiceConfig;
import me.lensferno.dogename.data.Data;
import me.lensferno.dogename.data.History;
import me.lensferno.dogename.utils.Random;
@ -14,7 +14,6 @@ public final class Worker {
private final Random randomNumber = new Random();
private final SimpleBooleanProperty stoppedIndicator = new SimpleBooleanProperty(true);
private final MainConfig config;
private final Data data;
private final History history;
private final VoicePlayer voicePlayer;
@ -40,9 +39,8 @@ public final class Worker {
private boolean continueSelecting = false;
private int resultLabelId = 0;
public Worker(StringProperty[] labelTexts, MainConfig config, Data data, History history, VoicePlayer voicePlayer) {
public Worker(StringProperty[] labelTexts, Data data, History history, VoicePlayer voicePlayer) {
this.labelTexts = labelTexts;
this.config = config;
this.data = data;
this.history = history;
this.voicePlayer = voicePlayer;
@ -69,8 +67,8 @@ public final class Worker {
// 如果不是这三种情况就选到符合其中一种情况为止
// 由于后两者在安排进程中不可能改变,因此继续挑选后只有选中的结果不在忽略名单中才会结束挑选
// 若忽略名单已满(名单中数量=总的名单数量),点击“安排一下”时会提示已满,无法再进行挑选,因此在运行时总有 忽略名单<总名单,总有名字不在忽略列表中,不会死循环,放心吧
boolean resultIgnored = (config.getChooseMethod() == MainConfig.METHOD_NAME) ? (data.checkNameIgnored(selectedResult)) : (data.checkNumberIgnored(selectedResult));
if (!resultIgnored || !config.getPassSelectedResult() || forceStop) {
boolean resultIgnored = (GlobalConfig.mainConfig.getChooseMethod() == MainConfig.METHOD_NAME) ? (data.checkNameIgnored(selectedResult)) : (data.checkNumberIgnored(selectedResult));
if (!resultIgnored || !GlobalConfig.mainConfig.getIgnoreSelectedResult() || forceStop) {
this.stopSelect();
finalResult = true;
return;
@ -83,25 +81,25 @@ public final class Worker {
resultLabelId = counter.getNewResultLabelId();
finalResult = false;
this.selectedResult = this.pick(config.getChooseMethod());
this.selectedResult = this.pick(GlobalConfig.mainConfig.getChooseMethod());
}
public void stopSelect() {
if (config.getPassSelectedResult()) {
if (config.nameChooseProperty().getValue()) {
if (GlobalConfig.mainConfig.getIgnoreSelectedResult()) {
if (GlobalConfig.mainConfig.getChooseMethod() == MainConfig.METHOD_NAME) {
data.addNameToIgnoreList(selectedResult);
} else {
data.addNumberToIgnoreList(selectedResult);
}
}
if (config.getEqualMode()) {
if (GlobalConfig.mainConfig.getEqualMode()) {
data.writeIgnoreList(Data.IGNORELIST_ALL);
}
history.addHistory(selectedResult);
if (config.getVoicePlay() && voicePlayer != null) {
if (GlobalConfig.mainConfig.getVoicePlay() && voicePlayer != null) {
voicePlayer.playVoice(selectedResult);
}
@ -114,9 +112,9 @@ public final class Worker {
private String pick(int selectMethod) {
switch (selectMethod) {
case MainConfig.METHOD_NAME:
return data.randomGet(config.getSecureRandom());
return data.randomGet(GlobalConfig.mainConfig.getSecureRandom());
case MainConfig.METHOD_NUMBER:
randomNumber.setUseSecureRandom(config.getSecureRandom());
randomNumber.setUseSecureRandom(GlobalConfig.mainConfig.getSecureRandom());
return String.valueOf(randomNumber.getRandomNumber(numberRange[MIN_NUMBER], numberRange[MAX_NUMBER]));
default:
return "(´・ω・`)?";

@ -1,6 +1,7 @@
package me.lensferno.dogename.voice;
import javazoom.spi.mpeg.sampled.file.MpegAudioFileReader;
import me.lensferno.dogename.configs.GlobalConfig;
import me.lensferno.dogename.configs.VoiceConfig;
import me.lensferno.dogename.utils.FilePath;
import me.lensferno.dogename.utils.IOUtil;
@ -17,23 +18,20 @@ public class VoicePlayer {
public static final String cachePath = FilePath.toSpecificPathForm("caches/voice/");
private final Token token;
public static VoiceConfig voiceConfig;
public VoicePlayer(Token token, VoiceConfig voiceConfig) {
public VoicePlayer(Token token) {
this.token = token;
VoicePlayer.voiceConfig = voiceConfig;
}
public void playVoice(String name) {
String speaker = voiceConfig.getSpeakerIdString();
String intonation = String.valueOf(voiceConfig.getIntonation());
String speed = String.valueOf(voiceConfig.getSpeed());
String speaker = GlobalConfig.voiceConfig.getSpeakerIdString();
String intonation = String.valueOf(GlobalConfig.voiceConfig.getIntonation());
String speed = String.valueOf(GlobalConfig.voiceConfig.getSpeed());
String cacheName = String.format("%s_%s_%s_%s", name, speaker, speed, intonation);
File cache = new File(String.format("%s%s.%s", cachePath, cacheName, VoiceConfig.getAudioFileSuffix(voiceConfig.getAudioFormat())));
File cache = new File(String.format("%s%s.%s", cachePath, cacheName, VoiceConfig.getAudioFileSuffix(GlobalConfig.voiceConfig.getAudioFormat())));
new Thread(() -> {
if (!cache.exists()) {
@ -59,7 +57,7 @@ public class VoicePlayer {
speed,
intonation,
speaker,
voiceConfig.getAudioFormat(),
GlobalConfig.voiceConfig.getAudioFormat(),
URLEncoder.encode(name, "UTF-8")
);
@ -94,12 +92,12 @@ public class VoicePlayer {
private void playSound(File file) {
try {
AudioInputStream sourceAudioInputStream;
if (voiceConfig.getAudioFormat() == VoiceConfig.AUDIO_FORMAT_WAV) {
if (GlobalConfig.voiceConfig.getAudioFormat() == VoiceConfig.AUDIO_FORMAT_WAV) {
sourceAudioInputStream = AudioSystem.getAudioInputStream(file);
} else {
sourceAudioInputStream = new MpegAudioFileReader().getAudioInputStream(file);
}
AudioFormat sourceFormat = sourceAudioInputStream.getFormat();
AudioFormat targetFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
sourceFormat.getSampleRate(), 16, sourceFormat.getChannels(),

@ -1,22 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.shape.*?>
<?import com.jfoenix.controls.*?>
<?import javafx.geometry.*?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXCheckBox?>
<?import com.jfoenix.controls.JFXRadioButton?>
<?import com.jfoenix.controls.JFXSlider?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.shape.Line?>
<?import javafx.scene.text.*?>
<?import javafx.scene.text.Font?>
<fx:root prefHeight="360.0" prefWidth="593.0" style="-fx-background-color: white;" type="VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<fx:root prefHeight="360.0" prefWidth="593.0" style="-fx-background-color: white;" type="VBox" xmlns="http://javafx.com/javafx/8.0.321" xmlns:fx="http://javafx.com/fxml/1">
<children>
<HBox prefHeight="50.0" prefWidth="592.0" spacing="10.0">
<children>
<Pane prefHeight="50.0" prefWidth="296.0">
<children>
<JFXRadioButton fx:id="ignoreOnce" layoutX="14.0" layoutY="14.0" selected="true" text="人人有份(跳过已点过的名字)">
<JFXRadioButton fx:id="ignoreSelectedResultBtn" layoutX="14.0" layoutY="14.0" selected="true" text="人人有份(跳过已点过的名字)">
<font>
<Font name="Microsoft YaHei" size="17.0" />
</font>
@ -38,7 +40,7 @@
<children>
<Pane prefHeight="50.0" prefWidth="296.0">
<children>
<JFXRadioButton fx:id="chooseOnce" layoutX="14.0" layoutY="14.0" text="概率均分(不跳过已点过的名字)">
<JFXRadioButton fx:id="notIgnoreSelectedResultBtn" layoutX="14.0" layoutY="14.0" text="概率均分(不跳过已点过的名字)">
<font>
<Font name="Microsoft YaHei" size="17.0" />
</font>

Loading…
Cancel
Save