/* * ManualResizableRectangle.fx * * Created on 29-jul-2009, 14:59:52 */ package manualresizablerectangle; import javafx.scene.CustomNode; import javafx.scene.shape.Rectangle; import javafx.scene.paint.Paint; import javafx.scene.paint.Color; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.input.MouseEvent; import javafx.scene.Cursor; /** * @author Yannick Van Godtsenhoven */ public class ManualResizableRectangle extends CustomNode{ public def rectangle:Rectangle = Rectangle{}; def SPACE = 1; def clickColor = Color.TRANSPARENT; var oldRectangleX; var oldRectangleY; var oldRectangleWidth; var oldRectangleHeight; def leftClickArea:Rectangle = Rectangle{ x: bind rectangle.x - (clickAreaWidth/2) y: bind rectangle.y + (3*clickAreaWidth/2) + SPACE width: bind clickAreaWidth height: bind rectangle.height - (6*clickAreaWidth/2) - 2*SPACE fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableHorizontalResize){ cursor = Cursor.H_RESIZE; } } onMouseExited: function(me:MouseEvent) { if(enableHorizontalResize){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableHorizontalResize){ storeLeftResize(); } } onMouseDragged: function(me:MouseEvent) { if(enableHorizontalResize and maxLeftWidthNotReached(me) and minLeftWidthNotReached(me)){ leftResize(me); } } onMouseReleased: function(me:MouseEvent) { if(enableHorizontalResize){ correctLeftWidthToSteps(); storeLeftResize(); } } } def rightClickArea:Rectangle = Rectangle{ x: bind rectangle.x + rectangle.width - (clickAreaWidth/2) y: bind rectangle.y + (3*clickAreaWidth/2) + SPACE width: bind clickAreaWidth height: bind rectangle.height - (6*clickAreaWidth/2) - 2*SPACE fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableHorizontalResize){ cursor = Cursor.H_RESIZE; } } onMouseExited: function(me:MouseEvent) { if(enableHorizontalResize){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableHorizontalResize){ storeRightResize() } } onMouseDragged: function(me:MouseEvent) { if(enableHorizontalResize and maxRightWidthNotReached(me) and minRightWidthNotReached(me)){ rightResize(me); } } onMouseReleased: function(me:MouseEvent) { if(enableHorizontalResize){ correctRightWidthToSteps(); storeRightResize() } } } def upperClickArea:Rectangle = Rectangle{ x: bind rectangle.x + (3*clickAreaWidth/2) + SPACE y: bind rectangle.y - (clickAreaWidth/2) width: bind rectangle.width - (6*clickAreaWidth/2) - 2*SPACE height: bind clickAreaWidth fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableVerticalResize){ cursor = Cursor.V_RESIZE; } } onMouseExited: function(me:MouseEvent) { if(enableVerticalResize){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableVerticalResize){ storeUpperResize(); } } onMouseDragged: function(me:MouseEvent) { if(enableVerticalResize and maxUpperHeightNotReached(me) and minUpperHeightNotReached(me)){ upperResize(me); } } onMouseReleased: function(me:MouseEvent) { if(enableVerticalResize){ correctUpperHeightToSteps(); storeUpperResize(); } } } def lowerClickArea:Rectangle = Rectangle{ x: bind rectangle.x + (3*clickAreaWidth/2) + SPACE y: bind rectangle.y + rectangle.height - (clickAreaWidth/2) width: bind rectangle.width - (6*clickAreaWidth/2) - 2*SPACE height: bind clickAreaWidth fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableVerticalResize){ cursor = Cursor.V_RESIZE; } } onMouseExited: function(me:MouseEvent) { if(enableVerticalResize){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableVerticalResize){ storeLowerResize() } } onMouseDragged: function(me:MouseEvent) { if(enableVerticalResize and maxLowerHeightNotReached(me) and minLowerHeightNotReached(me)){ lowerResize(me); } } onMouseReleased: function(me:MouseEvent) { if(enableVerticalResize){ correctLowerHeightToSteps(); storeLowerResize() } } } def upperLeftClickArea:Rectangle = Rectangle{ x: bind rectangle.x - (clickAreaWidth/2) y: bind rectangle.y - (clickAreaWidth/2) width: bind clickAreaWidth*2 height: bind clickAreaWidth*2 fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ cursor = Cursor.NW_RESIZE } } onMouseExited: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ storeLeftResize(); storeUpperResize(); } } onMouseDragged: function(me:MouseEvent) { if(enableHorizontalResize and maxLeftWidthNotReached(me) and minLeftWidthNotReached(me)){ leftResize(me); } if(enableVerticalResize and maxUpperHeightNotReached(me) and minUpperHeightNotReached(me)){ upperResize(me); } } onMouseReleased: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ correctLeftWidthToSteps(); correctUpperHeightToSteps(); storeLeftResize(); storeUpperResize(); } } } def lowerLeftClickArea:Rectangle = Rectangle{ x: bind rectangle.x - (clickAreaWidth/2) y: bind rectangle.y + rectangle.height - (3*clickAreaWidth/2) width: bind clickAreaWidth*2 height: bind clickAreaWidth*2 fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ cursor = Cursor.SW_RESIZE } } onMouseExited: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ storeLeftResize(); storeLowerResize(); } } onMouseDragged: function(me:MouseEvent) { if(enableHorizontalResize and maxLeftWidthNotReached(me) and minLeftWidthNotReached(me)){ leftResize(me); } if(enableVerticalResize and maxLowerHeightNotReached(me) and minLowerHeightNotReached(me)){ lowerResize(me); } } onMouseReleased: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ correctLeftWidthToSteps(); correctLowerHeightToSteps(); storeLeftResize(); storeLowerResize(); } } } def upperRightClickArea:Rectangle = Rectangle{ x: bind rectangle.x + rectangle.width - (3*clickAreaWidth/2) y: bind rectangle.y - (clickAreaWidth/2) width: bind clickAreaWidth*2 height: bind clickAreaWidth*2 fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ cursor = Cursor.NE_RESIZE } } onMouseExited: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ storeRightResize(); storeUpperResize(); } } onMouseDragged: function(me:MouseEvent) { if(enableHorizontalResize and maxRightWidthNotReached(me) and minRightWidthNotReached(me)){ rightResize(me); } if(enableVerticalResize and maxUpperHeightNotReached(me) and minUpperHeightNotReached(me)){ upperResize(me); } } onMouseReleased: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ correctRightWidthToSteps(); correctUpperHeightToSteps(); storeRightResize(); storeUpperResize(); } } } def lowerRightClickArea:Rectangle = Rectangle{ x: bind rectangle.x + rectangle.width - (3*clickAreaWidth/2) y: bind rectangle.y + rectangle.height - (3*clickAreaWidth/2) width: bind clickAreaWidth*2 height: bind clickAreaWidth*2 fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ cursor = Cursor.SE_RESIZE } } onMouseExited: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ storeRightResize(); storeLowerResize() } } onMouseDragged: function(me:MouseEvent) { if(enableHorizontalResize and maxRightWidthNotReached(me) and minRightWidthNotReached(me)){ rightResize(me); } if(enableVerticalResize and maxLowerHeightNotReached(me) and minLowerHeightNotReached(me)){ lowerResize(me); } } onMouseReleased: function(me:MouseEvent) { if(enableHorizontalResize and enableVerticalResize){ correctRightWidthToSteps(); correctLowerHeightToSteps(); storeRightResize(); storeLowerResize() } } } def moveRectangle:Rectangle = Rectangle{ x: bind upperClickArea.x; y: bind leftClickArea.y; width: bind upperClickArea.width; height: bind leftClickArea.height; fill: clickColor onMouseEntered: function(me:MouseEvent) { if(enableMove){ cursor = Cursor.HAND; } } onMouseExited: function(me:MouseEvent) { if(enableMove){ cursor = Cursor.DEFAULT; } } onMousePressed: function(me:MouseEvent) { if(enableMove){ storePosition(); } } onMouseDragged: function(me:MouseEvent) { if(enableMove){ rectangle.x = oldRectangleX + me.dragX; rectangle.y = oldRectangleY + me.dragY; } } onMouseReleased: function(me:MouseEvent) { if(enableMove){ storePosition(); } } }; function storePosition() { oldRectangleX = rectangle.x; oldRectangleY = rectangle.y; } function storeLeftResize() { oldRectangleX = rectangle.x; oldRectangleWidth = rectangle.width; } function leftResize(me:MouseEvent) { rectangle.x = oldRectangleX + me.dragX; rectangle.width = oldRectangleWidth - (me.dragX); } function storeRightResize() { oldRectangleWidth = rectangle.width; } function rightResize(me:MouseEvent) { rectangle.width = oldRectangleWidth + (me.dragX); } function storeUpperResize() { oldRectangleY = rectangle.y; oldRectangleHeight = rectangle.height; } function upperResize(me:MouseEvent) { rectangle.y = oldRectangleY + me.dragY; rectangle.height = oldRectangleHeight - (me.dragY); } function storeLowerResize() { oldRectangleHeight = rectangle.height; } function lowerResize(me:MouseEvent) { rectangle.height = oldRectangleHeight + (me.dragY); } function maxLeftWidthNotReached(me:MouseEvent):Boolean { if( maxWidth != 0 and (oldRectangleWidth - me.dragX) > maxWidth) { //too fix width when dragging too fast rectangle.width = maxWidth; return false; } else { return true; } } function maxRightWidthNotReached(me:MouseEvent):Boolean { if( maxWidth != 0 and (oldRectangleWidth + me.dragX) > maxWidth) { //too fix width when dragging too fast rectangle.width = maxWidth; return false; } else { return true; } } function maxUpperHeightNotReached(me:MouseEvent):Boolean { if( maxHeight != 0 and (oldRectangleHeight - me.dragY) > maxHeight) { //too fix width when dragging too fast rectangle.height = maxHeight; return false; } else { return true; } } function maxLowerHeightNotReached(me:MouseEvent):Boolean { if( maxHeight != 0 and (oldRectangleHeight + me.dragY) > maxHeight) { //too fix width when dragging too fast rectangle.height = maxHeight; return false; } else { return true; } } function minLeftWidthNotReached(me:MouseEvent):Boolean { if( minWidth != 0 and (oldRectangleWidth - me.dragX) < minWidth) { //too fix width when dragging too fast rectangle.width = minWidth; return false; } else { return true; } } function minRightWidthNotReached(me:MouseEvent):Boolean { if( minWidth != 0 and (oldRectangleWidth + me.dragX) < minWidth) { //too fix width when dragging too fast rectangle.width = minWidth; return false; } else { return true; } } function minUpperHeightNotReached(me:MouseEvent):Boolean { if( minHeight != 0 and (oldRectangleHeight - me.dragY) < minHeight) { //too fix width when dragging too fast rectangle.height = minHeight; return false; } else { return true; } } function minLowerHeightNotReached(me:MouseEvent):Boolean { if( minHeight != 0 and (oldRectangleHeight + me.dragY) < minHeight) { //too fix width when dragging too fast rectangle.height = minHeight; return false; } else { return true; } } function correctLeftWidthToSteps() { var oldWidth = rectangle.width; var remainder = rectangle.width mod horizontalSteps; var number:Integer = (rectangle.width/horizontalSteps) as Integer; if(remainder != 0 and remainder >= horizontalSteps/2){ var futureWidth = (number+1)*horizontalSteps; if(futureWidth > maxWidth) { futureWidth = maxWidth; } rectangle.width = futureWidth; var difference = rectangle.width - oldWidth; rectangle.x = rectangle.x - difference; } else if(remainder != 0 and remainder < horizontalSteps/2){ var futureWidth = (number)*horizontalSteps; if(futureWidth < minWidth) { futureWidth = minWidth; } rectangle.width = futureWidth; var difference = oldWidth - rectangle.width; rectangle.x = rectangle.x + difference; } } function correctRightWidthToSteps() { var oldWidth = rectangle.width; var remainder = rectangle.width mod horizontalSteps; var number:Integer = (rectangle.width/horizontalSteps) as Integer; if(remainder != 0 and remainder >= horizontalSteps/2){ var futureWidth = (number+1)*horizontalSteps; if(futureWidth > maxWidth) { futureWidth = maxWidth; } rectangle.width = futureWidth; } else if(remainder != 0 and remainder < horizontalSteps/2){ var futureWidth = (number)*horizontalSteps; if(futureWidth < minWidth) { futureWidth = minWidth; } rectangle.width = futureWidth; } } function correctUpperHeightToSteps() { var oldHeight = rectangle.height; var remainder = rectangle.height mod verticalSteps; var number:Integer = (rectangle.height/verticalSteps) as Integer; if(remainder != 0 and remainder >= verticalSteps/2){ var futureHeigth = (number+1)*verticalSteps; if(futureHeigth > maxHeight) { futureHeigth = maxHeight; } rectangle.height = futureHeigth; var difference = rectangle.height - oldHeight; rectangle.y = rectangle.y - difference; } else if(remainder != 0 and remainder < verticalSteps/2){ var futureHeigth = (number)*verticalSteps; if(futureHeigth < minHeight) { futureHeigth = minHeight; } rectangle.height = futureHeigth; var difference = oldHeight - rectangle.height; rectangle.y = rectangle.y + difference; } } function correctLowerHeightToSteps() { var oldHeight = rectangle.height; var remainder = rectangle.height mod verticalSteps; var number:Integer = (rectangle.height/verticalSteps) as Integer; if(remainder != 0 and remainder >= verticalSteps/2){ var futureHeigth = (number+1)*verticalSteps; if(futureHeigth > maxHeight) { futureHeigth = maxHeight; } rectangle.height = futureHeigth; } else if(remainder != 0 and remainder < verticalSteps/2){ var futureHeigth = (number)*verticalSteps; if(futureHeigth < minHeight) { futureHeigth = minHeight; } rectangle.height = futureHeigth; } } public var clickAreaWidth = 5; public var enableHorizontalResize = false; public var enableVerticalResize = false; public var enableMove = false; public var maxWidth = 0; public var maxHeight = 0; public var minWidth = 0; public var minHeight = 0; public var horizontalSteps = 0; public var verticalSteps = 0; public var x = 0.0 on replace { rectangle.x = x; } public var y = 0.0 on replace { rectangle.y = y; } public var arcHeight = 0.0 on replace { rectangle.arcHeight = arcHeight; } public var arcWidth = 0.0 on replace { rectangle.arcWidth = arcWidth; } public var fill:Paint on replace { rectangle.fill = fill; } public var width = 0 on replace { rectangle.width = width; } public var height = 0 on replace { rectangle.height = height; } var contentItem:Group = Group { content: [rectangle, moveRectangle, leftClickArea, rightClickArea, upperClickArea, lowerClickArea, upperLeftClickArea, lowerLeftClickArea, upperRightClickArea, lowerRightClickArea] } override function create():Node { return contentItem; } }