Changeset 1513 for trunk/src/org/expeditee/core/Anchoring.java
- Timestamp:
- 02/19/20 15:29:48 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/core/Anchoring.java
r1097 r1513 4 4 5 5 /** 6 * Class for anchoring the position of items within the frame. 7 * Anchoring is when the position of the item is defined relative 8 * to the edge of the frame instead of being defined in absolute 9 * coordinates. Can't be anchored to the left and right edge at 10 * the same time, nor the top and bottom edge at the same time. 6 * Class for anchoring the position of items within the frame. Anchoring is when 7 * the position of the item is defined relative to the edge of the frame instead 8 * of being defined in absolute coordinates. Can't be anchored to the left and 9 * right edge at the same time, nor the top and bottom edge at the same time. 11 10 * 12 11 * @author cts16 13 12 */ 14 13 public class Anchoring { 14 15 private enum VerticalDirection { left, center, right } 16 private enum HorizontalDirection { top, center, bottom } 15 17 16 /** The relative distance from the left or right edge (null means unanchored). */ 18 /** 19 * The relative distance from the left, center or right edge (null means unanchored). 20 */ 17 21 private Integer _xAnchor = null; 18 /** The relative distance from the top or bottom edge (null means unanchored). */ 22 /** 23 * The relative distance from the top, center or bottom edge (null means unanchored). 24 */ 19 25 private Integer _yAnchor = null; 26 20 27 /** Whether the x-anchor is relative to the left or right edge. */ 21 private boolean _left = true;28 private VerticalDirection verticalDirection = VerticalDirection.left; 22 29 /** Whether the y-anchor is relative to the top or bottom edge. */ 23 private boolean _top = true; 24 25 public Anchoring() 26 { 30 private HorizontalDirection horizonalDirection = HorizontalDirection.top; 31 32 public Anchoring() { 27 33 } 28 34 29 public void setXAnchor(Integer offset, boolean left) 30 { 31 _xAnchor = offset; 32 _left = left; 35 public void setXAnchor(Anchoring other) { 36 if (other == null) { 37 return; 38 } 39 40 setXAnchor(other._xAnchor, other.verticalDirection); 41 } 42 43 public void setYAnchor(Anchoring other) { 44 if (other == null) 45 return; 46 47 setYAnchor(other._yAnchor, other.horizonalDirection); 48 } 49 50 public void setLeftAnchor(Integer offset) { 51 setXAnchor(offset, VerticalDirection.left); 33 52 } 34 53 35 public void setYAnchor(Integer offset, boolean top) 36 { 37 _yAnchor = offset; 38 _top = top; 54 public void setCenterXAnchor(Integer offset) { 55 setXAnchor(offset, VerticalDirection.center); 56 } 57 58 public void setRightAnchor(Integer offset) { 59 setXAnchor(offset, VerticalDirection.right); 60 } 61 62 public void setTopAnchor(Integer offset) { 63 setYAnchor(offset, HorizontalDirection.top); 39 64 } 40 65 41 public void setXAnchor(Anchoring other) 42 { 43 if (other == null) return; 44 45 setXAnchor(other._xAnchor, other._left); 66 public void setCenterYAnchor(Integer offset) { 67 setYAnchor(offset, HorizontalDirection.center); 46 68 } 47 69 48 public void setYAnchor(Anchoring other) 49 { 50 if (other == null) return; 51 52 setYAnchor(other._yAnchor, other._top); 70 public void setBottomAnchor(Integer offset) { 71 setYAnchor(offset, HorizontalDirection.bottom); 53 72 } 54 73 55 public void setLeftAnchor(Integer offset) 56 { 57 setXAnchor(offset, true); 74 public void removeXAnchor() { 75 setXAnchor(null, verticalDirection); 58 76 } 59 60 public void setRightAnchor(Integer offset) 61 { 62 setXAnchor(offset, false); 77 78 public void removeYAnchor() { 79 setYAnchor(null, horizonalDirection); 63 80 } 64 65 public void setTopAnchor(Integer offset) 66 { 67 setYAnchor(offset, true); 68 } 69 70 public void setBottomAnchor(Integer offset) 71 { 72 setYAnchor(offset, false); 73 } 74 75 public void removeXAnchor() 76 { 77 setXAnchor(null, _left); 78 } 79 80 public void removeYAnchor() 81 { 82 setYAnchor(null, _top); 83 } 84 85 public void removeAllAnchors() 86 { 81 82 public void removeAllAnchors() { 87 83 removeXAnchor(); 88 84 removeYAnchor(); 89 85 } 86 87 public Point getPositionInBox(AxisAlignedBoxBounds box) { 88 Integer x = getXPositionInBox(box); 89 Integer y = getYPositionInBox(box); 90 91 if (x == null || y == null) 92 return null; 93 94 return new Point(x, y); 95 } 96 97 public boolean isXAnchored() { 98 return _xAnchor != null; 99 } 100 101 public boolean isYAnchored() { 102 return _yAnchor != null; 103 } 104 105 public boolean isAnchored() { 106 return isXAnchored() || isYAnchored(); 107 } 90 108 91 public Integer getXPositionInBox(AxisAlignedBoxBounds box) 92 { 93 if (box == null || _xAnchor == null) return null; 109 public Integer getLeftAnchor() { 110 return verticalDirection.equals(VerticalDirection.left) ? _xAnchor : null; 111 } 112 113 public Integer getCenterXAnchor() { 114 return verticalDirection.equals(VerticalDirection.center) ? _xAnchor : null; 115 } 116 117 public Integer getRightAnchor() { 118 return verticalDirection.equals(VerticalDirection.right) ? _xAnchor : null; 119 } 120 121 public Integer getTopAnchor() { 122 return horizonalDirection.equals(HorizontalDirection.top) ? _yAnchor : null; 123 } 124 125 public Integer getCenterYAnchor() { 126 return horizonalDirection.equals(HorizontalDirection.center) ? _yAnchor : null; 127 } 128 129 public Integer getBottomAnchor() { 130 return horizonalDirection.equals(HorizontalDirection.bottom) ? _yAnchor : null; 131 } 132 133 private void setXAnchor(Integer offset, VerticalDirection left) { 134 _xAnchor = offset; 135 verticalDirection = left; 136 } 137 138 private void setYAnchor(Integer offset, HorizontalDirection top) { 139 _yAnchor = offset; 140 horizonalDirection = top; 141 } 142 143 private Integer getXPositionInBox(AxisAlignedBoxBounds box) { 144 if (box == null || _xAnchor == null) 145 return null; 94 146 95 if (_left) { 147 switch (verticalDirection) { 148 case left: 96 149 return box.getMinX() + _xAnchor; 97 } else { 150 case center: 151 return box.getCentreX() + _xAnchor; 152 case right: 98 153 return box.getMaxX() - _xAnchor; 154 default: 155 System.err.println("Anchoring::getXPositionInBox::VerticalDirection does not match available options. Has the enum been updated without this function being updated?"); 156 return null; 99 157 } 100 158 } 101 159 102 public Integer getYPositionInBox(AxisAlignedBoxBounds box) 103 { 104 if (box == null || _yAnchor == null) return null; 160 private Integer getYPositionInBox(AxisAlignedBoxBounds box) { 161 if (box == null || _yAnchor == null) { 162 return null; 163 } 105 164 106 if (_top) { 165 switch (horizonalDirection) { 166 case top: 107 167 return box.getMinY() + _yAnchor; 108 } else { 168 case center: 169 return box.getCentreY() + _yAnchor; 170 case bottom: 109 171 return box.getMaxY() - _yAnchor; 172 default: 173 System.err.println("Anchoring::getYPositionInBox::HorizonalDirection does not match available options. Has the enum been updated without this function being updated?"); 174 return null; 110 175 } 111 176 } 112 113 public Point getPositionInBox(AxisAlignedBoxBounds box)114 {115 Integer x = getXPositionInBox(box);116 Integer y = getYPositionInBox(box);117 118 if (x == null || y == null) return null;119 120 return new Point(x, y);121 }122 123 public boolean isXAnchored()124 {125 return _xAnchor != null;126 }127 128 public boolean isYAnchored()129 {130 return _yAnchor != null;131 }132 133 public boolean isAnchored()134 {135 return isXAnchored() || isYAnchored();136 }137 138 public Integer getLeftAnchor()139 {140 return _left ? _xAnchor : null;141 }142 143 public Integer getRightAnchor()144 {145 return !_left ? _xAnchor : null;146 }147 148 public Integer getTopAnchor()149 {150 return _top ? _yAnchor : null;151 }152 153 public Integer getBottomAnchor()154 {155 return !_top ? _yAnchor : null;156 }157 177 }
Note:
See TracChangeset
for help on using the changeset viewer.