Skip to content

Commit

Permalink
Add New Features
Browse files Browse the repository at this point in the history
  • Loading branch information
isHarryh committed Apr 27, 2024
1 parent 6836738 commit 1bef435
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 10 deletions.
5 changes: 2 additions & 3 deletions assets/ArkPetsConfigDefault.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
"behavior_do_peer_repulsion":true,
"canvas_fitting_samples":16,
"character_asset":"",
"character_files":{

},
"character_files":{},
"character_label":"",
"display_fps":30,
"display_margin_bottom":0,
"display_multi_monitors":true,
"display_scale":1.0,
"initial_relative_position":[0.2,0.2],
"launcher_solid_exit":true,
"logging_level":"INFO",
"physic_air_friction_acc":100.0,
Expand Down
24 changes: 24 additions & 0 deletions assets/UI/BehaviorModule.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

<!-- ********* Wrapper 2 ********* -->
<?import com.jfoenix.controls.*?>
<?import javafx.scene.canvas.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.shape.*?>
<?import java.lang.*?>
<Pane prefHeight="376.0" prefWidth="460.0" styleClass="wrapper" stylesheets="@Main.css"
xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="cn.harryh.arkpets.controllers.BehaviorModule">
Expand Down Expand Up @@ -44,6 +47,27 @@
<JFXSlider fx:id="configDeployMarginBottom"/>
<Label fx:id="configDeployMarginBottomValue" text="0"/>
</HBox>
<HBox>
<Label text="初始部署位置"/>
<JFXButton fx:id="toggleConfigDeployPosition" minHeight="-Infinity" minWidth="-Infinity"
mnemonicParsing="false" prefHeight="28.0" prefWidth="60.0" text="配置">
<graphic>
<AnchorPane prefHeight="25.0" prefWidth="16.0" scaleX="0.0" styleClass="btn-icon">
<SVGPath
content="m12 1c-3.148 0-6 2.553-6 5.702 0 3.148 2.602 6.907 6 12.298 3.398-5.391 6-9.15 6-12.298 0-3.149-2.851-5.702-6-5.702zm0 8c-1.105 0-2-.895-2-2s.895-2 2-2 2 .895 2 2-.895 2-2 2zm12 14h-24l4-8h3.135c.385.641.798 1.309 1.232 2h-3.131l-2 4h17.527l-2-4h-3.131c.435-.691.848-1.359 1.232-2h3.136l4 8z"
scaleX="1.2" scaleY="1.2" AnchorPane.leftAnchor="-9.0"/>
</AnchorPane>
</graphic>
<styleClass>
<String fx:value="btn-secondary"/>
<String fx:value="btn-with-icon"/>
</styleClass>
</JFXButton>
</HBox>
<HBox fx:id="wrapperConfigDeployPosition" managed="false" visible="false">
<Label styleClass="config-help-text" text="在矩形区域内单击以选定桌宠生成时在屏幕中的初始位置"/>
<Canvas fx:id="configDeployPosition" height="150.0" width="150.0"/>
</HBox>
<Separator/>
<Label styleClass="config-group-title" text="物理"/>
<HBox>
Expand Down
6 changes: 6 additions & 0 deletions assets/UI/Main.css
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ JFXSlider {
-fx-opacity: 0.382;
}

.config-help-text {
-fx-font-size: 12px;
-fx-text-fill: dimgray;
-fx-wrap-text: true;
}

