Search in sources :

Example 1 with AddShape

use of net.sf.latexdraw.command.shape.AddShape in project latexdraw by arnobl.

the class Pencil method bindPressToAddShape.

/**
 * Binds a press interaction to add a shape.
 */
private void bindPressToAddShape() {
    // Add axes, grids, or dots
    nodeBinder().usingInteraction(Press::new).toProduce(i -> {
        final PositionShape sh = (PositionShape) editing.createShapeInstance();
        sh.setPosition(getAdaptedPoint(i.getSrcLocalPoint()));
        return new AddShape(sh, canvas.getDrawing());
    }).on(canvas).when(i -> i.getButton() == MouseButton.PRIMARY && (editing.getCurrentChoice() == EditionChoice.GRID || editing.getCurrentChoice() == EditionChoice.DOT || editing.getCurrentChoice() == EditionChoice.AXES)).bind();
    // When a user starts to type a text using the text setter and then he clicks somewhere else in the canvas,
    // the text typed must be added (if possible to the canvas) before starting typing a new text.
    nodeBinder().usingInteraction(Press::new).toProduce(i -> new AddShape(ShapeFactory.INST.createText(ShapeFactory.INST.createPoint(textSetter.getPosition()), textSetter.getTextField().getText()), canvas.getDrawing())).on(canvas).when(i -> textSetter.isActivated() && !textSetter.getTextField().getText().isEmpty() && editing.getCurrentChoice() == EditionChoice.TEXT).bind();
}
Also used : EditingService(net.sf.latexdraw.service.EditingService) Arrays(java.util.Arrays) MouseButton(javafx.scene.input.MouseButton) ControlPointShape(net.sf.latexdraw.model.api.shape.ControlPointShape) BezierCurve(net.sf.latexdraw.model.api.shape.BezierCurve) Freehand(net.sf.latexdraw.model.api.shape.Freehand) Point3D(javafx.geometry.Point3D) BorderPos(net.sf.latexdraw.model.api.shape.BorderPos) InsertPicture(net.sf.latexdraw.command.shape.InsertPicture) Function(java.util.function.Function) Polyline(net.sf.latexdraw.model.api.shape.Polyline) ShapeFactory(net.sf.latexdraw.model.ShapeFactory) Shape(net.sf.latexdraw.model.api.shape.Shape) Inject(net.sf.latexdraw.util.Inject) PointsData(io.github.interacto.jfx.interaction.library.PointsData) MathUtils(net.sf.latexdraw.model.MathUtils) AddShape(net.sf.latexdraw.command.shape.AddShape) Canvas(net.sf.latexdraw.view.jfx.Canvas) ViewFactory(net.sf.latexdraw.view.jfx.ViewFactory) Click(io.github.interacto.jfx.interaction.library.Click) InitTextSetter(net.sf.latexdraw.command.shape.InitTextSetter) MultiClick(io.github.interacto.jfx.interaction.library.MultiClick) ModifiablePointsShape(net.sf.latexdraw.model.api.shape.ModifiablePointsShape) Polygon(net.sf.latexdraw.model.api.shape.Polygon) Press(io.github.interacto.jfx.interaction.library.Press) PositionShape(net.sf.latexdraw.model.api.shape.PositionShape) Point(net.sf.latexdraw.model.api.shape.Point) MagneticGrid(net.sf.latexdraw.view.jfx.MagneticGrid) Objects(java.util.Objects) FileChooser(javafx.stage.FileChooser) SquaredShape(net.sf.latexdraw.model.api.shape.SquaredShape) RectangularShape(net.sf.latexdraw.model.api.shape.RectangularShape) NotNull(org.jetbrains.annotations.NotNull) DnD(io.github.interacto.jfx.interaction.library.DnD) AddShape(net.sf.latexdraw.command.shape.AddShape) PositionShape(net.sf.latexdraw.model.api.shape.PositionShape) Press(io.github.interacto.jfx.interaction.library.Press)

Example 2 with AddShape

use of net.sf.latexdraw.command.shape.AddShape in project latexdraw by arnobl.

the class Pencil method bindMultiClic2AddShape.

/**
 * Binds a multi-click interaction to creates multi-point shapes.
 */
