Search in sources :

Example 1 with DnD

use of org.malai.javafx.interaction.library.DnD in project latexdraw by arnobl.

the class Pencil method bindDnDToDrawFreeHandShape.

/**
 * Binds a DnD interaction to create shape.
 */
private void bindDnDToDrawFreeHandShape() {
    nodeBinder(AddShape.class, new DnD(false, true)).on(canvas).map(i -> {
        final IShape sh = createShapeInstance();
        final IPoint pt = getAdaptedPoint(i.getSrcLocalPoint());
        sh.getPoints().get(0).setPoint(pt.getX(), pt.getY());
        return new AddShape(sh, canvas.getDrawing());
    }).first((c, i) -> Platform.runLater(() -> canvas.requestFocus())).then((c, i) -> {
        final IPoint last = c.getShape().get().getPtAt(-1);
        final IPoint endPt = getAdaptedPoint(i.getEndLocalPt());
        if (!MathUtils.INST.equalsDouble(last.getX(), endPt.getX(), 0.0001) && !MathUtils.INST.equalsDouble(last.getY(), endPt.getY(), 0.0001)) {
            c.setShape(ShapeFactory.INST.createFreeHandFrom((IFreehand) c.getShape().get(), endPt));
        }
        canvas.setTempView(ViewFactory.INSTANCE.createView(c.getShape().orElse(null)).orElse(null));
    }).endOrCancel((c, i) -> canvas.setTempView(null)).when(i -> i.getButton() == MouseButton.PRIMARY && currentChoice.get() == EditionChoice.FREE_HAND).strictStart().bind();
}
Also used : ISquaredShape(net.sf.latexdraw.models.interfaces.shape.ISquaredShape) Arrays(java.util.Arrays) MouseButton(javafx.scene.input.MouseButton) IPositionShape(net.sf.latexdraw.models.interfaces.shape.IPositionShape) Point3D(javafx.geometry.Point3D) ShapeFactory(net.sf.latexdraw.models.ShapeFactory) IRectangularShape(net.sf.latexdraw.models.interfaces.shape.IRectangularShape) MathUtils(net.sf.latexdraw.models.MathUtils) AddShape(net.sf.latexdraw.commands.shape.AddShape) Function(java.util.function.Function) BorderPos(net.sf.latexdraw.models.interfaces.shape.BorderPos) Inject(net.sf.latexdraw.util.Inject) IShape(net.sf.latexdraw.models.interfaces.shape.IShape) InsertPicture(net.sf.latexdraw.commands.shape.InsertPicture) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) DnD(org.malai.javafx.interaction.library.DnD) IFreehand(net.sf.latexdraw.models.interfaces.shape.IFreehand) ObjectProperty(javafx.beans.property.ObjectProperty) ViewFactory(net.sf.latexdraw.view.jfx.ViewFactory) IControlPointShape(net.sf.latexdraw.models.interfaces.shape.IControlPointShape) IPolygon(net.sf.latexdraw.models.interfaces.shape.IPolygon) IPolyline(net.sf.latexdraw.models.interfaces.shape.IPolyline) LangTool(net.sf.latexdraw.util.LangTool) IGroup(net.sf.latexdraw.models.interfaces.shape.IGroup) IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) InitTextSetter(net.sf.latexdraw.commands.shape.InitTextSetter) MultiClick(org.malai.javafx.interaction.library.MultiClick) Platform(javafx.application.Platform) Cursor(javafx.scene.Cursor) FileChooser(javafx.stage.FileChooser) IModifiablePointsShape(net.sf.latexdraw.models.interfaces.shape.IModifiablePointsShape) Press(org.malai.javafx.interaction.library.Press) SimpleObjectProperty(javafx.beans.property.SimpleObjectProperty) Collections(java.util.Collections) IFreehand(net.sf.latexdraw.models.interfaces.shape.IFreehand) AddShape(net.sf.latexdraw.commands.shape.AddShape) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) DnD(org.malai.javafx.interaction.library.DnD) IShape(net.sf.latexdraw.models.interfaces.shape.IShape)

Example 2 with DnD

