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. 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.
|
---|
10 | *
|
---|
11 | * @author cts16
|
---|
12 | */
|
---|
13 | public class Anchoring {
|
---|
14 |
|
---|
15 | private enum VerticalDirection { left, center, right }
|
---|
16 | private enum HorizontalDirection { top, center, bottom }
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * The relative distance from the left, center or right edge (null means unanchored).
|
---|
20 | */
|
---|
21 | private Integer _xAnchor = null;
|
---|
22 | /**
|
---|
23 | * The relative distance from the top, center or bottom edge (null means unanchored).
|
---|
24 | */
|
---|
25 | private Integer _yAnchor = null;
|
---|
26 |
|
---|
27 | /** Whether the x-anchor is relative to the left or right edge. */
|
---|
28 | private VerticalDirection verticalDirection = VerticalDirection.left;
|
---|
29 | /** Whether the y-anchor is relative to the top or bottom edge. */
|
---|
30 | private HorizontalDirection horizonalDirection = HorizontalDirection.top;
|
---|
31 |
|
---|
32 | public Anchoring() {
|
---|
33 | }
|
---|
34 |
|
---|
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);
|
---|
52 | }
|
---|
53 |
|
---|
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);
|
---|
64 | }
|
---|
65 |
|
---|
66 | public void setCenterYAnchor(Integer offset) {
|
---|
67 | setYAnchor(offset, HorizontalDirection.center);
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void setBottomAnchor(Integer offset) {
|
---|
71 | setYAnchor(offset, HorizontalDirection.bottom);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void removeXAnchor() {
|
---|
75 | setXAnchor(null, verticalDirection);
|
---|
76 | }
|
---|
77 |
|
---|
78 | public void removeYAnchor() {
|
---|
79 | setYAnchor(null, horizonalDirection);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void removeAllAnchors() {
|
---|
83 | removeXAnchor();
|
---|
84 | removeYAnchor();
|
---|
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 | }
|
---|
108 |
|
---|
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;
|
---|
146 |
|
---|
147 | switch (verticalDirection) {
|
---|
148 | case left:
|
---|
149 | return box.getMinX() + _xAnchor;
|
---|
150 | case center:
|
---|
151 | return box.getCentreX() + _xAnchor;
|
---|
152 | case right:
|
---|
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;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | private Integer getYPositionInBox(AxisAlignedBoxBounds box) {
|
---|
161 | if (box == null || _yAnchor == null) {
|
---|
162 | return null;
|
---|
163 | }
|
---|
164 |
|
---|
165 | switch (horizonalDirection) {
|
---|
166 | case top:
|
---|
167 | return box.getMinY() + _yAnchor;
|
---|
168 | case center:
|
---|
169 | return box.getCentreY() + _yAnchor;
|
---|
170 | case bottom:
|
---|
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;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|