[1097] | 1 | package org.expeditee.core;
|
---|
| 2 |
|
---|
| 3 | import org.expeditee.core.bounds.AxisAlignedBoxBounds;
|
---|
| 4 |
|
---|
| 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.
|
---|
| 11 | *
|
---|
| 12 | * @author cts16
|
---|
| 13 | */
|
---|
| 14 | public class Anchoring {
|
---|
| 15 |
|
---|
| 16 | /** The relative distance from the left or right edge (null means unanchored). */
|
---|
| 17 | private Integer _xAnchor = null;
|
---|
| 18 | /** The relative distance from the top or bottom edge (null means unanchored). */
|
---|
| 19 | private Integer _yAnchor = null;
|
---|
| 20 | /** Whether the x-anchor is relative to the left or right edge. */
|
---|
| 21 | private boolean _left = true;
|
---|
| 22 | /** Whether the y-anchor is relative to the top or bottom edge. */
|
---|
| 23 | private boolean _top = true;
|
---|
| 24 |
|
---|
| 25 | public Anchoring()
|
---|
| 26 | {
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | public void setXAnchor(Integer offset, boolean left)
|
---|
| 30 | {
|
---|
| 31 | _xAnchor = offset;
|
---|
| 32 | _left = left;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | public void setYAnchor(Integer offset, boolean top)
|
---|
| 36 | {
|
---|
| 37 | _yAnchor = offset;
|
---|
| 38 | _top = top;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | public void setXAnchor(Anchoring other)
|
---|
| 42 | {
|
---|
| 43 | if (other == null) return;
|
---|
| 44 |
|
---|
| 45 | setXAnchor(other._xAnchor, other._left);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | public void setYAnchor(Anchoring other)
|
---|
| 49 | {
|
---|
| 50 | if (other == null) return;
|
---|
| 51 |
|
---|
| 52 | setYAnchor(other._yAnchor, other._top);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public void setLeftAnchor(Integer offset)
|
---|
| 56 | {
|
---|
| 57 | setXAnchor(offset, true);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | public void setRightAnchor(Integer offset)
|
---|
| 61 | {
|
---|
| 62 | setXAnchor(offset, false);
|
---|
| 63 | }
|
---|
| 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 | {
|
---|
| 87 | removeXAnchor();
|
---|
| 88 | removeYAnchor();
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | public Integer getXPositionInBox(AxisAlignedBoxBounds box)
|
---|
| 92 | {
|
---|
| 93 | if (box == null || _xAnchor == null) return null;
|
---|
| 94 |
|
---|
| 95 | if (_left) {
|
---|
| 96 | return box.getMinX() + _xAnchor;
|
---|
| 97 | } else {
|
---|
| 98 | return box.getMaxX() - _xAnchor;
|
---|
| 99 | }
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | public Integer getYPositionInBox(AxisAlignedBoxBounds box)
|
---|
| 103 | {
|
---|
| 104 | if (box == null || _yAnchor == null) return null;
|
---|
| 105 |
|
---|
| 106 | if (_top) {
|
---|
| 107 | return box.getMinY() + _yAnchor;
|
---|
| 108 | } else {
|
---|
| 109 | return box.getMaxY() - _yAnchor;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 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 | }
|
---|