source: trunk/src/org/expeditee/core/Anchoring.java@ 1513

Last change on this file since 1513 was 1513, checked in by bnemhaus, 4 years ago

You now have the ability to anchor Items to the center of the frame. AnchorCenterX: 0 will anchor a item directly to the vertical center of the frame. AnchorCenterY: 0 to the horizontal center of the frame. Negative numbers go left/up from the center, positive numbers right/down.

More work to come to deal with connected items such as rectangles made up on connected dots.

File size: 4.5 KB
RevLine 
[1097]1package org.expeditee.core;
2
3import org.expeditee.core.bounds.AxisAlignedBoxBounds;
4
5/**
[1513]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.
[1097]10 *
11 * @author cts16
12 */
13public class Anchoring {
[1513]14
15 private enum VerticalDirection { left, center, right }
16 private enum HorizontalDirection { top, center, bottom }
[1097]17
[1513]18 /**
19 * The relative distance from the left, center or right edge (null means unanchored).
20 */
[1097]21 private Integer _xAnchor = null;
[1513]22 /**
23 * The relative distance from the top, center or bottom edge (null means unanchored).
24 */
[1097]25 private Integer _yAnchor = null;
[1513]26
[1097]27 /** Whether the x-anchor is relative to the left or right edge. */
[1513]28 private VerticalDirection verticalDirection = VerticalDirection.left;
[1097]29 /** Whether the y-anchor is relative to the top or bottom edge. */
[1513]30 private HorizontalDirection horizonalDirection = HorizontalDirection.top;
31
32 public Anchoring() {
[1097]33 }
34
[1513]35 public void setXAnchor(Anchoring other) {
36 if (other == null) {
37 return;
38 }
39
40 setXAnchor(other._xAnchor, other.verticalDirection);
[1097]41 }
[1513]42
43 public void setYAnchor(Anchoring other) {
44 if (other == null)
45 return;
46
47 setYAnchor(other._yAnchor, other.horizonalDirection);
[1097]48 }
[1513]49
50 public void setLeftAnchor(Integer offset) {
51 setXAnchor(offset, VerticalDirection.left);
52 }
[1097]53
[1513]54 public void setCenterXAnchor(Integer offset) {
55 setXAnchor(offset, VerticalDirection.center);
[1097]56 }
57
[1513]58 public void setRightAnchor(Integer offset) {
59 setXAnchor(offset, VerticalDirection.right);
[1097]60 }
[1513]61
62 public void setTopAnchor(Integer offset) {
63 setYAnchor(offset, HorizontalDirection.top);
[1097]64 }
65
[1513]66 public void setCenterYAnchor(Integer offset) {
67 setYAnchor(offset, HorizontalDirection.center);
[1097]68 }
[1513]69
70 public void setBottomAnchor(Integer offset) {
71 setYAnchor(offset, HorizontalDirection.bottom);
[1097]72 }
73
[1513]74 public void removeXAnchor() {
75 setXAnchor(null, verticalDirection);
[1097]76 }
[1513]77
78 public void removeYAnchor() {
79 setYAnchor(null, horizonalDirection);
[1097]80 }
[1513]81
82 public void removeAllAnchors() {
[1097]83 removeXAnchor();
84 removeYAnchor();
85 }
[1513]86
87 public Point getPositionInBox(AxisAlignedBoxBounds box) {
[1097]88 Integer x = getXPositionInBox(box);
89 Integer y = getYPositionInBox(box);
[1513]90
91 if (x == null || y == null)
92 return null;
93
[1097]94 return new Point(x, y);
95 }
[1513]96
97 public boolean isXAnchored() {
[1097]98 return _xAnchor != null;
99 }
[1513]100
101 public boolean isYAnchored() {
[1097]102 return _yAnchor != null;
103 }
[1513]104
105 public boolean isAnchored() {
[1097]106 return isXAnchored() || isYAnchored();
107 }
108
[1513]109 public Integer getLeftAnchor() {
110 return verticalDirection.equals(VerticalDirection.left) ? _xAnchor : null;
[1097]111 }
112
[1513]113 public Integer getCenterXAnchor() {
114 return verticalDirection.equals(VerticalDirection.center) ? _xAnchor : null;
[1097]115 }
[1513]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 }
[1097]124
[1513]125 public Integer getCenterYAnchor() {
126 return horizonalDirection.equals(HorizontalDirection.center) ? _yAnchor : null;
[1097]127 }
128
[1513]129 public Integer getBottomAnchor() {
130 return horizonalDirection.equals(HorizontalDirection.bottom) ? _yAnchor : null;
[1097]131 }
[1513]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 }
[1097]177}
Note: See TracBrowser for help on using the repository browser.