private void bindMultiClic2AddShape() {
    final Function<PointsData, AddShape> creation = i -> new AddShape(setInitialPtsShape(editing.createShapeInstance(), i.getPointsData().get(0).getSrcLocalPoint()), canvas.getDrawing());
    // Binding for polygons
    nodeBinder().usingInteraction(() -> new MultiClick(3)).toProduce(creation).on(canvas).then((i, c) -> {
        final Point currPoint = getAdaptedPoint(i.getCurrentPosition());
        if (c.getShape().getNbPoints() == i.getPointsData().size() && i.getLastButton().orElse(MouseButton.NONE) == MouseButton.PRIMARY) {
            c.setShape(ShapeFactory.INST.createPolygonFrom((Polygon) c.getShape(), ShapeFactory.INST.createPoint(currPoint.getX(), currPoint.getY())));
        } else {
            ((ModifiablePointsShape) c.getShape()).setPoint(currPoint.getX(), currPoint.getY(), -1);
        }
        canvas.setTempView(viewFactory.createView(c.getShape()).orElse(null));
    }).strictStart().endOrCancel(i -> canvas.setTempView(null)).when(() -> editing.getCurrentChoice() == EditionChoice.POLYGON).bind();
    // Binding for polyline
    nodeBinder().usingInteraction(MultiClick::new).toProduce(creation).on(canvas).then((i, c) -> {
        final Point currPoint = getAdaptedPoint(i.getCurrentPosition());
        if (c.getShape().getNbPoints() == i.getPointsData().size() && i.getLastButton().orElse(MouseButton.NONE) == MouseButton.PRIMARY) {
            c.setShape(ShapeFactory.INST.createPolylineFrom((Polyline) c.getShape(), ShapeFactory.INST.createPoint(currPoint.getX(), currPoint.getY())));
        } else {
            ((ModifiablePointsShape) c.getShape()).setPoint(currPoint.getX(), currPoint.getY(), -1);
        }
        canvas.setTempView(viewFactory.createView(c.getShape()).orElse(null));
    }).strictStart().endOrCancel(i -> canvas.setTempView(null)).when(() -> editing.getCurrentChoice() == EditionChoice.LINES).bind();
    // Binding for bézier curves
    nodeBinder().usingInteraction(MultiClick::new).toProduce(creation).on(canvas).then((i, c) -> {
        final Point currPoint = getAdaptedPoint(i.getCurrentPosition());
        if (c.getShape().getNbPoints() == i.getPointsData().size() && i.getLastButton().orElse(MouseButton.NONE) == MouseButton.PRIMARY) {
            c.setShape(ShapeFactory.INST.createBezierCurveFrom((BezierCurve) c.getShape(), ShapeFactory.INST.createPoint(currPoint.getX(), currPoint.getY())));
        } else {
            ((ModifiablePointsShape) c.getShape()).setPoint(currPoint.getX(), currPoint.getY(), -1);
        }
        ((ControlPointShape) c.getShape()).balance();
        canvas.setTempView(viewFactory.createView(c.getShape()).orElse(null));
    }).strictStart().endOrCancel(i -> canvas.setTempView(null)).when(() -> editing.getCurrentChoice() == EditionChoice.BEZIER_CURVE).bind();
}
Also used : EditingService(net.sf.latexdraw.service.EditingService) Arrays(java.util.Arrays) MouseButton(javafx.scene.input.MouseButton) ControlPointShape(net.sf.latexdraw.model.api.shape.ControlPointShape) BezierCurve(net.sf.latexdraw.model.api.shape.BezierCurve) Freehand(net.sf.latexdraw.model.api.shape.Freehand) Point3D(javafx.geometry.Point3D) BorderPos(net.sf.latexdraw.model.api.shape.BorderPos) InsertPicture(net.sf.latexdraw.command.shape.InsertPicture) Function(java.util.function.Function) Polyline(net.sf.latexdraw.model.api.shape.Polyline) ShapeFactory(net.sf.latexdraw.model.ShapeFactory) Shape(net.sf.latexdraw.model.api.shape.Shape) Inject(net.sf.latexdraw.util.Inject) PointsData(io.github.interacto.jfx.interaction.library.PointsData) MathUtils(net.sf.latexdraw.model.MathUtils) AddShape(net.sf.latexdraw.command.shape.AddShape) Canvas(net.sf.latexdraw.view.jfx.Canvas) ViewFactory(net.sf.latexdraw.view.jfx.ViewFactory) Click(io.github.interacto.jfx.interaction.library.Click) InitTextSetter(net.sf.latexdraw.command.shape.InitTextSetter) MultiClick(io.github.interacto.jfx.interaction.library.MultiClick) ModifiablePointsShape(net.sf.latexdraw.model.api.shape.ModifiablePointsShape) Polygon(net.sf.latexdraw.model.api.shape.Polygon) Press(io.github.interacto.jfx.interaction.library.Press) PositionShape(net.sf.latexdraw.model.api.shape.PositionShape) Point(net.sf.latexdraw.model.api.shape.Point) MagneticGrid(net.sf.latexdraw.view.jfx.MagneticGrid) Objects(java.util.Objects) FileChooser(javafx.stage.FileChooser) SquaredShape(net.sf.latexdraw.model.api.shape.SquaredShape) RectangularShape(net.sf.latexdraw.model.api.shape.RectangularShape) NotNull(org.jetbrains.annotations.NotNull) DnD(io.github.interacto.jfx.interaction.library.DnD) MultiClick(io.github.interacto.jfx.interaction.library.MultiClick) PointsData(io.github.interacto.jfx.interaction.library.PointsData) AddShape(net.sf.latexdraw.command.shape.AddShape) Polyline(net.sf.latexdraw.model.api.shape.Polyline) ModifiablePointsShape(net.sf.latexdraw.model.api.shape.ModifiablePointsShape) Point(net.sf.latexdraw.model.api.shape.Point) Polygon(net.sf.latexdraw.model.api.shape.Polygon) BezierCurve(net.sf.latexdraw.model.api.shape.BezierCurve) ControlPointShape(net.sf.latexdraw.model.api.shape.ControlPointShape)

