diff options
author | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-06 21:58:53 +0100 |
---|---|---|
committer | Eduardo Pedroni <ep625@york.ac.uk> | 2014-04-06 21:58:53 +0100 |
commit | e6dd7711c7dad5e000445208eb5845801f4ccffc (patch) | |
tree | 1454bd20a8dd7069b1283184c42f4def6d5f7e6f /src/jcgp/gui/dragresize | |
parent | c7969623b44f375e30fa3f15dcd7581609276a0f (diff) |
About to make big changes to the way fitness works, committing just in case
Diffstat (limited to 'src/jcgp/gui/dragresize')
-rw-r--r-- | src/jcgp/gui/dragresize/HorizontalDragResize.java | 90 | ||||
-rw-r--r-- | src/jcgp/gui/dragresize/VerticalDragResize.java | 90 |
2 files changed, 180 insertions, 0 deletions
diff --git a/src/jcgp/gui/dragresize/HorizontalDragResize.java b/src/jcgp/gui/dragresize/HorizontalDragResize.java new file mode 100644 index 0000000..d580878 --- /dev/null +++ b/src/jcgp/gui/dragresize/HorizontalDragResize.java @@ -0,0 +1,90 @@ +package jcgp.gui.dragresize; + +import javafx.event.EventHandler; +import javafx.scene.Cursor; +import javafx.scene.input.MouseEvent; +import javafx.scene.layout.Region; +import jcgp.gui.GUI; + +/** + * + * http://andrewtill.blogspot.co.uk/2012/12/dragging-to-resize-javafx-region.html + * + * @author eddy + * + */ +public class HorizontalDragResize { + + private boolean dragging = false; + private final Region region; + + private HorizontalDragResize(Region region) { + this.region = region; + } + + public static void makeDragResizable(final Region region) { + final HorizontalDragResize dr = new HorizontalDragResize(region); + + region.setOnMousePressed(new EventHandler<MouseEvent>() { + @Override + public void handle(MouseEvent event) { + dr.mousePressed(event); + } + }); + region.setOnMouseDragged(new EventHandler<MouseEvent>() { + @Override + public void handle(MouseEvent event) { + dr.mouseDragged(event); + } + }); + region.setOnMouseMoved(new EventHandler<MouseEvent>() { + @Override + public void handle(MouseEvent event) { + dr.mouseMoved(event); + } + }); + region.setOnMouseReleased(new EventHandler<MouseEvent>() { + @Override + public void handle(MouseEvent event) { + dr.mouseReleased(); + } + }); + + } + + private void mousePressed(MouseEvent event) { + if(isInDraggableZone(event)) { + dragging = true; + } + } + + private void mouseDragged(MouseEvent event) { + if(dragging) { + double newWidth = region.getWidth() - event.getX(); + if (newWidth >= region.getMinWidth()) { + region.setPrefWidth(newWidth); + } else { + region.setPrefWidth(region.getMinWidth()); + } + } + } + + private void mouseMoved(MouseEvent event) { + if(isInDraggableZone(event) || dragging) { + region.setCursor(Cursor.H_RESIZE); + } + else { + region.setCursor(Cursor.DEFAULT); + } + } + + private void mouseReleased() { + dragging = false; + region.setCursor(Cursor.DEFAULT); + } + + private boolean isInDraggableZone(MouseEvent event) { + return event.getX() < (GUI.RESIZE_MARGIN); + } + +} diff --git a/src/jcgp/gui/dragresize/VerticalDragResize.java b/src/jcgp/gui/dragresize/VerticalDragResize.java new file mode 100644 index 0000000..32a7526 --- /dev/null +++ b/src/jcgp/gui/dragresize/VerticalDragResize.java @@ -0,0 +1,90 @@ +package jcgp.gui.dragresize; + +import javafx.event.EventHandler; +import javafx.scene.Cursor; +import javafx.scene.input.MouseEvent; +import javafx.scene.layout.Region; +import jcgp.gui.GUI; + +/** + * + * http://andrewtill.blogspot.co.uk/2012/12/dragging-to-resize-javafx-region.html + * + * @author eddy + * + */ +public class VerticalDragResize { + + private boolean dragging = false; + private final Region region; + + private VerticalDragResize(Region region) { + this.region = region; + } + + public static void makeDragResizable(final Region region) { + final VerticalDragResize dr = new VerticalDragResize(region); + + region.setOnMousePressed(new EventHandler<MouseEvent>() { + @Override + public void handle(MouseEvent event) { + dr.mousePressed(event); + } + }); + region.setOnMouseDragged(new EventHandler<MouseEvent>() { + @Override + public void handle(MouseEvent event) { + dr.mouseDragged(event); + } + }); + region.setOnMouseMoved(new EventHandler<MouseEvent>() { + @Override + public void handle(MouseEvent event) { + dr.mouseMoved(event); + } + }); + region.setOnMouseReleased(new EventHandler<MouseEvent>() { + @Override + public void handle(MouseEvent event) { + dr.mouseReleased(); + } + }); + + } + + private void mousePressed(MouseEvent event) { + if(isInDraggableZone(event)) { + dragging = true; + } + } + + private void mouseDragged(MouseEvent event) { + if(dragging) { + double newHeight = region.getHeight() - event.getY(); + if (newHeight >= region.getMinHeight()) { + region.setPrefHeight(newHeight); + } else { + region.setPrefHeight(region.getMinHeight()); + } + } + } + + private void mouseMoved(MouseEvent event) { + if(isInDraggableZone(event) || dragging) { + region.setCursor(Cursor.V_RESIZE); + } + else { + region.setCursor(Cursor.DEFAULT); + } + } + + private void mouseReleased() { + dragging = false; + region.setCursor(Cursor.DEFAULT); + } + + private boolean isInDraggableZone(MouseEvent event) { + return event.getY() < (GUI.RESIZE_MARGIN); + } + +} |