Skip to content

Commit

Permalink
Code Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Apr 5, 2024
1 parent d3a6ae5 commit 5833d6b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 52 deletions.
2 changes: 1 addition & 1 deletion assets/UI/SettingsModule.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<JFXComboBox fx:id="configDisplayFps" prefWidth="100.0"/>
</HBox>
<HBox>
<Label text="画布边界"/>
<Label text="窗口边界"/>
<JFXComboBox fx:id="configCanvasSize" prefWidth="100.0"/>
</HBox>
<Separator/>
Expand Down
32 changes: 24 additions & 8 deletions desktop/src/cn/harryh/arkpets/controllers/ModelsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,32 @@ public boolean initModelsDataset(boolean doPopNotice) {
} catch (FileNotFoundException e) {
Logger.warn("ModelManager", "Failed to initialize model dataset due to file not found. (" + e.getMessage() + ")");
if (doPopNotice) {
JFXDialog dialog = GuiPrefabs.DialogUtil.createCommonDialog(app.root, GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING), "模型载入失败", "模型未成功载入:未找到数据集。",
"模型数据集文件 " + PathConfig.fileModelsDataPath + " 可能不在工作目录下。\n请先前往 [选项] 进行模型下载。", null);
JFXDialog dialog = GuiPrefabs.DialogUtil.createCommonDialog(app.root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING),
"模型载入失败",
"模型未成功载入:未找到数据集。",
"模型数据集文件 " + PathConfig.fileModelsDataPath + " 可能不在工作目录下。\n请先前往 [选项] 进行模型下载。",
null);
dialog.show();
}
} catch (ModelsDataset.DatasetKeyException e) {
Logger.warn("ModelManager", "Failed to initialize model dataset due to dataset parsing error. (" + e.getMessage() + ")");
if (doPopNotice)
GuiPrefabs.DialogUtil.createCommonDialog(app.root, GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING), "模型载入失败", "模型未成功载入:数据集解析失败。",
"模型数据集可能不完整,或无法被启动器正确识别。请尝试更新模型或更新软件。", null).show();
GuiPrefabs.DialogUtil.createCommonDialog(app.root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING),
"模型载入失败",
"模型未成功载入:数据集解析失败。",
"模型数据集可能不完整,或无法被启动器正确识别。请尝试更新模型或更新软件。",
null).show();
} catch (IOException e) {
Logger.error("ModelManager", "Failed to initialize model dataset due to unknown reasons, details see below.", e);
if (doPopNotice)
GuiPrefabs.DialogUtil.createCommonDialog(app.root, GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING), "模型载入失败", "模型未成功载入:发生意外错误。",
"失败原因概要:" + e.getLocalizedMessage(), null).show();
GuiPrefabs.DialogUtil.createCommonDialog(app.root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING),
"模型载入失败",
"模型未成功载入:发生意外错误。",
"失败原因概要:" + e.getLocalizedMessage(),
null).show();
}
if (mngBtnComposer.getActivatedId() != 0)
mngBtnComposer.activate(0);
Expand Down Expand Up @@ -570,8 +582,12 @@ private boolean assertModelLoaded(boolean doPopNotice) {
if (app.modelsDataset == null) {
// Not loaded:
if (doPopNotice)
GuiPrefabs.DialogUtil.createCommonDialog(app.root, GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING), "未能加载模型", "请确保模型加载成功后再进行此操作。",
"请先在[选项]中进行模型下载。\n如您已下载模型,请尝试点击[重载]按钮。", null).show();
GuiPrefabs.DialogUtil.createCommonDialog(app.root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING),
"未能加载模型",
"请确保模型加载成功后再进行此操作。",
"请先在[选项]中进行模型下载。\n如您已下载模型,请尝试点击[重载]按钮。",
null).show();
return false;
} else {
// Loaded:
Expand Down
62 changes: 37 additions & 25 deletions desktop/src/cn/harryh/arkpets/controllers/SettingsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import cn.harryh.arkpets.guitasks.CheckAppUpdateTask;
import cn.harryh.arkpets.guitasks.GuiTask;
import cn.harryh.arkpets.utils.*;
import cn.harryh.arkpets.utils.GuiComponents.NamedOption;
import cn.harryh.arkpets.utils.GuiComponents.NamedItem;
import com.jfoenix.controls.*;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
Expand All @@ -32,11 +32,11 @@ public final class SettingsModule implements Controller<ArkHomeFX> {
@FXML
private Pane noticeBox;
@FXML
private JFXComboBox<NamedOption<Float>> configDisplayScale;
private JFXComboBox<NamedItem<Float>> configDisplayScale;
@FXML
private JFXComboBox<NamedOption<Integer>> configDisplayFps;
private JFXComboBox<NamedItem<Integer>> configDisplayFps;
@FXML
public JFXComboBox<NamedOption<Integer>> configCanvasSize;
public JFXComboBox<NamedItem<Integer>> configCanvasSize;
@FXML
private JFXComboBox<String> configLoggingLevel;
@FXML
Expand Down Expand Up @@ -74,31 +74,31 @@ public void initializeWith(ArkHomeFX app) {
}

private void initConfigDisplay() {
new GuiComponents.ComboBoxSetup<>(configDisplayScale).setItems(new NamedOption<>("x0.5", 0.5f),
new NamedOption<>("x0.75", 0.75f),
new NamedOption<>("x1.0", 1f),
new NamedOption<>("x1.25", 1.25f),
new NamedOption<>("x1.5", 1.5f),
new NamedOption<>("x2.0", 2f))
new GuiComponents.ComboBoxSetup<>(configDisplayScale).setItems(new NamedItem<>("x0.5", 0.5f),
new NamedItem<>("x0.75", 0.75f),
new NamedItem<>("x1.0", 1f),
new NamedItem<>("x1.25", 1.25f),
new NamedItem<>("x1.5", 1.5f),
new NamedItem<>("x2.0", 2f))
.selectValue(app.config.display_scale, "x" + app.config.display_scale + "(自定义)")
.setOnNonNullValueUpdated((observable, oldValue, newValue) -> {
app.config.display_scale = newValue.value();
app.config.saveConfig();
});
new GuiComponents.ComboBoxSetup<>(configDisplayFps).setItems(new NamedOption<>("25", 25),
new NamedOption<>("30", 30),
new NamedOption<>("45", 45),
new NamedOption<>("60", 60))
new GuiComponents.ComboBoxSetup<>(configDisplayFps).setItems(new NamedItem<>("25", 25),
new NamedItem<>("30", 30),
new NamedItem<>("45", 45),
new NamedItem<>("60", 60))
.selectValue(app.config.display_fps, app.config.display_fps + "(自定义)")
.setOnNonNullValueUpdated((observable, oldValue, newValue) -> {
app.config.display_fps = newValue.value();
app.config.saveConfig();
});
new GuiComponents.ComboBoxSetup<>(configCanvasSize).setItems(new NamedOption<>("最宽", 4),
new NamedOption<>("较宽", 8),
new NamedOption<>("标准", 16),
new NamedOption<>("较窄", 32),
new NamedOption<>("最窄", 0))
new GuiComponents.ComboBoxSetup<>(configCanvasSize).setItems(new NamedItem<>("最宽", 4),
new NamedItem<>("较宽", 8),
new NamedItem<>("标准", 16),
new NamedItem<>("较窄", 32),
new NamedItem<>("最窄", 0))
.selectValue(app.config.canvas_fitting_samples, "每" + app.config.canvas_fitting_samples + "帧采样(自定义)")
.setOnNonNullValueUpdated((observable, oldValue, newValue) -> {
app.config.canvas_fitting_samples = newValue.value();
Expand Down Expand Up @@ -170,15 +170,27 @@ else if (args.contains(Const.LogConfig.debugArg))
configAutoStartup.setOnAction(e -> {
if (configAutoStartup.isSelected()) {
if (ArkConfig.StartupConfig.addStartup()) {
GuiPrefabs.DialogUtil.createCommonDialog(app.root, GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_SUCCESS_ALT, GuiPrefabs.Colors.COLOR_SUCCESS), "开机自启动", "开机自启动设置成功。",
"下次开机时将会自动生成您最后一次启动的桌宠。", null).show();
GuiPrefabs.DialogUtil.createCommonDialog(app.root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_SUCCESS_ALT, GuiPrefabs.Colors.COLOR_SUCCESS),
"开机自启动",
"开机自启动设置成功。",
"下次开机时将会自动生成您最后一次启动的桌宠。",
null).show();
} else {
if (ArkConfig.StartupConfig.generateScript() == null)
GuiPrefabs.DialogUtil.createCommonDialog(app.root, GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING), "开机自启动", "开机自启动设置失败。",
"无法确认目标程序的位置,其原因和相关解决方案如下:", "为确保自启动服务的稳定性,直接打开的ArkPets的\".jar\"版启动器,是不支持配置自启动的。请使用exe版的安装包安装ArkPets后运行,或使用zip版的压缩包解压程序文件后运行。另外,当您使用错误的工作目录运行启动器时也可能出现此情况。").show();
GuiPrefabs.DialogUtil.createCommonDialog(app.root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING),
"开机自启动",
"开机自启动设置失败。",
"无法确认目标程序的位置,其原因和相关解决方案如下:",
"为确保自启动服务的稳定性,直接打开的ArkPets的\".jar\"版启动器,是不支持配置自启动的。请使用exe版的安装包安装ArkPets后运行,或使用zip版的压缩包解压程序文件后运行。另外,当您使用错误的工作目录运行启动器时也可能出现此情况。").show();
else
GuiPrefabs.DialogUtil.createCommonDialog(app.root, GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING), "开机自启动", "开机自启动设置失败。",
"无法写入系统的启动目录,其原因可参见日志文件。", "这有可能是由于权限不足导致的。请尝试关闭反病毒软件,并以管理员权限运行启动器。").show();
GuiPrefabs.DialogUtil.createCommonDialog(app.root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING),
"开机自启动",
"开机自启动设置失败。",
"无法写入系统的启动目录,其原因可参见日志文件。",
"这有可能是由于权限不足导致的。请尝试关闭反病毒软件,并以管理员权限运行启动器。").show();
configAutoStartup.setSelected(false);
}
} else {
Expand Down
12 changes: 4 additions & 8 deletions desktop/src/cn/harryh/arkpets/guitasks/CheckModelUpdateTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ protected void onSucceeded(boolean result) {
// When failed to parse the remote repo models info
versionDescription = "unknown";
Logger.error("Checker", "Unable to parse remote model repo version, details see below.", e);
GuiPrefabs.DialogUtil.createCommonDialog(
root,
GuiPrefabs.DialogUtil.createCommonDialog(root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING),
"检查模型更新",
"无法判断模型仓库版本。",
Expand All @@ -68,8 +67,7 @@ protected void onSucceeded(boolean result) {

if (IOUtils.FileUtil.getMD5(new File(PathConfig.fileModelsDataPath)).equals(IOUtils.FileUtil.getMD5(new File(PathConfig.tempDirPath + PathConfig.fileModelsDataPath)))) {
Logger.info("Checker", "Model repo version check finished (up-to-dated)");
GuiPrefabs.DialogUtil.createCommonDialog(
root,
GuiPrefabs.DialogUtil.createCommonDialog(root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_SUCCESS_ALT, GuiPrefabs.Colors.COLOR_SUCCESS),
"检查模型更新",
"无需进行模型库更新。",
Expand All @@ -86,16 +84,14 @@ protected void onSucceeded(boolean result) {
// When failed to parse the remote local models info
oldVersionDescription = "unknown";
Logger.error("Checker", "Unable to parse local model repo version, details see below.", e);
GuiPrefabs.DialogUtil.createCommonDialog(
root,
GuiPrefabs.DialogUtil.createCommonDialog(root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_WARNING_ALT, GuiPrefabs.Colors.COLOR_WARNING),
"检查模型更新",
"无法判断模型库版本。",
"因发生错误,无法解析本地模型库的版本。",
null).show();
}
GuiPrefabs.DialogUtil.createCommonDialog(
root,
GuiPrefabs.DialogUtil.createCommonDialog(root,
GuiPrefabs.Icons.getIcon(GuiPrefabs.Icons.ICON_INFO_ALT, GuiPrefabs.Colors.COLOR_INFO),
"检查模型更新",
"模型库似乎有更新!",
Expand Down
26 changes: 16 additions & 10 deletions desktop/src/cn/harryh/arkpets/utils/GuiComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@


public class GuiComponents {
/** A useful tool to initialize a {@link Slider} control.
* @param <N> The value type of the Slider.
*/
@SuppressWarnings("UnusedReturnValue")
abstract public static class SliderSetup<N extends Number> {
protected final Slider slider;
Expand Down Expand Up @@ -111,28 +114,31 @@ public final N getValidatedValue() {
}


/** A useful tool to initialize a {@link ComboBox} control whose items are numeric values.
* @param <N> The value type of the items.
*/
@SuppressWarnings("UnusedReturnValue")
public static class ComboBoxSetup<N extends Number> {
protected final ComboBox<NamedOption<N>> comboBox;
protected final ComboBox<NamedItem<N>> comboBox;

public ComboBoxSetup(ComboBox<NamedOption<N>> comboBox) {
public ComboBoxSetup(ComboBox<NamedItem<N>> comboBox) {
this.comboBox = comboBox;
}

@SafeVarargs
public final ComboBoxSetup<N> setItems(NamedOption<N>... elements) {
public final ComboBoxSetup<N> setItems(NamedItem<N>... elements) {
comboBox.getItems().setAll(elements);
return this;
}

public final ComboBoxSetup<N> selectValue(N targetValue, String defaultName) {
if (NamedOption.match(comboBox.getItems(), targetValue) == null)
comboBox.getItems().add(new NamedOption<>(defaultName, targetValue));
comboBox.getSelectionModel().select(NamedOption.match(comboBox.getItems(), targetValue));
if (NamedItem.match(comboBox.getItems(), targetValue) == null)
comboBox.getItems().add(new NamedItem<>(defaultName, targetValue));
comboBox.getSelectionModel().select(NamedItem.match(comboBox.getItems(), targetValue));
return this;
}

public final ComboBoxSetup<N> setOnNonNullValueUpdated(ChangeListener<NamedOption<N>> listener) {
public final ComboBoxSetup<N> setOnNonNullValueUpdated(ChangeListener<NamedItem<N>> listener) {
comboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
if (newValue != null)
listener.changed(observable, oldValue, newValue);
Expand Down Expand Up @@ -169,14 +175,14 @@ protected Integer adjustValue(double rawValue) {
}


public record NamedOption<N extends Number>(String name, N value) {
public record NamedItem<N extends Number>(String name, N value) {
@Override
public String toString() {
return name;
}

public static <N extends Number> NamedOption<N> match(List<NamedOption<N>> candidateOptions, N targetValue) {
for (NamedOption<N> i : candidateOptions)
public static <N extends Number> NamedItem<N> match(List<NamedItem<N>> candidateOptions, N targetValue) {
for (NamedItem<N> i : candidateOptions)
if (targetValue.equals(i.value))
return i;
return null;
Expand Down

0 comments on commit 5833d6b

Please sign in to comment.