Skip to content

Commit

Permalink
Merge pull request #100 from DecwLK/various_fixes
Browse files Browse the repository at this point in the history
Various fixes
  • Loading branch information
DecwLK authored Feb 12, 2024
2 parents a6646a7 + 8ffc9e9 commit 8bf059d
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ boolean run() throws GeckoException {
element.setSize(oldSizeCopy);
element.setPosition(oldPosCopy);
} else {
elementScalerBlock.getDecoratorTarget()
.setEdgePoint(elementScalerBlock.getIndex(), elementScalerBlock.getCenter());
if (!elementScalerBlock.getDecoratorTarget()
.setEdgePoint(elementScalerBlock.getIndex(), elementScalerBlock.getCenter())) {
return false;
}
}

editorViewModel.updateRegions();
Expand Down
32 changes: 17 additions & 15 deletions src/main/java/org/gecko/tools/CursorTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,9 @@ private void setBlockScalerElementHandlers(ElementScalerBlock scaler) {
return;
}
Point2D newPosition = scaler.localToParent(scaler.sceneToLocal(event.getSceneX(), event.getSceneY()));
scaler.setCenter(newPosition);
PositionableViewModelElement<?> target = scaler.getDecoratorTarget().getTarget();
if (target.getArea() < PositionableViewModelElement.MIN_AREA) {
scaler.setCenter(previousDragPosition);
} else {
previousDragPosition = newPosition;

if (!scaler.setCenter(newPosition)) {
cancelDrag(scaler);
}
});

Expand All @@ -170,9 +167,7 @@ private void setBlockScalerElementHandlers(ElementScalerBlock scaler) {
(BlockViewModelElement<?>) scaler.getDecoratorTarget().getTarget(), scaler);
actionManager.run(resizeAction);

scaler.setDragging(false);
oldPosition = null;
oldSize = null;
cancelDrag(scaler);
});
}

Expand Down Expand Up @@ -214,9 +209,7 @@ private void setConnectionScalerElementsHandlers(ElementScalerBlock scaler) {
}

actionManager.run(moveAction);
startDragPosition = null;
draggedElement = null;
scaler.setDragging(false);
cancelDrag(scaler);
});
}