Example 3 with AddShape

use of net.sf.latexdraw.command.shape.AddShape in project latexdraw by arnobl.

the class TextSetter method configureBindings.

@Override
protected void configureBindings() {
    final var bindingFragment = shortcutBinder().on(textField).end(() -> canvas.requestFocus());
    final var enterBinder = bindingFragment.with(KeyCode.ENTER);
    // Key Enter to validate the text.
    enterBinder.toProduce(() -> new ModifyShapeProperty<>(ShapeProperties.TEXT, ShapeFactory.INST.createGroup(text), textField.getText())).when(() -> text != null && editing.getCurrentChoice() == EditionChoice.HAND && !textField.getText().isEmpty()).bind();
    // Key Enter to validate the equation of a plot shape.
    enterBinder.toProduce(() -> new ModifyShapeProperty<>(ShapeProperties.PLOT_EQ, ShapeFactory.INST.createGroup(plot), textField.getText())).when(() -> plot != null && editing.getCurrentChoice() == EditionChoice.HAND && checkValidPlotFct()).bind();
    // Key Enter to add a text shape.
    enterBinder.toProduce(() -> {
        text = (Text) editing.createShapeInstance();
        text.setPosition(ShapeFactory.INST.createPoint(position.getX(), position.getY()));
        text.setText(textField.getText());
        return new AddShape(text, drawing);
    }).when(() -> editing.getCurrentChoice() == EditionChoice.TEXT && !textField.getText().isEmpty()).bind();
    // Key Enter to add a plot shape.
    enterBinder.toProduce(() -> {
        plot = (Plot) editing.createShapeInstance();
        plot.setPosition(ShapeFactory.INST.createPoint(position.getX(), position.getY() + textField.getHeight()));
        plot.setPlotEquation(textField.getText());
        return new AddShape(plot, drawing);
    }).when(() -> editing.getCurrentChoice() == EditionChoice.PLOT && checkValidPlotFct()).bind();
    enterBinder.toProduce(() -> new ActivateInactivateInstruments(null, Collections.singletonList(this), false, false)).when(() -> textField.isValidText() && !textField.getText().isEmpty()).bind();
    bindingFragment.toProduce(() -> new ActivateInactivateInstruments(null, Collections.singletonList(this), false, false)).with(KeyCode.ESCAPE).bind();
}
Also used : AddShape(net.sf.latexdraw.command.shape.AddShape) ActivateInactivateInstruments(io.github.interacto.jfx.command.ActivateInactivateInstruments)

Example 4 with AddShape

use of net.sf.latexdraw.command.shape.AddShape in project latexdraw by arnobl.

the class Pencil method bindDnDToDrawFreeHandShape.

/**
 * Binds a DnD interaction to create shape.
 */