use of org.malai.javafx.interaction.library.DnD in project latexdraw by arnobl.

the class CanvasController method configureBindings.

@Override
protected void configureBindings() {
    nodeBinder(MoveCamera.class, new DnD()).on(canvas).first((c, i) -> c.setScrollPane(canvas.getScrollPane())).then((c, i) -> {
        final ScrollPane pane = canvas.getScrollPane();
        c.setPx(pane.getHvalue() - (i.getEndLocalPt().getX() - i.getSrcLocalPoint().getX()) / canvas.getWidth());
        c.setPy(pane.getVvalue() - (i.getEndLocalPt().getY() - i.getSrcLocalPoint().getY()) / canvas.getHeight());
    }).when(i -> i.getButton() == MouseButton.MIDDLE).feedback(() -> canvas.setCursor(Cursor.MOVE)).exec().bind();
}
Also used : Cursor(javafx.scene.Cursor) Initializable(javafx.fxml.Initializable) MouseButton(javafx.scene.input.MouseButton) ScrollPane(javafx.scene.control.ScrollPane) ResourceBundle(java.util.ResourceBundle) URL(java.net.URL) MoveCamera(org.malai.javafx.command.MoveCamera) DnD(org.malai.javafx.interaction.library.DnD) ScrollPane(javafx.scene.control.ScrollPane) MoveCamera(org.malai.javafx.command.MoveCamera) DnD(org.malai.javafx.interaction.library.DnD)

Example 3 with DnD

use of org.malai.javafx.interaction.library.DnD in project latexdraw by arnobl.

the class Border method configureMovePointBinding.

private void configureMovePointBinding() {
    nodeBinder(MovePointShape.class, new DnD()).on(mvPtHandlers).first((c, i) -> i.getSrcObject().filter(o -> o instanceof MovePtHandler).map(o -> (MovePtHandler) o).ifPresent(handler -> {
        final IGroup group = canvas.getDrawing().getSelection();
        if (group.size() == 1 && group.getShapeAt(0) instanceof IModifiablePointsShape) {
            c.setPoint(handler.getPoint());
            c.setShape((IModifiablePointsShape) group.getShapeAt(0));
        }
    })).then((c, i) -> i.getSrcObject().ifPresent(node -> {
        final Point3D startPt = node.localToParent(i.getSrcLocalPoint());
        final Point3D endPt = node.localToParent(i.getEndLocalPt());
        final IPoint ptToMove = ((MovePtHandler) node).getPoint();
        final double x = ptToMove.getX() + endPt.getX() - startPt.getX();
        final double y = ptToMove.getY() + endPt.getY() - startPt.getY();
        c.setNewCoord(grid.getTransformedPointToGrid(new Point3D(x, y, 0d)));
    })).exec().when(i -> i.getSrcLocalPoint() != null && i.getEndLocalPt() != null && i.getSrcObject().orElse(null) instanceof MovePtHandler).bind();
}
Also used : Arrays(java.util.Arrays) Initializable(javafx.fxml.Initializable) Command(org.malai.command.Command) MovePtHandler(net.sf.latexdraw.handlers.MovePtHandler) Point3D(javafx.geometry.Point3D) URL(java.net.URL) ShapeFactory(net.sf.latexdraw.models.ShapeFactory) ScaleHandler(net.sf.latexdraw.handlers.ScaleHandler) FXCollections(javafx.collections.FXCollections) MathUtils(net.sf.latexdraw.models.MathUtils) MovePointShape(net.sf.latexdraw.commands.shape.MovePointShape) JfXWidgetBinding(org.malai.javafx.binding.JfXWidgetBinding) Function(java.util.function.Function) IArc(net.sf.latexdraw.models.interfaces.shape.IArc) Position(net.sf.latexdraw.models.interfaces.shape.Position) Inject(net.sf.latexdraw.util.Inject) IShape(net.sf.latexdraw.models.interfaces.shape.IShape) ResourceBundle(java.util.ResourceBundle) ListChangeListener(javafx.collections.ListChangeListener) IDrawing(net.sf.latexdraw.models.interfaces.shape.IDrawing) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) ScaleShapes(net.sf.latexdraw.commands.shape.ScaleShapes) DnD(org.malai.javafx.interaction.library.DnD) ArcAngleHandler(net.sf.latexdraw.handlers.ArcAngleHandler) IControlPointShape(net.sf.latexdraw.models.interfaces.shape.IControlPointShape) Node(javafx.scene.Node) ShapeProperties(net.sf.latexdraw.commands.shape.ShapeProperties) ModifyShapeProperty(net.sf.latexdraw.commands.shape.ModifyShapeProperty) RotationHandler(net.sf.latexdraw.handlers.RotationHandler) IGroup(net.sf.latexdraw.models.interfaces.shape.IGroup) IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) Collectors(java.util.stream.Collectors) MoveCtrlPoint(net.sf.latexdraw.commands.shape.MoveCtrlPoint) Cursor(javafx.scene.Cursor) List(java.util.List) IModifiablePointsShape(net.sf.latexdraw.models.interfaces.shape.IModifiablePointsShape) CtrlPointHandler(net.sf.latexdraw.handlers.CtrlPointHandler) ObservableList(javafx.collections.ObservableList) Handler(net.sf.latexdraw.handlers.Handler) RotateShapes(net.sf.latexdraw.commands.shape.RotateShapes) IModifiablePointsShape(net.sf.latexdraw.models.interfaces.shape.IModifiablePointsShape) Point3D(javafx.geometry.Point3D) MovePtHandler(net.sf.latexdraw.handlers.MovePtHandler) IGroup(net.sf.latexdraw.models.interfaces.shape.IGroup) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) DnD(org.malai.javafx.interaction.library.DnD)