.config-hyper-link {
-fx-text-fill: -theme-color;
-fx-alignment: top-center;
Expand Down
13 changes: 12 additions & 1 deletion core/src/cn/harryh/arkpets/ArkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class ArkConfig {
public int display_margin_bottom;
public boolean display_multi_monitors;
public float display_scale;
public float[] initial_relative_position;
public boolean launcher_solid_exit;
public String logging_level;
public float physic_gravity_acc;
Expand Down Expand Up @@ -97,10 +98,12 @@ public static ArkConfig getConfig() {
}
// Read and parse the custom config file.
try {
return Objects.requireNonNull(
ArkConfig ac = Objects.requireNonNull(
JSONObject.parseObject(IOUtils.FileUtil.readString(configCustom, charsetDefault), ArkConfig.class),
"JSON parsing returns null."
);
ac.fix();
return ac;
} catch (IOException e) {
Logger.error("Config", "Config reading failed, details see below.", e);
} catch (NullPointerException e) {
Expand Down Expand Up @@ -135,6 +138,14 @@ public boolean isAllPhysicConfigZeroed() {
physic_speed_limit_y == 0;
}

private void fix() {
if (initial_relative_position == null
|| initial_relative_position.length != 2
|| initial_relative_position[0] < 0
|| initial_relative_position[1] < 0)
initial_relative_position = defaultConfig.initial_relative_position;
}


/** Only available in Windows OS.
*/
Expand Down
3 changes: 2 additions & 1 deletion core/src/cn/harryh/arkpets/ArkPets.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public void create() {
plane.setObjSize(WD_W, WD_H);
plane.setSpeedLimit(config.physic_speed_limit_x, config.physic_speed_limit_y);
ArkConfig.Monitor primaryMonitor = ArkConfig.Monitor.getMonitors()[0];
initWindow((int)(primaryMonitor.size[0] * 0.1f), (int)(primaryMonitor.size[0] * 0.1f));
initWindow((int)(primaryMonitor.size[0] * config.initial_relative_position[0] - (WD_SCALE * cha.camera.getWidth()) / 2),
(int)(primaryMonitor.size[1] * config.initial_relative_position[1]));
// 5.Tray icon setup
tray = new MemberTrayImpl(this, new SocketClient());
// Setup complete
Expand Down
31 changes: 30 additions & 1 deletion desktop/src/cn/harryh/arkpets/controllers/BehaviorModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
import cn.harryh.arkpets.ArkConfig;
import cn.harryh.arkpets.ArkHomeFX;
import cn.harryh.arkpets.utils.GuiComponents;
import cn.harryh.arkpets.utils.GuiPrefabs;
import cn.harryh.arkpets.utils.Logger;
import com.jfoenix.controls.*;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.canvas.Canvas;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.util.Duration;

import static cn.harryh.arkpets.Const.durationFast;


public final class BehaviorModule implements Controller<ArkHomeFX> {
@FXML
Expand All @@ -39,6 +44,12 @@ public final class BehaviorModule implements Controller<ArkHomeFX> {
@FXML
private Label configDeployMarginBottomValue;
@FXML
private JFXButton toggleConfigDeployPosition;
@FXML
public HBox wrapperConfigDeployPosition;
@FXML
private Canvas configDeployPosition;
@FXML
private JFXSlider configPhysicGravity;
@FXML
private Label configPhysicGravityValue;
Expand Down Expand Up @@ -120,6 +131,24 @@ private void initConfigBehavior() {
app.config.display_margin_bottom = setupDeployMarginBottom.getValidatedValue();
app.config.saveConfig();
});

toggleConfigDeployPosition.setOnAction(e -> {
if (wrapperConfigDeployPosition.isVisible())
GuiPrefabs.fadeOutNode(wrapperConfigDeployPosition, durationFast, null);
else
GuiPrefabs.fadeInNode(wrapperConfigDeployPosition, durationFast, null);
});
GuiComponents.DotPickerSetup setupDeployPosition = new GuiComponents.DotPickerSetup(configDeployPosition);
setupDeployPosition.setRelXY(app.config.initial_relative_position[0], app.config.initial_relative_position[1]);
setupDeployPosition.setOnDotPicked(e -> {
float x = (float)setupDeployPosition.getRelX();
float y = (float)setupDeployPosition.getRelY();
Logger.debug("Config", "Specified deploy position to " + x + ", " + y);
app.config.initial_relative_position[0] = x;
app.config.initial_relative_position[1] = y;
app.config.saveConfig();
});

GuiComponents.SliderSetup<Integer> setupPhysicGravity = new GuiComponents.SimpleMultipleIntegerSliderSetup(configPhysicGravity, 10);
setupPhysicGravity
.setDisplay(configPhysicGravityValue, "%d px/s²", "像素每平方秒 (pixel/s²)")
Expand Down
117 changes: 117 additions & 0 deletions desktop/src/cn/harryh/arkpets/utils/GuiComponents.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.DoublePropertyBase;
import javafx.beans.value.ChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
Expand Down Expand Up @@ -148,6 +152,119 @@ public final ComboBoxSetup<N> setOnNonNullValueUpdated(ChangeListener<NamedItem<
}


/** A useful tool to transform a {@link Canvas} into a dot picker control
* that allows user to pick a dot on a 2D plane.
* Note that the dot's x and y property ranges from {@code 0} (left/top) to {@code 1} (right/bottom),
* which is called "the relative position".
*/
public static class DotPickerSetup {
public static final Color themeColor = Color.valueOf(GuiPrefabs.Colors.COLOR_INFO);
public static final Color alarmColor = Color.valueOf(GuiPrefabs.Colors.COLOR_WARNING);
public static final double metaLengthFactor = 0.025;
public static final double referLineThreshold = 0.025;

public final Canvas canvas;
public final GraphicsContext gc;

protected EventHandler<ActionEvent> listener;
protected boolean picked = false;
protected boolean pending = false;
protected double pickedRelX = 0;
protected double pickedRelY = 0;
protected double pendingRelX = 0;
protected double pendingRelY = 0;

public DotPickerSetup(Canvas canvas) {
this.canvas = canvas;
this.gc = canvas.getGraphicsContext2D();
canvas.setCursor(Cursor.CROSSHAIR);
canvas.setOnMouseClicked(e -> {
setRelXY(e.getX() / canvas.getWidth(), e.getY() / canvas.getHeight());
if (listener != null)
listener.handle(new ActionEvent(this, canvas));
});
canvas.setOnMouseDragged(e -> setPendingRelXY(e.getX() / canvas.getWidth(), e.getY() / canvas.getHeight()));
canvas.setOnMouseMoved(e -> setPendingRelXY(e.getX() / canvas.getWidth(), e.getY() / canvas.getHeight()));
canvas.setOnMouseExited(e -> {
pending = false;
repaint();
});
}

public final double getRelX() {
return pickedRelX;
}

public final double getRelY() {
return pickedRelY;
}

public final void setOnDotPicked(EventHandler<ActionEvent> listener) {
this.listener = listener;
}

public final void setRelXY(double x, double y) {
picked = true;
pickedRelX = x;
pickedRelY = y;
repaint();
}

protected final void setPendingRelXY(double x, double y) {
pending = true;
pendingRelX = x;
pendingRelY = y;
repaint();
}

protected void repaint() {
double maxSide = Math.max(canvas.getWidth(), canvas.getHeight());
double lineSize = maxSide * metaLengthFactor / 2;
double dotSize = maxSide * metaLengthFactor * 2;
gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
// Rectangle
gc.setFill(Color.WHITE);
gc.fillRoundRect(lineSize / 2, lineSize / 2,
canvas.getWidth() - lineSize, canvas.getHeight() - lineSize, dotSize, dotSize);
gc.setLineWidth(lineSize);
gc.setStroke(themeColor.deriveColor(0, 1, 1, 0.7));
gc.strokeRoundRect(lineSize / 2, lineSize / 2,
canvas.getWidth() - lineSize, canvas.getHeight() - lineSize, dotSize, dotSize);
// Reference lines
if (picked) {
pickedRelX = Math.abs(pickedRelX - 0.5) <= referLineThreshold ? 0.5 : pickedRelX;
pickedRelY = Math.abs(pickedRelY - 0.5) <= referLineThreshold ? 0.5 : pickedRelY;
}
if (pending) {
gc.setFill(alarmColor.deriveColor(0, 1, 1, 0.5));
if (Math.abs(pendingRelX - 0.5) <= referLineThreshold) {
pendingRelX = 0.5;
gc.fillRect(canvas.getWidth() / 2 - lineSize / 2, lineSize,
lineSize, canvas.getHeight() - lineSize * 2);
}
if (Math.abs(pendingRelY - 0.5) <= referLineThreshold) {
pendingRelY = 0.5;
gc.fillRect(lineSize, canvas.getHeight() / 2 - lineSize / 2,
canvas.getWidth() - lineSize * 2, lineSize);
}
}
// Dots
if (picked) {
gc.setFill(themeColor);
gc.fillRoundRect(pickedRelX * canvas.getWidth() - dotSize / 2,
pickedRelY * canvas.getHeight() - dotSize / 2,
dotSize, dotSize, dotSize, dotSize);
}
if (pending) {
gc.setFill(themeColor.deriveColor(0, 1, 1, 0.3));
gc.fillRoundRect(pendingRelX * canvas.getWidth() - dotSize / 2,
pendingRelY * canvas.getHeight() - dotSize / 2,
dotSize, dotSize, dotSize, dotSize);
}
}
}


public static final class SimpleIntegerSliderSetup extends SliderSetup<Integer> {
public SimpleIntegerSliderSetup(Slider slider) {
super(slider);
Expand Down
7 changes: 3 additions & 4 deletions desktop/src/cn/harryh/arkpets/utils/GuiPrefabs.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import cn.harryh.arkpets.guitasks.GuiTask;
import cn.harryh.arkpets.guitasks.ZipTask;
import com.jfoenix.controls.*;
import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.animation.*;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.Event;
Expand Down Expand Up @@ -67,6 +64,7 @@ private Colors() {
public static void fadeInNode(Node node, Duration duration, EventHandler<ActionEvent> onFinished) {
FadeTransition fadeT = new FadeTransition(duration, node);
node.setVisible(true);
node.setManaged(true);
if (onFinished != null)
fadeT.setOnFinished(onFinished);
fadeT.setFromValue(0);
Expand All @@ -78,6 +76,7 @@ public static void fadeOutNode(Node node, Duration duration, EventHandler<Action
FadeTransition fadeT = new FadeTransition(duration, node);
fadeT.setOnFinished(e -> {
node.setVisible(false);
node.setManaged(false);
if (onFinished != null)
onFinished.handle(e);
});
Expand Down

0 comments on commit 1bef435

Please sign in to comment.