Expand Down Expand Up @@ -256,7 +249,6 @@ private void stopDraggingElementHandler(MouseEvent event) {
if (!isDragging) {
return;
}
isDragging = false;
Point2D endWorldPos = getWorldCoordinates(draggedElement).add(new Point2D(event.getX(), event.getY()));
selectionManager.getCurrentSelection().forEach(element -> {
element.setCurrentlyModified(false);
Expand All @@ -265,8 +257,7 @@ private void stopDraggingElementHandler(MouseEvent event) {
Action moveAction = actionManager.getActionFactory()
.createMoveBlockViewModelElementAction(endWorldPos.subtract(startDragPosition));
actionManager.run(moveAction);
startDragPosition = null;
draggedElement = null;
cancelDrag();
}

private void selectElement(ViewElement<?> viewElement, boolean newSelection) {
Expand Down Expand Up @@ -297,4 +288,15 @@ private Point2D getWorldCoordinates(Node node, Point2D point) {
return editorViewModel.transformViewPortToWorldCoordinates(
new Point2D(parentX + point.getX(), parentY + point.getY()).multiply(editorViewModel.getZoomScale()));
}

private void cancelDrag(ElementScalerBlock scaler) {
scaler.setDragging(false);
cancelDrag();
}

private void cancelDrag() {
startDragPosition = null;
draggedElement = null;
isDragging = false;
}
}
5 changes: 3 additions & 2 deletions src/main/java/org/gecko/tools/RegionCreatorTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import org.gecko.actions.ActionManager;
import org.gecko.viewmodel.BlockViewModelElement;
import org.gecko.viewmodel.PositionableViewModelElement;

/**
Expand All @@ -31,12 +32,12 @@ Rectangle createNewArea() {

@Override
void onAreaCreated(MouseEvent event, Bounds worldAreaBounds) {
if (worldAreaBounds.getWidth() * worldAreaBounds.getHeight() < PositionableViewModelElement.MIN_AREA) {
if (worldAreaBounds.getWidth() < BlockViewModelElement.MIN_WIDTH
|| worldAreaBounds.getHeight() < BlockViewModelElement.MIN_HEIGHT) {
return;
}
actionManager.run(actionManager.getActionFactory()
.createCreateRegionViewModelElementAction(new Point2D(worldAreaBounds.getMinX(), worldAreaBounds.getMinY()),
new Point2D(worldAreaBounds.getWidth(), worldAreaBounds.getHeight()), color));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ private ListChangeListener<? super EdgeViewModel> updateMaskPathSourceListeners(
private void maskPathSource() {
// If source and destination are the same, draw a loop
if (edgeViewModel.getSource().equals(edgeViewModel.getDestination()) && getEdgePoints().size() == 2) {
setLoop(true);
if (!isLoop()) {
setLoop(true);
}
setEdgePoint(0, edgeViewModel.getSource().getPosition());
setEdgePoint(getEdgePoints().size() - 1, edgeViewModel.getSource()
.getPosition()
Expand All @@ -165,6 +167,11 @@ private void maskPathSource() {
return;
}

if (isLoop()) {
setLoop(false);
updatePathVisualization();
}

double sourceEdgeOffset = edgeViewModel.getSource().getEdgeOffset(edgeViewModel);
Point2D firstPoint = maskBlock(edgeViewModel.getSource().getPosition(), edgeViewModel.getSource().getSize(),
edgeViewModel.getDestination().getCenter(), edgeViewModel.getSource().getCenter(), sourceEdgeOffset);
Expand Down Expand Up @@ -194,8 +201,9 @@ public ObservableList<Property<Point2D>> getEdgePoints() {
}

@Override
public void setEdgePoint(int index, Point2D point) {
public boolean setEdgePoint(int index, Point2D point) {
edgeViewModel.setEdgePoint(index, point);
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public Node drawElement() {
}

@Override
public void setEdgePoint(int index, Point2D point) {
regionViewModel.manipulate(
public boolean setEdgePoint(int index, Point2D point) {
return regionViewModel.manipulate(
getEdgePoints().get((index + getEdgePoints().size() / 2) % getEdgePoints().size()).getValue(), point);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public Node drawElement() {
return this;
}

@Override
public void setEdgePoint(int index, Point2D point) {
}

@Override
public StateViewModel getTarget() {
return stateViewModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ public ObservableList<Property<Point2D>> getEdgePoints() {
}

@Override
public void setEdgePoint(int index, Point2D point) {
public boolean setEdgePoint(int index, Point2D point) {
systemConnectionViewModel.setEdgePoint(index, point);
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ public SystemViewElement(SystemViewModel systemViewModel) {
this.inputPortsAligner = new VBox();
this.outputPortsAligner = new VBox();

inputPortsAligner.layoutBoundsProperty()
.addListener((observable, oldValue, newValue) -> updatePortViewModels());
outputPortsAligner.layoutBoundsProperty()
.addListener((observable, oldValue, newValue) -> updatePortViewModels());
bindViewModel();
constructVisualization();
}
Expand All @@ -67,10 +63,6 @@ public Node drawElement() {
return this;
}

@Override
public void setEdgePoint(int index, Point2D point) {
}

@Override
public SystemViewModel getTarget() {
return systemViewModel;
Expand Down Expand Up @@ -145,6 +137,7 @@ private void updatePortViewModels() {
private void addPort(PortViewModel portViewModel) {
portViewModel.getVisibilityProperty().addListener(this::onVisibilityChanged);
PortViewElement portViewElement = new PortViewElement(portViewModel);
portViewElement.layoutYProperty().addListener((observable, oldValue, newValue) -> updatePortViewModels());
portViewElements.add(portViewElement);
if (portViewModel.getVisibility() == Visibility.INPUT) {
inputPortsAligner.getChildren().add(portViewElement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public Node drawElement() {
return this;
}

@Override
public void setEdgePoint(int index, Point2D point) {
}

@Override
public PortViewModel getTarget() {
return portViewModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ public interface ViewElement<T extends PositionableViewModelElement<?>> {
*
* @param index the index
* @param point new point
* @return true if the edge point was set, false if the edge point could not be set due to size limitations
*/
void setEdgePoint(int index, Point2D point);
default boolean setEdgePoint(int index, Point2D point) {
return true;
}

/**
* Set the selected state of the element.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ public void setPosition(Point2D point) {
*
* @param point The new center of the scaler block.
*/
public void setCenter(Point2D point) {
Point2D center = new Point2D(point.getX() - (getWidth() / 2), point.getY() - (getHeight() / 2));
setLayoutX(center.getX());
setLayoutY(center.getY());

decoratorTarget.setEdgePoint(index, point);
public boolean setCenter(Point2D point) {
if (decoratorTarget.setEdgePoint(index, point)) {
Point2D center = new Point2D(point.getX() - (getWidth() / 2), point.getY() - (getHeight() / 2));
setLayoutX(center.getX());
setLayoutY(center.getY());
return true;
}
return false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ObservableList<Property<Point2D>> getEdgePoints() {
}

@Override
public void setEdgePoint(int index, Point2D point) {
getDecoratorTarget().setEdgePoint(index, point);
public boolean setEdgePoint(int index, Point2D point) {
return getDecoratorTarget().setEdgePoint(index, point);
}
}
12 changes: 11 additions & 1 deletion src/main/java/org/gecko/viewmodel/BlockViewModelElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
@Getter
public abstract class BlockViewModelElement<T extends Element & org.gecko.model.Renamable>
extends PositionableViewModelElement<T> implements Renamable {
public static final double MIN_WIDTH = 100;
public static final double MIN_HEIGHT = 100;

private final StringProperty nameProperty;

BlockViewModelElement(int id, @NonNull T target) {
Expand All @@ -40,13 +43,20 @@ public void setName(@NonNull String name) {
* @param firstCornerPoint the first corner point
* @param secondCornerPoint the second corner point that is diagonally opposite to the first corner point
*/
public void manipulate(@NonNull Point2D firstCornerPoint, @NonNull Point2D secondCornerPoint) {
public boolean manipulate(@NonNull Point2D firstCornerPoint, @NonNull Point2D secondCornerPoint) {
Point2D newStartPosition = new Point2D(Math.min(firstCornerPoint.getX(), secondCornerPoint.getX()),
Math.min(firstCornerPoint.getY(), secondCornerPoint.getY()));
Point2D newEndPosition = new Point2D(Math.max(firstCornerPoint.getX(), secondCornerPoint.getX()),
Math.max(firstCornerPoint.getY(), secondCornerPoint.getY()));

if (Math.abs(newEndPosition.getX() - newStartPosition.getX()) < MIN_WIDTH
|| Math.abs(newEndPosition.getY() - newStartPosition.getY()) < MIN_HEIGHT) {
return false;
}

setPosition(newStartPosition);
setSize(newEndPosition.subtract(newStartPosition));
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
@Getter
@Setter
public abstract class PositionableViewModelElement<T extends Element> extends AbstractViewModelElement<T> {
/**
* The minimum area of a {@link PositionableViewModelElement} that can be resized.
*/
public static final double MIN_AREA = 10000;
protected final Property<Point2D> positionProperty;
protected final Property<Point2D> sizeProperty;
private boolean isCurrentlyModified;
Expand Down Expand Up @@ -62,8 +58,4 @@ public void setCenter(@NonNull Point2D point) {
}

public abstract Object accept(@NonNull PositionableViewModelElementVisitor visitor);

public double getArea() {
return getSize().getX() * getSize().getY();
}
}
9 changes: 9 additions & 0 deletions src/main/java/org/gecko/viewmodel/ViewModelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,15 @@ public SystemConnectionViewModel createSystemConnectionViewModelFrom(
return result;
}

/**
* Expects the source and destination of the system connection to be in the view model.
*/
public SystemConnectionViewModel createSystemConnectionViewModelFrom(
SystemConnection systemConnection) throws MissingViewModelElementException {
return createSystemConnectionViewModelFrom(geckoViewModel.getCurrentEditor().getCurrentSystem().getTarget(),
systemConnection);
}

public SystemViewModel createSystemViewModelIn(SystemViewModel parentSystem) throws ModelException {
System system = modelFactory.createSystem(parentSystem.getTarget());
SystemViewModel result = new SystemViewModel(getNewViewModelElementId(), system);
Expand Down

0 comments on commit 8bf059d

Please sign in to comment.