Example 4 with DnD

use of org.malai.javafx.interaction.library.DnD in project latexdraw by arnobl.

the class Border method configureBindings.

@Override
protected void configureBindings() {
    addBinding(new DnD2Scale(this));
    configureMovePointBinding();
    nodeBinder(MoveCtrlPoint.class, new DnD()).on(ctrlPt1Handlers).on(ctrlPt2Handlers).first((c, i) -> {
        final IGroup group = canvas.getDrawing().getSelection();
        if (group.size() == 1 && group.getShapeAt(0) instanceof IControlPointShape) {
            c.setPoint(i.getSrcObject().map(h -> ((CtrlPointHandler) h).getPoint()).orElse(null));
            c.setShape((IControlPointShape) group.getShapeAt(0));
            c.setIsFirstCtrlPt(ctrlPt1Handlers.contains(i.getSrcObject().orElse(null)));
        }
    }).then((c, i) -> {
        final Point3D startPt = i.getSrcObject().map(n -> n.localToParent(i.getSrcLocalPoint())).orElse(new Point3D(0d, 0d, 0d));
        final Point3D endPt = i.getSrcObject().map(n -> n.localToParent(i.getEndLocalPt())).orElse(new Point3D(0d, 0d, 0d));
        final IPoint ptToMove = i.getSrcObject().map(n -> ((CtrlPointHandler) n).getPoint()).orElse(ShapeFactory.INST.createPoint());
        final double x = ptToMove.getX() + endPt.getX() - startPt.getX();
        final double y = ptToMove.getY() + endPt.getY() - startPt.getY();
        c.setNewCoord(grid.getTransformedPointToGrid(new Point3D(x, y, 0d)));
    }).exec().bind();
    nodeBinder(RotateShapes.class, new DnD()).on(rotHandler).first((c, i) -> {
        final IDrawing drawing = canvas.getDrawing();
        c.setGravityCentre(drawing.getSelection().getGravityCentre());
        c.getGc().translate(canvas.getOrigin().getX(), canvas.getOrigin().getY());
        c.setShape(drawing.getSelection().duplicateDeep(false));
    }).then((c, i) -> c.setRotationAngle(c.getGc().computeRotationAngle(ShapeFactory.INST.createPoint(canvas.sceneToLocal(i.getSrcScenePoint())), ShapeFactory.INST.createPoint(canvas.sceneToLocal(i.getEndScenePt()))))).exec().bind();
    addBinding(new DnD2ArcAngle(this));
}
Also used : Arrays(java.util.Arrays) Initializable(javafx.fxml.Initializable) Command(org.malai.command.Command) MovePtHandler(net.sf.latexdraw.handlers.MovePtHandler) Point3D(javafx.geometry.Point3D) URL(java.net.URL) ShapeFactory(net.sf.latexdraw.models.ShapeFactory) ScaleHandler(net.sf.latexdraw.handlers.ScaleHandler) FXCollections(javafx.collections.FXCollections) MathUtils(net.sf.latexdraw.models.MathUtils) MovePointShape(net.sf.latexdraw.commands.shape.MovePointShape) JfXWidgetBinding(org.malai.javafx.binding.JfXWidgetBinding) Function(java.util.function.Function) IArc(net.sf.latexdraw.models.interfaces.shape.IArc) Position(net.sf.latexdraw.models.interfaces.shape.Position) Inject(net.sf.latexdraw.util.Inject) IShape(net.sf.latexdraw.models.interfaces.shape.IShape) ResourceBundle(java.util.ResourceBundle) ListChangeListener(javafx.collections.ListChangeListener) IDrawing(net.sf.latexdraw.models.interfaces.shape.IDrawing) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) ScaleShapes(net.sf.latexdraw.commands.shape.ScaleShapes) DnD(org.malai.javafx.interaction.library.DnD) ArcAngleHandler(net.sf.latexdraw.handlers.ArcAngleHandler) IControlPointShape(net.sf.latexdraw.models.interfaces.shape.IControlPointShape) Node(javafx.scene.Node) ShapeProperties(net.sf.latexdraw.commands.shape.ShapeProperties) ModifyShapeProperty(net.sf.latexdraw.commands.shape.ModifyShapeProperty) RotationHandler(net.sf.latexdraw.handlers.RotationHandler) IGroup(net.sf.latexdraw.models.interfaces.shape.IGroup) IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) Collectors(java.util.stream.Collectors) MoveCtrlPoint(net.sf.latexdraw.commands.shape.MoveCtrlPoint) Cursor(javafx.scene.Cursor) List(java.util.List) IModifiablePointsShape(net.sf.latexdraw.models.interfaces.shape.IModifiablePointsShape) CtrlPointHandler(net.sf.latexdraw.handlers.CtrlPointHandler) ObservableList(javafx.collections.ObservableList) Handler(net.sf.latexdraw.handlers.Handler) RotateShapes(net.sf.latexdraw.commands.shape.RotateShapes) RotateShapes(net.sf.latexdraw.commands.shape.RotateShapes) CtrlPointHandler(net.sf.latexdraw.handlers.CtrlPointHandler) IDrawing(net.sf.latexdraw.models.interfaces.shape.IDrawing) Point3D(javafx.geometry.Point3D) IGroup(net.sf.latexdraw.models.interfaces.shape.IGroup) IControlPointShape(net.sf.latexdraw.models.interfaces.shape.IControlPointShape) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) DnD(org.malai.javafx.interaction.library.DnD)

