Skip to content

Commit

Permalink
Model Viewer: Use spinners instead of text fields in the teleport dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadelessFox committed Mar 17, 2024
1 parent cc82d5e commit a62a404
Showing 1 changed file with 19 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import com.shade.decima.model.viewer.Camera;
import com.shade.decima.model.viewer.ModelViewport;
import com.shade.platform.ui.controls.Mnemonic;
import com.shade.platform.ui.controls.validation.InputValidator;
import com.shade.platform.ui.controls.validation.Validation;
import com.shade.platform.ui.dialogs.BaseDialog;
import com.shade.platform.ui.dialogs.BaseEditDialog;
import com.shade.platform.ui.menus.MenuItem;
Expand All @@ -17,7 +15,6 @@
import org.joml.Vector3fc;

import javax.swing.*;
import javax.swing.text.JTextComponent;

import static com.shade.decima.ui.menu.MenuConstants.*;

Expand All @@ -35,81 +32,58 @@ public void perform(@NotNull MenuItemContext ctx) {
}

private static class TeleportToDialog extends BaseEditDialog {
private final JTextField xField;
private final JTextField yField;
private final JTextField zField;
private final JSpinner xSpinner;
private final JSpinner ySpinner;
private final JSpinner zSpinner;

public TeleportToDialog(@NotNull Vector3fc v) {
super("Teleport To");

this.xField = new JTextField(String.valueOf(v.x()));
this.yField = new JTextField(String.valueOf(v.y()));
this.zField = new JTextField(String.valueOf(v.z()));
this.xSpinner = new JSpinner(new SpinnerNumberModel(v.x(), null, null, 0.1f));
this.ySpinner = new JSpinner(new SpinnerNumberModel(v.y(), null, null, 0.1f));
this.zSpinner = new JSpinner(new SpinnerNumberModel(v.z(), null, null, 0.1f));
}

@NotNull
@Override
protected JComponent createContentsPane() {
final JPanel panel = new JPanel();
panel.setLayout(new MigLayout("ins 0", "[][grow,fill][][grow,fill][][grow,fill]"));

UIUtils.installInputValidator(xField, new DoubleValidator(xField), this);
UIUtils.installInputValidator(yField, new DoubleValidator(yField), this);
UIUtils.installInputValidator(zField, new DoubleValidator(zField), this);
panel.setLayout(new MigLayout("ins 0,wrap", "[][grow,fill]"));

final JLabel xLabel = Mnemonic.resolve(new JLabel("&X:"));
final JLabel yLabel = Mnemonic.resolve(new JLabel("&Y:"));
final JLabel zLabel = Mnemonic.resolve(new JLabel("&Z:"));

panel.add(xLabel);
panel.add(xField);
panel.add(xSpinner);

panel.add(yLabel);
panel.add(yField);
panel.add(ySpinner);

panel.add(zLabel);
panel.add(zField);
panel.add(zSpinner);

xLabel.setLabelFor(xField);
yLabel.setLabelFor(yField);
zLabel.setLabelFor(zField);
xLabel.setLabelFor(xSpinner);
yLabel.setLabelFor(ySpinner);
zLabel.setLabelFor(zSpinner);

return panel;
}

@Override
protected boolean isComplete() {
return UIUtils.isValid(xField)
&& UIUtils.isValid(yField)
&& UIUtils.isValid(zField);
return UIUtils.isValid(xSpinner)
&& UIUtils.isValid(ySpinner)
&& UIUtils.isValid(zSpinner);
}

@NotNull
public Vector3f getPosition() {
return new Vector3f(
Float.parseFloat(xField.getText().trim()),
Float.parseFloat(yField.getText().trim()),
Float.parseFloat(zField.getText().trim())
(float) xSpinner.getValue(),
(float) ySpinner.getValue(),
(float) zSpinner.getValue()
);
}
}

private static class DoubleValidator extends InputValidator {
public DoubleValidator(@NotNull JComponent component) {
super(component);
}

@NotNull
@Override
protected Validation validate(@NotNull JComponent input) {
final String text = ((JTextComponent) input).getText().trim();

try {
Float.parseFloat(text);
return Validation.ok();
} catch (NumberFormatException e) {
return Validation.error("Not a number");
}
}
}
}

0 comments on commit a62a404

Please sign in to comment.