初步实现创建界面,编辑界面
This commit is contained in:
@@ -0,0 +1,317 @@
|
||||
package dev.bytevibe.hyperpoint;
|
||||
|
||||
import javafx.geometry.Insets;
|
||||
import javafx.scene.control.*;
|
||||
import javafx.scene.layout.HBox;
|
||||
import javafx.scene.layout.VBox;
|
||||
import javafx.scene.paint.Color;
|
||||
|
||||
/**
|
||||
* 属性编辑面板,用于编辑选中对象的属性
|
||||
*/
|
||||
public class PropertyPanel extends VBox {
|
||||
private DrawingCanvas canvas;
|
||||
private Label objectTypeLabel;
|
||||
private Label positionLabel;
|
||||
private TextField textContentField;
|
||||
private ComboBox<String> fontFamilyCombo;
|
||||
private Spinner<Double> fontSizeSpinner;
|
||||
private ComboBox<String> fontStyleCombo;
|
||||
private ColorPicker textColorPicker;
|
||||
private ColorPicker fillColorPicker;
|
||||
private ColorPicker strokeColorPicker;
|
||||
private Spinner<Double> strokeWidthSpinner;
|
||||
private Button deleteButton;
|
||||
|
||||
public PropertyPanel(DrawingCanvas canvas) {
|
||||
this.canvas = canvas;
|
||||
setPrefWidth(200);
|
||||
setPadding(new Insets(10));
|
||||
setSpacing(8);
|
||||
setStyle("-fx-border-color: #e0e0e0; -fx-border-width: 1 0 0 0;");
|
||||
|
||||
// 对象类型标签
|
||||
objectTypeLabel = new Label("未选中对象");
|
||||
objectTypeLabel.setStyle("-fx-font-size: 14; -fx-font-weight: bold;");
|
||||
|
||||
// 位置标签
|
||||
positionLabel = new Label("X: 0, Y: 0");
|
||||
|
||||
// 文本内容
|
||||
Label textLabel = new Label("文本内容:");
|
||||
textContentField = new TextField();
|
||||
textContentField.setOnKeyReleased(e -> updateTextContent());
|
||||
|
||||
// 字体选择
|
||||
Label fontLabel = new Label("字体:");
|
||||
fontFamilyCombo = new ComboBox<>();
|
||||
fontFamilyCombo.getItems().addAll("Arial", "Times New Roman", "Courier New", "Verdana", "Georgia");
|
||||
fontFamilyCombo.setValue("Arial");
|
||||
fontFamilyCombo.setOnAction(e -> updateTextStyle());
|
||||
|
||||
// 字体大小
|
||||
Label sizeLabel = new Label("大小:");
|
||||
fontSizeSpinner = new Spinner<>(8.0, 72.0, 16.0, 2.0);
|
||||
fontSizeSpinner.setEditable(true);
|
||||
fontSizeSpinner.valueProperty().addListener((obs, oldVal, newVal) -> updateTextStyle());
|
||||
|
||||
// 字体风格
|
||||
Label styleLabel = new Label("风格:");
|
||||
fontStyleCombo = new ComboBox<>();
|
||||
fontStyleCombo.getItems().addAll("NORMAL", "BOLD", "ITALIC", "BOLD_ITALIC");
|
||||
fontStyleCombo.setValue("NORMAL");
|
||||
fontStyleCombo.setOnAction(e -> updateTextStyle());
|
||||
|
||||
// 文本颜色
|
||||
Label textColorLabel = new Label("文本颜色:");
|
||||
textColorPicker = new ColorPicker(Color.BLACK);
|
||||
textColorPicker.setOnAction(e -> updateTextColor());
|
||||
|
||||
// 填充颜色
|
||||
Label fillColorLabel = new Label("填充颜色:");
|
||||
fillColorPicker = new ColorPicker(Color.WHITE);
|
||||
fillColorPicker.setOnAction(e -> updateFillColor());
|
||||
|
||||
// 边框颜色
|
||||
Label strokeColorLabel = new Label("边框颜色:");
|
||||
strokeColorPicker = new ColorPicker(Color.BLACK);
|
||||
strokeColorPicker.setOnAction(e -> updateStrokeColor());
|
||||
|
||||
// 边框宽度
|
||||
Label strokeWidthLabel = new Label("边框宽度:");
|
||||
strokeWidthSpinner = new Spinner<>(1.0, 10.0, 2.0, 1.0);
|
||||
strokeWidthSpinner.setEditable(true);
|
||||
strokeWidthSpinner.valueProperty().addListener((obs, oldVal, newVal) -> updateStrokeWidth());
|
||||
|
||||
// 删除按钮
|
||||
deleteButton = new Button("删除对象");
|
||||
deleteButton.setStyle("-fx-font-size: 12;");
|
||||
deleteButton.setOnAction(e -> deleteSelectedObject());
|
||||
|
||||
// 添加所有控件到面板
|
||||
getChildren().addAll(
|
||||
objectTypeLabel,
|
||||
new Separator(),
|
||||
positionLabel,
|
||||
new Separator(),
|
||||
textLabel,
|
||||
textContentField,
|
||||
fontLabel,
|
||||
fontFamilyCombo,
|
||||
sizeLabel,
|
||||
fontSizeSpinner,
|
||||
styleLabel,
|
||||
fontStyleCombo,
|
||||
textColorLabel,
|
||||
textColorPicker,
|
||||
fillColorLabel,
|
||||
fillColorPicker,
|
||||
strokeColorLabel,
|
||||
strokeColorPicker,
|
||||
strokeWidthLabel,
|
||||
strokeWidthSpinner,
|
||||
new Separator(),
|
||||
deleteButton
|
||||
);
|
||||
|
||||
// 默认隐藏所有编辑控件
|
||||
setEditingVisible(false);
|
||||
|
||||
// 监听canvas的选择变化
|
||||
canvas.setOnSelectionChanged(this::updatePropertyPanel);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新属性面板显示
|
||||
*/
|
||||
private void updatePropertyPanel() {
|
||||
DrawableObject selected = canvas.getSelectedObject();
|
||||
if (selected == null) {
|
||||
setEditingVisible(false);
|
||||
objectTypeLabel.setText("未选中对象");
|
||||
return;
|
||||
}
|
||||
|
||||
objectTypeLabel.setText("选中: " + selected.getTypeName());
|
||||
positionLabel.setText(String.format("X: %.0f, Y: %.0f", selected.getX(), selected.getY()));
|
||||
|
||||
// 清除之前的监听
|
||||
textContentField.setOnKeyReleased(null);
|
||||
|
||||
if (selected instanceof TextObject) {
|
||||
TextObject textObj = (TextObject) selected;
|
||||
textContentField.setText(textObj.getText());
|
||||
fontFamilyCombo.setValue(textObj.getFontFamily());
|
||||
fontSizeSpinner.getValueFactory().setValue(textObj.getFontSize());
|
||||
fontStyleCombo.setValue(textObj.getFontStyle());
|
||||
textColorPicker.setValue(Color.web("#" + textObj.getTextColor()));
|
||||
|
||||
showTextControls();
|
||||
} else if (selected instanceof ShapeObject) {
|
||||
ShapeObject shapeObj = (ShapeObject) selected;
|
||||
fillColorPicker.setValue(Color.web("#" + shapeObj.getFillColor()));
|
||||
strokeColorPicker.setValue(Color.web("#" + shapeObj.getStrokeColor()));
|
||||
strokeWidthSpinner.getValueFactory().setValue(shapeObj.getStrokeWidth());
|
||||
|
||||
showShapeControls();
|
||||
} else if (selected instanceof ImageObject) {
|
||||
showImageControls();
|
||||
}
|
||||
|
||||
// 重新添加监听
|
||||
textContentField.setOnKeyReleased(e -> updateTextContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示文本控件
|
||||
*/
|
||||
private void showTextControls() {
|
||||
textContentField.setDisable(false);
|
||||
fontFamilyCombo.setDisable(false);
|
||||
fontSizeSpinner.setDisable(false);
|
||||
fontStyleCombo.setDisable(false);
|
||||
textColorPicker.setDisable(false);
|
||||
fillColorPicker.setDisable(true);
|
||||
strokeColorPicker.setDisable(true);
|
||||
strokeWidthSpinner.setDisable(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示形状控件
|
||||
*/
|
||||
private void showShapeControls() {
|
||||
textContentField.setDisable(true);
|
||||
fontFamilyCombo.setDisable(true);
|
||||
fontSizeSpinner.setDisable(true);
|
||||
fontStyleCombo.setDisable(true);
|
||||
textColorPicker.setDisable(true);
|
||||
fillColorPicker.setDisable(false);
|
||||
strokeColorPicker.setDisable(false);
|
||||
strokeWidthSpinner.setDisable(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示图片控件
|
||||
*/
|
||||
private void showImageControls() {
|
||||
textContentField.setDisable(true);
|
||||
fontFamilyCombo.setDisable(true);
|
||||
fontSizeSpinner.setDisable(true);
|
||||
fontStyleCombo.setDisable(true);
|
||||
textColorPicker.setDisable(true);
|
||||
fillColorPicker.setDisable(true);
|
||||
strokeColorPicker.setDisable(true);
|
||||
strokeWidthSpinner.setDisable(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置编辑控件的可见性
|
||||
*/
|
||||
private void setEditingVisible(boolean visible) {
|
||||
textContentField.setDisable(!visible);
|
||||
fontFamilyCombo.setDisable(!visible);
|
||||
fontSizeSpinner.setDisable(!visible);
|
||||
fontStyleCombo.setDisable(!visible);
|
||||
textColorPicker.setDisable(!visible);
|
||||
fillColorPicker.setDisable(!visible);
|
||||
strokeColorPicker.setDisable(!visible);
|
||||
strokeWidthSpinner.setDisable(!visible);
|
||||
deleteButton.setDisable(!visible);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文本内容
|
||||
*/
|
||||
private void updateTextContent() {
|
||||
DrawableObject selected = canvas.getSelectedObject();
|
||||
if (selected instanceof TextObject) {
|
||||
((TextObject) selected).setText(textContentField.getText());
|
||||
canvas.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文本风格
|
||||
*/
|
||||
private void updateTextStyle() {
|
||||
DrawableObject selected = canvas.getSelectedObject();
|
||||
if (selected instanceof TextObject) {
|
||||
TextObject textObj = (TextObject) selected;
|
||||
textObj.setFontFamily(fontFamilyCombo.getValue());
|
||||
textObj.setFontSize(fontSizeSpinner.getValue());
|
||||
textObj.setFontStyle(fontStyleCombo.getValue());
|
||||
canvas.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文本颜色
|
||||
*/
|
||||
private void updateTextColor() {
|
||||
DrawableObject selected = canvas.getSelectedObject();
|
||||
if (selected instanceof TextObject) {
|
||||
Color color = textColorPicker.getValue();
|
||||
String hexColor = String.format("%02X%02X%02X",
|
||||
(int) (color.getRed() * 255),
|
||||
(int) (color.getGreen() * 255),
|
||||
(int) (color.getBlue() * 255));
|
||||
((TextObject) selected).setTextColor(hexColor);
|
||||
canvas.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新填充颜色
|
||||
*/
|
||||
private void updateFillColor() {
|
||||
DrawableObject selected = canvas.getSelectedObject();
|
||||
if (selected instanceof ShapeObject) {
|
||||
Color color = fillColorPicker.getValue();
|
||||
String hexColor = String.format("%02X%02X%02X",
|
||||
(int) (color.getRed() * 255),
|
||||
(int) (color.getGreen() * 255),
|
||||
(int) (color.getBlue() * 255));
|
||||
((ShapeObject) selected).setFillColor(hexColor);
|
||||
canvas.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新边框颜色
|
||||
*/
|
||||
private void updateStrokeColor() {
|
||||
DrawableObject selected = canvas.getSelectedObject();
|
||||
if (selected instanceof ShapeObject) {
|
||||
Color color = strokeColorPicker.getValue();
|
||||
String hexColor = String.format("%02X%02X%02X",
|
||||
(int) (color.getRed() * 255),
|
||||
(int) (color.getGreen() * 255),
|
||||
(int) (color.getBlue() * 255));
|
||||
((ShapeObject) selected).setStrokeColor(hexColor);
|
||||
canvas.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新边框宽度
|
||||
*/
|
||||
private void updateStrokeWidth() {
|
||||
DrawableObject selected = canvas.getSelectedObject();
|
||||
if (selected instanceof ShapeObject) {
|
||||
((ShapeObject) selected).setStrokeWidth(strokeWidthSpinner.getValue());
|
||||
canvas.redraw();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除选中的对象
|
||||
*/
|
||||
private void deleteSelectedObject() {
|
||||
DrawableObject selected = canvas.getSelectedObject();
|
||||
if (selected != null) {
|
||||
canvas.removeObject(selected);
|
||||
updatePropertyPanel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user