Example 5 with DnD

use of org.malai.javafx.interaction.library.DnD in project Malai by arnobl.

the class Pencil method configureBindings.

@Override
protected void configureBindings() {
    // A DnD interaction with the left button of the mouse will produce an AddShape command while interacting on the canvas.
    // A temporary view of the created shape is created and displayed by the canvas.
    // This view is removed at the end of the interaction.
    nodeBinder(new DnD(), i -> new AddShape(drawing, new MyRect(i.getSrcLocalPoint().getX(), i.getSrcLocalPoint().getY()))).on(canvas).first((i, c) -> canvas.setTmpShape(ViewFactory.INSTANCE.createViewShape(c.getShape()))).then((i, c) -> {
        final MyRect sh = (MyRect) c.getShape();
        sh.setWidth(i.getTgtLocalPoint().getX() - sh.getX());
        sh.setHeight(i.getTgtLocalPoint().getY() - sh.getY());
    }).when(i -> i.getButton() == MouseButton.PRIMARY).end((i, c) -> canvas.setTmpShape(null)).log(LogLevel.INTERACTION).strictStart().help(new AddRectHelpAnimation(learningPane, canvas)).bind();
    // A DnD interaction with the right button of the mouse moves the targeted shape.
    // To incrementally moves the shape, the DnD interaction has its parameter 'updateSrcOnUpdate' set to true:
    // At each interaction updates, the source point and object take the latest target point and object.
    // The DnD interaction can be stopped (aborted) by pressing the key 'ESC'. This cancels the ongoing command (that thus needs to be undoable).
    nodeBinder(new DnD(true, true), // In the command these binding are @autoUnbind to be unbound on their command termination.
    i -> {
        final MyShape sh = i.getSrcObject().map(o -> (MyShape) o.getUserData()).get();
        return new MoveShape(sh, Bindings.createDoubleBinding(() -> sh.getX() + (i.getTgtScenePoint().getX() - i.getSrcScenePoint().getX()), i.tgtScenePointProperty(), i.srcScenePointProperty()), Bindings.createDoubleBinding(() -> sh.getY() + (i.getTgtScenePoint().getY() - i.getSrcScenePoint().getY()), i.tgtScenePointProperty(), i.srcScenePointProperty()));
    }).on(canvas.getShapesPane().getChildren()).when(i -> i.getButton() == MouseButton.SECONDARY).exec().first((i, c) -> {
        // Required to grab the focus to get key events
        Platform.runLater(() -> i.getSrcObject().get().requestFocus());
        i.getSrcObject().get().setEffect(new DropShadow(20d, Color.BLACK));
    }).endOrCancel((i, c) -> i.getSrcObject().get().setEffect(null)).strictStart().help(new MoveRectHelpAnimation(learningPane, canvas)).throttle(40L).bind();
    // nodeBinder(new DnD(true, true), i -> new MoveShape(i.getSrcObject().map(o ->(MyShape) o.getUserData()).orElse(null))).
    // // The binding dynamically registers elements of the given observable list.
    // // When nodes are added to this list, these nodes register the binding.
    // // When nodes are removed from this list, their binding is cancelled.
    // // This permits to interact on nodes (here, shapes) that are dynamically added to/removed from the canvas.
    // on(canvas.getShapesPane().getChildren()).
    // then((i, c) -> c.setCoord(c.getShape().getX() + (i.getTgtScenePoint().getX() - i.getSrcScenePoint().getX()),
    // c.getShape().getY() + (i.getTgtScenePoint().getY() - i.getSrcScenePoint().getY()))).
    // when(i -> i.getButton() == MouseButton.SECONDARY).
    // // exec(true): this allows to execute the command each time the interaction updates (and 'when' is true).
    // exec(true).
    // first((i, c) -> {
    // // Required to grab the focus to get key events
    // Platform.runLater(() -> i.getSrcObject().get().requestFocus());
    // i.getSrcObject().get().setEffect(new DropShadow(20d, Color.BLACK));
    // }).
    // end((i, c) -> i.getSrcObject().get().setEffect(null)).
    // strictStart().
    // bind();
    /*
		 * A DnD on the colour picker produces ChangeCol commands when the target of the DnD is a shape
		 * (the shape we want to change the colour). The interim feedback changes the cursor during the DnD to show the dragged colour.
		 * Note that the feedback callback is not optimised here as the colour does not change during the DnD. The cursor
		 * should be changed in 'first'
		 */
    nodeBinder(new DnD(), i -> new ChangeColour(lineCol.getValue(), null)).on(lineCol).then((i, c) -> i.getTgtObject().map(view -> (MyShape) view.getUserData()).ifPresent(sh -> c.setShape(sh))).when(i -> i.getTgtObject().orElse(null) instanceof Shape).feedback(() -> lineCol.getScene().setCursor(new ColorCursor(lineCol.getValue()))).endOrCancel((a, i) -> lineCol.getScene().setCursor(Cursor.DEFAULT)).bind();
    /*
		 * A mouse pressure creates an anonymous command that simply shows a message in the console.
		 */
    anonCmdBinder(new Press(), () -> System.out.println("An example of the anonymous command.")).on(canvas).bind();
    /*
		 * A widget binding that execute a command asynchronously.
		 * Widgets and properties are provided to the binding to:
		 * show/hide the cancel button, provide widgets with information regarding the progress of the command execution.
		 */
    buttonBinder(Save::new).on(save).async(cancel, progressbar.progressProperty(), textProgress.textProperty()).bind();
}
Also used : Button(javafx.scene.control.Button) AddShape(org.malai.ex.draw.command.AddShape) Initializable(javafx.fxml.Initializable) MouseButton(javafx.scene.input.MouseButton) MyCanvas(org.malai.ex.draw.view.MyCanvas) MyDrawing(org.malai.ex.draw.model.MyDrawing) URL(java.net.URL) Bindings(javafx.beans.binding.Bindings) AddRectHelpAnimation(org.malai.ex.draw.learning.AddRectHelpAnimation) ProgressBar(javafx.scene.control.ProgressBar) MyRect(org.malai.ex.draw.model.MyRect) ResourceBundle(java.util.ResourceBundle) ListChangeListener(javafx.collections.ListChangeListener) MyShape(org.malai.ex.draw.model.MyShape) DnD(org.malai.javafx.interaction.library.DnD) Pane(javafx.scene.layout.Pane) Save(org.malai.ex.draw.command.Save) MoveRectHelpAnimation(org.malai.ex.draw.learning.MoveRectHelpAnimation) ColorPicker(javafx.scene.control.ColorPicker) LogLevel(org.malai.logging.LogLevel) Color(javafx.scene.paint.Color) Label(javafx.scene.control.Label) MoveShape(org.malai.ex.draw.command.MoveShape) ViewFactory(org.malai.ex.draw.view.ViewFactory) ChangeColour(org.malai.ex.draw.command.ChangeColour) JfxInstrument(org.malai.javafx.instrument.JfxInstrument) Group(javafx.scene.Group) Collectors(java.util.stream.Collectors) DropShadow(javafx.scene.effect.DropShadow) Objects(java.util.Objects) Platform(javafx.application.Platform) FXML(javafx.fxml.FXML) Cursor(javafx.scene.Cursor) Press(org.malai.javafx.interaction.library.Press) ColorCursor(org.malai.ex.util.ColorCursor) Shape(javafx.scene.shape.Shape) AddShape(org.malai.ex.draw.command.AddShape) MoveShape(org.malai.ex.draw.command.MoveShape) AddShape(org.malai.ex.draw.command.AddShape) MyShape(org.malai.ex.draw.model.MyShape) MoveShape(org.malai.ex.draw.command.MoveShape) Shape(javafx.scene.shape.Shape) MyRect(org.malai.ex.draw.model.MyRect) AddRectHelpAnimation(org.malai.ex.draw.learning.AddRectHelpAnimation) ChangeColour(org.malai.ex.draw.command.ChangeColour) MyShape(org.malai.ex.draw.model.MyShape) ColorCursor(org.malai.ex.util.ColorCursor) DropShadow(javafx.scene.effect.DropShadow) MoveRectHelpAnimation(org.malai.ex.draw.learning.MoveRectHelpAnimation) DnD(org.malai.javafx.interaction.library.DnD) Press(org.malai.javafx.interaction.library.Press)

Aggregations

DnD (org.malai.javafx.interaction.library.DnD)12 Cursor (javafx.scene.Cursor)8 ResourceBundle (java.util.ResourceBundle)7 Platform (javafx.application.Platform)7 ShapeFactory (net.sf.latexdraw.models.ShapeFactory)6 Inject (net.sf.latexdraw.util.Inject)6 URL (java.net.URL)5 Point3D (javafx.geometry.Point3D)5 MouseButton (javafx.scene.input.MouseButton)5 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)5 IShape (net.sf.latexdraw.models.interfaces.shape.IShape)5 Arrays (java.util.Arrays)4 Function (java.util.function.Function)4 Initializable (javafx.fxml.Initializable)4 Node (javafx.scene.Node)4 KeyCode (javafx.scene.input.KeyCode)4 Rectangle (javafx.scene.shape.Rectangle)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 DoubleProperty (javafx.beans.property.DoubleProperty)3