private void bindDnDToDrawFreeHandShape() {
    nodeBinder().usingInteraction(() -> new DnD(false, true)).toProduce(i -> {
        final Shape sh = editing.createShapeInstance();
        final Point pt = getAdaptedPoint(i.getSrcLocalPoint());
        sh.getPoints().get(0).setPoint(pt.getX(), pt.getY());
        return new AddShape(sh, canvas.getDrawing());
    }).on(canvas).first((i, c) -> canvas.requestFocus()).then((i, c) -> {
        final Point last = c.getShape().getPtAt(-1);
        final Point endPt = getAdaptedPoint(i.getTgtLocalPoint());
        if (!MathUtils.INST.equalsDouble(last.getX(), endPt.getX(), 0.0001) && !MathUtils.INST.equalsDouble(last.getY(), endPt.getY(), 0.0001)) {
            c.setShape(ShapeFactory.INST.createFreeHandFrom((Freehand) c.getShape(), endPt));
        }
        canvas.setTempView(viewFactory.createView(c.getShape()).orElse(null));
    }).endOrCancel(i -> canvas.setTempView(null)).when(i -> i.getButton() == MouseButton.PRIMARY && editing.getCurrentChoice() == EditionChoice.FREE_HAND).strictStart().bind();
}
Also used : EditingService(net.sf.latexdraw.service.EditingService) Arrays(java.util.Arrays) MouseButton(javafx.scene.input.MouseButton) ControlPointShape(net.sf.latexdraw.model.api.shape.ControlPointShape) BezierCurve(net.sf.latexdraw.model.api.shape.BezierCurve) Freehand(net.sf.latexdraw.model.api.shape.Freehand) Point3D(javafx.geometry.Point3D) BorderPos(net.sf.latexdraw.model.api.shape.BorderPos) InsertPicture(net.sf.latexdraw.command.shape.InsertPicture) Function(java.util.function.Function) Polyline(net.sf.latexdraw.model.api.shape.Polyline) ShapeFactory(net.sf.latexdraw.model.ShapeFactory) Shape(net.sf.latexdraw.model.api.shape.Shape) Inject(net.sf.latexdraw.util.Inject) PointsData(io.github.interacto.jfx.interaction.library.PointsData) MathUtils(net.sf.latexdraw.model.MathUtils) AddShape(net.sf.latexdraw.command.shape.AddShape) Canvas(net.sf.latexdraw.view.jfx.Canvas) ViewFactory(net.sf.latexdraw.view.jfx.ViewFactory) Click(io.github.interacto.jfx.interaction.library.Click) InitTextSetter(net.sf.latexdraw.command.shape.InitTextSetter) MultiClick(io.github.interacto.jfx.interaction.library.MultiClick) ModifiablePointsShape(net.sf.latexdraw.model.api.shape.ModifiablePointsShape) Polygon(net.sf.latexdraw.model.api.shape.Polygon) Press(io.github.interacto.jfx.interaction.library.Press) PositionShape(net.sf.latexdraw.model.api.shape.PositionShape) Point(net.sf.latexdraw.model.api.shape.Point) MagneticGrid(net.sf.latexdraw.view.jfx.MagneticGrid) Objects(java.util.Objects) FileChooser(javafx.stage.FileChooser) SquaredShape(net.sf.latexdraw.model.api.shape.SquaredShape) RectangularShape(net.sf.latexdraw.model.api.shape.RectangularShape) NotNull(org.jetbrains.annotations.NotNull) DnD(io.github.interacto.jfx.interaction.library.DnD) AddShape(net.sf.latexdraw.command.shape.AddShape) ControlPointShape(net.sf.latexdraw.model.api.shape.ControlPointShape) Shape(net.sf.latexdraw.model.api.shape.Shape) AddShape(net.sf.latexdraw.command.shape.AddShape) ModifiablePointsShape(net.sf.latexdraw.model.api.shape.ModifiablePointsShape) PositionShape(net.sf.latexdraw.model.api.shape.PositionShape) SquaredShape(net.sf.latexdraw.model.api.shape.SquaredShape) RectangularShape(net.sf.latexdraw.model.api.shape.RectangularShape) Freehand(net.sf.latexdraw.model.api.shape.Freehand) DnD(io.github.interacto.jfx.interaction.library.DnD) Point(net.sf.latexdraw.model.api.shape.Point)

Example 5 with AddShape

use of net.sf.latexdraw.command.shape.AddShape in project latexdraw by arnobl.

the class EditingSelector method configureBindings.

