Skip to content

Commit

Permalink
feat:增加字体大小改变,提示功能
Browse files Browse the repository at this point in the history
  • Loading branch information
iammmmmmm committed Sep 14, 2024
1 parent b937099 commit b99f0e5
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .idea/JavaSceneConfigState.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
> 灰色代表此单词中没有此字母
> 一共有单词长度+1次机会,机会用尽前猜中即为胜利
## 仅支持windows,虽然有安卓和Linux版本但属于是只能亮其它功能随缘能用

安卓平台目前无法正常使用存在一下问题:
~~安卓平台目前无法正常使用存在一下问题:~~

1. ~~中文字体缺失~~(2024.08.26解决)
2. ~~文本输入组件不能正常输入~~(2024.09.12解决)
Expand Down Expand Up @@ -36,12 +34,15 @@
3. ~~解决ui布局问题~~
1. ~~ui布局存在问题,不应使用硬编码设置布局,应调整设置为居中~~
4. 增加GitHubAction自动打包
1. widows未被官方支持
2. linux一直报link失败的错误无法解决
5. 既然有数据库那就增加:
1. 统计系统
1. 字体大小记忆
2. 统计系统
1. 胜率
2. 历史记录
3. 游戏时长
6. 增加提示功能
1. 将黄色块单词的正确位置展示
1. ~~将黄色块单词的正确位置展示~~ (2024.09.14实现)
7. 增加音效
1. 有生之年
96 changes: 77 additions & 19 deletions src/main/java/caidanci/HelloController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import org.kordamp.ikonli.bootstrapicons.BootstrapIcons;
Expand All @@ -27,6 +24,8 @@ public class HelloController {
private final SqlTools sqlTools = new SqlTools();
private final ThemeManager tm = ThemeManager.getInstance();
String[] theme = {new PrimerLight().getUserAgentStylesheet(), new PrimerDark().getUserAgentStylesheet()};
@FXML
private Spinner<Integer> fontSize;
/**
* 主题 0 默认 1 暗黑
*/
Expand All @@ -35,6 +34,7 @@ public class HelloController {
* 单词长度
*/
private int wordLength = 5;
private String yellowWords = "";
private int inputTime = 0;
private boolean gameIsStart = false;
private String answerWord = "12345";
Expand All @@ -56,6 +56,8 @@ public class HelloController {
private Button startGame;
@FXML
private ComboBox<String> fontChose;
@FXML
private Button showHint;

/**
* 切换主题
Expand Down Expand Up @@ -120,22 +122,56 @@ private void inputCheck(String inputText) {
inputTime--;
wordList.add(inputText);
refresh(inputText);
inputTimeUpdate();
if (inputText.equals(answerWord)) {
showAlert(Alert.AlertType.INFORMATION, "胜利!", "恭喜你,成功猜出单词!", answerWord + " :\n" + sqlTools.getWordInfo(answerWord));
startGame();
return;
}
if (inputTime == 0) {
showAlert(Alert.AlertType.INFORMATION, "不,你失败了!", "你耗尽了机会!", "答案:" + answerWord + " :\n" + sqlTools.getWordInfo(answerWord));
startGame();
}
}

private void getYellowWord() {
StringBuilder sb = new StringBuilder();
for (var c : wordList) {
char[] chars = c.toCharArray();
for (var ch : chars) {
if (answerWord.contains(String.valueOf(ch))) {
sb.append(ch);
}
}
}
this.yellowWords = sb.toString();
}

private String retain(String str, String keepChar) {
StringBuilder sb = new StringBuilder();
for (var c : str.toCharArray()) {
if (keepChar.indexOf(c) != -1) {
sb.append(c);
} else {
sb.append(" ");
}
}
return sb.toString();
}

/**
* 刷新显示
*/
private void refresh(String inputText) {
refresh(inputText, outputGrid);
}

/**
* 刷新显示
*/
private void refresh(String inputText, GridPane gridPane) {
int charSize = 40; // 每个字符的大小
outputGrid.setAlignment(Pos.CENTER); // 居中对齐
gridPane.setAlignment(Pos.CENTER); // 居中对齐

// 将字符串分割成字符数组,并在每个格子中添加一个标签
char[] chars = inputText.toCharArray();
Expand All @@ -145,11 +181,11 @@ private void refresh(String inputText) {
label.setPrefSize(charSize, charSize);
label.setAlignment(Pos.CENTER); // 居中对齐
label.setBackground(new Background(new BackgroundFill(getColor(temp, i), CornerRadii.EMPTY, null)));
label.setStyle("-fx-border-color: black; -fx-border-width: 1px;"); // 设置边框样式
outputGrid.add(label, i, rowIndex); // 放置在第一行的不同列
label.setFont(inputButton.getFont());//与全局字体同步
label.setStyle("-fx-border-color: black; -fx-border-width: 1px;-fx-font-size: %d;".formatted(charSize)); // 设置边框样式
gridPane.add(label, i, rowIndex); // 放置在第一行的不同列
}
rowIndex++;
inputTimeUpdate();
}

/**
Expand All @@ -166,24 +202,23 @@ private boolean checkWord(String word) {
*/
@FXML
void initialize() {
Font a = Font.loadFont(Objects.requireNonNull(this.getClass().getResourceAsStream("fonts/fzjt.ttf")), 20);
Font a = Font.loadFont(Objects.requireNonNull(this.getClass().getResourceAsStream("fonts/fzjt.ttf")), 0);
Platform.runLater(() -> tm.setFontFamily(a.getFamily()));
tools.makeFontFamilyChooser(fontChose);
tools.makeFontSizeChooser(fontSize);
Platform.runLater(() -> Application.setUserAgentStylesheet(theme[themeFlag]));
changeTheme.setGraphic(new FontIcon(BootstrapIcons.MOON));
info.setGraphic(new FontIcon(BootstrapIcons.INFO_CIRCLE));
startGame.setText("开始游戏");
levelChose.setDisable(false);
inputButton.setDisable(true);
inputTextFiled.setDisable(true);
gameIsStart = false;
startGame();
SpinnerValueFactory<Integer> valueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(3, 10, wordLength);
levelChose.setValueFactory(valueFactory);
levelChose.valueProperty().addListener((observable, oldValue, newValue) -> {
wordLength = newValue;
inputTimeUpdate();
});
inputTimeUpdate();
this.yellowWords = "";
}

/**
Expand All @@ -195,7 +230,7 @@ private void inputTimeUpdate() {
}

/**
* 获取颜色
* 获取颜色并
*/
private Color getColor(String letter, int index) {
if (answerWord.contains(letter)) {
Expand Down Expand Up @@ -236,23 +271,26 @@ void infoButtonClicked() {
*/
@FXML
void startGame() {
if (gameIsStart) {
if (!gameIsStart) {
startGame.setText("开始游戏");
levelChose.setDisable(false);
inputButton.setDisable(true);
inputTextFiled.setDisable(true);
gameIsStart = false;
showHint.setDisable(true);
gameIsStart = true;
reset();
} else {
answerUpdate();
startGame.setText("结束游戏");
levelChose.setDisable(true);
inputButton.setDisable(false);
inputTextFiled.setDisable(false);
gameIsStart = true;
showHint.setDisable(false);
gameIsStart = false;
wordList = new ArrayList<>();
inputTimeUpdate();
}
inputTimeUpdate();
this.yellowWords = "";
}

/**
Expand Down Expand Up @@ -280,7 +318,27 @@ private void showAlert(Alert.AlertType alertType, String title, String header, S
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(content);
alert.initOwner(inputTextFiled.getScene().getWindow());

alert.showAndWait();
}

@FXML
void showHint() {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.initOwner(inputTextFiled.getScene().getWindow());
tm.addScene(alert.getDialogPane().getScene());
alert.setTitle("提示");
alert.setHeaderText(null);
// 创建自定义的内容
VBox content = new VBox();
// 添加自定义的节点,比如一个进度条
GridPane gridPane = new GridPane();
getYellowWord();
refresh(retain(answerWord, yellowWords), gridPane);
content.getChildren().add(gridPane);

// 设置对话框的内容
alert.getDialogPane().setContent(content);
alert.show();
}
}
10 changes: 9 additions & 1 deletion src/main/java/caidanci/ThemeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ThemeManager {
private final Map<String, String> customCSSRules = new LinkedHashMap<>(); // .foo | -fx-property: value;
private final List<Scene> scene = new ArrayList<>();
private String fontFamily = "FZKai-Z03S";
private Integer fontSize;

public static ThemeManager getInstance() {
return InstanceHolder.INSTANCE;
Expand Down Expand Up @@ -50,7 +51,6 @@ private void reloadCustomCSS() {
css.append("}\n");
});
for (Scene scene : getScene()) {

scene.getRoot().getStylesheets().removeIf(uri -> uri.startsWith("data:text/css"));
scene.getRoot().getStylesheets().add("data:text/css;base64," + Base64.getEncoder().encodeToString(css.toString().getBytes(UTF_8)));
scene.getRoot().pseudoClassStateChanged(USER_CUSTOM, true);
Expand Down Expand Up @@ -87,6 +87,14 @@ public void setFontFamily(String fontFamily) {

public void removeScene(Scene scene) {
this.scene.remove(scene);
}

public void setFontSize(Integer fontSize) {

setCustomDeclaration("-fx-font-size", fontSize.toString());

this.fontSize = fontSize;

reloadCustomCSS();
}

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/caidanci/tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import javafx.geometry.Rectangle2D;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.text.Font;
import javafx.stage.Screen;

Expand Down Expand Up @@ -82,4 +84,16 @@ protected void updateItem(String item, boolean empty) {
}
});
}

public static void makeFontSizeChooser(Spinner<Integer> fontSize) {
SpinnerValueFactory<Integer> valueFactory = new SpinnerValueFactory.IntegerSpinnerValueFactory(
8, 72, 14);
fontSize.setValueFactory(valueFactory);
var tm = ThemeManager.getInstance();
// 添加监听器,当Spinner值改变时,更新Label的字体大小
fontSize.valueProperty().addListener((observable, oldValue, newValue) -> {
tm.setFontSize(newValue);
});

}
}
43 changes: 21 additions & 22 deletions src/main/resources/caidanci/hello-view.fxml
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.StackPane?>
<?import javafx.scene.layout.VBox?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" BorderPane.alignment="CENTER" xmlns="http://javafx.com/javafx/17" xmlns:fx="http://javafx.com/fxml/1" fx:controller="caidanci.HelloController">
<StackPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" maxHeight="-Infinity" maxWidth="-Infinity" BorderPane.alignment="CENTER"
xmlns="http://javafx.com/javafx/17" fx:controller="caidanci.HelloController">
<StackPane AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
AnchorPane.topAnchor="0.0">
<BorderPane prefHeight="606.0" prefWidth="337.0" BorderPane.alignment="CENTER">
<center>
<VBox alignment="TOP_CENTER" spacing="10.0" BorderPane.alignment="CENTER">
<GridPane fx:id="outputGrid" prefHeight="362.0" prefWidth="331.0">
</GridPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="333.0">
<Label text="单词长度:" />
<Spinner fx:id="levelChose" prefHeight="23.0" prefWidth="154.0" />
<Label text="单词长度:"/>
<Spinner fx:id="levelChose" prefHeight="23.0" prefWidth="154.0"/>
</FlowPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="322.0">
<Button fx:id="inputButton" mnemonicParsing="false" onAction="#inputButtonClicked" onMouseClicked="#inputButtonClicked" onTouchReleased="#inputButtonTouched" text="输入" textAlignment="CENTER" />
<TextField fx:id="inputTextFiled" onKeyReleased="#inputFiledKeyRelease" />
<Button fx:id="inputButton" mnemonicParsing="false" onAction="#inputButtonClicked"
onMouseClicked="#inputButtonClicked" onTouchReleased="#inputButtonTouched" text="输入"
textAlignment="CENTER"/>
<TextField fx:id="inputTextFiled" onKeyReleased="#inputFiledKeyRelease"/>
</FlowPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="322.0">
<Button fx:id="startGame" mnemonicParsing="false" onMouseClicked="#startGame" text="开始游戏" />
<Button fx:id="startGame" mnemonicParsing="false" onMouseClicked="#startGame" text="开始游戏"/>
<Button fx:id="showHint" mnemonicParsing="false" onAction="#showHint" text="提示"/>
</FlowPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="322.0">
<Button fx:id="changeTheme" mnemonicParsing="false" onAction="#changeTheme" />
<Button fx:id="info" mnemonicParsing="false" onMouseClicked="#infoButtonClicked" />
<ComboBox fx:id="fontChose" prefWidth="150.0" />
<Button fx:id="changeTheme" mnemonicParsing="false" onAction="#changeTheme"/>
<Button fx:id="info" mnemonicParsing="false" onMouseClicked="#infoButtonClicked"/>
<ComboBox fx:id="fontChose" prefWidth="150.0"/>
</FlowPane>
<FlowPane alignment="TOP_CENTER" prefHeight="23.0" prefWidth="412.0">
<Label text="字体大小:"/>
<Spinner fx:id="fontSize"/>
</FlowPane>
</VBox>
</center>
Expand Down

0 comments on commit b99f0e5

Please sign in to comment.