Skip to content

Commit

Permalink
Add Insets.width and Insets.height
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuxel committed Nov 18, 2023
1 parent b5c5182 commit e79301a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ public void layout() {
if (axis == Axis.HORIZONTAL) {
int y = switch (verticalAlignment) {
case TOP -> insets.top();
case CENTER -> insets.top() + (getHeight() - insets.top() - insets.bottom() - child.getHeight()) / 2;
case CENTER -> insets.top() + (getHeight() - insets.height() - child.getHeight()) / 2;
case BOTTOM -> getHeight() - insets.bottom() - child.getHeight();
};

child.setLocation(dimension, y);
} else {
int x = switch (horizontalAlignment) {
case LEFT -> insets.left();
case CENTER -> insets.left() + (getWidth() - insets.left() - insets.right() - child.getWidth()) / 2;
case CENTER -> insets.left() + (getWidth() - insets.width() - child.getWidth()) / 2;
case RIGHT -> getWidth() - insets.right() - child.getWidth();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void layout() {

Insets insets = getInsets();
int gap = getGap();
int layoutHeight = this.getHeight() - insets.top() - insets.bottom();
int layoutHeight = this.getHeight() - insets.height();
int cellsHigh = Math.max((layoutHeight + gap) / (cellHeight + gap), 1); // At least one cell is always visible

//System.out.println("Adding children...");
Expand Down Expand Up @@ -210,7 +210,7 @@ public void layout() {

//At this point, w is nonnull and configured by d
if (w.canResize()) {
w.setSize(this.width - insets.left() - insets.right() - scrollBar.getWidth(), cellHeight);
w.setSize(this.width - insets.width() - scrollBar.getWidth(), cellHeight);
}
w.x = insets.left();
w.y = insets.top() + ((cellHeight + gap) * i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public WPanelWithInsets setInsets(Insets insets) {
Insets old = this.insets;
this.insets = Objects.requireNonNull(insets, "insets");

setSize(getWidth() - old.left() - old.right(), getHeight() - old.top() - old.bottom());
setSize(getWidth() - old.width(), getHeight() - old.height());

for (WWidget child : children) {
child.setLocation(child.getX() - old.left() + insets.left(), child.getY() - old.top() + insets.top());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) {
Insets insets = getInsets();
for (WWidget child : children) {
if (child == widget) {
Scissors.push(x + insets.left(), y + insets.top(), width - insets.left() - insets.right(), height - insets.top() - insets.bottom());
Scissors.push(x + insets.left(), y + insets.top(), width - insets.width(), height - insets.height());
}

child.paint(context, x + child.getX(), y + child.getY(), mouseX - child.getX(), mouseY - child.getY());
Expand Down Expand Up @@ -189,9 +189,9 @@ public void layout() {
int y = insets.top() + (vertical ? -verticalScrollBar.getValue() : 0);
widget.setLocation(x, y);

verticalScrollBar.setWindow(this.height - insets.top() - insets.bottom() - (horizontal ? SCROLL_BAR_SIZE : 0));
verticalScrollBar.setWindow(this.height - insets.height() - (horizontal ? SCROLL_BAR_SIZE : 0));
verticalScrollBar.setMaxValue(widget.getHeight());
horizontalScrollBar.setWindow(this.width - insets.left() - insets.right() - (vertical ? SCROLL_BAR_SIZE : 0));
horizontalScrollBar.setWindow(this.width - insets.width() - (vertical ? SCROLL_BAR_SIZE : 0));
horizontalScrollBar.setMaxValue(widget.getWidth());

if (vertical) children.add(verticalScrollBar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,26 @@ public Insets(int vertical, int horizontal) {
public Insets(int size) {
this(size, size, size, size);
}

/**
* {@return the total width of these insets}
*
* <p>Equivalent to <code>{@link #left()} + {@link #right()}</code>.
*
* @since 9.1.0
*/
public int width() {
return left + right;
}

/**
* {@return the total height of these insets}
*
* <p>Equivalent to <code>{@link #top()} + {@link #bottom()}</code>.
*
* @since 9.1.0
*/
public int height() {
return top + bottom;
}
}

0 comments on commit e79301a

Please sign in to comment.