@Override
protected void configureBindings() {
    final var shapeBaseBinder = toggleButtonBinder().on(getShapeButtons()).end(() -> canvas.requestFocus());
    toggleButtonBinder().toProduce(() -> new AddShape(ShapeFactory.INST.createText(ShapeFactory.INST.createPoint(textSetter.getPosition()), textSetter.getTextField().getText()), canvas.getDrawing())).on(handB).when(() -> textSetter.isActivated() && !textSetter.getTextField().getText().isEmpty()).end(() -> canvas.requestFocus()).bind();
    shapeBaseBinder.on(handB).toProduce(i -> new ModifyEditingMode(editing, (EditionChoice) i.getWidget().getUserData())).bind();
    toggleButtonBinder().toProduce(ActivateInactivateInstruments::new).on(handB).first(c -> {
        final boolean noSelection = canvas.getDrawing().getSelection().isEmpty();
        c.setActivateFirst(false);
        c.addInstrumentToActivate(hand);
        if (!noSelection) {
            c.addInstrumentToActivate(deleter);
        }
        c.addInstrumentToInactivate(pencil);
        if (noSelection) {
            c.addInstrumentToInactivate(metaShapeCustomiser);
            c.addInstrumentToInactivate(border);
        } else {
            c.addInstrumentToActivate(metaShapeCustomiser);
            c.addInstrumentToActivate(border);
        }
    }).end(i -> canvas.requestFocus()).bind();
    shapeBaseBinder.toProduce(ActivateInactivateInstruments::new).first((i, c) -> {
        c.setActivateFirst(false);
        if (i.getWidget() != textB) {
            c.addInstrumentToInactivate(textSetter);
        }
        c.addInstrumentToInactivate(hand);
        c.addInstrumentToInactivate(border);
        c.addInstrumentToInactivate(deleter);
        c.addInstrumentToActivate(pencil);
        c.addInstrumentToActivate(metaShapeCustomiser);
    }).bind();
    buttonBinder().toProduce(ActivateInactivateInstruments::new).on(codeB).first(c -> c.addInstrumentToActivate(codeInserter)).end(() -> canvas.requestFocus()).bind();
}
Also used : Button(javafx.scene.control.Button) EditingService(net.sf.latexdraw.service.EditingService) Arrays(java.util.Arrays) Initializable(javafx.fxml.Initializable) ModifyEditingMode(net.sf.latexdraw.command.ModifyEditingMode) ActivateInactivateInstruments(io.github.interacto.jfx.command.ActivateInactivateInstruments) URL(java.net.URL) Objects(java.util.Objects) FXML(javafx.fxml.FXML) ShapeFactory(net.sf.latexdraw.model.ShapeFactory) ToggleGroup(javafx.scene.control.ToggleGroup) Inject(net.sf.latexdraw.util.Inject) ResourceBundle(java.util.ResourceBundle) ToggleButton(javafx.scene.control.ToggleButton) JfxInstrument(io.github.interacto.jfx.instrument.JfxInstrument) AddShape(net.sf.latexdraw.command.shape.AddShape) NotNull(org.jetbrains.annotations.NotNull) Canvas(net.sf.latexdraw.view.jfx.Canvas) ModifyEditingMode(net.sf.latexdraw.command.ModifyEditingMode) AddShape(net.sf.latexdraw.command.shape.AddShape) ActivateInactivateInstruments(io.github.interacto.jfx.command.ActivateInactivateInstruments)

Aggregations

AddShape (net.sf.latexdraw.command.shape.AddShape)5 Arrays (java.util.Arrays)4 Objects (java.util.Objects)4 ShapeFactory (net.sf.latexdraw.model.ShapeFactory)4 EditingService (net.sf.latexdraw.service.EditingService)4 Inject (net.sf.latexdraw.util.Inject)4 Canvas (net.sf.latexdraw.view.jfx.Canvas)4 NotNull (org.jetbrains.annotations.NotNull)4 Click (io.github.interacto.jfx.interaction.library.Click)3 DnD (io.github.interacto.jfx.interaction.library.DnD)3 MultiClick (io.github.interacto.jfx.interaction.library.MultiClick)3 PointsData (io.github.interacto.jfx.interaction.library.PointsData)3 Press (io.github.interacto.jfx.interaction.library.Press)3 Function (java.util.function.Function)3 Point3D (javafx.geometry.Point3D)3 MouseButton (javafx.scene.input.MouseButton)3 FileChooser (javafx.stage.FileChooser)3 InitTextSetter (net.sf.latexdraw.command.shape.InitTextSetter)3 InsertPicture (net.sf.latexdraw.command.shape.InsertPicture)3 MathUtils (net.sf.latexdraw.model.MathUtils)3