source: trunk/src/org/expeditee/core/Clip.java@ 1097

Last change on this file since 1097 was 1097, checked in by davidb, 6 years ago

Newly structured files from Corey's work on logic/graphics separation

File size: 3.2 KB
Line 
1package org.expeditee.core;
2
3import org.expeditee.core.bounds.AxisAlignedBoxBounds;
4
5/**
6 * An area to clip drawing operations to.
7 *
8 * @author cts16
9 */
10public class Clip {
11
12 /** The bounds of this clip. Null means there is no clipping. */
13 private AxisAlignedBoxBounds _bounds = null;
14
15 /** Flag to identify when there is no unclipped area. */
16 private boolean _isFullyClipped = false;
17
18 /** Default constructor. */
19 public Clip()
20 {
21 }
22
23 /** Construct a clip encompassing the given bounds. */
24 public Clip(AxisAlignedBoxBounds bounds)
25 {
26 this(bounds, false);
27 }
28
29 /** Standard constructor. */
30 protected Clip(AxisAlignedBoxBounds bounds, boolean isFullyClipped)
31 {
32 if (bounds != null) _bounds = bounds.clone();
33 _isFullyClipped = isFullyClipped;
34 }
35
36 /** Copy constructor. */
37 public Clip(Clip other)
38 {
39 if (other._bounds != null) _bounds = other._bounds.clone();
40 _isFullyClipped = other._isFullyClipped;
41 }
42
43 /** Creates a clip which clips the entire area. */
44 public static Clip getFullClip()
45 {
46 return new Clip(null, true);
47 }
48
49 /** Checks if this clip clips all possible space. */
50 public boolean isFullyClipped()
51 {
52 return _isFullyClipped;
53 }
54
55 /** Checks if this clip is not clipping anything. */
56 public boolean isNotClipped()
57 {
58 return !isFullyClipped() && _bounds == null;
59 }
60
61 /**
62 * Gets the bounds of the clip. Null means no clipping.
63 * Should check isFullyClipped() to see if the entire area is clipped.
64 */
65 public AxisAlignedBoxBounds getBounds()
66 {
67 if (_bounds == null) return null;
68
69 return _bounds.clone();
70 }
71
72 /** Resets the clip so that no area is clipped. */
73 public Clip clear()
74 {
75 _bounds = null;
76 _isFullyClipped = false;
77
78 return this;
79 }
80
81 /** Expands the clip area to incorporate the given bounds. */
82 public Clip includeBounds(AxisAlignedBoxBounds bounds)
83 {
84 if (bounds == null) return this;
85
86 // If we are fully clipped then the given bounds will be the only unclipped area
87 if (isFullyClipped()) {
88 _isFullyClipped = false;
89 _bounds = bounds.clone();
90
91 // If _bounds is null then we already contain the given bounds
92 } else if (_bounds != null) {
93 _bounds = _bounds.combineWith(bounds);
94 }
95
96 return this;
97 }
98
99 /** Updates the clip to include only those areas that are inside the given bounds. */
100 public Clip intersectWith(AxisAlignedBoxBounds bounds)
101 {
102 if (bounds == null) return this;
103
104 // If fully clipped then no area is inside the given bounds
105 if (isFullyClipped()) return this;
106
107 // If there is no clipped area then the given bounds defines the intersection
108 if (_bounds == null) {
109 _bounds = bounds.clone();
110 } else {
111 AxisAlignedBoxBounds newClip = _bounds.intersectionWith(bounds);
112
113 // If the current bounds and given bounds don't intersect, the area is fully clipped
114 if (newClip == null) {
115 _isFullyClipped = true;
116 } else {
117 _bounds = newClip;
118 }
119 }
120
121 return this;
122 }
123
124 /** Updates the clip to include only those areas that are inside the given clip. */
125 public Clip intersectWith(Clip clip)
126 {
127 if (clip == null) return this;
128
129 if (!clip.isFullyClipped()) return intersectWith(clip.getBounds());
130
131 _isFullyClipped = true;
132
133 return this;
134 }
135
136 @Override
137 public Clip clone()
138 {
139 return new Clip(this);
140 }
141}
Note: See TracBrowser for help on using the repository browser.