Skip to content

Commit

Permalink
bringToFront and sendToBack
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Sep 18, 2023
1 parent 0c6a461 commit 20aa1e5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
38 changes: 38 additions & 0 deletions src/main/java/paintingcanvas/drawable/Drawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -585,10 +585,26 @@ public T setFilled(boolean filled) {
return getThis();
}

/**
* Gets the current layer of the object.
* By default, all objects are on layer 0.
* @see #setLayer(int)
* @see #bringToFront()
* @see #sendToBack()
* @return the original object to allow method chaining
*/
public int getLayer() {
return this.layer;
}

/**
* Puts the object on a specific layer.
* @see #getLayer()
* @see #bringToFront()
* @see #sendToBack()
* @param layer the layer to set the object to
* @return the original object to allow method chaining
*/
public T setLayer(int layer) {
synchronized (Canvas.drawableSync) {
this.layer = layer;
Expand All @@ -597,6 +613,28 @@ public T setLayer(int layer) {
return getThis();
}

/**
* Brings the object in front of all other objects.
* @see #getLayer()
* @see #setLayer(int)
* @see #sendToBack()
* @return the original object to allow method chaining
*/
public T bringToFront() {
return this.setLayer(Canvas.getGlobalInstance().elements.getMaxLayer() + 1);
}

/**
* Puts the object behind all other objects.
* @see #getLayer()
* @see #setLayer(int)
* @see #bringToFront()
* @return the original object to allow method chaining
*/
public T sendToBack() {
return this.setLayer(Canvas.getGlobalInstance().elements.getMinLayer() - 1);
}

/**
* Erase this object from the canvas. This object will be gone and cannot* be added back!
* <p>
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/paintingcanvas/misc/ElementContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
public class ElementContainer {
final List<Drawable<?>> elements = new Vector<>();
boolean dirty = false;
int minLayer = 0;
int maxLayer = 0;

public void foreach(DrawableConsumer consumer) {
prepareRender();
Expand All @@ -22,7 +24,12 @@ public void foreach(DrawableConsumer consumer) {
void prepareRender() {
if (!dirty) return;
synchronized (Canvas.drawableSync) {
elements.sort(Comparator.comparingInt(Drawable::getLayer));
elements.sort(Comparator.comparingInt(a -> {
int layer = a.getLayer();
minLayer = Math.min(minLayer, layer);
maxLayer = Math.max(maxLayer, layer);
return layer;
}));
}
dirty = false;
}
Expand All @@ -31,6 +38,14 @@ public void setDirty() {
this.dirty = true;
}

public int getMinLayer() {
return minLayer;
}

public int getMaxLayer() {
return maxLayer;
}

public int size() {
return elements.size();
}
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/examples/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@ public static void main(String[] args) {
Image image = new Image(200, 200, "src/test/java/examples/flop.jpg")
.rotate(90);

int i = 0;
int i = -1;
Square[] squares = new Square[Hue.values().length];
for (Hue h : Hue.values())
new Square(50 + (i % 8) * 100, 50 + (i++ / 8) * 100, 100, new Color(h.hex));
squares[++i] = new Square(50 + (i % 8) * 100, 50 + (i / 8) * 100, 100, new Color(h.hex));

canvas.sleep(3);
image.setLayer(1);
image.bringToFront();

for (Square square : squares) {
square.bringToFront();
canvas.sleep(0.1);
}

for (Square square : squares) {
square.sendToBack();
canvas.sleep(0.1);
}
}
}

0 comments on commit 20aa1e5

Please sign in to comment.