-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement resize transformed region (#1421)
Signed-off-by: Pablo Herrera <[email protected]>
- Loading branch information
1 parent
3dd6af7
commit 2e4165e
Showing
10 changed files
with
255 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package tc.oc.pgm.regions; | ||
|
||
import org.bukkit.util.Vector; | ||
import tc.oc.pgm.api.region.Region; | ||
import tc.oc.pgm.util.math.TransformMatrix; | ||
|
||
public class ResizedRegion extends TransformedRegion { | ||
private final Vector min, max; | ||
private final boolean relative; | ||
private TransformMatrix matrix; | ||
private TransformMatrix inverse; | ||
|
||
public ResizedRegion(Region region, Vector min, Vector max, boolean relative) { | ||
super(region); | ||
this.min = min; | ||
this.max = max; | ||
this.relative = relative; | ||
} | ||
|
||
@Override | ||
protected Vector transform(Vector point) { | ||
if (matrix == null) ensureInitialized(); | ||
return matrix.transform(point); | ||
} | ||
|
||
@Override | ||
protected Vector untransform(Vector point) { | ||
if (inverse == null) ensureInitialized(); | ||
return inverse.transform(point); | ||
} | ||
|
||
@Override | ||
public Bounds getBounds() { | ||
ensureInitialized(); | ||
return super.getBounds(); | ||
} | ||
|
||
private void ensureInitialized() { | ||
if (matrix != null) return; | ||
|
||
var oldBounds = region.getBounds(); | ||
if (oldBounds.isEmpty() || !oldBounds.isBlockFinite()) { | ||
this.bounds = oldBounds; | ||
this.matrix = this.inverse = TransformMatrix.identity(); | ||
return; | ||
} | ||
|
||
var oldSize = oldBounds.getSize(); | ||
|
||
if (relative) { | ||
min.multiply(oldSize); | ||
max.multiply(oldSize); | ||
} | ||
|
||
this.bounds = | ||
new Bounds(oldBounds.getMin().subtract(min), oldBounds.getMax().add(max)); | ||
var newSize = bounds.getSize(); | ||
|
||
this.matrix = TransformMatrix.concat( | ||
TransformMatrix.untranslate(oldBounds.getMin()), | ||
TransformMatrix.scale(newSize.clone().divide(oldSize)), | ||
TransformMatrix.translate(bounds.getMin())); | ||
|
||
this.inverse = TransformMatrix.concat( | ||
TransformMatrix.untranslate(bounds.getMin()), | ||
TransformMatrix.scale(oldSize.clone().divide(newSize)), | ||
TransformMatrix.translate(oldBounds.getMin())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
core/src/main/java/tc/oc/pgm/util/xml/parsers/NumberBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package tc.oc.pgm.util.xml.parsers; | ||
|
||
import org.jdom2.Element; | ||
import org.jetbrains.annotations.Nullable; | ||
import tc.oc.pgm.util.xml.InvalidXMLException; | ||
import tc.oc.pgm.util.xml.Node; | ||
import tc.oc.pgm.util.xml.XMLUtils; | ||
|
||
public class NumberBuilder<T extends Number> extends Builder<T, NumberBuilder<T>> { | ||
|
||
private final Class<T> type; | ||
private boolean infinity; | ||
|
||
public NumberBuilder(Class<T> type, @Nullable Element el, String... prop) { | ||
super(el, prop); | ||
this.type = type; | ||
} | ||
|
||
/** Allow infinity like oo or -oo */ | ||
public NumberBuilder<T> inf() { | ||
this.infinity = true; | ||
return this; | ||
} | ||
|
||
@Override | ||
protected T parse(Node node) throws InvalidXMLException { | ||
return XMLUtils.parseNumber(node, type, infinity); | ||
} | ||
|
||
@Override | ||
protected NumberBuilder<T> getThis() { | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.